blob: e82874c4c95aa2ef55bd07d893800d457748597d [file] [log] [blame]
yanbzhu08ce6ab2015-12-02 13:01:29 -05001
Emeric Brun46591952012-05-18 15:47:34 +02002/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003 * SSL/TLS transport layer over SOCK_STREAM sockets
Emeric Brun46591952012-05-18 15:47:34 +02004 *
5 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Willy Tarreau69845df2012-09-10 09:43:09 +020012 * Acknowledgement:
13 * We'd like to specially thank the Stud project authors for a very clean
14 * and well documented code which helped us understand how the OpenSSL API
15 * ought to be used in non-blocking mode. This is one difficult part which
16 * is not easy to get from the OpenSSL doc, and reading the Stud code made
17 * it much more obvious than the examples in the OpenSSL package. Keep up
18 * the good works, guys !
19 *
20 * Stud is an extremely efficient and scalable SSL/TLS proxy which combines
21 * particularly well with haproxy. For more info about this project, visit :
22 * https://github.com/bumptech/stud
23 *
Emeric Brun46591952012-05-18 15:47:34 +020024 */
25
Willy Tarreau8d164dc2019-05-10 09:35:00 +020026/* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */
Emeric Brun46591952012-05-18 15:47:34 +020027#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020028#include <ctype.h>
29#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020030#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020034#include <string.h>
35#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020036
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020040#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020041#include <netinet/tcp.h>
42
Christopher Faulet31af49d2015-06-09 17:29:50 +020043#include <import/lru.h>
44#include <import/xxhash.h>
45
Emeric Brun46591952012-05-18 15:47:34 +020046#include <common/buffer.h>
Willy Tarreau843b7cb2018-07-13 10:54:26 +020047#include <common/chunk.h>
Emeric Brun46591952012-05-18 15:47:34 +020048#include <common/compat.h>
49#include <common/config.h>
50#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020051#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010052#include <common/initcall.h>
Willy Tarreau55994562019-05-09 14:52:44 +020053#include <common/openssl-compat.h>
Emeric Brun46591952012-05-18 15:47:34 +020054#include <common/standard.h>
55#include <common/ticks.h>
56#include <common/time.h>
Emeric Brun2c86cbf2014-10-30 15:56:50 +010057#include <common/cfgparse.h>
Nenad Merdanovic05552d42015-02-27 19:56:49 +010058#include <common/base64.h>
Emeric Brun46591952012-05-18 15:47:34 +020059
Emeric Brunfc0421f2012-09-07 17:30:07 +020060#include <ebsttree.h>
61
William Lallemand32af2032016-10-29 18:09:35 +020062#include <types/applet.h>
63#include <types/cli.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020064#include <types/global.h>
65#include <types/ssl_sock.h>
William Lallemand32af2032016-10-29 18:09:35 +020066#include <types/stats.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020067
Willy Tarreau7875d092012-09-10 08:20:03 +020068#include <proto/acl.h>
69#include <proto/arg.h>
William Lallemand32af2032016-10-29 18:09:35 +020070#include <proto/channel.h>
Emeric Brun46591952012-05-18 15:47:34 +020071#include <proto/connection.h>
William Lallemand32af2032016-10-29 18:09:35 +020072#include <proto/cli.h>
Emeric Brun46591952012-05-18 15:47:34 +020073#include <proto/fd.h>
74#include <proto/freq_ctr.h>
75#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020076#include <proto/http_rules.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020077#include <proto/listener.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010078#include <proto/pattern.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020079#include <proto/proto_tcp.h>
Olivier Houchardccaa7de2017-10-02 11:51:03 +020080#include <proto/proto_http.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020081#include <proto/server.h>
William Lallemand32af2032016-10-29 18:09:35 +020082#include <proto/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020083#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020084#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020085#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020086#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020087#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020088#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010089#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020090
Willy Tarreau9356dac2019-05-10 09:22:53 +020091/* ***** READ THIS before adding code here! *****
92 *
93 * Due to API incompatibilities between multiple OpenSSL versions and their
94 * derivatives, it's often tempting to add macros to (re-)define certain
95 * symbols. Please do not do this here, and do it in common/openssl-compat.h
96 * exclusively so that the whole code consistently uses the same macros.
97 *
98 * Whenever possible if a macro is missing in certain versions, it's better
99 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
100 */
101
Willy Tarreau518cedd2014-02-17 15:43:01 +0100102/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +0200103#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +0100104#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +0100105#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +0200106#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
107
Emeric Brunf282a812012-09-21 15:27:54 +0200108/* bits 0xFFFF0000 are reserved to store verify errors */
109
110/* Verify errors macros */
111#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
112#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
113#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
114
115#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
116#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
117#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +0200118
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200119/* ssl_methods flags for ssl options */
120#define MC_SSL_O_ALL 0x0000
121#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
122#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
123#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
124#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200125#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200126
127/* ssl_methods versions */
128enum {
129 CONF_TLSV_NONE = 0,
130 CONF_TLSV_MIN = 1,
131 CONF_SSLV3 = 1,
132 CONF_TLSV10 = 2,
133 CONF_TLSV11 = 3,
134 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200135 CONF_TLSV13 = 5,
136 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200137};
138
Emeric Brun850efd52014-01-29 12:24:34 +0100139/* server and bind verify method, it uses a global value as default */
140enum {
141 SSL_SOCK_VERIFY_DEFAULT = 0,
142 SSL_SOCK_VERIFY_REQUIRED = 1,
143 SSL_SOCK_VERIFY_OPTIONAL = 2,
144 SSL_SOCK_VERIFY_NONE = 3,
145};
146
William Lallemand3f85c9a2017-10-09 16:30:50 +0200147
Willy Tarreau71b734c2014-01-28 15:19:44 +0100148int sslconns = 0;
149int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100150static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100151int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200152
Willy Tarreauef934602016-12-22 23:12:01 +0100153static struct {
154 char *crt_base; /* base directory path for certificates */
155 char *ca_base; /* base directory path for CAs and CRLs */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000156 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100157
158 char *listen_default_ciphers;
159 char *connect_default_ciphers;
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 */
Olivier Houchardf6715e72019-12-19 15:02:39 +0100220 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100221 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
356
357
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100358/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100359struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100360 unsigned long long int xxh64;
361 unsigned char ciphersuite_len;
362 char ciphersuite[0];
363};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100364struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100365static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200366static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100367
Emmanuel Hocdet96b78342017-10-31 15:46:07 +0100368static int ssl_pkey_info_index = -1;
369
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200370#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
371struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
372#endif
373
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200374#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000375static unsigned int openssl_engines_initialized;
376struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
377struct ssl_engine_list {
378 struct list list;
379 ENGINE *e;
380};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200381#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000382
Remi Gacogne8de54152014-07-15 11:36:40 +0200383#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200384static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200385static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200386static DH *local_dh_1024 = NULL;
387static DH *local_dh_2048 = NULL;
388static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100389static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200390#endif /* OPENSSL_NO_DH */
391
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100392#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200393/* X509V3 Extensions that will be added on generated certificates */
394#define X509V3_EXT_SIZE 5
395static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
396 "basicConstraints",
397 "nsComment",
398 "subjectKeyIdentifier",
399 "authorityKeyIdentifier",
400 "keyUsage",
401};
402static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
403 "CA:FALSE",
404 "\"OpenSSL Generated Certificate\"",
405 "hash",
406 "keyid,issuer:always",
407 "nonRepudiation,digitalSignature,keyEncipherment"
408};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200409/* LRU cache to store generated certificate */
410static struct lru64_head *ssl_ctx_lru_tree = NULL;
411static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200412static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100413__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200414
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200415#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
416
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100417static struct ssl_bind_kw ssl_bind_kws[];
418
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200419#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500420/* The order here matters for picking a default context,
421 * keep the most common keytype at the bottom of the list
422 */
423const char *SSL_SOCK_KEYTYPE_NAMES[] = {
424 "dsa",
425 "ecdsa",
426 "rsa"
427};
428#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100429#else
430#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500431#endif
432
William Lallemandc3cd35f2017-11-28 11:04:43 +0100433static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100434static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
435
436#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
437
438#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
439 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
440
441#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
442 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200443
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100444/*
445 * This function gives the detail of the SSL error. It is used only
446 * if the debug mode and the verbose mode are activated. It dump all
447 * the SSL error until the stack was empty.
448 */
449static forceinline void ssl_sock_dump_errors(struct connection *conn)
450{
451 unsigned long ret;
452
453 if (unlikely(global.mode & MODE_DEBUG)) {
454 while(1) {
455 ret = ERR_get_error();
456 if (ret == 0)
457 return;
Willy Tarreau838379a2021-03-02 19:32:39 +0100458 fprintf(stderr, "fd[%#x] OpenSSL error[0x%lx] %s: %s\n",
459 conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100460 ERR_func_error_string(ret), ERR_reason_error_string(ret));
461 }
462 }
463}
464
yanbzhube2774d2015-12-10 15:07:30 -0500465
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200466#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000467static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
468{
469 int err_code = ERR_ABORT;
470 ENGINE *engine;
471 struct ssl_engine_list *el;
472
473 /* grab the structural reference to the engine */
474 engine = ENGINE_by_id(engine_id);
475 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100476 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000477 goto fail_get;
478 }
479
480 if (!ENGINE_init(engine)) {
481 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100482 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000483 goto fail_init;
484 }
485
486 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100487 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000488 goto fail_set_method;
489 }
490
491 el = calloc(1, sizeof(*el));
Remi Tricot-Le Bretone5759bd2021-05-12 17:45:21 +0200492 if (!el)
493 goto fail_alloc;
Grant Zhang872f9c22017-01-21 01:10:18 +0000494 el->e = engine;
495 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100496 nb_engines++;
497 if (global_ssl.async)
498 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000499 return 0;
500
Remi Tricot-Le Bretone5759bd2021-05-12 17:45:21 +0200501fail_alloc:
Grant Zhang872f9c22017-01-21 01:10:18 +0000502fail_set_method:
503 /* release the functional reference from ENGINE_init() */
504 ENGINE_finish(engine);
505
506fail_init:
507 /* release the structural reference from ENGINE_by_id() */
508 ENGINE_free(engine);
509
510fail_get:
511 return err_code;
512}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200513#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000514
Willy Tarreau5db847a2019-05-09 14:13:35 +0200515#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200516/*
517 * openssl async fd handler
518 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200519void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000520{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200521 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000522
Emeric Brun3854e012017-05-17 20:42:48 +0200523 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000524 * to poll this fd until it is requested
525 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000526 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000527 fd_cant_recv(fd);
528
529 /* crypto engine is available, let's notify the associated
530 * connection that it can pursue its processing.
531 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200532 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000533}
534
Emeric Brun3854e012017-05-17 20:42:48 +0200535/*
536 * openssl async delayed SSL_free handler
537 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200538void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000539{
540 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200541 OSSL_ASYNC_FD all_fd[32];
542 size_t num_all_fds = 0;
543 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000544
Emeric Brun3854e012017-05-17 20:42:48 +0200545 /* We suppose that the async job for a same SSL *
546 * are serialized. So if we are awake it is
547 * because the running job has just finished
548 * and we can remove all async fds safely
549 */
550 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
551 if (num_all_fds > 32) {
552 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
553 return;
554 }
555
556 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
557 for (i=0 ; i < num_all_fds ; i++)
558 fd_remove(all_fd[i]);
559
560 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000561 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100562 _HA_ATOMIC_SUB(&sslconns, 1);
563 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000564}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000565/*
Emeric Brun3854e012017-05-17 20:42:48 +0200566 * function used to manage a returned SSL_ERROR_WANT_ASYNC
567 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000568 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200569static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000570{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100571 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200572 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200573 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000574 size_t num_add_fds = 0;
575 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200576 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000577
578 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
579 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200580 if (num_add_fds > 32 || num_del_fds > 32) {
581 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 +0000582 return;
583 }
584
Emeric Brun3854e012017-05-17 20:42:48 +0200585 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000586
Emeric Brun3854e012017-05-17 20:42:48 +0200587 /* We remove unused fds from the fdtab */
588 for (i=0 ; i < num_del_fds ; i++)
589 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000590
Emeric Brun3854e012017-05-17 20:42:48 +0200591 /* We add new fds to the fdtab */
592 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200593 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000594 }
595
Emeric Brun3854e012017-05-17 20:42:48 +0200596 num_add_fds = 0;
597 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
598 if (num_add_fds > 32) {
599 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
600 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000601 }
Emeric Brun3854e012017-05-17 20:42:48 +0200602
603 /* We activate the polling for all known async fds */
604 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000605 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200606 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000607 /* To ensure that the fd cache won't be used
608 * We'll prefer to catch a real RD event
609 * because handling an EAGAIN on this fd will
610 * result in a context switch and also
611 * some engines uses a fd in blocking mode.
612 */
613 fd_cant_recv(add_fd[i]);
614 }
Emeric Brun3854e012017-05-17 20:42:48 +0200615
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000616}
617#endif
618
William Lallemandc33d83d2019-10-14 14:14:59 +0200619#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200620/*
621 * This function returns the number of seconds elapsed
622 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
623 * date presented un ASN1_GENERALIZEDTIME.
624 *
625 * In parsing error case, it returns -1.
626 */
627static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
628{
629 long epoch;
630 char *p, *end;
631 const unsigned short month_offset[12] = {
632 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
633 };
634 int year, month;
635
636 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
637
638 p = (char *)d->data;
639 end = p + d->length;
640
641 if (end - p < 4) return -1;
642 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
643 p += 4;
644 if (end - p < 2) return -1;
645 month = 10 * (p[0] - '0') + p[1] - '0';
646 if (month < 1 || month > 12) return -1;
647 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
648 We consider leap years and the current month (<marsh or not) */
649 epoch = ( ((year - 1970) * 365)
650 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
651 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
652 + month_offset[month-1]
653 ) * 24 * 60 * 60;
654 p += 2;
655 if (end - p < 2) return -1;
656 /* Add the number of seconds of completed days of current month */
657 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
658 p += 2;
659 if (end - p < 2) return -1;
660 /* Add the completed hours of the current day */
661 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
662 p += 2;
663 if (end - p < 2) return -1;
664 /* Add the completed minutes of the current hour */
665 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
666 p += 2;
667 if (p == end) return -1;
668 /* Test if there is available seconds */
669 if (p[0] < '0' || p[0] > '9')
670 goto nosec;
671 if (end - p < 2) return -1;
672 /* Add the seconds of the current minute */
673 epoch += 10 * (p[0] - '0') + p[1] - '0';
674 p += 2;
675 if (p == end) return -1;
676 /* Ignore seconds float part if present */
677 if (p[0] == '.') {
678 do {
679 if (++p == end) return -1;
680 } while (p[0] >= '0' && p[0] <= '9');
681 }
682
683nosec:
684 if (p[0] == 'Z') {
685 if (end - p != 1) return -1;
686 return epoch;
687 }
688 else if (p[0] == '+') {
689 if (end - p != 5) return -1;
690 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700691 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 +0200692 }
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
699 return -1;
700}
701
William Lallemandc33d83d2019-10-14 14:14:59 +0200702/*
703 * struct alignment works here such that the key.key is the same as key_data
704 * Do not change the placement of key_data
705 */
706struct certificate_ocsp {
707 struct ebmb_node key;
708 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
709 struct buffer response;
710 long expire;
711};
712
713struct ocsp_cbk_arg {
714 int is_single;
715 int single_kt;
716 union {
717 struct certificate_ocsp *s_ocsp;
718 /*
719 * m_ocsp will have multiple entries dependent on key type
720 * Entry 0 - DSA
721 * Entry 1 - ECDSA
722 * Entry 2 - RSA
723 */
724 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
725 };
726};
727
Emeric Brun1d3865b2014-06-20 15:37:32 +0200728static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200729
730/* This function starts to check if the OCSP response (in DER format) contained
731 * in chunk 'ocsp_response' is valid (else exits on error).
732 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
733 * contained in the OCSP Response and exits on error if no match.
734 * If it's a valid OCSP Response:
735 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
736 * pointed by 'ocsp'.
737 * If 'ocsp' is NULL, the function looks up into the OCSP response's
738 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
739 * from the response) and exits on error if not found. Finally, If an OCSP response is
740 * already present in the container, it will be overwritten.
741 *
742 * Note: OCSP response containing more than one OCSP Single response is not
743 * considered valid.
744 *
745 * Returns 0 on success, 1 in error case.
746 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200747static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
748 struct certificate_ocsp *ocsp,
749 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200750{
751 OCSP_RESPONSE *resp;
752 OCSP_BASICRESP *bs = NULL;
753 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200754 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200755 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200756 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200757 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200758 int reason;
759 int ret = 1;
760
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200761 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
762 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200763 if (!resp) {
764 memprintf(err, "Unable to parse OCSP response");
765 goto out;
766 }
767
768 rc = OCSP_response_status(resp);
769 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
770 memprintf(err, "OCSP response status not successful");
771 goto out;
772 }
773
774 bs = OCSP_response_get1_basic(resp);
775 if (!bs) {
776 memprintf(err, "Failed to get basic response from OCSP Response");
777 goto out;
778 }
779
780 count_sr = OCSP_resp_count(bs);
781 if (count_sr > 1) {
782 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
783 goto out;
784 }
785
786 sr = OCSP_resp_get0(bs, 0);
787 if (!sr) {
788 memprintf(err, "Failed to get OCSP single response");
789 goto out;
790 }
791
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200792 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
793
Emeric Brun4147b2e2014-06-16 18:36:30 +0200794 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200795 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200796 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200797 goto out;
798 }
799
Emeric Brun13a6b482014-06-20 15:44:34 +0200800 if (!nextupd) {
801 memprintf(err, "OCSP single response: missing nextupdate");
802 goto out;
803 }
804
Emeric Brunc8b27b62014-06-19 14:16:17 +0200805 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200806 if (!rc) {
807 memprintf(err, "OCSP single response: no longer valid.");
808 goto out;
809 }
810
811 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200812 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200813 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
814 goto out;
815 }
816 }
817
818 if (!ocsp) {
819 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
820 unsigned char *p;
821
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200822 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200823 if (!rc) {
824 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
825 goto out;
826 }
827
828 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
829 memprintf(err, "OCSP single response: Certificate ID too long");
830 goto out;
831 }
832
833 p = key;
834 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200835 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200836 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
837 if (!ocsp) {
838 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
839 goto out;
840 }
841 }
842
843 /* According to comments on "chunk_dup", the
844 previous chunk buffer will be freed */
845 if (!chunk_dup(&ocsp->response, ocsp_response)) {
846 memprintf(err, "OCSP response: Memory allocation error");
847 goto out;
848 }
849
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200850 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
851
Emeric Brun4147b2e2014-06-16 18:36:30 +0200852 ret = 0;
853out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +0100854 ERR_clear_error();
855
Emeric Brun4147b2e2014-06-16 18:36:30 +0200856 if (bs)
857 OCSP_BASICRESP_free(bs);
858
859 if (resp)
860 OCSP_RESPONSE_free(resp);
861
862 return ret;
863}
864/*
865 * External function use to update the OCSP response in the OCSP response's
866 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
867 * to update in DER format.
868 *
869 * Returns 0 on success, 1 in error case.
870 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200871int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200872{
873 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
874}
875
876/*
877 * This function load the OCSP Resonse in DER format contained in file at
878 * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response'
879 *
880 * Returns 0 on success, 1 in error case.
881 */
882static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err)
883{
884 int fd = -1;
885 int r = 0;
886 int ret = 1;
887
888 fd = open(ocsp_path, O_RDONLY);
889 if (fd == -1) {
890 memprintf(err, "Error opening OCSP response file");
891 goto end;
892 }
893
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200894 trash.data = 0;
895 while (trash.data < trash.size) {
896 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200897 if (r < 0) {
898 if (errno == EINTR)
899 continue;
900
901 memprintf(err, "Error reading OCSP response from file");
902 goto end;
903 }
904 else if (r == 0) {
905 break;
906 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200907 trash.data += r;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200908 }
909
910 close(fd);
911 fd = -1;
912
913 ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err);
914end:
915 if (fd != -1)
916 close(fd);
917
918 return ret;
919}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100920#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +0200921
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100922#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
923static 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)
924{
Christopher Faulet16f45c82018-02-16 11:23:49 +0100925 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +0100926 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100927 struct connection *conn;
928 int head;
929 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100930 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100931
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200932 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +0200933 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100934 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
935
936 keys = ref->tlskeys;
937 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100938
939 if (enc) {
940 memcpy(key_name, keys[head].name, 16);
941
942 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +0100943 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100944
Emeric Brun9e754772019-01-10 17:51:55 +0100945 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100946
Emeric Brun9e754772019-01-10 17:51:55 +0100947 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
948 goto end;
949
Willy Tarreau9356dac2019-05-10 09:22:53 +0200950 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100951 ret = 1;
952 }
953 else if (ref->key_size_bits == 256 ) {
954
955 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
956 goto end;
957
Willy Tarreau9356dac2019-05-10 09:22:53 +0200958 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100959 ret = 1;
960 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100961 } else {
962 for (i = 0; i < TLS_TICKETS_NO; i++) {
963 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
964 goto found;
965 }
Christopher Faulet16f45c82018-02-16 11:23:49 +0100966 ret = 0;
967 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100968
Christopher Faulet16f45c82018-02-16 11:23:49 +0100969 found:
Emeric Brun9e754772019-01-10 17:51:55 +0100970 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +0200971 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 +0100972 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
973 goto end;
974 /* 2 for key renewal, 1 if current key is still valid */
975 ret = i ? 2 : 1;
976 }
977 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +0200978 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 +0100979 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
980 goto end;
981 /* 2 for key renewal, 1 if current key is still valid */
982 ret = i ? 2 : 1;
983 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100984 }
Emeric Brun9e754772019-01-10 17:51:55 +0100985
Christopher Faulet16f45c82018-02-16 11:23:49 +0100986 end:
987 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
988 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200989}
990
991struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
992{
993 struct tls_keys_ref *ref;
994
995 list_for_each_entry(ref, &tlskeys_reference, list)
996 if (ref->filename && strcmp(filename, ref->filename) == 0)
997 return ref;
998 return NULL;
999}
1000
1001struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1002{
1003 struct tls_keys_ref *ref;
1004
1005 list_for_each_entry(ref, &tlskeys_reference, list)
1006 if (ref->unique_id == unique_id)
1007 return ref;
1008 return NULL;
1009}
1010
Emeric Brun9e754772019-01-10 17:51:55 +01001011/* Update the key into ref: if keysize doesnt
1012 * match existing ones, this function returns -1
1013 * else it returns 0 on success.
1014 */
1015int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001016 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001017{
Emeric Brun9e754772019-01-10 17:51:55 +01001018 if (ref->key_size_bits == 128) {
1019 if (tlskey->data != sizeof(struct tls_sess_key_128))
1020 return -1;
1021 }
1022 else if (ref->key_size_bits == 256) {
1023 if (tlskey->data != sizeof(struct tls_sess_key_256))
1024 return -1;
1025 }
1026 else
1027 return -1;
1028
Christopher Faulet16f45c82018-02-16 11:23:49 +01001029 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001030 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1031 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001032 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1033 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001034
1035 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001036}
1037
Willy Tarreau83061a82018-07-13 11:56:34 +02001038int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001039{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001040 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1041
1042 if(!ref) {
1043 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1044 return 1;
1045 }
Emeric Brun9e754772019-01-10 17:51:55 +01001046 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1047 memprintf(err, "Invalid key size");
1048 return 1;
1049 }
1050
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001051 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001052}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001053
1054/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001055 * automatic ids. It's called just after the basic checks. It returns
1056 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001057 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001058static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001059{
1060 int i = 0;
1061 struct tls_keys_ref *ref, *ref2, *ref3;
1062 struct list tkr = LIST_HEAD_INIT(tkr);
1063
1064 list_for_each_entry(ref, &tlskeys_reference, list) {
1065 if (ref->unique_id == -1) {
1066 /* Look for the first free id. */
1067 while (1) {
1068 list_for_each_entry(ref2, &tlskeys_reference, list) {
1069 if (ref2->unique_id == i) {
1070 i++;
1071 break;
1072 }
1073 }
1074 if (&ref2->list == &tlskeys_reference)
1075 break;
1076 }
1077
1078 /* Uses the unique id and increment it for the next entry. */
1079 ref->unique_id = i;
1080 i++;
1081 }
1082 }
1083
1084 /* This sort the reference list by id. */
1085 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1086 LIST_DEL(&ref->list);
1087 list_for_each_entry(ref3, &tkr, list) {
1088 if (ref->unique_id < ref3->unique_id) {
1089 LIST_ADDQ(&ref3->list, &ref->list);
1090 break;
1091 }
1092 }
1093 if (&ref3->list == &tkr)
1094 LIST_ADDQ(&tkr, &ref->list);
1095 }
1096
1097 /* swap root */
1098 LIST_ADD(&tkr, &tlskeys_reference);
1099 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001100 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001101}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001102#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1103
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001104#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001105int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1106{
1107 switch (evp_keytype) {
1108 case EVP_PKEY_RSA:
1109 return 2;
1110 case EVP_PKEY_DSA:
1111 return 0;
1112 case EVP_PKEY_EC:
1113 return 1;
1114 }
1115
1116 return -1;
1117}
1118
Emeric Brun4147b2e2014-06-16 18:36:30 +02001119/*
1120 * Callback used to set OCSP status extension content in server hello.
1121 */
1122int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1123{
yanbzhube2774d2015-12-10 15:07:30 -05001124 struct certificate_ocsp *ocsp;
1125 struct ocsp_cbk_arg *ocsp_arg;
1126 char *ssl_buf;
1127 EVP_PKEY *ssl_pkey;
1128 int key_type;
1129 int index;
1130
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001131 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001132
1133 ssl_pkey = SSL_get_privatekey(ssl);
1134 if (!ssl_pkey)
1135 return SSL_TLSEXT_ERR_NOACK;
1136
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001137 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001138
1139 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1140 ocsp = ocsp_arg->s_ocsp;
1141 else {
1142 /* For multiple certs per context, we have to find the correct OCSP response based on
1143 * the certificate type
1144 */
1145 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1146
1147 if (index < 0)
1148 return SSL_TLSEXT_ERR_NOACK;
1149
1150 ocsp = ocsp_arg->m_ocsp[index];
1151
1152 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001153
1154 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001155 !ocsp->response.area ||
1156 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001157 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001158 return SSL_TLSEXT_ERR_NOACK;
1159
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001160 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001161 if (!ssl_buf)
1162 return SSL_TLSEXT_ERR_NOACK;
1163
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001164 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1165 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001166
1167 return SSL_TLSEXT_ERR_OK;
1168}
1169
1170/*
1171 * This function enables the handling of OCSP status extension on 'ctx' if a
1172 * file name 'cert_path' suffixed using ".ocsp" is present.
1173 * To enable OCSP status extension, the issuer's certificate is mandatory.
1174 * It should be present in the certificate's extra chain builded from file
1175 * 'cert_path'. If not found, the issuer certificate is loaded from a file
1176 * named 'cert_path' suffixed using '.issuer'.
1177 *
1178 * In addition, ".ocsp" file content is loaded as a DER format of an OCSP
1179 * response. If file is empty or content is not a valid OCSP response,
1180 * OCSP status extension is enabled but OCSP response is ignored (a warning
1181 * is displayed).
1182 *
1183 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001184 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001185 */
1186static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path)
1187{
1188
1189 BIO *in = NULL;
1190 X509 *x, *xi = NULL, *issuer = NULL;
1191 STACK_OF(X509) *chain = NULL;
1192 OCSP_CERTID *cid = NULL;
1193 SSL *ssl;
1194 char ocsp_path[MAXPATHLEN+1];
1195 int i, ret = -1;
1196 struct stat st;
1197 struct certificate_ocsp *ocsp = NULL, *iocsp;
1198 char *warn = NULL;
1199 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001200 pem_password_cb *passwd_cb;
1201 void *passwd_cb_userdata;
1202 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001203
1204 snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path);
1205
1206 if (stat(ocsp_path, &st))
1207 return 1;
1208
1209 ssl = SSL_new(ctx);
1210 if (!ssl)
1211 goto out;
1212
1213 x = SSL_get_certificate(ssl);
1214 if (!x)
1215 goto out;
1216
1217 /* Try to lookup for issuer in certificate extra chain */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001218 SSL_CTX_get_extra_chain_certs(ctx, &chain);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001219 for (i = 0; i < sk_X509_num(chain); i++) {
1220 issuer = sk_X509_value(chain, i);
1221 if (X509_check_issued(issuer, x) == X509_V_OK)
1222 break;
1223 else
1224 issuer = NULL;
1225 }
1226
1227 /* If not found try to load issuer from a suffixed file */
1228 if (!issuer) {
1229 char issuer_path[MAXPATHLEN+1];
1230
1231 in = BIO_new(BIO_s_file());
1232 if (!in)
1233 goto out;
1234
1235 snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path);
1236 if (BIO_read_filename(in, issuer_path) <= 0)
1237 goto out;
1238
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001239 passwd_cb = SSL_CTX_get_default_passwd_cb(ctx);
1240 passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx);
1241
1242 xi = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001243 if (!xi)
1244 goto out;
1245
1246 if (X509_check_issued(xi, x) != X509_V_OK)
1247 goto out;
1248
1249 issuer = xi;
1250 }
1251
1252 cid = OCSP_cert_to_id(0, x, issuer);
1253 if (!cid)
1254 goto out;
1255
1256 i = i2d_OCSP_CERTID(cid, NULL);
1257 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1258 goto out;
1259
Vincent Bernat02779b62016-04-03 13:48:43 +02001260 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001261 if (!ocsp)
1262 goto out;
1263
1264 p = ocsp->key_data;
1265 i2d_OCSP_CERTID(cid, &p);
1266
1267 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1268 if (iocsp == ocsp)
1269 ocsp = NULL;
1270
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001271#ifndef SSL_CTX_get_tlsext_status_cb
1272# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1273 *cb = (void (*) (void))ctx->tlsext_status_cb;
1274#endif
1275 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1276
1277 if (!callback) {
William Lallemanda4864492020-07-31 11:43:20 +02001278 struct ocsp_cbk_arg *cb_arg;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001279 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001280
William Lallemanda4864492020-07-31 11:43:20 +02001281 cb_arg = calloc(1, sizeof(*cb_arg));
1282 if (!cb_arg)
1283 goto out;
1284
yanbzhube2774d2015-12-10 15:07:30 -05001285 cb_arg->is_single = 1;
1286 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001287
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001288 pkey = X509_get_pubkey(x);
1289 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1290 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001291
1292 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1293 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1294 } else {
1295 /*
1296 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1297 * Update that cb_arg with the new cert's staple
1298 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001299 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001300 struct certificate_ocsp *tmp_ocsp;
1301 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001302 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001303 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001304
1305#ifdef SSL_CTX_get_tlsext_status_arg
1306 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1307#else
1308 cb_arg = ctx->tlsext_status_arg;
1309#endif
yanbzhube2774d2015-12-10 15:07:30 -05001310
1311 /*
1312 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1313 * the order of operations below matter, take care when changing it
1314 */
1315 tmp_ocsp = cb_arg->s_ocsp;
1316 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1317 cb_arg->s_ocsp = NULL;
1318 cb_arg->m_ocsp[index] = tmp_ocsp;
1319 cb_arg->is_single = 0;
1320 cb_arg->single_kt = 0;
1321
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001322 pkey = X509_get_pubkey(x);
1323 key_type = EVP_PKEY_base_id(pkey);
1324 EVP_PKEY_free(pkey);
1325
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001326 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001327 if (index >= 0 && !cb_arg->m_ocsp[index])
1328 cb_arg->m_ocsp[index] = iocsp;
1329
1330 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001331
1332 ret = 0;
1333
1334 warn = NULL;
1335 if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) {
1336 memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001337 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001338 }
1339
1340out:
1341 if (ssl)
1342 SSL_free(ssl);
1343
1344 if (in)
1345 BIO_free(in);
1346
1347 if (xi)
1348 X509_free(xi);
1349
1350 if (cid)
1351 OCSP_CERTID_free(cid);
1352
1353 if (ocsp)
1354 free(ocsp);
1355
1356 if (warn)
1357 free(warn);
1358
1359
1360 return ret;
1361}
1362
1363#endif
1364
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001365#ifdef OPENSSL_IS_BORINGSSL
1366static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path)
1367{
1368 char ocsp_path[MAXPATHLEN+1];
1369 struct stat st;
1370 int fd = -1, r = 0;
1371
1372 snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path);
1373 if (stat(ocsp_path, &st))
1374 return 0;
1375
1376 fd = open(ocsp_path, O_RDONLY);
1377 if (fd == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001378 ha_warning("Error opening OCSP response file %s.\n", ocsp_path);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001379 return -1;
1380 }
1381
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001382 trash.data = 0;
1383 while (trash.data < trash.size) {
1384 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001385 if (r < 0) {
1386 if (errno == EINTR)
1387 continue;
Christopher Faulet767a84b2017-11-24 16:50:31 +01001388 ha_warning("Error reading OCSP response from file %s.\n", ocsp_path);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001389 close(fd);
1390 return -1;
1391 }
1392 else if (r == 0) {
1393 break;
1394 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001395 trash.data += r;
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001396 }
1397 close(fd);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001398 return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *) trash.area,
1399 trash.data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001400}
1401#endif
1402
Willy Tarreau5db847a2019-05-09 14:13:35 +02001403#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001404
1405#define CT_EXTENSION_TYPE 18
1406
1407static int sctl_ex_index = -1;
1408
1409/*
1410 * Try to parse Signed Certificate Timestamp List structure. This function
1411 * makes only basic test if the data seems like SCTL. No signature validation
1412 * is performed.
1413 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001414static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001415{
1416 int ret = 1;
1417 int len, pos, sct_len;
1418 unsigned char *data;
1419
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001420 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001421 goto out;
1422
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001423 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001424 len = (data[0] << 8) | data[1];
1425
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001426 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001427 goto out;
1428
1429 data = data + 2;
1430 pos = 0;
1431 while (pos < len) {
1432 if (len - pos < 2)
1433 goto out;
1434
1435 sct_len = (data[pos] << 8) | data[pos + 1];
1436 if (pos + sct_len + 2 > len)
1437 goto out;
1438
1439 pos += sct_len + 2;
1440 }
1441
1442 ret = 0;
1443
1444out:
1445 return ret;
1446}
1447
Willy Tarreau83061a82018-07-13 11:56:34 +02001448static int ssl_sock_load_sctl_from_file(const char *sctl_path,
1449 struct buffer **sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001450{
1451 int fd = -1;
1452 int r = 0;
1453 int ret = 1;
1454
1455 *sctl = NULL;
1456
1457 fd = open(sctl_path, O_RDONLY);
1458 if (fd == -1)
1459 goto end;
1460
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001461 trash.data = 0;
1462 while (trash.data < trash.size) {
1463 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001464 if (r < 0) {
1465 if (errno == EINTR)
1466 continue;
1467
1468 goto end;
1469 }
1470 else if (r == 0) {
1471 break;
1472 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001473 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001474 }
1475
1476 ret = ssl_sock_parse_sctl(&trash);
1477 if (ret)
1478 goto end;
1479
Vincent Bernat02779b62016-04-03 13:48:43 +02001480 *sctl = calloc(1, sizeof(**sctl));
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001481 if (!chunk_dup(*sctl, &trash)) {
1482 free(*sctl);
1483 *sctl = NULL;
1484 goto end;
1485 }
1486
1487end:
1488 if (fd != -1)
1489 close(fd);
1490
1491 return ret;
1492}
1493
1494int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1495{
Willy Tarreau83061a82018-07-13 11:56:34 +02001496 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001497
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001498 *out = (unsigned char *) sctl->area;
1499 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001500
1501 return 1;
1502}
1503
1504int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1505{
1506 return 1;
1507}
1508
1509static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path)
1510{
1511 char sctl_path[MAXPATHLEN+1];
1512 int ret = -1;
1513 struct stat st;
Willy Tarreau83061a82018-07-13 11:56:34 +02001514 struct buffer *sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001515
1516 snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path);
1517
1518 if (stat(sctl_path, &st))
1519 return 1;
1520
1521 if (ssl_sock_load_sctl_from_file(sctl_path, &sctl))
1522 goto out;
1523
1524 if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) {
1525 free(sctl);
1526 goto out;
1527 }
1528
1529 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1530
1531 ret = 0;
1532
1533out:
1534 return ret;
1535}
1536
1537#endif
1538
Emeric Brune1f38db2012-09-03 20:36:47 +02001539void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1540{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001541 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001542 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001543 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001544 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001545
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001546#ifndef SSL_OP_NO_RENEGOTIATION
1547 /* Please note that BoringSSL defines this macro to zero so don't
1548 * change this to #if and do not assign a default value to this macro!
1549 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001550 if (where & SSL_CB_HANDSHAKE_START) {
1551 /* Disable renegotiation (CVE-2009-3555) */
Olivier Houchard90084a12017-11-23 18:21:29 +01001552 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 +02001553 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001554 conn->err_code = CO_ER_SSL_RENEG;
1555 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001556 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001557#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001558
1559 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001560 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001561 /* Long certificate chains optimz
1562 If write and read bios are differents, we
1563 consider that the buffering was activated,
1564 so we rise the output buffer size from 4k
1565 to 16k */
1566 write_bio = SSL_get_wbio(ssl);
1567 if (write_bio != SSL_get_rbio(ssl)) {
1568 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001569 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001570 }
1571 }
1572 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001573}
1574
Emeric Brune64aef12012-09-21 13:15:06 +02001575/* Callback is called for each certificate of the chain during a verify
1576 ok is set to 1 if preverify detect no error on current certificate.
1577 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001578int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001579{
1580 SSL *ssl;
1581 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001582 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001583 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001584
1585 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001586 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001587
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001588 ctx = conn->xprt_ctx;
1589
1590 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001591
Emeric Brun81c00f02012-09-21 14:31:21 +02001592 if (ok) /* no errors */
1593 return ok;
1594
1595 depth = X509_STORE_CTX_get_error_depth(x_store);
1596 err = X509_STORE_CTX_get_error(x_store);
1597
1598 /* check if CA error needs to be ignored */
1599 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001600 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1601 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1602 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001603 }
1604
Willy Tarreaua9d299f2020-02-04 14:02:02 +01001605 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001606 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001607 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001608 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001609 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001610
Willy Tarreau20879a02012-12-03 16:32:10 +01001611 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001612 return 0;
1613 }
1614
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001615 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1616 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001617
Emeric Brun81c00f02012-09-21 14:31:21 +02001618 /* check if certificate error needs to be ignored */
Willy Tarreaua9d299f2020-02-04 14:02:02 +01001619 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001620 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001621 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001622 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001623 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001624
Willy Tarreau20879a02012-12-03 16:32:10 +01001625 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001626 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001627}
1628
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001629static inline
1630void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001631 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001632{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001633 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001634 unsigned char *msg;
1635 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001636 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001637
1638 /* This function is called for "from client" and "to server"
1639 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001640 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001641 */
1642
1643 /* "write_p" is set to 0 is the bytes are received messages,
1644 * otherwise it is set to 1.
1645 */
1646 if (write_p != 0)
1647 return;
1648
1649 /* content_type contains the type of message received or sent
1650 * according with the SSL/TLS protocol spec. This message is
1651 * encoded with one byte. The value 256 (two bytes) is used
1652 * for designing the SSL/TLS record layer. According with the
1653 * rfc6101, the expected message (other than 256) are:
1654 * - change_cipher_spec(20)
1655 * - alert(21)
1656 * - handshake(22)
1657 * - application_data(23)
1658 * - (255)
1659 * We are interessed by the handshake and specially the client
1660 * hello.
1661 */
1662 if (content_type != 22)
1663 return;
1664
1665 /* The message length is at least 4 bytes, containing the
1666 * message type and the message length.
1667 */
1668 if (len < 4)
1669 return;
1670
1671 /* First byte of the handshake message id the type of
1672 * message. The konwn types are:
1673 * - hello_request(0)
1674 * - client_hello(1)
1675 * - server_hello(2)
1676 * - certificate(11)
1677 * - server_key_exchange (12)
1678 * - certificate_request(13)
1679 * - server_hello_done(14)
1680 * We are interested by the client hello.
1681 */
1682 msg = (unsigned char *)buf;
1683 if (msg[0] != 1)
1684 return;
1685
1686 /* Next three bytes are the length of the message. The total length
1687 * must be this decoded length + 4. If the length given as argument
1688 * is not the same, we abort the protocol dissector.
1689 */
1690 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1691 if (len < rec_len + 4)
1692 return;
1693 msg += 4;
1694 end = msg + rec_len;
1695 if (end < msg)
1696 return;
1697
1698 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1699 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001700 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1701 */
1702 msg += 1 + 1 + 4 + 28;
1703 if (msg > end)
1704 return;
1705
1706 /* Next, is session id:
1707 * if present, we have to jump by length + 1 for the size information
1708 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001709 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001710 if (msg[0] > 0)
1711 msg += msg[0];
1712 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001713 if (msg > end)
1714 return;
1715
1716 /* Next two bytes are the ciphersuite length. */
1717 if (msg + 2 > end)
1718 return;
1719 rec_len = (msg[0] << 8) + msg[1];
1720 msg += 2;
1721 if (msg + rec_len > end || msg + rec_len < msg)
1722 return;
1723
Willy Tarreaubafbe012017-11-24 17:34:44 +01001724 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001725 if (!capture)
1726 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001727 /* Compute the xxh64 of the ciphersuite. */
1728 capture->xxh64 = XXH64(msg, rec_len, 0);
1729
1730 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001731 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1732 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001733 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001734
1735 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001736}
1737
Emeric Brun29f037d2014-04-25 19:05:36 +02001738/* Callback is called for ssl protocol analyse */
1739void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1740{
Emeric Brun29f037d2014-04-25 19:05:36 +02001741#ifdef TLS1_RT_HEARTBEAT
1742 /* test heartbeat received (write_p is set to 0
1743 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001744 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001745 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001746 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001747 const unsigned char *p = buf;
1748 unsigned int payload;
1749
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001750 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001751
1752 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1753 if (*p != TLS1_HB_REQUEST)
1754 return;
1755
Willy Tarreauaeed6722014-04-25 23:59:58 +02001756 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001757 goto kill_it;
1758
1759 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001760 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001761 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001762 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001763 /* We have a clear heartbleed attack (CVE-2014-0160), the
1764 * advertised payload is larger than the advertised packet
1765 * length, so we have garbage in the buffer between the
1766 * payload and the end of the buffer (p+len). We can't know
1767 * if the SSL stack is patched, and we don't know if we can
1768 * safely wipe out the area between p+3+len and payload.
1769 * So instead, we prevent the response from being sent by
1770 * setting the max_send_fragment to 0 and we report an SSL
1771 * error, which will kill this connection. It will be reported
1772 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001773 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1774 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001775 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001776 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1777 return;
1778 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001779#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001780 if (global_ssl.capture_cipherlist > 0)
1781 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001782}
1783
Bernard Spil13c53f82018-02-15 13:34:58 +01001784#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001785static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1786 const unsigned char *in, unsigned int inlen,
1787 void *arg)
1788{
1789 struct server *srv = arg;
1790
1791 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1792 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1793 return SSL_TLSEXT_ERR_OK;
1794 return SSL_TLSEXT_ERR_NOACK;
1795}
1796#endif
1797
1798#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001799/* This callback is used so that the server advertises the list of
1800 * negociable protocols for NPN.
1801 */
1802static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1803 unsigned int *len, void *arg)
1804{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001805 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001806
1807 *data = (const unsigned char *)conf->npn_str;
1808 *len = conf->npn_len;
1809 return SSL_TLSEXT_ERR_OK;
1810}
1811#endif
1812
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001813#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001814/* This callback is used so that the server advertises the list of
1815 * negociable protocols for ALPN.
1816 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001817static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1818 unsigned char *outlen,
1819 const unsigned char *server,
1820 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001821{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001822 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001823
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001824 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1825 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1826 return SSL_TLSEXT_ERR_NOACK;
1827 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001828 return SSL_TLSEXT_ERR_OK;
1829}
1830#endif
1831
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001832#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001833#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001834
Christopher Faulet30548802015-06-11 13:39:32 +02001835/* Create a X509 certificate with the specified servername and serial. This
1836 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001837static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001838ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001839{
Christopher Faulet7969a332015-10-09 11:15:03 +02001840 X509 *cacert = bind_conf->ca_sign_cert;
1841 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001842 SSL_CTX *ssl_ctx = NULL;
1843 X509 *newcrt = NULL;
1844 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001845 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001846 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001847 X509_NAME *name;
1848 const EVP_MD *digest;
1849 X509V3_CTX ctx;
1850 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001851 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001852
Christopher Faulet48a83322017-07-28 16:56:09 +02001853 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001854#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001855 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1856#else
1857 tmp_ssl = SSL_new(bind_conf->default_ctx);
1858 if (tmp_ssl)
1859 pkey = SSL_get_privatekey(tmp_ssl);
1860#endif
1861 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001862 goto mkcert_error;
1863
1864 /* Create the certificate */
1865 if (!(newcrt = X509_new()))
1866 goto mkcert_error;
1867
1868 /* Set version number for the certificate (X509v3) and the serial
1869 * number */
1870 if (X509_set_version(newcrt, 2L) != 1)
1871 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001872 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001873
1874 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001875 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1876 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001877 goto mkcert_error;
1878
1879 /* set public key in the certificate */
1880 if (X509_set_pubkey(newcrt, pkey) != 1)
1881 goto mkcert_error;
1882
1883 /* Set issuer name from the CA */
1884 if (!(name = X509_get_subject_name(cacert)))
1885 goto mkcert_error;
1886 if (X509_set_issuer_name(newcrt, name) != 1)
1887 goto mkcert_error;
1888
1889 /* Set the subject name using the same, but the CN */
1890 name = X509_NAME_dup(name);
1891 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
1892 (const unsigned char *)servername,
1893 -1, -1, 0) != 1) {
1894 X509_NAME_free(name);
1895 goto mkcert_error;
1896 }
1897 if (X509_set_subject_name(newcrt, name) != 1) {
1898 X509_NAME_free(name);
1899 goto mkcert_error;
1900 }
1901 X509_NAME_free(name);
1902
1903 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001904 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001905 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
1906 for (i = 0; i < X509V3_EXT_SIZE; i++) {
1907 X509_EXTENSION *ext;
1908
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001909 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001910 goto mkcert_error;
1911 if (!X509_add_ext(newcrt, ext, -1)) {
1912 X509_EXTENSION_free(ext);
1913 goto mkcert_error;
1914 }
1915 X509_EXTENSION_free(ext);
1916 }
1917
1918 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001919
1920 key_type = EVP_PKEY_base_id(capkey);
1921
1922 if (key_type == EVP_PKEY_DSA)
1923 digest = EVP_sha1();
1924 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001925 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001926 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02001927 digest = EVP_sha256();
1928 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02001929#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02001930 int nid;
1931
1932 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
1933 goto mkcert_error;
1934 if (!(digest = EVP_get_digestbynid(nid)))
1935 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02001936#else
1937 goto mkcert_error;
1938#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02001939 }
1940
Christopher Faulet31af49d2015-06-09 17:29:50 +02001941 if (!(X509_sign(newcrt, capkey, digest)))
1942 goto mkcert_error;
1943
1944 /* Create and set the new SSL_CTX */
1945 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
1946 goto mkcert_error;
1947 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1948 goto mkcert_error;
1949 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
1950 goto mkcert_error;
1951 if (!SSL_CTX_check_private_key(ssl_ctx))
1952 goto mkcert_error;
1953
1954 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02001955
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001956#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001957 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001958#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001959#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
1960 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001961 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001962 EC_KEY *ecc;
1963 int nid;
1964
1965 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
1966 goto end;
1967 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
1968 goto end;
1969 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
1970 EC_KEY_free(ecc);
1971 }
1972#endif
1973 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02001974 return ssl_ctx;
1975
1976 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001977 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001978 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001979 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
1980 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001981 return NULL;
1982}
1983
Christopher Faulet7969a332015-10-09 11:15:03 +02001984SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001985ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02001986{
Willy Tarreau07d94e42018-09-20 10:57:52 +02001987 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01001988 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001989
Olivier Houchard66ab4982019-02-26 18:37:15 +01001990 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02001991}
1992
Christopher Faulet30548802015-06-11 13:39:32 +02001993/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02001994 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02001995SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02001996ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02001997{
1998 struct lru64 *lru = NULL;
1999
2000 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002001 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002002 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002003 if (lru && lru->domain) {
2004 if (ssl)
2005 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002006 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002007 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002008 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002009 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002010 }
2011 return NULL;
2012}
2013
Emeric Brun821bb9b2017-06-15 16:37:39 +02002014/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2015 * function is not thread-safe, it should only be used to check if a certificate
2016 * exists in the lru cache (with no warranty it will not be removed by another
2017 * thread). It is kept for backward compatibility. */
2018SSL_CTX *
2019ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2020{
2021 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2022}
2023
Christopher Fauletd2cab922015-07-28 16:03:47 +02002024/* Set a certificate int the LRU cache used to store generated
2025 * certificate. Return 0 on success, otherwise -1 */
2026int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002027ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002028{
2029 struct lru64 *lru = NULL;
2030
2031 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002032 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002033 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002034 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002035 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002036 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002037 }
Christopher Faulet30548802015-06-11 13:39:32 +02002038 if (lru->domain && lru->data)
2039 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002040 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002041 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002042 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002043 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002044 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002045}
2046
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002047/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002048unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002049ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002050{
2051 return XXH32(data, len, ssl_ctx_lru_seed);
2052}
2053
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002054/* Generate a cert and immediately assign it to the SSL session so that the cert's
2055 * refcount is maintained regardless of the cert's presence in the LRU cache.
2056 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002057static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002058ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002059{
2060 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002061 SSL_CTX *ssl_ctx = NULL;
2062 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002063 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002064
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002065 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002066 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002067 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002068 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002069 if (lru && lru->domain)
2070 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002071 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002072 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002073 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002074 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002075 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002076 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002077 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002078 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002079 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002080 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002081 SSL_set_SSL_CTX(ssl, ssl_ctx);
2082 /* No LRU cache, this CTX will be released as soon as the session dies */
2083 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002084 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002085 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002086 return 0;
2087}
2088static int
2089ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2090{
2091 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002092 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002093
2094 conn_get_to_addr(conn);
2095 if (conn->flags & CO_FL_ADDR_TO_SET) {
2096 key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002097 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002098 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002099 }
2100 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002101}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002102#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002103
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002104#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002105typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2106
2107static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002108{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002109#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002110 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002111 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2112#endif
2113}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002114static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2115 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002116 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2117}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002118static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002119#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002120 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002121 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2122#endif
2123}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002124static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002125#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002126 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002127 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2128#endif
2129}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002130/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002131static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2132/* Unusable in this context. */
2133static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2134static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2135static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2136static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2137static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002138#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002139typedef enum { SET_MIN, SET_MAX } set_context_func;
2140
2141static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2142 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002143 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2144}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002145static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2146 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2147 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2148}
2149static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2150 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002151 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2152}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002153static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2154 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2155 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2156}
2157static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2158 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002159 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2160}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002161static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2162 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2163 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2164}
2165static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2166 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002167 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2168}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002169static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2170 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2171 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2172}
2173static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002174#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002175 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002176 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2177#endif
2178}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002179static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2180#if SSL_OP_NO_TLSv1_3
2181 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2182 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002183#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002184}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002185#endif
2186static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2187static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002188
2189static struct {
2190 int option;
2191 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002192 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2193 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002194 const char *name;
2195} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002196 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2197 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2198 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2199 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2200 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2201 {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 +02002202};
2203
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002204static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2205{
2206 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2207 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2208 SSL_set_SSL_CTX(ssl, ctx);
2209}
2210
Willy Tarreau5db847a2019-05-09 14:13:35 +02002211#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002212
2213static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2214{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002215 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002216 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002217
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002218 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2219 return SSL_TLSEXT_ERR_OK;
2220 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002221}
2222
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002223#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002224static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2225{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002226 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002227#else
2228static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2229{
2230#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002231 struct connection *conn;
2232 struct bind_conf *s;
2233 const uint8_t *extension_data;
2234 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002235 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002236
2237 char *wildp = NULL;
2238 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002239 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002240 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002241 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002242 int i;
2243
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002244 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002245 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002246
Olivier Houchard9679ac92017-10-27 14:58:08 +02002247 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002248 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002249#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002250 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2251 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002252#else
2253 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2254#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002255 /*
2256 * The server_name extension was given too much extensibility when it
2257 * was written, so parsing the normal case is a bit complex.
2258 */
2259 size_t len;
2260 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002261 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002262 /* Extract the length of the supplied list of names. */
2263 len = (*extension_data++) << 8;
2264 len |= *extension_data++;
2265 if (len + 2 != extension_len)
2266 goto abort;
2267 /*
2268 * The list in practice only has a single element, so we only consider
2269 * the first one.
2270 */
2271 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2272 goto abort;
2273 extension_len = len - 1;
2274 /* Now we can finally pull out the byte array with the actual hostname. */
2275 if (extension_len <= 2)
2276 goto abort;
2277 len = (*extension_data++) << 8;
2278 len |= *extension_data++;
2279 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2280 || memchr(extension_data, 0, len) != NULL)
2281 goto abort;
2282 servername = extension_data;
2283 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002284 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002285#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2286 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002287 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002288 }
2289#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002290 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002291 if (!s->strict_sni) {
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002292 ssl_sock_switchctx_set(ssl, s->default_ctx);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002293 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002294 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002295 goto abort;
2296 }
2297
2298 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002299#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002300 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002301#else
2302 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2303#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002304 uint8_t sign;
2305 size_t len;
2306 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002307 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002308 len = (*extension_data++) << 8;
2309 len |= *extension_data++;
2310 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002311 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002312 if (len % 2 != 0)
2313 goto abort;
2314 for (; len > 0; len -= 2) {
2315 extension_data++; /* hash */
2316 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002317 switch (sign) {
2318 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002319 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002320 break;
2321 case TLSEXT_signature_ecdsa:
2322 has_ecdsa_sig = 1;
2323 break;
2324 default:
2325 continue;
2326 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002327 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002328 break;
2329 }
2330 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002331 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002332 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002333 }
2334 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002335 const SSL_CIPHER *cipher;
2336 size_t len;
2337 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002338 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002339#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002340 len = ctx->cipher_suites_len;
2341 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002342#else
2343 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2344#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002345 if (len % 2 != 0)
2346 goto abort;
2347 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002348#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002349 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002350 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002351#else
2352 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2353#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002354 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002355 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002356 break;
2357 }
2358 }
2359 }
2360
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002361 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002362 trash.area[i] = tolower(servername[i]);
2363 if (!wildp && (trash.area[i] == '.'))
2364 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002365 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002366 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002367
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002368
William Lallemand6c98f492020-08-14 14:43:35 +02002369 /* Look for an ECDSA, RSA and DSA certificate, first in the single
2370 * name and if not found in the wildcard */
Emmanuel Hocdetcc957c32019-11-06 16:05:34 +01002371 for (i = 0; i < 2; i++) {
2372 if (i == 0) /* lookup in full qualified names */
2373 node = ebst_lookup(&s->sni_ctx, trash.area);
2374 else if (i == 1 && wildp) /* lookup in wildcards names */
2375 node = ebst_lookup(&s->sni_w_ctx, wildp);
2376 else
2377 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002378 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdetcc957c32019-11-06 16:05:34 +01002379 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002380 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002381 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002382 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002383 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002384 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002385 break;
2386 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002387 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002388 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002389 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002390 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002391 if (!node_anonymous)
2392 node_anonymous = n;
2393 break;
2394 }
2395 }
2396 }
William Lallemand6c98f492020-08-14 14:43:35 +02002397 }
2398 /* Once the certificates are found, select them depending on what is
2399 * supported in the client and by key_signature priority order: EDSA >
2400 * RSA > DSA */
2401 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2402 : ((has_rsa_sig && node_rsa) ? node_rsa
2403 : (node_anonymous ? node_anonymous
2404 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2405 : node_rsa /* no rsa signature case (far far away) */
2406 )));
2407 if (node) {
2408 /* switch ctx */
2409 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2410 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
2411 if (conf) {
2412 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2413 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2414 if (conf->early_data)
2415 allow_early = 1;
Emmanuel Hocdetcc957c32019-11-06 16:05:34 +01002416 }
William Lallemand6c98f492020-08-14 14:43:35 +02002417 goto allow_early;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002418 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002419#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002420 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002421 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002422 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002423 }
2424#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002425 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002426 /* no certificate match, is the default_ctx */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002427 ssl_sock_switchctx_set(ssl, s->default_ctx);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002428 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002429allow_early:
2430#ifdef OPENSSL_IS_BORINGSSL
2431 if (allow_early)
2432 SSL_set_early_data_enabled(ssl, 1);
2433#else
2434 if (!allow_early)
2435 SSL_set_max_early_data(ssl, 0);
2436#endif
2437 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002438 abort:
2439 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2440 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002441#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002442 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002443#else
2444 *al = SSL_AD_UNRECOGNIZED_NAME;
2445 return 0;
2446#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002447}
2448
2449#else /* OPENSSL_IS_BORINGSSL */
2450
Emeric Brunfc0421f2012-09-07 17:30:07 +02002451/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2452 * warning when no match is found, which implies the default (first) cert
2453 * will keep being used.
2454 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002455static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002456{
2457 const char *servername;
2458 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002459 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002460 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002461 int i;
2462 (void)al; /* shut gcc stupid warning */
2463
2464 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002465 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002466#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002467 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2468 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002469#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002470 if (s->strict_sni)
2471 return SSL_TLSEXT_ERR_ALERT_FATAL;
2472 ssl_sock_switchctx_set(ssl, s->default_ctx);
2473 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002474 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002475
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002476 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002477 if (!servername[i])
2478 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002479 trash.area[i] = tolower(servername[i]);
2480 if (!wildp && (trash.area[i] == '.'))
2481 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002482 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002483 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002484
Emmanuel Hocdet26b7b802019-11-04 15:49:46 +01002485 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002486 /* lookup in full qualified names */
Emmanuel Hocdet26b7b802019-11-04 15:49:46 +01002487 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2488 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002489 if (!container_of(n, struct sni_ctx, name)->neg) {
2490 node = n;
2491 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002492 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002493 }
2494 if (!node && wildp) {
2495 /* lookup in wildcards names */
Emmanuel Hocdet26b7b802019-11-04 15:49:46 +01002496 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2497 /* lookup a not neg filter */
2498 if (!container_of(n, struct sni_ctx, name)->neg) {
2499 node = n;
2500 break;
2501 }
2502 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002503 }
Emmanuel Hocdet26b7b802019-11-04 15:49:46 +01002504 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002505#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002506 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2507 /* switch ctx done in ssl_sock_generate_certificate */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002508 return SSL_TLSEXT_ERR_OK;
2509 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002510#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002511 if (s->strict_sni)
2512 return SSL_TLSEXT_ERR_ALERT_FATAL;
2513 ssl_sock_switchctx_set(ssl, s->default_ctx);
2514 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002515 }
2516
2517 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002518 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002519 return SSL_TLSEXT_ERR_OK;
2520}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002521#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002522#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2523
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002524#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002525
2526static DH * ssl_get_dh_1024(void)
2527{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002528 static unsigned char dh1024_p[]={
2529 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2530 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2531 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2532 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2533 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2534 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2535 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2536 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2537 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2538 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2539 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2540 };
2541 static unsigned char dh1024_g[]={
2542 0x02,
2543 };
2544
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002545 BIGNUM *p;
2546 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002547 DH *dh = DH_new();
2548 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002549 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2550 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002551
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002552 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002553 DH_free(dh);
2554 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002555 } else {
2556 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002557 }
2558 }
2559 return dh;
2560}
2561
2562static DH *ssl_get_dh_2048(void)
2563{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002564 static unsigned char dh2048_p[]={
2565 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2566 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2567 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2568 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2569 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2570 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2571 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2572 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2573 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2574 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2575 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2576 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2577 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2578 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2579 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2580 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2581 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2582 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2583 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2584 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2585 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2586 0xB7,0x1F,0x77,0xF3,
2587 };
2588 static unsigned char dh2048_g[]={
2589 0x02,
2590 };
2591
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002592 BIGNUM *p;
2593 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002594 DH *dh = DH_new();
2595 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002596 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2597 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002598
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002599 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002600 DH_free(dh);
2601 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002602 } else {
2603 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002604 }
2605 }
2606 return dh;
2607}
2608
2609static DH *ssl_get_dh_4096(void)
2610{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002611 static unsigned char dh4096_p[]={
2612 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2613 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2614 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2615 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2616 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2617 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2618 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2619 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2620 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2621 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2622 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2623 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2624 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2625 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2626 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2627 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2628 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2629 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2630 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2631 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2632 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2633 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2634 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2635 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2636 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2637 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2638 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2639 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2640 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2641 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2642 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2643 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2644 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2645 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2646 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2647 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2648 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2649 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2650 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2651 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2652 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2653 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2654 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002655 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002656 static unsigned char dh4096_g[]={
2657 0x02,
2658 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002659
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002660 BIGNUM *p;
2661 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002662 DH *dh = DH_new();
2663 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002664 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2665 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002666
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002667 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002668 DH_free(dh);
2669 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002670 } else {
2671 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002672 }
2673 }
2674 return dh;
2675}
2676
2677/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002678 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002679static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2680{
2681 DH *dh = NULL;
2682 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002683 int type;
2684
2685 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002686
2687 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2688 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2689 */
2690 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2691 keylen = EVP_PKEY_bits(pkey);
2692 }
2693
Willy Tarreauef934602016-12-22 23:12:01 +01002694 if (keylen > global_ssl.default_dh_param) {
2695 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002696 }
2697
Remi Gacogned3a341a2015-05-29 16:26:17 +02002698 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002699 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002700 }
2701 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002702 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002703 }
2704 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002705 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002706 }
2707
2708 return dh;
2709}
2710
Remi Gacogne47783ef2015-05-29 15:53:22 +02002711static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002712{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002713 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002714 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002715
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002716 if (in == NULL)
2717 goto end;
2718
Remi Gacogne47783ef2015-05-29 15:53:22 +02002719 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002720 goto end;
2721
Remi Gacogne47783ef2015-05-29 15:53:22 +02002722 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2723
2724end:
2725 if (in)
2726 BIO_free(in);
2727
Emeric Brune1b4ed42018-08-16 15:14:12 +02002728 ERR_clear_error();
2729
Remi Gacogne47783ef2015-05-29 15:53:22 +02002730 return dh;
2731}
2732
2733int ssl_sock_load_global_dh_param_from_file(const char *filename)
2734{
2735 global_dh = ssl_sock_get_dh_from_file(filename);
2736
2737 if (global_dh) {
2738 return 0;
2739 }
2740
2741 return -1;
2742}
2743
Emeric Bruncfc1afe2019-10-17 13:27:40 +02002744/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
2745 * If there is no DH paramater availaible in the ckchs, the global
2746 * DH parameter is loaded into the SSL_CTX and if there is no
2747 * DH parameter available in ckchs nor in global, the default
2748 * DH parameters are applied on the SSL_CTX.
2749 * Returns a bitfield containing the flags:
2750 * ERR_FATAL in any fatal error case
2751 * ERR_ALERT if a reason of the error is availabine in err
2752 * ERR_WARN if a warning is available into err
2753 * The value 0 means there is no error nor warning and
2754 * the operation succeed.
2755 */
2756static int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file, char **err)
2757 {
2758 int ret = 0;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002759 DH *dh = ssl_sock_get_dh_from_file(file);
2760
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002761 if (dh) {
Emeric Brund6de1512019-10-17 14:53:03 +02002762 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
2763 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
2764 err && *err ? *err : "", file);
2765#if defined(SSL_CTX_set_dh_auto)
2766 SSL_CTX_set_dh_auto(ctx, 1);
2767 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2768 err && *err ? *err : "");
2769#else
2770 memprintf(err, "%s, DH ciphers won't be available.\n",
2771 err && *err ? *err : "");
2772#endif
2773 ret |= ERR_WARN;
2774 goto end;
2775 }
Remi Gacogne4f902b82015-05-28 16:23:00 +02002776
2777 if (ssl_dh_ptr_index >= 0) {
2778 /* store a pointer to the DH params to avoid complaining about
2779 ssl-default-dh-param not being set for this SSL_CTX */
2780 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
2781 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002782 }
Remi Gacogne47783ef2015-05-29 15:53:22 +02002783 else if (global_dh) {
Emeric Brund6de1512019-10-17 14:53:03 +02002784 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
2785 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
2786 err && *err ? *err : "", file);
2787#if defined(SSL_CTX_set_dh_auto)
2788 SSL_CTX_set_dh_auto(ctx, 1);
2789 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2790 err && *err ? *err : "");
2791#else
2792 memprintf(err, "%s, DH ciphers won't be available.\n",
2793 err && *err ? *err : "");
2794#endif
2795 ret |= ERR_WARN;
2796 goto end;
2797 }
Remi Gacogne47783ef2015-05-29 15:53:22 +02002798 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002799 else {
Emeric Brun41fdb3c2013-04-26 11:05:44 +02002800 /* Clear openssl global errors stack */
2801 ERR_clear_error();
2802
Willy Tarreauef934602016-12-22 23:12:01 +01002803 if (global_ssl.default_dh_param <= 1024) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002804 /* we are limited to DH parameter of 1024 bits anyway */
Remi Gacognec7e12632016-07-02 16:26:10 +02002805 if (local_dh_1024 == NULL)
2806 local_dh_1024 = ssl_get_dh_1024();
2807
Emeric Bruncfc1afe2019-10-17 13:27:40 +02002808 if (local_dh_1024 == NULL) {
2809 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
2810 err && *err ? *err : "", file);
2811 ret |= ERR_ALERT | ERR_FATAL;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002812 goto end;
Emeric Bruncfc1afe2019-10-17 13:27:40 +02002813 }
Willy Tarreau6e774b42014-04-25 21:35:23 +02002814
Emeric Brund6de1512019-10-17 14:53:03 +02002815 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
2816 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
2817 err && *err ? *err : "", file);
2818#if defined(SSL_CTX_set_dh_auto)
2819 SSL_CTX_set_dh_auto(ctx, 1);
2820 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2821 err && *err ? *err : "");
2822#else
2823 memprintf(err, "%s, DH ciphers won't be available.\n",
2824 err && *err ? *err : "");
2825#endif
2826 ret |= ERR_WARN;
2827 goto end;
2828 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002829 }
2830 else {
2831 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
2832 }
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002833 }
Emeric Brun644cde02012-12-14 11:21:13 +01002834
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002835end:
2836 if (dh)
2837 DH_free(dh);
2838
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002839 return ret;
2840}
2841#endif
2842
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002843static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf,
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002844 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002845{
2846 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002847 int wild = 0, neg = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002848 struct ebmb_node *node;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002849
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002850 if (*name == '!') {
2851 neg = 1;
2852 name++;
2853 }
2854 if (*name == '*') {
2855 wild = 1;
2856 name++;
2857 }
2858 /* !* filter is a nop */
2859 if (neg && wild)
2860 return order;
2861 if (*name) {
2862 int j, len;
2863 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002864 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002865 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002866 if (j >= trash.size)
William Lallemand24e292c2019-10-03 23:46:33 +02002867 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002868 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002869
2870 /* Check for duplicates. */
2871 if (wild)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002872 node = ebst_lookup(&s->sni_w_ctx, trash.area);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002873 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002874 node = ebst_lookup(&s->sni_ctx, trash.area);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002875 for (; node; node = ebmb_next_dup(node)) {
2876 sc = ebmb_entry(node, struct sni_ctx, name);
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002877 if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg)
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002878 return order;
2879 }
2880
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002881 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002882 if (!sc)
William Lallemand24e292c2019-10-03 23:46:33 +02002883 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002884 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002885 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002886 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002887 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002888 sc->order = order++;
2889 sc->neg = neg;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01002890 if (kinfo.sig != TLSEXT_signature_anonymous)
2891 SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002892 if (wild)
2893 ebst_insert(&s->sni_w_ctx, &sc->name);
2894 else
2895 ebst_insert(&s->sni_ctx, &sc->name);
2896 }
2897 return order;
2898}
2899
yanbzhu488a4d22015-12-01 15:16:07 -05002900
2901/* The following code is used for loading multiple crt files into
2902 * SSL_CTX's based on CN/SAN
2903 */
Willy Tarreau5db847a2019-05-09 14:13:35 +02002904#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05002905/* This is used to preload the certifcate, private key
2906 * and Cert Chain of a file passed in via the crt
2907 * argument
2908 *
2909 * This way, we do not have to read the file multiple times
2910 */
2911struct cert_key_and_chain {
2912 X509 *cert;
2913 EVP_PKEY *key;
2914 unsigned int num_chain_certs;
2915 /* This is an array of X509 pointers */
2916 X509 **chain_certs;
2917};
2918
yanbzhu08ce6ab2015-12-02 13:01:29 -05002919#define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES))
2920
2921struct key_combo_ctx {
2922 SSL_CTX *ctx;
2923 int order;
2924};
2925
2926/* Map used for processing multiple keypairs for a single purpose
2927 *
2928 * This maps CN/SNI name to certificate type
2929 */
2930struct sni_keytype {
2931 int keytypes; /* BITMASK for keytypes */
2932 struct ebmb_node name; /* node holding the servername value */
2933};
2934
2935
yanbzhu488a4d22015-12-01 15:16:07 -05002936/* Frees the contents of a cert_key_and_chain
2937 */
2938static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
2939{
2940 int i;
2941
2942 if (!ckch)
2943 return;
2944
2945 /* Free the certificate and set pointer to NULL */
2946 if (ckch->cert)
2947 X509_free(ckch->cert);
2948 ckch->cert = NULL;
2949
2950 /* Free the key and set pointer to NULL */
2951 if (ckch->key)
2952 EVP_PKEY_free(ckch->key);
2953 ckch->key = NULL;
2954
2955 /* Free each certificate in the chain */
2956 for (i = 0; i < ckch->num_chain_certs; i++) {
2957 if (ckch->chain_certs[i])
2958 X509_free(ckch->chain_certs[i]);
2959 }
2960
2961 /* Free the chain obj itself and set to NULL */
2962 if (ckch->num_chain_certs > 0) {
2963 free(ckch->chain_certs);
2964 ckch->num_chain_certs = 0;
2965 ckch->chain_certs = NULL;
2966 }
2967
2968}
2969
2970/* checks if a key and cert exists in the ckch
2971 */
2972static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
2973{
2974 return (ckch->cert != NULL && ckch->key != NULL);
2975}
2976
2977
2978/* Loads the contents of a crt file (path) into a cert_key_and_chain
2979 * This allows us to carry the contents of the file without having to
2980 * read the file multiple times.
2981 *
2982 * returns:
2983 * 0 on Success
2984 * 1 on SSL Failure
2985 * 2 on file not found
2986 */
2987static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
2988{
2989
2990 BIO *in;
2991 X509 *ca = NULL;
2992 int ret = 1;
2993
2994 ssl_sock_free_cert_key_and_chain_contents(ckch);
2995
2996 in = BIO_new(BIO_s_file());
2997 if (in == NULL)
2998 goto end;
2999
3000 if (BIO_read_filename(in, path) <= 0)
3001 goto end;
3002
yanbzhu488a4d22015-12-01 15:16:07 -05003003 /* Read Private Key */
3004 ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3005 if (ckch->key == NULL) {
3006 memprintf(err, "%sunable to load private key from file '%s'.\n",
3007 err && *err ? *err : "", path);
3008 goto end;
3009 }
3010
Willy Tarreaubb137a82016-04-06 19:02:38 +02003011 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003012 if (BIO_reset(in) == -1) {
3013 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3014 err && *err ? *err : "", path);
3015 goto end;
3016 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003017
3018 /* Read Certificate */
3019 ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3020 if (ckch->cert == NULL) {
3021 memprintf(err, "%sunable to load certificate from file '%s'.\n",
3022 err && *err ? *err : "", path);
3023 goto end;
3024 }
3025
yanbzhu488a4d22015-12-01 15:16:07 -05003026 /* Read Certificate Chain */
3027 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3028 /* Grow the chain certs */
3029 ckch->num_chain_certs++;
3030 ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *)));
3031
3032 /* use - 1 here since we just incremented it above */
3033 ckch->chain_certs[ckch->num_chain_certs - 1] = ca;
3034 }
3035 ret = ERR_get_error();
3036 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3037 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
3038 err && *err ? *err : "", path);
3039 ret = 1;
3040 goto end;
3041 }
3042
3043 ret = 0;
3044
3045end:
3046
3047 ERR_clear_error();
3048 if (in)
3049 BIO_free(in);
3050
3051 /* Something went wrong in one of the reads */
3052 if (ret != 0)
3053 ssl_sock_free_cert_key_and_chain_contents(ckch);
3054
3055 return ret;
3056}
3057
3058/* Loads the info in ckch into ctx
Emeric Brun394701d2019-10-17 13:25:14 +02003059 * Returns a bitfield containing the flags:
3060 * ERR_FATAL in any fatal error case
3061 * ERR_ALERT if the reason of the error is available in err
3062 * ERR_WARN if a warning is available into err
3063 * The value 0 means there is no error nor warning and
3064 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003065 */
3066static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3067{
3068 int i = 0;
Emeric Brun394701d2019-10-17 13:25:14 +02003069 int errcode = 0;
yanbzhu488a4d22015-12-01 15:16:07 -05003070
3071 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3072 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3073 err && *err ? *err : "", path);
Emeric Brun394701d2019-10-17 13:25:14 +02003074 errcode |= ERR_ALERT | ERR_FATAL;
3075 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003076 }
3077
3078 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3079 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3080 err && *err ? *err : "", path);
Emeric Brun394701d2019-10-17 13:25:14 +02003081 errcode |= ERR_ALERT | ERR_FATAL;
3082 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003083 }
3084
yanbzhu488a4d22015-12-01 15:16:07 -05003085 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
3086 for (i = 0; i < ckch->num_chain_certs; i++) {
3087 if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05003088 memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3089 err && *err ? *err : "", (i+1), path);
Emeric Brun394701d2019-10-17 13:25:14 +02003090 errcode |= ERR_ALERT | ERR_FATAL;
3091 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003092 }
3093 }
3094
3095 if (SSL_CTX_check_private_key(ctx) <= 0) {
3096 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
3097 err && *err ? *err : "", path);
Emeric Brun394701d2019-10-17 13:25:14 +02003098 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu488a4d22015-12-01 15:16:07 -05003099 }
3100
Emeric Brun394701d2019-10-17 13:25:14 +02003101 end:
3102 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003103}
3104
yanbzhu08ce6ab2015-12-02 13:01:29 -05003105
William Lallemand4801c702019-10-04 17:36:55 +02003106static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003107{
3108 struct sni_keytype *s_kt = NULL;
3109 struct ebmb_node *node;
3110 int i;
3111
3112 for (i = 0; i < trash.size; i++) {
3113 if (!str[i])
3114 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003115 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003116 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003117 trash.area[i] = 0;
3118 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003119 if (!node) {
3120 /* CN not found in tree */
3121 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3122 /* Using memcpy here instead of strncpy.
3123 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3124 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3125 */
William Lallemand4801c702019-10-04 17:36:55 +02003126 if (!s_kt)
3127 return -1;
3128
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003129 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003130 s_kt->keytypes = 0;
3131 ebst_insert(sni_keytypes, &s_kt->name);
3132 } else {
3133 /* CN found in tree */
3134 s_kt = container_of(node, struct sni_keytype, name);
3135 }
3136
3137 /* Mark that this CN has the keytype of key_index via keytypes mask */
3138 s_kt->keytypes |= 1<<key_index;
3139
William Lallemand4801c702019-10-04 17:36:55 +02003140 return 0;
3141
yanbzhu08ce6ab2015-12-02 13:01:29 -05003142}
3143
3144
3145/* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files.
3146 * If any are found, group these files into a set of SSL_CTX*
3147 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3148 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003149 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003150 *
Willy Tarreaub131c872019-10-16 16:42:19 +02003151 * Returns a set of ERR_* flags possibly with an error in <err>.
3152 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003153 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003154static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3155 char **sni_filter, int fcount, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003156{
3157 char fp[MAXPATHLEN+1] = {0};
3158 int n = 0;
3159 int i = 0;
3160 struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} };
3161 struct eb_root sni_keytypes_map = { {0} };
3162 struct ebmb_node *node;
3163 struct ebmb_node *next;
3164 /* Array of SSL_CTX pointers corresponding to each possible combo
3165 * of keytypes
3166 */
3167 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Willy Tarreaub131c872019-10-16 16:42:19 +02003168 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003169 X509_NAME *xname = NULL;
3170 char *str = NULL;
3171#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3172 STACK_OF(GENERAL_NAME) *names = NULL;
3173#endif
3174
3175 /* Load all possible certs and keys */
3176 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3177 struct stat buf;
3178
3179 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3180 if (stat(fp, &buf) == 0) {
3181 if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) {
Willy Tarreaub131c872019-10-16 16:42:19 +02003182 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003183 goto end;
3184 }
3185 }
3186 }
3187
3188 /* Process each ckch and update keytypes for each CN/SAN
3189 * for example, if CN/SAN www.a.com is associated with
3190 * certs with keytype 0 and 2, then at the end of the loop,
3191 * www.a.com will have:
3192 * keyindex = 0 | 1 | 4 = 5
3193 */
3194 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand4801c702019-10-04 17:36:55 +02003195 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003196
3197 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3198 continue;
3199
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003200 if (fcount) {
William Lallemand4801c702019-10-04 17:36:55 +02003201 for (i = 0; i < fcount; i++) {
3202 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3203 if (ret < 0) {
3204 memprintf(err, "%sunable to allocate SSL context.\n",
3205 err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003206 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand4801c702019-10-04 17:36:55 +02003207 goto end;
3208 }
3209 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003210 } else {
3211 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3212 * so the line that contains logic is marked via comments
3213 */
3214 xname = X509_get_subject_name(certs_and_keys[n].cert);
3215 i = -1;
3216 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3217 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003218 ASN1_STRING *value;
3219 value = X509_NAME_ENTRY_get_data(entry);
3220 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003221 /* Important line is here */
William Lallemand4801c702019-10-04 17:36:55 +02003222 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003223
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003224 OPENSSL_free(str);
3225 str = NULL;
William Lallemand4801c702019-10-04 17:36:55 +02003226 if (ret < 0) {
3227 memprintf(err, "%sunable to allocate SSL context.\n",
3228 err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003229 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand4801c702019-10-04 17:36:55 +02003230 goto end;
3231 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003232 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003233 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003234
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003235 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003236#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003237 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3238 if (names) {
3239 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3240 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003241
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003242 if (name->type == GEN_DNS) {
3243 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3244 /* Important line is here */
William Lallemand4801c702019-10-04 17:36:55 +02003245 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003246
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003247 OPENSSL_free(str);
3248 str = NULL;
William Lallemand4801c702019-10-04 17:36:55 +02003249 if (ret < 0) {
3250 memprintf(err, "%sunable to allocate SSL context.\n",
3251 err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003252 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand4801c702019-10-04 17:36:55 +02003253 goto end;
3254 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003255 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003256 }
3257 }
3258 }
3259 }
3260#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3261 }
3262
3263 /* If no files found, return error */
3264 if (eb_is_empty(&sni_keytypes_map)) {
3265 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3266 err && *err ? *err : "", path);
Willy Tarreaub131c872019-10-16 16:42:19 +02003267 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003268 goto end;
3269 }
3270
3271 /* We now have a map of CN/SAN to keytypes that are loaded in
3272 * Iterate through the map to create the SSL_CTX's (if needed)
3273 * and add each CTX to the SNI tree
3274 *
3275 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003276 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003277 * combination is denoted by the key in the map. Each key
3278 * has a value between 1 and 2^n - 1. Conveniently, the array
3279 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3280 * entry in the array to correspond to the unique combo (key)
3281 * associated with i. This unique key combo (i) will be associated
3282 * with combos[i-1]
3283 */
3284
3285 node = ebmb_first(&sni_keytypes_map);
3286 while (node) {
3287 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003288 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003289 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003290
3291 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3292 i = container_of(node, struct sni_keytype, name)->keytypes;
3293 cur_ctx = key_combos[i-1].ctx;
3294
3295 if (cur_ctx == NULL) {
3296 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003297 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003298 if (cur_ctx == NULL) {
3299 memprintf(err, "%sunable to allocate SSL context.\n",
3300 err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003301 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003302 goto end;
3303 }
3304
yanbzhube2774d2015-12-10 15:07:30 -05003305 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003306 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3307 if (i & (1<<n)) {
3308 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003309 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Brun394701d2019-10-17 13:25:14 +02003310 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3311 if (errcode & ERR_CODE) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05003312 SSL_CTX_free(cur_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003313 goto end;
3314 }
yanbzhube2774d2015-12-10 15:07:30 -05003315
3316#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
3317 /* Load OCSP Info into context */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003318 if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) {
yanbzhube2774d2015-12-10 15:07:30 -05003319 if (err)
3320 memprintf(err, "%s '%s.ocsp' is present and activates OCSP but it is impossible to compute the OCSP certificate ID (maybe the issuer could not be found)'.\n",
Bertrand Jacquin5424ee02016-11-13 16:37:14 +00003321 *err ? *err : "", cur_file);
yanbzhube2774d2015-12-10 15:07:30 -05003322 SSL_CTX_free(cur_ctx);
Willy Tarreaub131c872019-10-16 16:42:19 +02003323 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhube2774d2015-12-10 15:07:30 -05003324 goto end;
3325 }
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02003326#elif (defined OPENSSL_IS_BORINGSSL)
3327 ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file);
yanbzhube2774d2015-12-10 15:07:30 -05003328#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003329 }
3330 }
3331
3332 /* Load DH params into the ctx to support DHE keys */
3333#ifndef OPENSSL_NO_DH
3334 if (ssl_dh_ptr_index >= 0)
3335 SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL);
3336
Emeric Bruncfc1afe2019-10-17 13:27:40 +02003337 errcode |= ssl_sock_load_dh_params(cur_ctx, NULL, err);
3338 if (errcode & ERR_CODE) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05003339 if (err)
3340 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3341 *err ? *err : "", path);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003342 goto end;
3343 }
3344#endif
3345
3346 /* Update key_combos */
3347 key_combos[i-1].ctx = cur_ctx;
3348 }
3349
3350 /* Update SNI Tree */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003351 key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf,
William Lallemand24e292c2019-10-03 23:46:33 +02003352 kinfo, str, key_combos[i-1].order);
3353 if (key_combos[i-1].order < 0) {
3354 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003355 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand24e292c2019-10-03 23:46:33 +02003356 goto end;
3357 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003358 node = ebmb_next(node);
3359 }
3360
3361
3362 /* Mark a default context if none exists, using the ctx that has the most shared keys */
3363 if (!bind_conf->default_ctx) {
3364 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
3365 if (key_combos[i].ctx) {
3366 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003367 bind_conf->default_ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003368 break;
3369 }
3370 }
3371 }
3372
3373end:
3374
3375 if (names)
3376 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
3377
3378 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3379 ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]);
3380
3381 node = ebmb_first(&sni_keytypes_map);
3382 while (node) {
3383 next = ebmb_next(node);
3384 ebmb_delete(node);
William Lallemande92c0302019-10-04 17:24:39 +02003385 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05003386 node = next;
3387 }
3388
Willy Tarreaub131c872019-10-16 16:42:19 +02003389 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003390}
3391#else
3392/* This is a dummy, that just logs an error and returns error */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003393static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3394 char **sni_filter, int fcount, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003395{
3396 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3397 err && *err ? *err : "", path, strerror(errno));
3398 return 1;
3399}
3400
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003401#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05003402
Emeric Brunfc0421f2012-09-07 17:30:07 +02003403/* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if
3404 * an early error happens and the caller must call SSL_CTX_free() by itelf.
3405 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003406static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s,
3407 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003408{
3409 BIO *in;
3410 X509 *x = NULL, *ca;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003411 int i, err;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003412 int ret = -1;
3413 int order = 0;
3414 X509_NAME *xname;
3415 char *str;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003416 pem_password_cb *passwd_cb;
3417 void *passwd_cb_userdata;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003418 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003419 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003420
Emeric Brunfc0421f2012-09-07 17:30:07 +02003421#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3422 STACK_OF(GENERAL_NAME) *names;
3423#endif
3424
3425 in = BIO_new(BIO_s_file());
3426 if (in == NULL)
3427 goto end;
3428
3429 if (BIO_read_filename(in, file) <= 0)
3430 goto end;
3431
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003432
3433 passwd_cb = SSL_CTX_get_default_passwd_cb(ctx);
3434 passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx);
3435
3436 x = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003437 if (x == NULL)
3438 goto end;
3439
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003440 pkey = X509_get_pubkey(x);
3441 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003442 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003443 switch(EVP_PKEY_base_id(pkey)) {
3444 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003445 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003446 break;
3447 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003448 kinfo.sig = TLSEXT_signature_ecdsa;
3449 break;
3450 case EVP_PKEY_DSA:
3451 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003452 break;
3453 }
3454 EVP_PKEY_free(pkey);
3455 }
3456
Emeric Brun50bcecc2013-04-22 13:05:23 +02003457 if (fcount) {
William Lallemand24e292c2019-10-03 23:46:33 +02003458 while (fcount--) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003459 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, sni_filter[fcount], order);
William Lallemand24e292c2019-10-03 23:46:33 +02003460 if (order < 0)
3461 goto end;
3462 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003463 }
3464 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003465#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003466 names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
3467 if (names) {
3468 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3469 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
3470 if (name->type == GEN_DNS) {
3471 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003472 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003473 OPENSSL_free(str);
William Lallemand24e292c2019-10-03 23:46:33 +02003474 if (order < 0)
3475 goto end;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003476 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003477 }
3478 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003479 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003480 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003481#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003482 xname = X509_get_subject_name(x);
3483 i = -1;
3484 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3485 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003486 ASN1_STRING *value;
3487
3488 value = X509_NAME_ENTRY_get_data(entry);
3489 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003490 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003491 OPENSSL_free(str);
William Lallemand24e292c2019-10-03 23:46:33 +02003492 if (order < 0)
3493 goto end;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003494 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003495 }
3496 }
3497
3498 ret = 0; /* the caller must not free the SSL_CTX argument anymore */
3499 if (!SSL_CTX_use_certificate(ctx, x))
3500 goto end;
3501
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003502#ifdef SSL_CTX_clear_extra_chain_certs
3503 SSL_CTX_clear_extra_chain_certs(ctx);
3504#else
Emeric Brunfc0421f2012-09-07 17:30:07 +02003505 if (ctx->extra_certs != NULL) {
3506 sk_X509_pop_free(ctx->extra_certs, X509_free);
3507 ctx->extra_certs = NULL;
3508 }
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003509#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02003510
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003511 while ((ca = PEM_read_bio_X509(in, NULL, passwd_cb, passwd_cb_userdata))) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003512 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3513 X509_free(ca);
3514 goto end;
3515 }
3516 }
3517
3518 err = ERR_get_error();
3519 if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
3520 /* we successfully reached the last cert in the file */
3521 ret = 1;
3522 }
3523 ERR_clear_error();
3524
3525end:
3526 if (x)
3527 X509_free(x);
3528
3529 if (in)
3530 BIO_free(in);
3531
3532 return ret;
3533}
3534
Willy Tarreaub131c872019-10-16 16:42:19 +02003535/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003536static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3537 char **sni_filter, int fcount, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003538{
Emeric Bruncfc1afe2019-10-17 13:27:40 +02003539 int errcode = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003540 int ret;
3541 SSL_CTX *ctx;
3542
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003543 ctx = SSL_CTX_new(SSLv23_server_method());
Emeric Brunfc0421f2012-09-07 17:30:07 +02003544 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003545 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3546 err && *err ? *err : "", path);
Willy Tarreaub131c872019-10-16 16:42:19 +02003547 return ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003548 }
3549
3550 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003551 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
3552 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003553 SSL_CTX_free(ctx);
Willy Tarreaub131c872019-10-16 16:42:19 +02003554 return ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003555 }
3556
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003557 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf, ssl_conf, sni_filter, fcount);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003558 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003559 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
3560 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003561 if (ret < 0) /* serious error, must do that ourselves */
3562 SSL_CTX_free(ctx);
Willy Tarreaub131c872019-10-16 16:42:19 +02003563 return ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003564 }
Emeric Brun61694ab2012-10-26 13:35:33 +02003565
3566 if (SSL_CTX_check_private_key(ctx) <= 0) {
3567 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
3568 err && *err ? *err : "", path);
Willy Tarreaub131c872019-10-16 16:42:19 +02003569 return ERR_ALERT | ERR_FATAL;
Emeric Brun61694ab2012-10-26 13:35:33 +02003570 }
3571
Emeric Brunfc0421f2012-09-07 17:30:07 +02003572 /* we must not free the SSL_CTX anymore below, since it's already in
3573 * the tree, so it will be discovered and cleaned in time.
3574 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003575#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +02003576 /* store a NULL pointer to indicate we have not yet loaded
3577 a custom DH param file */
3578 if (ssl_dh_ptr_index >= 0) {
3579 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3580 }
3581
Emeric Bruncfc1afe2019-10-17 13:27:40 +02003582 errcode |= ssl_sock_load_dh_params(ctx, path, err);
3583 if (errcode & ERR_CODE) {
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003584 if (err)
3585 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3586 *err ? *err : "", path);
Emeric Bruncfc1afe2019-10-17 13:27:40 +02003587 return errcode;
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003588 }
3589#endif
3590
Lukas Tribuse4e30f72014-12-09 16:32:51 +01003591#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02003592 ret = ssl_sock_load_ocsp(ctx, path);
3593 if (ret < 0) {
3594 if (err)
3595 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",
3596 *err ? *err : "", path);
Willy Tarreaub131c872019-10-16 16:42:19 +02003597 return ERR_ALERT | ERR_FATAL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02003598 }
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02003599#elif (defined OPENSSL_IS_BORINGSSL)
3600 ssl_sock_set_ocsp_response_from_file(ctx, path);
Emeric Brun4147b2e2014-06-16 18:36:30 +02003601#endif
3602
Willy Tarreau5db847a2019-05-09 14:13:35 +02003603#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01003604 if (sctl_ex_index >= 0) {
3605 ret = ssl_sock_load_sctl(ctx, path);
3606 if (ret < 0) {
3607 if (err)
3608 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
3609 *err ? *err : "", path);
Willy Tarreaub131c872019-10-16 16:42:19 +02003610 return ERR_ALERT | ERR_FATAL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01003611 }
3612 }
3613#endif
3614
Emeric Brunfc0421f2012-09-07 17:30:07 +02003615#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003616 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003617 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
3618 err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003619 return ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003620 }
3621#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003622 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003623 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003624 bind_conf->default_ssl_conf = ssl_conf;
3625 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003626
Emeric Bruncfc1afe2019-10-17 13:27:40 +02003627 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003628}
3629
Willy Tarreaub131c872019-10-16 16:42:19 +02003630
3631/* Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau03209342016-12-22 17:08:28 +01003632int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003633{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003634 struct dirent **de_list;
3635 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003636 DIR *dir;
3637 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01003638 char *end;
3639 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02003640 int cfgerr = 0;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003641#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05003642 int is_bundle;
3643 int j;
3644#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02003645
yanbzhu08ce6ab2015-12-02 13:01:29 -05003646 if (stat(path, &buf) == 0) {
3647 dir = opendir(path);
3648 if (!dir)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003649 return ssl_sock_load_cert_file(path, bind_conf, NULL, NULL, 0, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003650
yanbzhu08ce6ab2015-12-02 13:01:29 -05003651 /* strip trailing slashes, including first one */
3652 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
3653 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003654
yanbzhu08ce6ab2015-12-02 13:01:29 -05003655 n = scandir(path, &de_list, 0, alphasort);
3656 if (n < 0) {
3657 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
3658 err && *err ? *err : "", path, strerror(errno));
Willy Tarreaub131c872019-10-16 16:42:19 +02003659 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003660 }
3661 else {
3662 for (i = 0; i < n; i++) {
3663 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02003664
yanbzhu08ce6ab2015-12-02 13:01:29 -05003665 end = strrchr(de->d_name, '.');
3666 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
3667 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003668
yanbzhu08ce6ab2015-12-02 13:01:29 -05003669 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
3670 if (stat(fp, &buf) != 0) {
3671 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3672 err && *err ? *err : "", fp, strerror(errno));
Willy Tarreaub131c872019-10-16 16:42:19 +02003673 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003674 goto ignore_entry;
3675 }
3676 if (!S_ISREG(buf.st_mode))
3677 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05003678
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003679#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05003680 is_bundle = 0;
3681 /* Check if current entry in directory is part of a multi-cert bundle */
3682
3683 if (end) {
3684 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
3685 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
3686 is_bundle = 1;
3687 break;
3688 }
3689 }
3690
3691 if (is_bundle) {
yanbzhu63ea8462015-12-09 13:35:14 -05003692 int dp_len;
3693
3694 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05003695
3696 /* increment i and free de until we get to a non-bundle cert
3697 * Note here that we look at de_list[i + 1] before freeing de
Willy Tarreau5b8a8652019-10-29 10:48:50 +01003698 * this is important since ignore_entry will free de. This also
3699 * guarantees that de->d_name continues to hold the same prefix.
yanbzhu63ea8462015-12-09 13:35:14 -05003700 */
Willy Tarreau5b8a8652019-10-29 10:48:50 +01003701 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
yanbzhu63ea8462015-12-09 13:35:14 -05003702 free(de);
3703 i++;
3704 de = de_list[i];
3705 }
3706
Willy Tarreau5b8a8652019-10-29 10:48:50 +01003707 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
Willy Tarreaub131c872019-10-16 16:42:19 +02003708 cfgerr |= ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05003709 /* Successfully processed the bundle */
3710 goto ignore_entry;
3711 }
3712 }
3713
3714#endif
Willy Tarreaub131c872019-10-16 16:42:19 +02003715 cfgerr |= ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003716ignore_entry:
3717 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003718 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003719 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003720 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003721 closedir(dir);
3722 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003723 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003724
Willy Tarreaub131c872019-10-16 16:42:19 +02003725 cfgerr |= ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003726
Emeric Brunfc0421f2012-09-07 17:30:07 +02003727 return cfgerr;
3728}
3729
Thierry Fournier383085f2013-01-24 14:15:43 +01003730/* Make sure openssl opens /dev/urandom before the chroot. The work is only
3731 * done once. Zero is returned if the operation fails. No error is returned
3732 * if the random is said as not implemented, because we expect that openssl
3733 * will use another method once needed.
3734 */
3735static int ssl_initialize_random()
3736{
3737 unsigned char random;
3738 static int random_initialized = 0;
3739
3740 if (!random_initialized && RAND_bytes(&random, 1) != 0)
3741 random_initialized = 1;
3742
3743 return random_initialized;
3744}
3745
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003746/* release ssl bind conf */
3747void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003748{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003749 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01003750#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003751 free(conf->npn_str);
3752 conf->npn_str = NULL;
3753#endif
3754#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
3755 free(conf->alpn_str);
3756 conf->alpn_str = NULL;
3757#endif
3758 free(conf->ca_file);
3759 conf->ca_file = NULL;
3760 free(conf->crl_file);
3761 conf->crl_file = NULL;
3762 free(conf->ciphers);
3763 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02003764#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02003765 free(conf->ciphersuites);
3766 conf->ciphersuites = NULL;
3767#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01003768 free(conf->curves);
3769 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003770 free(conf->ecdhe);
3771 conf->ecdhe = NULL;
3772 }
3773}
3774
Willy Tarreaub131c872019-10-16 16:42:19 +02003775/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003776int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
3777{
3778 char thisline[CRT_LINESIZE];
3779 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003780 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05003781 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003782 int linenum = 0;
3783 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003784
Willy Tarreauad1731d2013-04-02 17:35:58 +02003785 if ((f = fopen(file, "r")) == NULL) {
3786 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaub131c872019-10-16 16:42:19 +02003787 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003788 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003789
3790 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003791 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003792 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003793 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003794 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003795 char *crt_path;
3796 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003797
3798 linenum++;
3799 end = line + strlen(line);
3800 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
3801 /* Check if we reached the limit and the last char is not \n.
3802 * Watch out for the last line without the terminating '\n'!
3803 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02003804 memprintf(err, "line %d too long in file '%s', limit is %d characters",
3805 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaub131c872019-10-16 16:42:19 +02003806 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003807 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003808 }
3809
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003810 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02003811 newarg = 1;
3812 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003813 if (*line == '#' || *line == '\n' || *line == '\r') {
3814 /* end of string, end of loop */
3815 *line = 0;
3816 break;
Willy Tarreau26e4ab42020-02-25 07:51:59 +01003817 } else if (isspace((unsigned char)*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02003818 newarg = 1;
3819 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003820 } else if (*line == '[') {
3821 if (ssl_b) {
3822 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003823 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003824 break;
3825 }
3826 if (!arg) {
3827 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003828 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003829 break;
3830 }
3831 ssl_b = arg;
3832 newarg = 1;
3833 *line = 0;
3834 } else if (*line == ']') {
3835 if (ssl_e) {
3836 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003837 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02003838 break;
3839 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003840 if (!ssl_b) {
3841 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003842 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003843 break;
3844 }
3845 ssl_e = arg;
3846 newarg = 1;
3847 *line = 0;
3848 } else if (newarg) {
3849 if (arg == MAX_CRT_ARGS) {
3850 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003851 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003852 break;
3853 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02003854 newarg = 0;
3855 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003856 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02003857 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003858 }
Willy Tarreaub131c872019-10-16 16:42:19 +02003859 if (cfgerr & ERR_CODE)
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003860 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003861 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003862
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003863 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003864 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003865 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003866
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003867 crt_path = args[0];
3868 if (*crt_path != '/' && global_ssl.crt_base) {
3869 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
3870 memprintf(err, "'%s' : path too long on line %d in file '%s'",
3871 crt_path, linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003872 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003873 break;
3874 }
3875 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
3876 crt_path = path;
3877 }
3878
3879 ssl_conf = calloc(1, sizeof *ssl_conf);
3880 cur_arg = ssl_b ? ssl_b : 1;
3881 while (cur_arg < ssl_e) {
3882 newarg = 0;
3883 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
3884 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
3885 newarg = 1;
Willy Tarreaub131c872019-10-16 16:42:19 +02003886 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003887 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
3888 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
3889 args[cur_arg], linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003890 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003891 }
3892 cur_arg += 1 + ssl_bind_kws[i].skip;
3893 break;
3894 }
3895 }
3896 if (!cfgerr && !newarg) {
3897 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
3898 args[cur_arg], linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003899 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003900 break;
3901 }
3902 }
Willy Tarreaub131c872019-10-16 16:42:19 +02003903
3904 if (cfgerr & ERR_CODE) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003905 ssl_sock_free_ssl_conf(ssl_conf);
3906 free(ssl_conf);
3907 ssl_conf = NULL;
3908 break;
3909 }
3910
3911 if (stat(crt_path, &buf) == 0) {
Willy Tarreaub131c872019-10-16 16:42:19 +02003912 cfgerr |= ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf,
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003913 &args[cur_arg], arg - cur_arg - 1, err);
Willy Tarreaub131c872019-10-16 16:42:19 +02003914 } else {
3915 cfgerr |= ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf,
3916 &args[cur_arg], arg - cur_arg - 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05003917 }
3918
Willy Tarreaub131c872019-10-16 16:42:19 +02003919 if (cfgerr & ERR_CODE) {
Willy Tarreauad1731d2013-04-02 17:35:58 +02003920 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003921 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003922 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003923 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003924 fclose(f);
3925 return cfgerr;
3926}
3927
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003928/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003929static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003930ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003931{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003932 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003933 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003934 SSL_OP_ALL | /* all known workarounds for bugs */
3935 SSL_OP_NO_SSLv2 |
3936 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003937 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02003938 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003939 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02003940 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003941 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003942 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003943 SSL_MODE_ENABLE_PARTIAL_WRITE |
3944 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01003945 SSL_MODE_RELEASE_BUFFERS |
3946 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02003947 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003948 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003949 int flags = MC_SSL_O_ALL;
3950 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003951
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003952 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003953 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003954
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003955 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01003956 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
3957 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3958 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003959 else
3960 flags = conf_ssl_methods->flags;
3961
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003962 min = conf_ssl_methods->min;
3963 max = conf_ssl_methods->max;
3964 /* start with TLSv10 to remove SSLv3 per default */
3965 if (!min && (!max || max >= CONF_TLSV10))
3966 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003967 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003968 if (min)
3969 flags |= (methodVersions[min].flag - 1);
3970 if (max)
3971 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003972 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003973 min = max = CONF_TLSV_NONE;
3974 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003975 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003976 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003977 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003978 if (min) {
3979 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003980 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
3981 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3982 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
3983 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003984 hole = 0;
3985 }
3986 max = i;
3987 }
3988 else {
3989 min = max = i;
3990 }
3991 }
3992 else {
3993 if (min)
3994 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003995 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003996 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003997 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
3998 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003999 cfgerr += 1;
4000 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004001 /* save real min/max in bind_conf */
4002 conf_ssl_methods->min = min;
4003 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004004
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004005#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004006 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004007 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004008 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004009 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004010 else
William Lallemand6dbb9a12020-06-11 17:34:00 +02004011 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) {
4012 /* clear every version flags in case SSL_CTX_new()
4013 * returns an SSL_CTX with disabled versions */
4014 SSL_CTX_clear_options(ctx, methodVersions[i].option);
4015
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004016 if (flags & methodVersions[i].flag)
4017 options |= methodVersions[i].option;
William Lallemand6dbb9a12020-06-11 17:34:00 +02004018
4019 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004020#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004021 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004022 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4023 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004024#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004025
4026 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4027 options |= SSL_OP_NO_TICKET;
4028 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4029 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004030
4031#ifdef SSL_OP_NO_RENEGOTIATION
4032 options |= SSL_OP_NO_RENEGOTIATION;
4033#endif
4034
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004035 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004036
Willy Tarreau5db847a2019-05-09 14:13:35 +02004037#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004038 if (global_ssl.async)
4039 mode |= SSL_MODE_ASYNC;
4040#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004041 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004042 if (global_ssl.life_time)
4043 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004044
4045#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4046#ifdef OPENSSL_IS_BORINGSSL
4047 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4048 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Willy Tarreau5db847a2019-05-09 14:13:35 +02004049#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houcharde82c1d42019-12-17 15:39:54 +01004050 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004051 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004052 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4053 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004054#else
4055 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004056#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004057 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004058#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004059 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004060}
4061
William Lallemand4f45bb92017-10-30 20:08:51 +01004062
4063static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4064{
4065 if (first == block) {
4066 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4067 if (first->len > 0)
4068 sh_ssl_sess_tree_delete(sh_ssl_sess);
4069 }
4070}
4071
4072/* return first block from sh_ssl_sess */
4073static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4074{
4075 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4076
4077}
4078
4079/* store a session into the cache
4080 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4081 * data: asn1 encoded session
4082 * data_len: asn1 encoded session length
4083 * Returns 1 id session was stored (else 0)
4084 */
4085static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4086{
4087 struct shared_block *first;
4088 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4089
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004090 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004091 if (!first) {
4092 /* Could not retrieve enough free blocks to store that session */
4093 return 0;
4094 }
4095
4096 /* STORE the key in the first elem */
4097 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4098 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4099 first->len = sizeof(struct sh_ssl_sess_hdr);
4100
4101 /* it returns the already existing node
4102 or current node if none, never returns null */
4103 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4104 if (oldsh_ssl_sess != sh_ssl_sess) {
4105 /* NOTE: Row couldn't be in use because we lock read & write function */
4106 /* release the reserved row */
4107 shctx_row_dec_hot(ssl_shctx, first);
4108 /* replace the previous session already in the tree */
4109 sh_ssl_sess = oldsh_ssl_sess;
4110 /* ignore the previous session data, only use the header */
4111 first = sh_ssl_sess_first_block(sh_ssl_sess);
4112 shctx_row_inc_hot(ssl_shctx, first);
4113 first->len = sizeof(struct sh_ssl_sess_hdr);
4114 }
4115
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004116 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004117 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004118 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004119 }
4120
4121 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004122
4123 return 1;
4124}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004125
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004126/* SSL callback used when a new session is created while connecting to a server */
4127static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4128{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004129 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004130 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004131
Willy Tarreau07d94e42018-09-20 10:57:52 +02004132 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004133
Olivier Houcharde6060c52017-11-16 17:42:52 +01004134 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4135 int len;
4136 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004137
Olivier Houcharde6060c52017-11-16 17:42:52 +01004138 len = i2d_SSL_SESSION(sess, NULL);
4139 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4140 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4141 } else {
4142 free(s->ssl_ctx.reused_sess[tid].ptr);
4143 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4144 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4145 }
4146 if (s->ssl_ctx.reused_sess[tid].ptr) {
4147 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4148 &ptr);
4149 }
4150 } else {
4151 free(s->ssl_ctx.reused_sess[tid].ptr);
4152 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4153 }
4154
4155 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004156}
4157
Olivier Houcharde6060c52017-11-16 17:42:52 +01004158
William Lallemanded0b5ad2017-10-30 19:36:36 +01004159/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004160int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004161{
4162 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4163 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4164 unsigned char *p;
4165 int data_len;
Emeric Brun7b34de32019-10-08 18:27:37 +02004166 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004167 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004168
4169 /* Session id is already stored in to key and session id is known
4170 * so we dont store it to keep size.
Emeric Brun7b34de32019-10-08 18:27:37 +02004171 * note: SSL_SESSION_set1_id is using
4172 * a memcpy so we need to use a different pointer
4173 * than sid_data or sid_ctx_data to avoid valgrind
4174 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004175 */
4176
4177 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Brun7b34de32019-10-08 18:27:37 +02004178
4179 /* copy value in an other buffer */
4180 memcpy(encid, sid_data, sid_length);
4181
4182 /* pad with 0 */
4183 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4184 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4185
4186 /* force length to zero to avoid ASN1 encoding */
4187 SSL_SESSION_set1_id(sess, encid, 0);
4188
4189 /* force length to zero to avoid ASN1 encoding */
4190 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004191
4192 /* check if buffer is large enough for the ASN1 encoded session */
4193 data_len = i2d_SSL_SESSION(sess, NULL);
4194 if (data_len > SHSESS_MAX_DATA_LEN)
4195 goto err;
4196
4197 p = encsess;
4198
4199 /* process ASN1 session encoding before the lock */
4200 i2d_SSL_SESSION(sess, &p);
4201
William Lallemanded0b5ad2017-10-30 19:36:36 +01004202
William Lallemanda3c77cf2017-10-30 23:44:40 +01004203 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004204 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004205 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004206 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004207err:
4208 /* reset original length values */
Emeric Brun7b34de32019-10-08 18:27:37 +02004209 SSL_SESSION_set1_id(sess, encid, sid_length);
4210 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004211
4212 return 0; /* do not increment session reference count */
4213}
4214
4215/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004216SSL_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 +01004217{
William Lallemand4f45bb92017-10-30 20:08:51 +01004218 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004219 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4220 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004221 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004222 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004223
4224 global.shctx_lookups++;
4225
4226 /* allow the session to be freed automatically by openssl */
4227 *do_copy = 0;
4228
4229 /* tree key is zeros padded sessionid */
4230 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4231 memcpy(tmpkey, key, key_len);
4232 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4233 key = tmpkey;
4234 }
4235
4236 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004237 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004238
4239 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004240 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4241 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004242 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004243 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004244 global.shctx_misses++;
4245 return NULL;
4246 }
4247
William Lallemand4f45bb92017-10-30 20:08:51 +01004248 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4249 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004250
William Lallemand4f45bb92017-10-30 20:08:51 +01004251 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 +01004252
William Lallemanda3c77cf2017-10-30 23:44:40 +01004253 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004254
4255 /* decode ASN1 session */
4256 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004257 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004258 /* Reset session id and session id contenxt */
4259 if (sess) {
4260 SSL_SESSION_set1_id(sess, key, key_len);
4261 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4262 }
4263
4264 return sess;
4265}
4266
William Lallemand4f45bb92017-10-30 20:08:51 +01004267
William Lallemanded0b5ad2017-10-30 19:36:36 +01004268/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004269void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004270{
William Lallemand4f45bb92017-10-30 20:08:51 +01004271 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004272 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4273 unsigned int sid_length;
4274 const unsigned char *sid_data;
4275 (void)ctx;
4276
4277 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4278 /* tree key is zeros padded sessionid */
4279 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4280 memcpy(tmpkey, sid_data, sid_length);
4281 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4282 sid_data = tmpkey;
4283 }
4284
William Lallemanda3c77cf2017-10-30 23:44:40 +01004285 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004286
4287 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004288 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4289 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004290 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004291 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004292 }
4293
4294 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004295 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004296}
4297
4298/* Set session cache mode to server and disable openssl internal cache.
4299 * Set shared cache callbacks on an ssl context.
4300 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004301void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004302{
4303 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4304
4305 if (!ssl_shctx) {
4306 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4307 return;
4308 }
4309
4310 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4311 SSL_SESS_CACHE_NO_INTERNAL |
4312 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4313
4314 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004315 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4316 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4317 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004318}
4319
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004320int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx)
4321{
4322 struct proxy *curproxy = bind_conf->frontend;
4323 int cfgerr = 0;
4324 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004325 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004326 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004327#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004328 const char *conf_ciphersuites;
4329#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004330 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004331
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004332 if (ssl_conf) {
4333 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4334 int i, min, max;
4335 int flags = MC_SSL_O_ALL;
4336
4337 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004338 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4339 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004340 if (min)
4341 flags |= (methodVersions[min].flag - 1);
4342 if (max)
4343 flags |= ~((methodVersions[max].flag << 1) - 1);
4344 min = max = CONF_TLSV_NONE;
4345 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4346 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4347 if (min)
4348 max = i;
4349 else
4350 min = max = i;
4351 }
4352 /* save real min/max */
4353 conf_ssl_methods->min = min;
4354 conf_ssl_methods->max = max;
4355 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004356 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4357 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004358 cfgerr += 1;
4359 }
4360 }
4361
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004362 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01004363 case SSL_SOCK_VERIFY_NONE:
4364 verify = SSL_VERIFY_NONE;
4365 break;
4366 case SSL_SOCK_VERIFY_OPTIONAL:
4367 verify = SSL_VERIFY_PEER;
4368 break;
4369 case SSL_SOCK_VERIFY_REQUIRED:
4370 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
4371 break;
4372 }
4373 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
4374 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004375 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
4376 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
4377 if (ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004378 /* load CAfile to verify */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004379 if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004380 ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
4381 curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02004382 cfgerr++;
4383 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02004384 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
4385 /* set CA names for client cert request, function returns void */
4386 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file));
4387 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004388 }
Emeric Brun850efd52014-01-29 12:24:34 +01004389 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004390 ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
4391 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun850efd52014-01-29 12:24:34 +01004392 cfgerr++;
4393 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004394#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004395 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004396 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
4397
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004398 if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004399 ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
4400 curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02004401 cfgerr++;
4402 }
Emeric Brun561e5742012-10-02 15:20:55 +02004403 else {
4404 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4405 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004406 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004407#endif
Emeric Brun644cde02012-12-14 11:21:13 +01004408 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02004409 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004410#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02004411 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004412 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004413 ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
4414 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004415 cfgerr++;
4416 }
4417 }
4418#endif
4419
William Lallemand4f45bb92017-10-30 20:08:51 +01004420 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004421 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
4422 if (conf_ciphers &&
4423 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004424 ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
4425 curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004426 cfgerr++;
4427 }
4428
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004429#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004430 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
4431 if (conf_ciphersuites &&
4432 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
4433 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
4434 curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
4435 cfgerr++;
4436 }
4437#endif
4438
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004439#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02004440 /* If tune.ssl.default-dh-param has not been set,
4441 neither has ssl-default-dh-file and no static DH
4442 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01004443 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02004444 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02004445 (ssl_dh_ptr_index == -1 ||
4446 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004447 STACK_OF(SSL_CIPHER) * ciphers = NULL;
4448 const SSL_CIPHER * cipher = NULL;
4449 char cipher_description[128];
4450 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
4451 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
4452 which is not ephemeral DH. */
4453 const char dhe_description[] = " Kx=DH ";
4454 const char dhe_export_description[] = " Kx=DH(";
4455 int idx = 0;
4456 int dhe_found = 0;
4457 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02004458
Remi Gacogne23d5d372014-10-10 17:04:26 +02004459 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004460
Remi Gacogne23d5d372014-10-10 17:04:26 +02004461 if (ssl) {
4462 ciphers = SSL_get_ciphers(ssl);
4463
4464 if (ciphers) {
4465 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
4466 cipher = sk_SSL_CIPHER_value(ciphers, idx);
4467 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
4468 if (strstr(cipher_description, dhe_description) != NULL ||
4469 strstr(cipher_description, dhe_export_description) != NULL) {
4470 dhe_found = 1;
4471 break;
4472 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02004473 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004474 }
4475 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02004476 SSL_free(ssl);
4477 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02004478 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004479
Lukas Tribus90132722014-08-18 00:56:33 +02004480 if (dhe_found) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004481 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 +02004482 }
4483
Willy Tarreauef934602016-12-22 23:12:01 +01004484 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004485 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004486
Willy Tarreauef934602016-12-22 23:12:01 +01004487 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004488 if (local_dh_1024 == NULL) {
4489 local_dh_1024 = ssl_get_dh_1024();
4490 }
Willy Tarreauef934602016-12-22 23:12:01 +01004491 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004492 if (local_dh_2048 == NULL) {
4493 local_dh_2048 = ssl_get_dh_2048();
4494 }
Willy Tarreauef934602016-12-22 23:12:01 +01004495 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004496 if (local_dh_4096 == NULL) {
4497 local_dh_4096 = ssl_get_dh_4096();
4498 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004499 }
4500 }
4501 }
4502#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004503
Emeric Brunfc0421f2012-09-07 17:30:07 +02004504 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004505#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02004506 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02004507#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02004508
Bernard Spil13c53f82018-02-15 13:34:58 +01004509#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004510 ssl_conf_cur = NULL;
4511 if (ssl_conf && ssl_conf->npn_str)
4512 ssl_conf_cur = ssl_conf;
4513 else if (bind_conf->ssl_conf.npn_str)
4514 ssl_conf_cur = &bind_conf->ssl_conf;
4515 if (ssl_conf_cur)
4516 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02004517#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01004518#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004519 ssl_conf_cur = NULL;
4520 if (ssl_conf && ssl_conf->alpn_str)
4521 ssl_conf_cur = ssl_conf;
4522 else if (bind_conf->ssl_conf.alpn_str)
4523 ssl_conf_cur = &bind_conf->ssl_conf;
4524 if (ssl_conf_cur)
4525 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02004526#endif
Lukas Tribusd13e9252019-11-24 18:20:40 +01004527#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004528 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
4529 if (conf_curves) {
4530 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004531 ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
4532 curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004533 cfgerr++;
4534 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01004535 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004536 }
4537#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004538#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004539 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02004540 int i;
4541 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004542#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004543 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02004544 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4545 NULL);
4546
4547 if (ecdhe == NULL) {
Eric Salama27c21cd2019-11-20 11:33:40 +01004548 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02004549 return cfgerr;
4550 }
4551#else
4552 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
4553 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4554 ECDHE_DEFAULT_CURVE);
4555#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004556
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004557 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02004558 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004559 ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
4560 curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun2b58d042012-09-20 17:10:03 +02004561 cfgerr++;
4562 }
4563 else {
4564 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
4565 EC_KEY_free(ecdh);
4566 }
4567 }
4568#endif
4569
Emeric Brunfc0421f2012-09-07 17:30:07 +02004570 return cfgerr;
4571}
4572
Evan Broderbe554312013-06-27 00:05:25 -07004573static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
4574{
4575 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
4576 size_t prefixlen, suffixlen;
4577
4578 /* Trivial case */
William Lallemandb00a33b2020-09-14 15:20:10 +02004579 if (strcasecmp(pattern, hostname) == 0)
Evan Broderbe554312013-06-27 00:05:25 -07004580 return 1;
4581
Evan Broderbe554312013-06-27 00:05:25 -07004582 /* The rest of this logic is based on RFC 6125, section 6.4.3
4583 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
4584
Emeric Bruna848dae2013-10-08 11:27:28 +02004585 pattern_wildcard = NULL;
4586 pattern_left_label_end = pattern;
4587 while (*pattern_left_label_end != '.') {
4588 switch (*pattern_left_label_end) {
4589 case 0:
4590 /* End of label not found */
4591 return 0;
4592 case '*':
4593 /* If there is more than one wildcards */
4594 if (pattern_wildcard)
4595 return 0;
4596 pattern_wildcard = pattern_left_label_end;
4597 break;
4598 }
4599 pattern_left_label_end++;
4600 }
4601
4602 /* If it's not trivial and there is no wildcard, it can't
4603 * match */
4604 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07004605 return 0;
4606
4607 /* Make sure all labels match except the leftmost */
4608 hostname_left_label_end = strchr(hostname, '.');
4609 if (!hostname_left_label_end
William Lallemandb00a33b2020-09-14 15:20:10 +02004610 || strcasecmp(pattern_left_label_end, hostname_left_label_end) != 0)
Evan Broderbe554312013-06-27 00:05:25 -07004611 return 0;
4612
4613 /* Make sure the leftmost label of the hostname is long enough
4614 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02004615 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07004616 return 0;
4617
4618 /* Finally compare the string on either side of the
4619 * wildcard */
4620 prefixlen = pattern_wildcard - pattern;
4621 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
William Lallemandb00a33b2020-09-14 15:20:10 +02004622 if ((prefixlen && (strncasecmp(pattern, hostname, prefixlen) != 0))
4623 || (suffixlen && (strncasecmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07004624 return 0;
4625
4626 return 1;
4627}
4628
4629static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
4630{
4631 SSL *ssl;
4632 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004633 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004634 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02004635 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07004636
4637 int depth;
4638 X509 *cert;
4639 STACK_OF(GENERAL_NAME) *alt_names;
4640 int i;
4641 X509_NAME *cert_subject;
4642 char *str;
4643
4644 if (ok == 0)
4645 return ok;
4646
4647 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004648 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01004649 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07004650
Willy Tarreauad92a9a2017-07-28 11:38:41 +02004651 /* We're checking if the provided hostnames match the desired one. The
4652 * desired hostname comes from the SNI we presented if any, or if not
4653 * provided then it may have been explicitly stated using a "verifyhost"
4654 * directive. If neither is set, we don't care about the name so the
4655 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02004656 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004657 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02004658 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004659 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02004660 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004661 if (!servername)
4662 return ok;
4663 }
Evan Broderbe554312013-06-27 00:05:25 -07004664
4665 /* We only need to verify the CN on the actual server cert,
4666 * not the indirect CAs */
4667 depth = X509_STORE_CTX_get_error_depth(ctx);
4668 if (depth != 0)
4669 return ok;
4670
4671 /* At this point, the cert is *not* OK unless we can find a
4672 * hostname match */
4673 ok = 0;
4674
4675 cert = X509_STORE_CTX_get_current_cert(ctx);
4676 /* It seems like this might happen if verify peer isn't set */
4677 if (!cert)
4678 return ok;
4679
4680 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
4681 if (alt_names) {
4682 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
4683 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
4684 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004685#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02004686 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
4687#else
Evan Broderbe554312013-06-27 00:05:25 -07004688 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02004689#endif
Evan Broderbe554312013-06-27 00:05:25 -07004690 ok = ssl_sock_srv_hostcheck(str, servername);
4691 OPENSSL_free(str);
4692 }
4693 }
4694 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02004695 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07004696 }
4697
4698 cert_subject = X509_get_subject_name(cert);
4699 i = -1;
4700 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
4701 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004702 ASN1_STRING *value;
4703 value = X509_NAME_ENTRY_get_data(entry);
4704 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07004705 ok = ssl_sock_srv_hostcheck(str, servername);
4706 OPENSSL_free(str);
4707 }
4708 }
4709
Willy Tarreau71d058c2017-07-26 20:09:56 +02004710 /* report the mismatch and indicate if SNI was used or not */
4711 if (!ok && !conn->err_code)
4712 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07004713 return ok;
4714}
4715
Emeric Brun94324a42012-10-11 14:00:19 +02004716/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01004717int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02004718{
Willy Tarreau03209342016-12-22 17:08:28 +01004719 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02004720 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004721 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02004722 SSL_OP_ALL | /* all known workarounds for bugs */
4723 SSL_OP_NO_SSLv2 |
4724 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004725 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02004726 SSL_MODE_ENABLE_PARTIAL_WRITE |
4727 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004728 SSL_MODE_RELEASE_BUFFERS |
4729 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01004730 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004731 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004732 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004733 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004734 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02004735
Thierry Fournier383085f2013-01-24 14:15:43 +01004736 /* Make sure openssl opens /dev/urandom before the chroot */
4737 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004738 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01004739 cfgerr++;
4740 }
4741
Willy Tarreaufce03112015-01-15 21:32:40 +01004742 /* Automatic memory computations need to know we use SSL there */
4743 global.ssl_used_backend = 1;
4744
4745 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02004746 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01004747 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004748 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
4749 curproxy->id, srv->id,
4750 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02004751 cfgerr++;
4752 return cfgerr;
4753 }
4754 }
Christopher Faulet68d35ae2020-03-27 18:55:49 +01004755 if (srv->use_ssl == 1)
Emeric Brun94324a42012-10-11 14:00:19 +02004756 srv->xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02004757
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004758 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004759 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004760 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
4761 proxy_type_str(curproxy), curproxy->id,
4762 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02004763 cfgerr++;
4764 return cfgerr;
4765 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004766
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004767 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004768 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
4769 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4770 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004771 else
4772 flags = conf_ssl_methods->flags;
4773
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004774 /* Real min and max should be determinate with configuration and openssl's capabilities */
4775 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004776 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004777 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004778 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004779
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004780 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004781 min = max = CONF_TLSV_NONE;
4782 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004783 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004784 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004785 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004786 if (min) {
4787 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004788 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
4789 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4790 proxy_type_str(curproxy), curproxy->id, srv->id,
4791 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004792 hole = 0;
4793 }
4794 max = i;
4795 }
4796 else {
4797 min = max = i;
4798 }
4799 }
4800 else {
4801 if (min)
4802 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004803 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004804 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004805 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
4806 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004807 cfgerr += 1;
4808 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004809
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004810#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004811 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004812 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004813 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004814 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004815 else
4816 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4817 if (flags & methodVersions[i].flag)
4818 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004819#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004820 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004821 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4822 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004823#endif
4824
4825 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
4826 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004827 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004828
Willy Tarreau5db847a2019-05-09 14:13:35 +02004829#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004830 if (global_ssl.async)
4831 mode |= SSL_MODE_ASYNC;
4832#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004833 SSL_CTX_set_mode(ctx, mode);
4834 srv->ssl_ctx.ctx = ctx;
4835
Emeric Bruna7aa3092012-10-26 12:58:00 +02004836 if (srv->ssl_ctx.client_crt) {
4837 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 +01004838 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
4839 proxy_type_str(curproxy), curproxy->id,
4840 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004841 cfgerr++;
4842 }
4843 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 +01004844 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
4845 proxy_type_str(curproxy), curproxy->id,
4846 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004847 cfgerr++;
4848 }
4849 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004850 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
4851 proxy_type_str(curproxy), curproxy->id,
4852 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004853 cfgerr++;
4854 }
4855 }
Emeric Brun94324a42012-10-11 14:00:19 +02004856
Emeric Brun850efd52014-01-29 12:24:34 +01004857 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
4858 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01004859 switch (srv->ssl_ctx.verify) {
4860 case SSL_SOCK_VERIFY_NONE:
4861 verify = SSL_VERIFY_NONE;
4862 break;
4863 case SSL_SOCK_VERIFY_REQUIRED:
4864 verify = SSL_VERIFY_PEER;
4865 break;
4866 }
Evan Broderbe554312013-06-27 00:05:25 -07004867 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01004868 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02004869 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01004870 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02004871 if (srv->ssl_ctx.ca_file) {
4872 /* load CAfile to verify */
4873 if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004874 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n",
4875 curproxy->id, srv->id,
4876 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004877 cfgerr++;
4878 }
4879 }
Emeric Brun850efd52014-01-29 12:24:34 +01004880 else {
4881 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01004882 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",
4883 curproxy->id, srv->id,
4884 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004885 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01004886 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
4887 curproxy->id, srv->id,
4888 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004889 cfgerr++;
4890 }
Emeric Brunef42d922012-10-11 16:11:36 +02004891#ifdef X509_V_FLAG_CRL_CHECK
4892 if (srv->ssl_ctx.crl_file) {
4893 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
4894
4895 if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004896 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
4897 curproxy->id, srv->id,
4898 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004899 cfgerr++;
4900 }
4901 else {
4902 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4903 }
4904 }
4905#endif
4906 }
4907
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004908 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
4909 SSL_SESS_CACHE_NO_INTERNAL_STORE);
4910 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02004911 if (srv->ssl_ctx.ciphers &&
4912 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004913 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
4914 curproxy->id, srv->id,
4915 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02004916 cfgerr++;
4917 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004918
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004919#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004920 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00004921 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004922 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
4923 curproxy->id, srv->id,
4924 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
4925 cfgerr++;
4926 }
4927#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01004928#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
4929 if (srv->ssl_ctx.npn_str)
4930 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
4931#endif
4932#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4933 if (srv->ssl_ctx.alpn_str)
4934 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
4935#endif
4936
Emeric Brun94324a42012-10-11 14:00:19 +02004937
4938 return cfgerr;
4939}
4940
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004941/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004942 * be NULL, in which case nothing is done. Returns the number of errors
4943 * encountered.
4944 */
Willy Tarreau03209342016-12-22 17:08:28 +01004945int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004946{
4947 struct ebmb_node *node;
4948 struct sni_ctx *sni;
4949 int err = 0;
4950
Willy Tarreaufce03112015-01-15 21:32:40 +01004951 /* Automatic memory computations need to know we use SSL there */
4952 global.ssl_used_frontend = 1;
4953
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004954 /* Make sure openssl opens /dev/urandom before the chroot */
4955 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004956 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004957 err++;
4958 }
4959 /* Create initial_ctx used to start the ssl connection before do switchctx */
4960 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004961 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004962 /* It should not be necessary to call this function, but it's
4963 necessary first to check and move all initialisation related
4964 to initial_ctx in ssl_sock_initial_ctx. */
4965 err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx);
4966 }
Emeric Brun0bed9942014-10-30 19:25:24 +01004967 if (bind_conf->default_ctx)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004968 err += ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx);
Emeric Brun0bed9942014-10-30 19:25:24 +01004969
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004970 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004971 while (node) {
4972 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004973 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4974 /* only initialize the CTX on its first occurrence and
4975 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004976 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004977 node = ebmb_next(node);
4978 }
4979
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004980 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004981 while (node) {
4982 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004983 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4984 /* only initialize the CTX on its first occurrence and
4985 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004986 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004987 node = ebmb_next(node);
4988 }
4989 return err;
4990}
4991
Willy Tarreau55d37912016-12-21 23:38:39 +01004992/* Prepares all the contexts for a bind_conf and allocates the shared SSL
4993 * context if needed. Returns < 0 on error, 0 on success. The warnings and
4994 * alerts are directly emitted since the rest of the stack does it below.
4995 */
4996int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
4997{
4998 struct proxy *px = bind_conf->frontend;
4999 int alloc_ctx;
5000 int err;
5001
5002 if (!bind_conf->is_ssl) {
5003 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005004 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
5005 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01005006 }
5007 return 0;
5008 }
5009 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005010 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005011 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
5012 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005013 }
5014 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005015 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
5016 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005017 return -1;
5018 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005019 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005020 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005021 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005022 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01005023 sizeof(*sh_ssl_sess_tree),
5024 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005025 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005026 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5027 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");
5028 else
5029 ha_alert("Unable to allocate SSL session cache.\n");
5030 return -1;
5031 }
5032 /* free block callback */
5033 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5034 /* init the root tree within the extra space */
5035 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5036 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005037 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005038 err = 0;
5039 /* initialize all certificate contexts */
5040 err += ssl_sock_prepare_all_ctx(bind_conf);
5041
5042 /* initialize CA variables if the certificates generation is enabled */
5043 err += ssl_sock_load_ca(bind_conf);
5044
5045 return -err;
5046}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005047
5048/* release ssl context allocated for servers. */
5049void ssl_sock_free_srv_ctx(struct server *srv)
5050{
Olivier Houchardc7566002018-11-20 23:33:50 +01005051#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5052 if (srv->ssl_ctx.alpn_str)
5053 free(srv->ssl_ctx.alpn_str);
5054#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005055#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01005056 if (srv->ssl_ctx.npn_str)
5057 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005058#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005059 if (srv->ssl_ctx.ctx)
5060 SSL_CTX_free(srv->ssl_ctx.ctx);
5061}
5062
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005063/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005064 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5065 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005066void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005067{
5068 struct ebmb_node *node, *back;
5069 struct sni_ctx *sni;
5070
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005071 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005072 while (node) {
5073 sni = ebmb_entry(node, struct sni_ctx, name);
5074 back = ebmb_next(node);
5075 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005076 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005077 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005078 ssl_sock_free_ssl_conf(sni->conf);
5079 free(sni->conf);
5080 sni->conf = NULL;
5081 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005082 free(sni);
5083 node = back;
5084 }
5085
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005086 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005087 while (node) {
5088 sni = ebmb_entry(node, struct sni_ctx, name);
5089 back = ebmb_next(node);
5090 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005091 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005092 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005093 ssl_sock_free_ssl_conf(sni->conf);
5094 free(sni->conf);
5095 sni->conf = NULL;
5096 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005097 free(sni);
5098 node = back;
5099 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005100 SSL_CTX_free(bind_conf->initial_ctx);
5101 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005102 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005103 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005104}
5105
Willy Tarreau795cdab2016-12-22 17:30:54 +01005106/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5107void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5108{
5109 ssl_sock_free_ca(bind_conf);
5110 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005111 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005112 free(bind_conf->ca_sign_file);
5113 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005114 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005115 free(bind_conf->keys_ref->filename);
5116 free(bind_conf->keys_ref->tlskeys);
5117 LIST_DEL(&bind_conf->keys_ref->list);
5118 free(bind_conf->keys_ref);
5119 }
5120 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005121 bind_conf->ca_sign_pass = NULL;
5122 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005123}
5124
Christopher Faulet31af49d2015-06-09 17:29:50 +02005125/* Load CA cert file and private key used to generate certificates */
5126int
Willy Tarreau03209342016-12-22 17:08:28 +01005127ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005128{
Willy Tarreau03209342016-12-22 17:08:28 +01005129 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005130 FILE *fp;
5131 X509 *cacert = NULL;
5132 EVP_PKEY *capkey = NULL;
5133 int err = 0;
5134
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005135 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005136 return err;
5137
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005138#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005139 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005140 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005141 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005142 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005143 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005144#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005145
Christopher Faulet31af49d2015-06-09 17:29:50 +02005146 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005147 ha_alert("Proxy '%s': cannot enable certificate generation, "
5148 "no CA certificate File configured at [%s:%d].\n",
5149 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005150 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005151 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005152
5153 /* read in the CA certificate */
5154 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005155 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5156 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005157 goto load_error;
5158 }
5159 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005160 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5161 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005162 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005163 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005164 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005165 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005166 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5167 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005168 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005169 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005170
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005171 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005172 bind_conf->ca_sign_cert = cacert;
5173 bind_conf->ca_sign_pkey = capkey;
5174 return err;
5175
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005176 read_error:
5177 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005178 if (capkey) EVP_PKEY_free(capkey);
5179 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005180 load_error:
5181 bind_conf->generate_certs = 0;
5182 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005183 return err;
5184}
5185
5186/* Release CA cert and private key used to generate certificated */
5187void
5188ssl_sock_free_ca(struct bind_conf *bind_conf)
5189{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005190 if (bind_conf->ca_sign_pkey)
5191 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5192 if (bind_conf->ca_sign_cert)
5193 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005194 bind_conf->ca_sign_pkey = NULL;
5195 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005196}
5197
Emeric Brun46591952012-05-18 15:47:34 +02005198/*
5199 * This function is called if SSL * context is not yet allocated. The function
5200 * is designed to be called before any other data-layer operation and sets the
5201 * handshake flag on the connection. It is safe to call it multiple times.
5202 * It returns 0 on success and -1 in error case.
5203 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005204static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005205{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005206 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005207 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005208 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005209 return 0;
5210
Willy Tarreau3c728722014-01-23 13:50:42 +01005211 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005212 return 0;
5213
Olivier Houchard66ab4982019-02-26 18:37:15 +01005214 ctx = pool_alloc(ssl_sock_ctx_pool);
5215 if (!ctx) {
5216 conn->err_code = CO_ER_SSL_NO_MEM;
5217 return -1;
5218 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005219 ctx->wait_event.tasklet = tasklet_new();
5220 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005221 conn->err_code = CO_ER_SSL_NO_MEM;
5222 pool_free(ssl_sock_ctx_pool, ctx);
5223 return -1;
5224 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005225 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5226 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005227 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005228 ctx->sent_early_data = 0;
Olivier Houchardf6715e72019-12-19 15:02:39 +01005229 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005230 ctx->conn = conn;
Olivier Houchard81284e62019-06-06 13:21:23 +02005231 ctx->send_wait = NULL;
5232 ctx->recv_wait = NULL;
Emeric Brun87cfd662019-09-06 15:36:02 +02005233 ctx->xprt_st = 0;
5234 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005235
5236 /* Only work with sockets for now, this should be adapted when we'll
5237 * add QUIC support.
5238 */
5239 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005240 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005241 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5242 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005243 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005244
Willy Tarreau20879a02012-12-03 16:32:10 +01005245 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5246 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005247 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005248 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005249
Emeric Brun46591952012-05-18 15:47:34 +02005250 /* If it is in client mode initiate SSL session
5251 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005252 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005253 int may_retry = 1;
5254
5255 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005256 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005257 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5258 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005259 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005260 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005261 goto retry_connect;
5262 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005263 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005264 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005265 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005266 ctx->bio = BIO_new(ha_meth);
5267 if (!ctx->bio) {
Olivier Houchardb451b972020-01-24 15:17:38 +01005268 SSL_free(ctx->ssl);
5269 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005270 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005271 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005272 goto retry_connect;
5273 }
Emeric Brun55476152014-11-12 17:35:37 +01005274 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005275 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005276 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005277 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005278 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005279
Evan Broderbe554312013-06-27 00:05:25 -07005280 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005281 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5282 SSL_free(ctx->ssl);
5283 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005284 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005285 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005286 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005287 goto retry_connect;
5288 }
Emeric Brun55476152014-11-12 17:35:37 +01005289 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005290 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005291 }
5292
Olivier Houchard66ab4982019-02-26 18:37:15 +01005293 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005294 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5295 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5296 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 +01005297 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005298 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005299 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5300 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005301 } else if (sess) {
5302 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005303 }
5304 }
Evan Broderbe554312013-06-27 00:05:25 -07005305
Emeric Brun46591952012-05-18 15:47:34 +02005306 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005307 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005308
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005309 _HA_ATOMIC_ADD(&sslconns, 1);
5310 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005311 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005312 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005313 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005314 if (conn->flags & CO_FL_ERROR)
5315 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005316 return 0;
5317 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005318 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005319 int may_retry = 1;
5320
5321 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005322 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005323 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5324 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005325 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005326 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005327 goto retry_accept;
5328 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005329 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005330 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005331 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005332 ctx->bio = BIO_new(ha_meth);
5333 if (!ctx->bio) {
Olivier Houchardb451b972020-01-24 15:17:38 +01005334 SSL_free(ctx->ssl);
5335 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005336 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005337 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005338 goto retry_accept;
5339 }
Emeric Brun55476152014-11-12 17:35:37 +01005340 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005341 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005342 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005343 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005344 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005345
Emeric Brune1f38db2012-09-03 20:36:47 +02005346 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005347 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5348 SSL_free(ctx->ssl);
5349 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005350 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005351 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005352 goto retry_accept;
5353 }
Emeric Brun55476152014-11-12 17:35:37 +01005354 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005355 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005356 }
5357
Frédéric Lécaille583362f2020-01-24 14:56:18 +01005358#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5359 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
5360 b_alloc(&ctx->early_buf);
5361 SSL_set_max_early_data(ctx->ssl,
5362 /* Only allow early data if we managed to allocate
5363 * a buffer.
5364 */
5365 (!b_is_null(&ctx->early_buf)) ?
5366 global.tune.bufsize - global.tune.maxrewrite : 0);
5367 }
5368#endif
5369
Olivier Houchard66ab4982019-02-26 18:37:15 +01005370 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02005371
Emeric Brun46591952012-05-18 15:47:34 +02005372 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005373 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdet510fce52019-08-05 18:04:16 +02005374#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005375 conn->flags |= CO_FL_EARLY_SSL_HS;
5376#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02005377
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005378 _HA_ATOMIC_ADD(&sslconns, 1);
5379 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005380 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005381 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005382 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005383 if (conn->flags & CO_FL_ERROR)
5384 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005385 return 0;
5386 }
5387 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01005388 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005389err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005390 if (ctx && ctx->wait_event.tasklet)
5391 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005392 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02005393 return -1;
5394}
5395
5396
5397/* This is the callback which is used when an SSL handshake is pending. It
5398 * updates the FD status if it wants some polling before being called again.
5399 * It returns 0 if it fails in a fatal way or needs to poll to go further,
5400 * otherwise it returns non-zero and removes itself from the connection's
5401 * flags (the bit is provided in <flag> by the caller).
5402 */
Olivier Houchard000694c2019-05-23 14:45:12 +02005403static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02005404{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005405 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005406 int ret;
Willy Tarreau7a466f62021-02-02 15:42:25 +01005407 socklen_t lskerr;
5408 int skerr;
5409
Emeric Brun46591952012-05-18 15:47:34 +02005410
Willy Tarreau3c728722014-01-23 13:50:42 +01005411 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005412 return 0;
5413
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005414 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005415 goto out_error;
5416
Willy Tarreau7a466f62021-02-02 15:42:25 +01005417 /* don't start calculating a handshake on a dead connection */
5418 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))
5419 goto out_error;
5420
5421 /* FIXME/WT: for now we don't have a clear way to inspect the connection
5422 * status from the lower layers, so let's check the FD directly. Ideally
5423 * the xprt layers should provide some status indicating their knowledge
5424 * of shutdowns or error.
5425 */
5426 skerr = 0;
5427 lskerr = sizeof(skerr);
5428 if ((getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) < 0) ||
5429 skerr != 0)
5430 goto out_error;
5431
Willy Tarreau5db847a2019-05-09 14:13:35 +02005432#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02005433 /*
5434 * Check if we have early data. If we do, we have to read them
5435 * before SSL_do_handshake() is called, And there's no way to
5436 * detect early data, except to try to read them
5437 */
5438 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchardf6715e72019-12-19 15:02:39 +01005439 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005440
Olivier Houchardf6715e72019-12-19 15:02:39 +01005441 while (1) {
5442 ret = SSL_read_early_data(ctx->ssl,
5443 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
5444 &read_data);
5445 if (ret == SSL_READ_EARLY_DATA_ERROR)
5446 goto check_error;
5447 if (read_data > 0) {
5448 conn->flags |= CO_FL_EARLY_DATA;
5449 b_add(&ctx->early_buf, read_data);
5450 }
5451 if (ret == SSL_READ_EARLY_DATA_FINISH) {
5452 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5453 if (!b_data(&ctx->early_buf))
5454 b_free(&ctx->early_buf);
5455 break;
5456 }
5457 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005458 }
5459#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005460 /* If we use SSL_do_handshake to process a reneg initiated by
5461 * the remote peer, it sometimes returns SSL_ERROR_SSL.
5462 * Usually SSL_write and SSL_read are used and process implicitly
5463 * the reneg handshake.
5464 * Here we use SSL_peek as a workaround for reneg.
5465 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005466 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005467 char c;
5468
Olivier Houchard66ab4982019-02-26 18:37:15 +01005469 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01005470 if (ret <= 0) {
5471 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005472 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005473
Emeric Brun674b7432012-11-08 19:21:55 +01005474 if (ret == SSL_ERROR_WANT_WRITE) {
5475 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005476 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005477 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005478 return 0;
5479 }
5480 else if (ret == SSL_ERROR_WANT_READ) {
5481 /* handshake may have been completed but we have
5482 * no more data to read.
5483 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005484 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005485 ret = 1;
5486 goto reneg_ok;
5487 }
5488 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005489 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005490 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005491 return 0;
5492 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005493#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005494 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005495 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005496 return 0;
5497 }
5498#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005499 else if (ret == SSL_ERROR_SYSCALL) {
5500 /* if errno is null, then connection was successfully established */
5501 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5502 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01005503 if (!conn->err_code) {
Lukas Tribus5db881f2019-07-08 14:29:15 +02005504#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5505 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005506 conn->err_code = CO_ER_SSL_HANDSHAKE;
5507#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005508 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005509#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus5db881f2019-07-08 14:29:15 +02005510 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005511 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005512 empty_handshake = state == TLS_ST_BEFORE;
5513#else
Lukas Tribus5db881f2019-07-08 14:29:15 +02005514 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5515 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005516#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005517 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02005518 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005519 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005520 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5521 else
5522 conn->err_code = CO_ER_SSL_EMPTY;
5523 }
5524 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005525 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005526 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5527 else
5528 conn->err_code = CO_ER_SSL_ABORT;
5529 }
5530 }
5531 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005532 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005533 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01005534 else
Emeric Brun29f037d2014-04-25 19:05:36 +02005535 conn->err_code = CO_ER_SSL_HANDSHAKE;
5536 }
Lukas Tribus5db881f2019-07-08 14:29:15 +02005537#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01005538 }
Emeric Brun674b7432012-11-08 19:21:55 +01005539 goto out_error;
5540 }
5541 else {
5542 /* Fail on all other handshake errors */
5543 /* Note: OpenSSL may leave unread bytes in the socket's
5544 * buffer, causing an RST to be emitted upon close() on
5545 * TCP sockets. We first try to drain possibly pending
5546 * data to avoid this as much as possible.
5547 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005548 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005549 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005550 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005551 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01005552 goto out_error;
5553 }
5554 }
5555 /* read some data: consider handshake completed */
5556 goto reneg_ok;
5557 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005558 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005559check_error:
Emeric Brun46591952012-05-18 15:47:34 +02005560 if (ret != 1) {
5561 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005562 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005563
5564 if (ret == SSL_ERROR_WANT_WRITE) {
5565 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005566 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005567 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005568 return 0;
5569 }
5570 else if (ret == SSL_ERROR_WANT_READ) {
5571 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02005572 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005573 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5574 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005575 return 0;
5576 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005577#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005578 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005579 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005580 return 0;
5581 }
5582#endif
Willy Tarreau89230192012-09-28 20:22:13 +02005583 else if (ret == SSL_ERROR_SYSCALL) {
5584 /* if errno is null, then connection was successfully established */
5585 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5586 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005587 if (!conn->err_code) {
Lukas Tribus5db881f2019-07-08 14:29:15 +02005588#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5589 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005590 conn->err_code = CO_ER_SSL_HANDSHAKE;
5591#else
5592 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005593#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus5db881f2019-07-08 14:29:15 +02005594 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005595 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005596 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005597#else
Lukas Tribus5db881f2019-07-08 14:29:15 +02005598 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5599 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005600#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005601 if (empty_handshake) {
5602 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005603 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005604 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5605 else
5606 conn->err_code = CO_ER_SSL_EMPTY;
5607 }
5608 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005609 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005610 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5611 else
5612 conn->err_code = CO_ER_SSL_ABORT;
5613 }
Emeric Brun29f037d2014-04-25 19:05:36 +02005614 }
5615 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005616 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005617 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5618 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005619 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02005620 }
Lukas Tribus5db881f2019-07-08 14:29:15 +02005621#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02005622 }
Willy Tarreau89230192012-09-28 20:22:13 +02005623 goto out_error;
5624 }
Emeric Brun46591952012-05-18 15:47:34 +02005625 else {
5626 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02005627 /* Note: OpenSSL may leave unread bytes in the socket's
5628 * buffer, causing an RST to be emitted upon close() on
5629 * TCP sockets. We first try to drain possibly pending
5630 * data to avoid this as much as possible.
5631 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005632 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005633 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005634 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005635 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005636 goto out_error;
5637 }
5638 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005639#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01005640 else {
5641 /*
5642 * If the server refused the early data, we have to send a
5643 * 425 to the client, as we no longer have the data to sent
5644 * them again.
5645 */
5646 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005647 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01005648 conn->err_code = CO_ER_SSL_EARLY_FAILED;
5649 goto out_error;
5650 }
5651 }
5652 }
5653#endif
5654
Emeric Brun46591952012-05-18 15:47:34 +02005655
Emeric Brun674b7432012-11-08 19:21:55 +01005656reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00005657
Willy Tarreau5db847a2019-05-09 14:13:35 +02005658#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005659 /* ASYNC engine API doesn't support moving read/write
5660 * buffers. So we disable ASYNC mode right after
5661 * the handshake to avoid buffer oveflows.
5662 */
5663 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005664 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005665#endif
Emeric Brun46591952012-05-18 15:47:34 +02005666 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005667 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005668 if (objt_server(conn->target)) {
5669 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
5670 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
5671 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02005672 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005673 else {
5674 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
5675 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
5676 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
5677 }
Emeric Brun46591952012-05-18 15:47:34 +02005678 }
5679
5680 /* The connection is now established at both layers, it's time to leave */
5681 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
5682 return 1;
5683
5684 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01005685 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005686 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005687 ERR_clear_error();
5688
Emeric Brun9fa89732012-10-04 17:09:56 +02005689 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02005690 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5691 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5692 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02005693 }
5694
Emeric Brun46591952012-05-18 15:47:34 +02005695 /* Fail on all other handshake errors */
5696 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01005697 if (!conn->err_code)
5698 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005699 return 0;
5700}
5701
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005702static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01005703{
Olivier Houchardea8dd942019-05-20 14:02:16 +02005704 struct wait_event *sw;
5705 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005706
Olivier Houcharda37eb6a2019-06-24 18:57:39 +02005707 if (!ctx)
5708 return -1;
5709
Olivier Houchardea8dd942019-05-20 14:02:16 +02005710 if (event_type & SUB_RETRY_RECV) {
5711 sw = param;
5712 BUG_ON(ctx->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5713 sw->events |= SUB_RETRY_RECV;
5714 ctx->recv_wait = sw;
5715 if (!(conn->flags & CO_FL_SSL_WAIT_HS) &&
5716 !(ctx->wait_event.events & SUB_RETRY_RECV))
5717 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
5718 event_type &= ~SUB_RETRY_RECV;
5719 }
5720 if (event_type & SUB_RETRY_SEND) {
5721sw = param;
5722 BUG_ON(ctx->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5723 sw->events |= SUB_RETRY_SEND;
5724 ctx->send_wait = sw;
5725 if (!(conn->flags & CO_FL_SSL_WAIT_HS) &&
5726 !(ctx->wait_event.events & SUB_RETRY_SEND))
5727 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
5728 event_type &= ~SUB_RETRY_SEND;
5729
5730 }
5731 if (event_type != 0)
5732 return -1;
5733 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01005734}
5735
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005736static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01005737{
Olivier Houchardea8dd942019-05-20 14:02:16 +02005738 struct wait_event *sw;
5739 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005740
Olivier Houchardea8dd942019-05-20 14:02:16 +02005741 if (event_type & SUB_RETRY_RECV) {
5742 sw = param;
5743 BUG_ON(ctx->recv_wait != sw);
5744 ctx->recv_wait = NULL;
5745 sw->events &= ~SUB_RETRY_RECV;
5746 /* If we subscribed, and we're not doing the handshake,
5747 * then we subscribed because the upper layer asked for it,
5748 * as the upper layer is no longer interested, we can
5749 * unsubscribe too.
5750 */
5751 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) &&
5752 (ctx->wait_event.events & SUB_RETRY_RECV))
5753 conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV,
5754 &ctx->wait_event);
5755 }
5756 if (event_type & SUB_RETRY_SEND) {
5757 sw = param;
5758 BUG_ON(ctx->send_wait != sw);
5759 ctx->send_wait = NULL;
5760 sw->events &= ~SUB_RETRY_SEND;
5761 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) &&
5762 (ctx->wait_event.events & SUB_RETRY_SEND))
5763 conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND,
5764 &ctx->wait_event);
5765
5766 }
5767
5768 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01005769}
5770
Olivier Houchard2e055482019-05-27 19:50:12 +02005771/* Use the provided XPRT as an underlying XPRT, and provide the old one.
5772 * Returns 0 on success, and non-zero on failure.
5773 */
5774static 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)
5775{
5776 struct ssl_sock_ctx *ctx = xprt_ctx;
5777
5778 if (oldxprt_ops != NULL)
5779 *oldxprt_ops = ctx->xprt;
5780 if (oldxprt_ctx != NULL)
5781 *oldxprt_ctx = ctx->xprt_ctx;
5782 ctx->xprt = toadd_ops;
5783 ctx->xprt_ctx = toadd_ctx;
5784 return 0;
5785}
5786
Olivier Houchard5149b592019-05-23 17:47:36 +02005787/* Remove the specified xprt. If if it our underlying XPRT, remove it and
5788 * return 0, otherwise just call the remove_xprt method from the underlying
5789 * XPRT.
5790 */
5791static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
5792{
5793 struct ssl_sock_ctx *ctx = xprt_ctx;
5794
5795 if (ctx->xprt_ctx == toremove_ctx) {
5796 ctx->xprt_ctx = newctx;
5797 ctx->xprt = newops;
5798 return 0;
5799 }
5800 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
5801}
5802
Olivier Houchardea8dd942019-05-20 14:02:16 +02005803static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
5804{
5805 struct ssl_sock_ctx *ctx = context;
5806
5807 /* First if we're doing an handshake, try that */
5808 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
5809 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
5810 /* If we had an error, or the handshake is done and I/O is available,
5811 * let the upper layer know.
5812 * If no mux was set up yet, and nobody subscribed, then call
5813 * xprt_done_cb() ourself if it's set, or destroy the connection,
5814 * we can't be sure conn_fd_handler() will be called again.
5815 */
5816 if ((ctx->conn->flags & CO_FL_ERROR) ||
5817 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
5818 int ret = 0;
5819 int woke = 0;
5820
5821 /* On error, wake any waiter */
5822 if (ctx->recv_wait) {
5823 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005824 tasklet_wakeup(ctx->recv_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005825 ctx->recv_wait = NULL;
5826 woke = 1;
5827 }
5828 if (ctx->send_wait) {
5829 ctx->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005830 tasklet_wakeup(ctx->send_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005831 ctx->send_wait = NULL;
5832 woke = 1;
5833 }
5834 /* If we're the first xprt for the connection, let the
5835 * upper layers know. If xprt_done_cb() is set, call it,
5836 * otherwise, we should have a mux, so call its wake
5837 * method if we didn't woke a tasklet already.
5838 */
5839 if (ctx->conn->xprt_ctx == ctx) {
5840 if (ctx->conn->xprt_done_cb)
5841 ret = ctx->conn->xprt_done_cb(ctx->conn);
5842 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
5843 ctx->conn->mux->wake(ctx->conn);
5844 return NULL;
5845 }
5846 }
Olivier Houchardf6715e72019-12-19 15:02:39 +01005847#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5848 /* If we have early data and somebody wants to receive, let them */
5849 else if (b_data(&ctx->early_buf) && ctx->recv_wait) {
5850 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
5851 tasklet_wakeup(ctx->recv_wait->tasklet);
5852 ctx->recv_wait = NULL;
5853
5854 }
5855#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02005856 return NULL;
5857}
5858
Emeric Brun46591952012-05-18 15:47:34 +02005859/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01005860 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02005861 * buffer wraps, in which case a second call may be performed. The connection's
5862 * flags are updated with whatever special event is detected (error, read0,
5863 * empty). The caller is responsible for taking care of those events and
5864 * avoiding the call if inappropriate. The function does not call the
5865 * connection's polling update function, so the caller is responsible for this.
5866 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005867static 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 +02005868{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005869 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02005870 ssize_t ret;
5871 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02005872
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005873 conn_refresh_polling_flags(conn);
5874
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005875 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005876 goto out_error;
5877
Olivier Houchardf6715e72019-12-19 15:02:39 +01005878#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5879 if (b_data(&ctx->early_buf)) {
5880 try = b_contig_space(buf);
5881 if (try > b_data(&ctx->early_buf))
5882 try = b_data(&ctx->early_buf);
5883 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
5884 b_add(buf, try);
5885 b_del(&ctx->early_buf, try);
5886 if (b_data(&ctx->early_buf) == 0)
5887 b_free(&ctx->early_buf);
5888 return try;
5889 }
5890#endif
5891
Emeric Brun46591952012-05-18 15:47:34 +02005892 if (conn->flags & CO_FL_HANDSHAKE)
5893 /* a handshake was requested */
5894 return 0;
5895
Emeric Brun46591952012-05-18 15:47:34 +02005896 /* read the largest possible block. For this, we perform only one call
5897 * to recv() unless the buffer wraps and we exactly fill the first hunk,
5898 * in which case we accept to do it once again. A new attempt is made on
5899 * EINTR too.
5900 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01005901 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02005902
Willy Tarreau591d4452018-06-15 17:21:00 +02005903 try = b_contig_space(buf);
5904 if (!try)
5905 break;
5906
Willy Tarreauabf08d92014-01-14 11:31:27 +01005907 if (try > count)
5908 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02005909
Olivier Houchard66ab4982019-02-26 18:37:15 +01005910 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdet510fce52019-08-05 18:04:16 +02005911
Emeric Brune1f38db2012-09-03 20:36:47 +02005912 if (conn->flags & CO_FL_ERROR) {
5913 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01005914 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02005915 }
Emeric Brun46591952012-05-18 15:47:34 +02005916 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02005917 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005918 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02005919 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02005920 }
Emeric Brun46591952012-05-18 15:47:34 +02005921 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005922 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005923 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01005924 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02005925 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005926 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005927#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005928 /* Async mode can be re-enabled, because we're leaving data state.*/
5929 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005930 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005931#endif
Emeric Brun46591952012-05-18 15:47:34 +02005932 break;
5933 }
5934 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005935 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005936 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5937 SUB_RETRY_RECV,
5938 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01005939 /* handshake is running, and it may need to re-enable read */
5940 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005941#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005942 /* Async mode can be re-enabled, because we're leaving data state.*/
5943 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005944 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005945#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01005946 break;
5947 }
Emeric Brun46591952012-05-18 15:47:34 +02005948 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005949 } else if (ret == SSL_ERROR_ZERO_RETURN)
5950 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005951 /* For SSL_ERROR_SYSCALL, make sure to clear the error
5952 * stack before shutting down the connection for
5953 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005954 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
5955 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02005956 /* otherwise it's a real error */
5957 goto out_error;
5958 }
5959 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005960 leave:
Emeric Brun46591952012-05-18 15:47:34 +02005961 return done;
5962
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005963 clear_ssl_error:
5964 /* Clear openssl global errors stack */
5965 ssl_sock_dump_errors(conn);
5966 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02005967 read0:
5968 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005969 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005970
Emeric Brun46591952012-05-18 15:47:34 +02005971 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005972 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01005973 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005974 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005975 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005976 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02005977}
5978
5979
Willy Tarreau787db9a2018-06-14 18:31:46 +02005980/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5981 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5982 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02005983 * Only one call to send() is performed, unless the buffer wraps, in which case
5984 * a second call may be performed. The connection's flags are updated with
5985 * whatever special event is detected (error, empty). The caller is responsible
5986 * for taking care of those events and avoiding the call if inappropriate. The
5987 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02005988 * is responsible for this. The buffer's output is not adjusted, it's up to the
5989 * caller to take care of this. It's up to the caller to update the buffer's
5990 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02005991 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005992static 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 +02005993{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005994 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02005995 ssize_t ret;
5996 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02005997
5998 done = 0;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005999 conn_refresh_polling_flags(conn);
Emeric Brun46591952012-05-18 15:47:34 +02006000
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006001 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006002 goto out_error;
6003
Olivier Houchard010941f2019-05-03 20:56:19 +02006004 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006005 /* a handshake was requested */
6006 return 0;
6007
6008 /* send the largest possible block. For this we perform only one call
6009 * to send() unless the buffer wraps and we exactly fill the first hunk,
6010 * in which case we accept to do it once again.
6011 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02006012 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02006013#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006014 size_t written_data;
6015#endif
6016
Willy Tarreau787db9a2018-06-14 18:31:46 +02006017 try = b_contig_data(buf, done);
6018 if (try > count)
6019 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01006020
Willy Tarreau7bed9452014-02-02 02:00:24 +01006021 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006022 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01006023 global_ssl.max_record && try > global_ssl.max_record) {
6024 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006025 }
6026 else {
6027 /* we need to keep the information about the fact that
6028 * we're not limiting the upcoming send(), because if it
6029 * fails, we'll have to retry with at least as many data.
6030 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006031 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006032 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01006033
Willy Tarreau5db847a2019-05-09 14:13:35 +02006034#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02006035 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006036 unsigned int max_early;
6037
Olivier Houchard522eea72017-11-03 16:27:47 +01006038 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006039 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006040 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006041 if (SSL_get0_session(ctx->ssl))
6042 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006043 else
6044 max_early = 0;
6045 }
6046
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006047 if (try + ctx->sent_early_data > max_early) {
6048 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006049 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006050 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006051 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006052 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006053 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006054 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006055 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006056 if (ret == 1) {
6057 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006058 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006059 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006060 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006061 /* Initiate the handshake, now */
6062 tasklet_wakeup(ctx->wait_event.tasklet);
6063 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006064
Olivier Houchardc2aae742017-09-22 18:26:28 +02006065 }
6066
6067 } else
6068#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006069 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006070
Emeric Brune1f38db2012-09-03 20:36:47 +02006071 if (conn->flags & CO_FL_ERROR) {
6072 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006073 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006074 }
Emeric Brun46591952012-05-18 15:47:34 +02006075 if (ret > 0) {
Olivier Houchardf24502b2019-01-17 19:09:11 +01006076 /* A send succeeded, so we can consier ourself connected */
6077 conn->flags |= CO_FL_CONNECTED;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006078 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006079 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006080 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006081 }
6082 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006083 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006084
Emeric Brun46591952012-05-18 15:47:34 +02006085 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006086 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006087 /* handshake is running, and it may need to re-enable write */
6088 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006089 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006090#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006091 /* Async mode can be re-enabled, because we're leaving data state.*/
6092 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006093 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006094#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006095 break;
6096 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006097
Emeric Brun46591952012-05-18 15:47:34 +02006098 break;
6099 }
6100 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006101 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006102 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006103 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6104 SUB_RETRY_RECV,
6105 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006106#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006107 /* Async mode can be re-enabled, because we're leaving data state.*/
6108 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006109 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006110#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006111 break;
6112 }
Emeric Brun46591952012-05-18 15:47:34 +02006113 goto out_error;
6114 }
6115 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006116 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006117 return done;
6118
6119 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006120 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006121 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006122 ERR_clear_error();
6123
Emeric Brun46591952012-05-18 15:47:34 +02006124 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006125 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006126}
6127
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006128static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006129
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006130 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006131
Olivier Houchardea8dd942019-05-20 14:02:16 +02006132
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006133 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006134 if (ctx->wait_event.events != 0)
6135 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6136 ctx->wait_event.events,
6137 &ctx->wait_event);
6138 if (ctx->send_wait) {
6139 ctx->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006140 tasklet_wakeup(ctx->send_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006141 }
6142 if (ctx->recv_wait) {
6143 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006144 tasklet_wakeup(ctx->recv_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006145 }
Olivier Houchard692c1d02019-05-23 18:41:47 +02006146 if (ctx->xprt->close)
6147 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006148#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006149 if (global_ssl.async) {
6150 OSSL_ASYNC_FD all_fd[32], afd;
6151 size_t num_all_fds = 0;
6152 int i;
6153
Olivier Houchard66ab4982019-02-26 18:37:15 +01006154 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006155 if (num_all_fds > 32) {
6156 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6157 return;
6158 }
6159
Olivier Houchard66ab4982019-02-26 18:37:15 +01006160 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006161
6162 /* If an async job is pending, we must try to
6163 to catch the end using polling before calling
6164 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006165 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006166 for (i=0 ; i < num_all_fds ; i++) {
6167 /* switch on an handler designed to
6168 * handle the SSL_free
6169 */
6170 afd = all_fd[i];
6171 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006172 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006173 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006174 /* To ensure that the fd cache won't be used
6175 * and we'll catch a real RD event.
6176 */
6177 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006178 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006179 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006180 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006181 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006182 return;
6183 }
Emeric Brun3854e012017-05-17 20:42:48 +02006184 /* Else we can remove the fds from the fdtab
6185 * and call SSL_free.
6186 * note: we do a fd_remove and not a delete
6187 * because the fd is owned by the engine.
6188 * the engine is responsible to close
6189 */
6190 for (i=0 ; i < num_all_fds ; i++)
6191 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006192 }
6193#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006194 SSL_free(ctx->ssl);
Olivier Houchardf6715e72019-12-19 15:02:39 +01006195 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006196 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006197 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006198 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006199 }
Emeric Brun46591952012-05-18 15:47:34 +02006200}
6201
6202/* This function tries to perform a clean shutdown on an SSL connection, and in
6203 * any case, flags the connection as reusable if no handshake was in progress.
6204 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006205static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006206{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006207 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006208
Emeric Brun46591952012-05-18 15:47:34 +02006209 if (conn->flags & CO_FL_HANDSHAKE)
6210 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006211 if (!clean)
6212 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006213 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006214 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006215 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006216 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006217 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006218 ERR_clear_error();
6219 }
Emeric Brun46591952012-05-18 15:47:34 +02006220}
6221
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006222/* used for ppv2 pkey alog (can be used for logging) */
Willy Tarreau83061a82018-07-13 11:56:34 +02006223int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006224{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006225 struct ssl_sock_ctx *ctx;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006226 struct pkey_info *pkinfo;
6227 int bits = 0;
6228 int sig = TLSEXT_signature_anonymous;
6229 int len = -1;
6230
6231 if (!ssl_sock_is_ssl(conn))
6232 return 0;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006233 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006234 pkinfo = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ctx->ssl), ssl_pkey_info_index);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006235 if (pkinfo) {
6236 sig = pkinfo->sig;
6237 bits = pkinfo->bits;
6238 } else {
6239 /* multicert and generated cert have no pkey info */
6240 X509 *crt;
6241 EVP_PKEY *pkey;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006242 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006243 if (!crt)
6244 return 0;
6245 pkey = X509_get_pubkey(crt);
6246 if (pkey) {
6247 bits = EVP_PKEY_bits(pkey);
6248 switch(EVP_PKEY_base_id(pkey)) {
6249 case EVP_PKEY_RSA:
6250 sig = TLSEXT_signature_rsa;
6251 break;
6252 case EVP_PKEY_EC:
6253 sig = TLSEXT_signature_ecdsa;
6254 break;
6255 case EVP_PKEY_DSA:
6256 sig = TLSEXT_signature_dsa;
6257 break;
6258 }
6259 EVP_PKEY_free(pkey);
6260 }
6261 }
6262
6263 switch(sig) {
6264 case TLSEXT_signature_rsa:
6265 len = chunk_printf(out, "RSA%d", bits);
6266 break;
6267 case TLSEXT_signature_ecdsa:
6268 len = chunk_printf(out, "EC%d", bits);
6269 break;
6270 case TLSEXT_signature_dsa:
6271 len = chunk_printf(out, "DSA%d", bits);
6272 break;
6273 default:
6274 return 0;
6275 }
6276 if (len < 0)
6277 return 0;
6278 return 1;
6279}
6280
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006281/* used for ppv2 cert signature (can be used for logging) */
6282const char *ssl_sock_get_cert_sig(struct connection *conn)
6283{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006284 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006285
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006286 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6287 X509 *crt;
6288
6289 if (!ssl_sock_is_ssl(conn))
6290 return NULL;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006291 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006292 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006293 if (!crt)
6294 return NULL;
6295 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6296 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6297}
6298
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006299/* used for ppv2 authority */
6300const char *ssl_sock_get_sni(struct connection *conn)
6301{
6302#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006303 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006304
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006305 if (!ssl_sock_is_ssl(conn))
6306 return NULL;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006307 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006308 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006309#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006310 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006311#endif
6312}
6313
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006314/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006315const char *ssl_sock_get_cipher_name(struct connection *conn)
6316{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006317 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006318
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006319 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006320 return NULL;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006321 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006322 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006323}
6324
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006325/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006326const char *ssl_sock_get_proto_version(struct connection *conn)
6327{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006328 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006329
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006330 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006331 return NULL;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006332 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006333 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006334}
6335
Willy Tarreau8d598402012-10-22 17:58:39 +02006336/* Extract a serial from a cert, and copy it to a chunk.
6337 * Returns 1 if serial is found and copied, 0 if no serial found and
6338 * -1 if output is not large enough.
6339 */
6340static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006341ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006342{
6343 ASN1_INTEGER *serial;
6344
6345 serial = X509_get_serialNumber(crt);
6346 if (!serial)
6347 return 0;
6348
6349 if (out->size < serial->length)
6350 return -1;
6351
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006352 memcpy(out->area, serial->data, serial->length);
6353 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006354 return 1;
6355}
6356
Emeric Brun43e79582014-10-29 19:03:26 +01006357/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006358 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6359 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006360 */
6361static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006362ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006363{
6364 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006365 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01006366
6367 len =i2d_X509(crt, NULL);
6368 if (len <= 0)
6369 return 1;
6370
6371 if (out->size < len)
6372 return -1;
6373
6374 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006375 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01006376 return 1;
6377}
6378
Emeric Brunce5ad802012-10-22 14:11:22 +02006379
Willy Tarreau83061a82018-07-13 11:56:34 +02006380/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02006381 * Returns 1 if serial is found and copied, 0 if no valid time found
6382 * and -1 if output is not large enough.
6383 */
6384static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006385ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02006386{
6387 if (tm->type == V_ASN1_GENERALIZEDTIME) {
6388 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
6389
6390 if (gentm->length < 12)
6391 return 0;
6392 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
6393 return 0;
6394 if (out->size < gentm->length-2)
6395 return -1;
6396
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006397 memcpy(out->area, gentm->data+2, gentm->length-2);
6398 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02006399 return 1;
6400 }
6401 else if (tm->type == V_ASN1_UTCTIME) {
6402 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
6403
6404 if (utctm->length < 10)
6405 return 0;
6406 if (utctm->data[0] >= 0x35)
6407 return 0;
6408 if (out->size < utctm->length)
6409 return -1;
6410
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006411 memcpy(out->area, utctm->data, utctm->length);
6412 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02006413 return 1;
6414 }
6415
6416 return 0;
6417}
6418
Emeric Brun87855892012-10-17 17:39:35 +02006419/* Extract an entry from a X509_NAME and copy its value to an output chunk.
6420 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
6421 */
6422static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006423ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
6424 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006425{
6426 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006427 ASN1_OBJECT *obj;
6428 ASN1_STRING *data;
6429 const unsigned char *data_ptr;
6430 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006431 int i, j, n;
6432 int cur = 0;
6433 const char *s;
6434 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006435 int name_count;
6436
6437 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006438
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006439 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006440 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02006441 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006442 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02006443 else
6444 j = i;
6445
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006446 ne = X509_NAME_get_entry(a, j);
6447 obj = X509_NAME_ENTRY_get_object(ne);
6448 data = X509_NAME_ENTRY_get_data(ne);
6449 data_ptr = ASN1_STRING_get0_data(data);
6450 data_len = ASN1_STRING_length(data);
6451 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006452 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006453 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006454 s = tmp;
6455 }
6456
6457 if (chunk_strcasecmp(entry, s) != 0)
6458 continue;
6459
6460 if (pos < 0)
6461 cur--;
6462 else
6463 cur++;
6464
6465 if (cur != pos)
6466 continue;
6467
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006468 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02006469 return -1;
6470
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006471 memcpy(out->area, data_ptr, data_len);
6472 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006473 return 1;
6474 }
6475
6476 return 0;
6477
6478}
6479
6480/* Extract and format full DN from a X509_NAME and copy result into a chunk
6481 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
6482 */
6483static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006484ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006485{
6486 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006487 ASN1_OBJECT *obj;
6488 ASN1_STRING *data;
6489 const unsigned char *data_ptr;
6490 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006491 int i, n, ln;
6492 int l = 0;
6493 const char *s;
6494 char *p;
6495 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006496 int name_count;
6497
6498
6499 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006500
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006501 out->data = 0;
6502 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006503 for (i = 0; i < name_count; i++) {
6504 ne = X509_NAME_get_entry(a, i);
6505 obj = X509_NAME_ENTRY_get_object(ne);
6506 data = X509_NAME_ENTRY_get_data(ne);
6507 data_ptr = ASN1_STRING_get0_data(data);
6508 data_len = ASN1_STRING_length(data);
6509 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006510 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006511 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006512 s = tmp;
6513 }
6514 ln = strlen(s);
6515
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006516 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006517 if (l > out->size)
6518 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006519 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02006520
6521 *(p++)='/';
6522 memcpy(p, s, ln);
6523 p += ln;
6524 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006525 memcpy(p, data_ptr, data_len);
6526 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006527 }
6528
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006529 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02006530 return 0;
6531
6532 return 1;
6533}
6534
Olivier Houchardab28a322018-12-21 19:45:40 +01006535void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
6536{
6537#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006538 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006539
Olivier Houchardaa2ecea2019-06-28 14:10:33 +02006540 if (!ssl_sock_is_ssl(conn))
6541 return;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006542 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006543 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01006544#endif
6545}
6546
Willy Tarreau119a4082016-12-22 21:58:38 +01006547/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
6548 * to disable SNI.
6549 */
Willy Tarreau63076412015-07-10 11:33:32 +02006550void ssl_sock_set_servername(struct connection *conn, const char *hostname)
6551{
6552#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006553 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006554
Willy Tarreau119a4082016-12-22 21:58:38 +01006555 char *prev_name;
6556
Willy Tarreau63076412015-07-10 11:33:32 +02006557 if (!ssl_sock_is_ssl(conn))
6558 return;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006559 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02006560
Willy Tarreau119a4082016-12-22 21:58:38 +01006561 /* if the SNI changes, we must destroy the reusable context so that a
6562 * new connection will present a new SNI. As an optimization we could
6563 * later imagine having a small cache of ssl_ctx to hold a few SNI per
6564 * server.
6565 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006566 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01006567 if ((!prev_name && hostname) ||
6568 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006569 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01006570
Olivier Houchard66ab4982019-02-26 18:37:15 +01006571 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02006572#endif
6573}
6574
Emeric Brun0abf8362014-06-24 18:26:41 +02006575/* Extract peer certificate's common name into the chunk dest
6576 * Returns
6577 * the len of the extracted common name
6578 * or 0 if no CN found in DN
6579 * or -1 on error case (i.e. no peer certificate)
6580 */
Willy Tarreau83061a82018-07-13 11:56:34 +02006581int ssl_sock_get_remote_common_name(struct connection *conn,
6582 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04006583{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006584 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04006585 X509 *crt = NULL;
6586 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04006587 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02006588 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006589 .area = (char *)&find_cn,
6590 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04006591 };
Emeric Brun0abf8362014-06-24 18:26:41 +02006592 int result = -1;
David Safb76832014-05-08 23:42:08 -04006593
6594 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02006595 goto out;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006596 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04006597
6598 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006599 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006600 if (!crt)
6601 goto out;
6602
6603 name = X509_get_subject_name(crt);
6604 if (!name)
6605 goto out;
David Safb76832014-05-08 23:42:08 -04006606
Emeric Brun0abf8362014-06-24 18:26:41 +02006607 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
6608out:
David Safb76832014-05-08 23:42:08 -04006609 if (crt)
6610 X509_free(crt);
6611
6612 return result;
6613}
6614
Dave McCowan328fb582014-07-30 10:39:13 -04006615/* returns 1 if client passed a certificate for this session, 0 if not */
6616int ssl_sock_get_cert_used_sess(struct connection *conn)
6617{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006618 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006619 X509 *crt = NULL;
6620
6621 if (!ssl_sock_is_ssl(conn))
6622 return 0;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006623 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006624
6625 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006626 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04006627 if (!crt)
6628 return 0;
6629
6630 X509_free(crt);
6631 return 1;
6632}
6633
6634/* returns 1 if client passed a certificate for this connection, 0 if not */
6635int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04006636{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006637 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006638
David Safb76832014-05-08 23:42:08 -04006639 if (!ssl_sock_is_ssl(conn))
6640 return 0;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006641 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006642 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04006643}
6644
6645/* returns result from SSL verify */
6646unsigned int ssl_sock_get_verify_result(struct connection *conn)
6647{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006648 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006649
David Safb76832014-05-08 23:42:08 -04006650 if (!ssl_sock_is_ssl(conn))
6651 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006652 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006653 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006654}
6655
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006656/* Returns the application layer protocol name in <str> and <len> when known.
6657 * Zero is returned if the protocol name was not found, otherwise non-zero is
6658 * returned. The string is allocated in the SSL context and doesn't have to be
6659 * freed by the caller. NPN is also checked if available since older versions
6660 * of openssl (1.0.1) which are more common in field only support this one.
6661 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006662static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006663{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006664#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
6665 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006666 struct ssl_sock_ctx *ctx = xprt_ctx;
6667 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006668 return 0;
6669
6670 *str = NULL;
6671
6672#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01006673 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006674 if (*str)
6675 return 1;
6676#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01006677#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006678 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006679 if (*str)
6680 return 1;
6681#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006682#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006683 return 0;
6684}
6685
Willy Tarreau7875d092012-09-10 08:20:03 +02006686/***** Below are some sample fetching functions for ACL/patterns *****/
6687
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006688static int
6689smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
6690{
6691 struct connection *conn;
6692
6693 conn = objt_conn(smp->sess->origin);
6694 if (!conn || conn->xprt != &ssl_sock)
6695 return 0;
6696
6697 smp->flags = 0;
6698 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetbb8643c2019-08-07 14:44:49 +02006699#ifdef OPENSSL_IS_BORINGSSL
6700 {
6701 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6702 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
6703 SSL_early_data_accepted(ctx->ssl));
6704 }
6705#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01006706 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard629693f2020-01-23 14:57:36 +01006707 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetbb8643c2019-08-07 14:44:49 +02006708#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006709 return 1;
6710}
6711
Emeric Brune64aef12012-09-21 13:15:06 +02006712/* boolean, returns true if client cert was present */
6713static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006714smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02006715{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006716 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006717 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006718
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006719 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006720 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02006721 return 0;
6722
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006723 ctx = conn->xprt_ctx;
6724
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006725 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02006726 smp->flags |= SMP_F_MAY_CHANGE;
6727 return 0;
6728 }
6729
6730 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006731 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006732 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02006733
6734 return 1;
6735}
6736
Emeric Brun43e79582014-10-29 19:03:26 +01006737/* binary, returns a certificate in a binary chunk (der/raw).
6738 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6739 * should be use.
6740 */
6741static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006742smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01006743{
6744 int cert_peer = (kw[4] == 'c') ? 1 : 0;
6745 X509 *crt = NULL;
6746 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006747 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01006748 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006749 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01006750
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006751 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01006752 if (!conn || conn->xprt != &ssl_sock)
6753 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006754 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01006755
6756 if (!(conn->flags & CO_FL_CONNECTED)) {
6757 smp->flags |= SMP_F_MAY_CHANGE;
6758 return 0;
6759 }
6760
6761 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006762 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01006763 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006764 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01006765
6766 if (!crt)
6767 goto out;
6768
6769 smp_trash = get_trash_chunk();
6770 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
6771 goto out;
6772
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006773 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006774 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01006775 ret = 1;
6776out:
6777 /* SSL_get_peer_certificate, it increase X509 * ref count */
6778 if (cert_peer && crt)
6779 X509_free(crt);
6780 return ret;
6781}
6782
Emeric Brunba841a12014-04-30 17:05:08 +02006783/* binary, returns serial of certificate in a binary chunk.
6784 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6785 * should be use.
6786 */
Willy Tarreau8d598402012-10-22 17:58:39 +02006787static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006788smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02006789{
Emeric Brunba841a12014-04-30 17:05:08 +02006790 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02006791 X509 *crt = NULL;
6792 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006793 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006794 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006795 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006796
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006797 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006798 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02006799 return 0;
6800
Olivier Houchard66ab4982019-02-26 18:37:15 +01006801 ctx = conn->xprt_ctx;
6802
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006803 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02006804 smp->flags |= SMP_F_MAY_CHANGE;
6805 return 0;
6806 }
6807
Emeric Brunba841a12014-04-30 17:05:08 +02006808 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006809 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006810 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006811 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006812
Willy Tarreau8d598402012-10-22 17:58:39 +02006813 if (!crt)
6814 goto out;
6815
Willy Tarreau47ca5452012-12-23 20:22:19 +01006816 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02006817 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
6818 goto out;
6819
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006820 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006821 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02006822 ret = 1;
6823out:
Emeric Brunba841a12014-04-30 17:05:08 +02006824 /* SSL_get_peer_certificate, it increase X509 * ref count */
6825 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02006826 X509_free(crt);
6827 return ret;
6828}
Emeric Brune64aef12012-09-21 13:15:06 +02006829
Emeric Brunba841a12014-04-30 17:05:08 +02006830/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
6831 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6832 * should be use.
6833 */
James Votha051b4a2013-05-14 20:37:59 +02006834static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006835smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02006836{
Emeric Brunba841a12014-04-30 17:05:08 +02006837 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02006838 X509 *crt = NULL;
6839 const EVP_MD *digest;
6840 int ret = 0;
Willy Tarreau767e8ad2020-02-25 08:59:23 +01006841 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006842 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006843 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006844 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02006845
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006846 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006847 if (!conn || conn->xprt != &ssl_sock)
6848 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006849 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006850
6851 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02006852 smp->flags |= SMP_F_MAY_CHANGE;
6853 return 0;
6854 }
6855
Emeric Brunba841a12014-04-30 17:05:08 +02006856 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006857 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006858 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006859 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02006860 if (!crt)
6861 goto out;
6862
6863 smp_trash = get_trash_chunk();
6864 digest = EVP_sha1();
Willy Tarreau767e8ad2020-02-25 08:59:23 +01006865 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
6866 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006867 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006868 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02006869 ret = 1;
6870out:
Emeric Brunba841a12014-04-30 17:05:08 +02006871 /* SSL_get_peer_certificate, it increase X509 * ref count */
6872 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02006873 X509_free(crt);
6874 return ret;
6875}
6876
Emeric Brunba841a12014-04-30 17:05:08 +02006877/* string, returns certificate's notafter date in ASN1_UTCTIME format.
6878 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6879 * should be use.
6880 */
Emeric Brunce5ad802012-10-22 14:11:22 +02006881static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006882smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02006883{
Emeric Brunba841a12014-04-30 17:05:08 +02006884 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02006885 X509 *crt = NULL;
6886 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006887 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006888 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006889 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02006890
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006891 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006892 if (!conn || conn->xprt != &ssl_sock)
6893 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006894 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006895
6896 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02006897 smp->flags |= SMP_F_MAY_CHANGE;
6898 return 0;
6899 }
6900
Emeric Brunba841a12014-04-30 17:05:08 +02006901 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006902 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006903 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006904 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02006905 if (!crt)
6906 goto out;
6907
Willy Tarreau47ca5452012-12-23 20:22:19 +01006908 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08006909 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02006910 goto out;
6911
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006912 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006913 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02006914 ret = 1;
6915out:
Emeric Brunba841a12014-04-30 17:05:08 +02006916 /* SSL_get_peer_certificate, it increase X509 * ref count */
6917 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02006918 X509_free(crt);
6919 return ret;
6920}
6921
Emeric Brunba841a12014-04-30 17:05:08 +02006922/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
6923 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6924 * should be use.
6925 */
Emeric Brun87855892012-10-17 17:39:35 +02006926static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006927smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02006928{
Emeric Brunba841a12014-04-30 17:05:08 +02006929 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02006930 X509 *crt = NULL;
6931 X509_NAME *name;
6932 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006933 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006934 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006935 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02006936
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006937 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006938 if (!conn || conn->xprt != &ssl_sock)
6939 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006940 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006941
6942 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02006943 smp->flags |= SMP_F_MAY_CHANGE;
6944 return 0;
6945 }
6946
Emeric Brunba841a12014-04-30 17:05:08 +02006947 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006948 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006949 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006950 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02006951 if (!crt)
6952 goto out;
6953
6954 name = X509_get_issuer_name(crt);
6955 if (!name)
6956 goto out;
6957
Willy Tarreau47ca5452012-12-23 20:22:19 +01006958 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02006959 if (args && args[0].type == ARGT_STR) {
6960 int pos = 1;
6961
6962 if (args[1].type == ARGT_SINT)
6963 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02006964
6965 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
6966 goto out;
6967 }
6968 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
6969 goto out;
6970
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006971 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006972 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02006973 ret = 1;
6974out:
Emeric Brunba841a12014-04-30 17:05:08 +02006975 /* SSL_get_peer_certificate, it increase X509 * ref count */
6976 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02006977 X509_free(crt);
6978 return ret;
6979}
6980
Emeric Brunba841a12014-04-30 17:05:08 +02006981/* string, returns notbefore date in ASN1_UTCTIME format.
6982 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6983 * should be use.
6984 */
Emeric Brunce5ad802012-10-22 14:11:22 +02006985static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006986smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02006987{
Emeric Brunba841a12014-04-30 17:05:08 +02006988 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02006989 X509 *crt = NULL;
6990 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006991 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006992 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006993 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006994
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006995 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006996 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02006997 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006998 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02006999
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007000 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007001 smp->flags |= SMP_F_MAY_CHANGE;
7002 return 0;
7003 }
7004
Emeric Brunba841a12014-04-30 17:05:08 +02007005 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007006 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007007 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007008 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007009 if (!crt)
7010 goto out;
7011
Willy Tarreau47ca5452012-12-23 20:22:19 +01007012 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007013 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007014 goto out;
7015
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007016 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007017 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007018 ret = 1;
7019out:
Emeric Brunba841a12014-04-30 17:05:08 +02007020 /* SSL_get_peer_certificate, it increase X509 * ref count */
7021 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007022 X509_free(crt);
7023 return ret;
7024}
7025
Emeric Brunba841a12014-04-30 17:05:08 +02007026/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
7027 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7028 * should be use.
7029 */
Emeric Brun87855892012-10-17 17:39:35 +02007030static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007031smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007032{
Emeric Brunba841a12014-04-30 17:05:08 +02007033 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007034 X509 *crt = NULL;
7035 X509_NAME *name;
7036 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007037 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007038 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007039 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007040
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007041 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007042 if (!conn || conn->xprt != &ssl_sock)
7043 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007044 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007045
7046 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007047 smp->flags |= SMP_F_MAY_CHANGE;
7048 return 0;
7049 }
7050
Emeric Brunba841a12014-04-30 17:05:08 +02007051 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007052 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007053 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007054 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007055 if (!crt)
7056 goto out;
7057
7058 name = X509_get_subject_name(crt);
7059 if (!name)
7060 goto out;
7061
Willy Tarreau47ca5452012-12-23 20:22:19 +01007062 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02007063 if (args && args[0].type == ARGT_STR) {
7064 int pos = 1;
7065
7066 if (args[1].type == ARGT_SINT)
7067 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007068
7069 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7070 goto out;
7071 }
7072 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7073 goto out;
7074
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007075 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007076 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007077 ret = 1;
7078out:
Emeric Brunba841a12014-04-30 17:05:08 +02007079 /* SSL_get_peer_certificate, it increase X509 * ref count */
7080 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007081 X509_free(crt);
7082 return ret;
7083}
Emeric Brun9143d372012-12-20 15:44:16 +01007084
7085/* integer, returns true if current session use a client certificate */
7086static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007087smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007088{
7089 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007090 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007091 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007092
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007093 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007094 if (!conn || conn->xprt != &ssl_sock)
7095 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007096 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007097
7098 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01007099 smp->flags |= SMP_F_MAY_CHANGE;
7100 return 0;
7101 }
7102
7103 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007104 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007105 if (crt) {
7106 X509_free(crt);
7107 }
7108
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007109 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007110 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007111 return 1;
7112}
7113
Emeric Brunba841a12014-04-30 17:05:08 +02007114/* integer, returns the certificate version
7115 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7116 * should be use.
7117 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007118static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007119smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007120{
Emeric Brunba841a12014-04-30 17:05:08 +02007121 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007122 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007123 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007124 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007125
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007126 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007127 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007128 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007129 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007130
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007131 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007132 smp->flags |= SMP_F_MAY_CHANGE;
7133 return 0;
7134 }
7135
Emeric Brunba841a12014-04-30 17:05:08 +02007136 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007137 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007138 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007139 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007140 if (!crt)
7141 return 0;
7142
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007143 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007144 /* SSL_get_peer_certificate increase X509 * ref count */
7145 if (cert_peer)
7146 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007147 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007148
7149 return 1;
7150}
7151
Emeric Brunba841a12014-04-30 17:05:08 +02007152/* string, returns the certificate's signature algorithm.
7153 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7154 * should be use.
7155 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007156static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007157smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007158{
Emeric Brunba841a12014-04-30 17:05:08 +02007159 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007160 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007161 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007162 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007163 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007164 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007165
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007166 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007167 if (!conn || conn->xprt != &ssl_sock)
7168 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007169 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007170
7171 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007172 smp->flags |= SMP_F_MAY_CHANGE;
7173 return 0;
7174 }
7175
Emeric Brunba841a12014-04-30 17:05:08 +02007176 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007177 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007178 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007179 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007180 if (!crt)
7181 return 0;
7182
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007183 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7184 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007185
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007186 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7187 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007188 /* SSL_get_peer_certificate increase X509 * ref count */
7189 if (cert_peer)
7190 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007191 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007192 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007193
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007194 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007195 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007196 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007197 /* SSL_get_peer_certificate increase X509 * ref count */
7198 if (cert_peer)
7199 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007200
7201 return 1;
7202}
7203
Emeric Brunba841a12014-04-30 17:05:08 +02007204/* string, returns the certificate's key algorithm.
7205 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7206 * should be use.
7207 */
Emeric Brun521a0112012-10-22 12:22:55 +02007208static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007209smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02007210{
Emeric Brunba841a12014-04-30 17:05:08 +02007211 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02007212 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007213 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02007214 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007215 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007216 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02007217
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007218 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007219 if (!conn || conn->xprt != &ssl_sock)
7220 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007221 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007222
7223 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02007224 smp->flags |= SMP_F_MAY_CHANGE;
7225 return 0;
7226 }
7227
Emeric Brunba841a12014-04-30 17:05:08 +02007228 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007229 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007230 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007231 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02007232 if (!crt)
7233 return 0;
7234
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007235 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
7236 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02007237
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007238 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7239 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007240 /* SSL_get_peer_certificate increase X509 * ref count */
7241 if (cert_peer)
7242 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007243 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007244 }
Emeric Brun521a0112012-10-22 12:22:55 +02007245
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007246 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007247 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007248 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007249 if (cert_peer)
7250 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007251
7252 return 1;
7253}
7254
Emeric Brun645ae792014-04-30 14:21:06 +02007255/* boolean, returns true if front conn. transport layer is SSL.
7256 * This function is also usable on backend conn if the fetch keyword 5th
7257 * char is 'b'.
7258 */
Willy Tarreau7875d092012-09-10 08:20:03 +02007259static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007260smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007261{
Emeric Bruneb8def92018-02-19 15:59:48 +01007262 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7263 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007264
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007265 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007266 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007267 return 1;
7268}
7269
Emeric Brun2525b6b2012-10-18 15:59:43 +02007270/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007271static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007272smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007273{
7274#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007275 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007276 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007277
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007278 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007279 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007280 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007281 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007282 return 1;
7283#else
7284 return 0;
7285#endif
7286}
7287
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007288/* boolean, returns true if client session has been resumed.
7289 * This function is also usable on backend conn if the fetch keyword 5th
7290 * char is 'b'.
7291 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007292static int
7293smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
7294{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007295 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7296 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007297 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007298
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007299
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007300 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007301 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007302 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007303 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007304 return 1;
7305}
7306
Emeric Brun645ae792014-04-30 14:21:06 +02007307/* string, returns the used cipher if front conn. transport layer is SSL.
7308 * This function is also usable on backend conn if the fetch keyword 5th
7309 * char is 'b'.
7310 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007311static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007312smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007313{
Emeric Bruneb8def92018-02-19 15:59:48 +01007314 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7315 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007316 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007317
Willy Tarreaube508f12016-03-10 11:47:01 +01007318 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007319 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007320 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007321 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007322
Olivier Houchard66ab4982019-02-26 18:37:15 +01007323 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007324 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007325 return 0;
7326
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007327 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007328 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007329 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007330
7331 return 1;
7332}
7333
Emeric Brun645ae792014-04-30 14:21:06 +02007334/* integer, returns the algoritm's keysize if front conn. transport layer
7335 * is SSL.
7336 * This function is also usable on backend conn if the fetch keyword 5th
7337 * char is 'b'.
7338 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007339static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007340smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007341{
Emeric Bruneb8def92018-02-19 15:59:48 +01007342 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7343 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007344 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007345 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01007346
Emeric Brun589fcad2012-10-16 14:13:26 +02007347 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007348 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007349 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007350 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007351
Olivier Houchard66ab4982019-02-26 18:37:15 +01007352 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007353 return 0;
7354
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007355 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007356 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007357
7358 return 1;
7359}
7360
Emeric Brun645ae792014-04-30 14:21:06 +02007361/* integer, returns the used keysize if front conn. transport layer is SSL.
7362 * This function is also usable on backend conn if the fetch keyword 5th
7363 * char is 'b'.
7364 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007365static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007366smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007367{
Emeric Bruneb8def92018-02-19 15:59:48 +01007368 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7369 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007370 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007371
Emeric Brun589fcad2012-10-16 14:13:26 +02007372 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007373 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7374 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007375 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007376
Olivier Houchard66ab4982019-02-26 18:37:15 +01007377 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007378 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02007379 return 0;
7380
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007381 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007382
7383 return 1;
7384}
7385
Bernard Spil13c53f82018-02-15 13:34:58 +01007386#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02007387static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007388smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007389{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007390 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007391 struct ssl_sock_ctx *ctx;
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007392 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007393
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007394 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007395 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007396
Olivier Houchard6b77f492018-11-22 18:18:29 +01007397 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
7398 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007399 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7400 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007401 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007402
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007403 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007404 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007405 (const unsigned char **)&smp->data.u.str.area,
7406 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02007407
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007408 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007409 return 0;
7410
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007411 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007412 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007413}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007414#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02007415
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01007416#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02007417static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007418smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02007419{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007420 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007421 struct ssl_sock_ctx *ctx;
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007422 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007423
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007424 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007425 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02007426
Olivier Houchard6b77f492018-11-22 18:18:29 +01007427 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
7428 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7429
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007430 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02007431 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007432 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02007433
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007434 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007435 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007436 (const unsigned char **)&smp->data.u.str.area,
7437 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02007438
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007439 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02007440 return 0;
7441
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007442 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02007443 return 1;
7444}
7445#endif
7446
Emeric Brun645ae792014-04-30 14:21:06 +02007447/* string, returns the used protocol if front conn. transport layer is SSL.
7448 * This function is also usable on backend conn if the fetch keyword 5th
7449 * char is 'b'.
7450 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02007451static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007452smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007453{
Emeric Bruneb8def92018-02-19 15:59:48 +01007454 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7455 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007456 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007457
Emeric Brun589fcad2012-10-16 14:13:26 +02007458 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007459 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7460 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007461 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007462
Olivier Houchard66ab4982019-02-26 18:37:15 +01007463 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007464 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007465 return 0;
7466
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007467 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007468 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007469 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007470
7471 return 1;
7472}
7473
Willy Tarreau87b09662015-04-03 00:22:06 +02007474/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02007475 * This function is also usable on backend conn if the fetch keyword 5th
7476 * char is 'b'.
7477 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007478#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02007479static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007480smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02007481{
Emeric Bruneb8def92018-02-19 15:59:48 +01007482 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7483 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007484 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007485 struct ssl_sock_ctx *ctx;
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007486 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01007487
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007488 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007489 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02007490
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007491 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7492 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007493 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007494
Olivier Houchard66ab4982019-02-26 18:37:15 +01007495 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02007496 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02007497 return 0;
7498
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007499 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Dragan Dosen9dc47c42020-05-04 09:07:28 +02007500 if (!smp->data.u.str.area || !len)
Emeric Brunfe68f682012-10-16 14:59:28 +02007501 return 0;
7502
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007503 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02007504 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02007505}
Patrick Hemmer41966772018-04-28 19:15:48 -04007506#endif
7507
Emeric Brunfe68f682012-10-16 14:59:28 +02007508
Emmanuel Hocdet839af572019-05-14 16:27:35 +02007509#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04007510static int
Patrick Hemmer65674662019-06-04 08:13:03 -04007511smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
7512{
7513 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7514 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7515 struct buffer *data;
7516 struct ssl_sock_ctx *ctx;
7517
7518 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7519 return 0;
7520 ctx = conn->xprt_ctx;
7521
7522 data = get_trash_chunk();
7523 if (kw[7] == 'c')
7524 data->data = SSL_get_client_random(ctx->ssl,
7525 (unsigned char *) data->area,
7526 data->size);
7527 else
7528 data->data = SSL_get_server_random(ctx->ssl,
7529 (unsigned char *) data->area,
7530 data->size);
7531 if (!data->data)
7532 return 0;
7533
7534 smp->flags = 0;
7535 smp->data.type = SMP_T_BIN;
7536 smp->data.u.str = *data;
7537
7538 return 1;
7539}
7540
7541static int
Patrick Hemmere0275472018-04-28 19:15:51 -04007542smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
7543{
7544 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7545 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7546 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02007547 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007548 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04007549
7550 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7551 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007552 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04007553
Olivier Houchard66ab4982019-02-26 18:37:15 +01007554 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04007555 if (!ssl_sess)
7556 return 0;
7557
7558 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007559 data->data = SSL_SESSION_get_master_key(ssl_sess,
7560 (unsigned char *) data->area,
7561 data->size);
7562 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04007563 return 0;
7564
7565 smp->flags = 0;
7566 smp->data.type = SMP_T_BIN;
7567 smp->data.u.str = *data;
7568
7569 return 1;
7570}
7571#endif
7572
Patrick Hemmer41966772018-04-28 19:15:48 -04007573#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02007574static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007575smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007576{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007577 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007578 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007579
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007580 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007581 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02007582
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007583 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007584 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7585 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007586 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007587
Olivier Houchard66ab4982019-02-26 18:37:15 +01007588 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007589 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02007590 return 0;
7591
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007592 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02007593 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02007594}
Patrick Hemmer41966772018-04-28 19:15:48 -04007595#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02007596
David Sc1ad52e2014-04-08 18:48:47 -04007597static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007598smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
7599{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007600 struct connection *conn;
7601 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007602 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007603
7604 conn = objt_conn(smp->sess->origin);
7605 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7606 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007607 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007608
Olivier Houchard66ab4982019-02-26 18:37:15 +01007609 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007610 if (!capture)
7611 return 0;
7612
7613 smp->flags = SMP_F_CONST;
7614 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007615 smp->data.u.str.area = capture->ciphersuite;
7616 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007617 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007618}
7619
7620static int
7621smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
7622{
Willy Tarreau83061a82018-07-13 11:56:34 +02007623 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007624
7625 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
7626 return 0;
7627
7628 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007629 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007630 smp->data.type = SMP_T_BIN;
7631 smp->data.u.str = *data;
7632 return 1;
7633}
7634
7635static int
7636smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
7637{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007638 struct connection *conn;
7639 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007640 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007641
7642 conn = objt_conn(smp->sess->origin);
7643 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7644 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007645 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007646
Olivier Houchard66ab4982019-02-26 18:37:15 +01007647 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007648 if (!capture)
7649 return 0;
7650
7651 smp->data.type = SMP_T_SINT;
7652 smp->data.u.sint = capture->xxh64;
7653 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007654}
7655
7656static int
7657smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
7658{
Willy Tarreau5db847a2019-05-09 14:13:35 +02007659#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02007660 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007661 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007662
7663 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
7664 return 0;
7665
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007666 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007667 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007668 const char *str;
7669 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007670 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007671 uint16_t id = (bin[0] << 8) | bin[1];
7672#if defined(OPENSSL_IS_BORINGSSL)
7673 cipher = SSL_get_cipher_by_value(id);
7674#else
Willy Tarreaub7290772018-10-15 11:01:59 +02007675 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007676 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7677 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007678#endif
7679 str = SSL_CIPHER_get_name(cipher);
7680 if (!str || strcmp(str, "(NONE)") == 0)
7681 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007682 else
7683 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
7684 }
7685 smp->data.type = SMP_T_STR;
7686 smp->data.u.str = *data;
7687 return 1;
7688#else
7689 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
7690#endif
7691}
7692
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007693#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007694static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007695smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04007696{
Emeric Bruneb8def92018-02-19 15:59:48 +01007697 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7698 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04007699 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02007700 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007701 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04007702
7703 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04007704 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7705 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007706 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04007707
7708 if (!(conn->flags & CO_FL_CONNECTED)) {
7709 smp->flags |= SMP_F_MAY_CHANGE;
7710 return 0;
7711 }
7712
7713 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01007714 if (!SSL_session_reused(ctx->ssl))
7715 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007716 finished_trash->area,
7717 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04007718 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007719 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007720 finished_trash->area,
7721 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04007722
7723 if (!finished_len)
7724 return 0;
7725
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007726 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007727 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007728 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04007729
7730 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04007731}
Patrick Hemmer41966772018-04-28 19:15:48 -04007732#endif
David Sc1ad52e2014-04-08 18:48:47 -04007733
Emeric Brun2525b6b2012-10-18 15:59:43 +02007734/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02007735static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007736smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02007737{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007738 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007739 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007740
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007741 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007742 if (!conn || conn->xprt != &ssl_sock)
7743 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007744 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007745
7746 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007747 smp->flags = SMP_F_MAY_CHANGE;
7748 return 0;
7749 }
7750
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007751 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007752 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007753 smp->flags = 0;
7754
7755 return 1;
7756}
7757
Emeric Brun2525b6b2012-10-18 15:59:43 +02007758/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02007759static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007760smp_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 +02007761{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007762 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007763 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007764
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007765 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007766 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02007767 return 0;
7768
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007769 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007770 smp->flags = SMP_F_MAY_CHANGE;
7771 return 0;
7772 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007773 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02007774
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007775 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007776 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007777 smp->flags = 0;
7778
7779 return 1;
7780}
7781
Emeric Brun2525b6b2012-10-18 15:59:43 +02007782/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02007783static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007784smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02007785{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007786 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007787 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007788
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007789 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007790 if (!conn || conn->xprt != &ssl_sock)
7791 return 0;
7792
7793 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007794 smp->flags = SMP_F_MAY_CHANGE;
7795 return 0;
7796 }
7797
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007798 ctx = conn->xprt_ctx;
7799
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007800 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007801 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007802 smp->flags = 0;
7803
7804 return 1;
7805}
7806
Emeric Brun2525b6b2012-10-18 15:59:43 +02007807/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007808static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007809smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007810{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007811 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007812 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007813
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007814 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007815 if (!conn || conn->xprt != &ssl_sock)
7816 return 0;
7817
7818 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007819 smp->flags = SMP_F_MAY_CHANGE;
7820 return 0;
7821 }
7822
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007823 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007824 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007825 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007826
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007827 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007828 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007829 smp->flags = 0;
7830
7831 return 1;
7832}
7833
Emeric Brunfb510ea2012-10-05 12:00:26 +02007834/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007835static 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 +02007836{
7837 if (!*args[cur_arg + 1]) {
7838 if (err)
7839 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
7840 return ERR_ALERT | ERR_FATAL;
7841 }
7842
Willy Tarreauef934602016-12-22 23:12:01 +01007843 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7844 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007845 else
7846 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007847
Emeric Brund94b3fe2012-09-20 18:23:56 +02007848 return 0;
7849}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007850static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7851{
7852 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
7853}
Emeric Brund94b3fe2012-09-20 18:23:56 +02007854
Christopher Faulet31af49d2015-06-09 17:29:50 +02007855/* parse the "ca-sign-file" bind keyword */
7856static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7857{
7858 if (!*args[cur_arg + 1]) {
7859 if (err)
7860 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
7861 return ERR_ALERT | ERR_FATAL;
7862 }
7863
Willy Tarreauef934602016-12-22 23:12:01 +01007864 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7865 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02007866 else
7867 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
7868
7869 return 0;
7870}
7871
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007872/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02007873static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7874{
7875 if (!*args[cur_arg + 1]) {
7876 if (err)
7877 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
7878 return ERR_ALERT | ERR_FATAL;
7879 }
7880 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
7881 return 0;
7882}
7883
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007884/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007885static 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 +02007886{
7887 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007888 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007889 return ERR_ALERT | ERR_FATAL;
7890 }
7891
Emeric Brun76d88952012-10-05 15:47:31 +02007892 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02007893 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007894 return 0;
7895}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007896static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7897{
7898 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
7899}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007900
Emmanuel Hocdet839af572019-05-14 16:27:35 +02007901#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007902/* parse the "ciphersuites" bind keyword */
7903static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7904{
7905 if (!*args[cur_arg + 1]) {
7906 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
7907 return ERR_ALERT | ERR_FATAL;
7908 }
7909
7910 free(conf->ciphersuites);
7911 conf->ciphersuites = strdup(args[cur_arg + 1]);
7912 return 0;
7913}
7914static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7915{
7916 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
7917}
7918#endif
7919
Willy Tarreaub131c872019-10-16 16:42:19 +02007920/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02007921static 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 +02007922{
Willy Tarreau38011032013-08-13 16:59:39 +02007923 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02007924
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007925 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007926 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007927 return ERR_ALERT | ERR_FATAL;
7928 }
7929
Willy Tarreauef934602016-12-22 23:12:01 +01007930 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
7931 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02007932 memprintf(err, "'%s' : path too long", args[cur_arg]);
7933 return ERR_ALERT | ERR_FATAL;
7934 }
Willy Tarreauef934602016-12-22 23:12:01 +01007935 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaub131c872019-10-16 16:42:19 +02007936 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007937 }
7938
Willy Tarreaub131c872019-10-16 16:42:19 +02007939 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02007940}
7941
Willy Tarreaub131c872019-10-16 16:42:19 +02007942/* 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 +01007943static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7944{
Willy Tarreaub131c872019-10-16 16:42:19 +02007945 int err_code;
7946
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007947 if (!*args[cur_arg + 1]) {
7948 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
7949 return ERR_ALERT | ERR_FATAL;
7950 }
7951
Willy Tarreaub131c872019-10-16 16:42:19 +02007952 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err);
7953 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02007954 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007955
Willy Tarreaub131c872019-10-16 16:42:19 +02007956 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007957}
7958
Emeric Brunfb510ea2012-10-05 12:00:26 +02007959/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007960static 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 +02007961{
Emeric Brun051cdab2012-10-02 19:25:50 +02007962#ifndef X509_V_FLAG_CRL_CHECK
7963 if (err)
7964 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
7965 return ERR_ALERT | ERR_FATAL;
7966#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02007967 if (!*args[cur_arg + 1]) {
7968 if (err)
7969 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
7970 return ERR_ALERT | ERR_FATAL;
7971 }
Emeric Brun2b58d042012-09-20 17:10:03 +02007972
Willy Tarreauef934602016-12-22 23:12:01 +01007973 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7974 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007975 else
7976 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007977
Emeric Brun2b58d042012-09-20 17:10:03 +02007978 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02007979#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02007980}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007981static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7982{
7983 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
7984}
Emeric Brun2b58d042012-09-20 17:10:03 +02007985
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007986/* parse the "curves" bind keyword keyword */
7987static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7988{
Lukas Tribusd13e9252019-11-24 18:20:40 +01007989#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007990 if (!*args[cur_arg + 1]) {
7991 if (err)
7992 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
7993 return ERR_ALERT | ERR_FATAL;
7994 }
7995 conf->curves = strdup(args[cur_arg + 1]);
7996 return 0;
7997#else
7998 if (err)
7999 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
8000 return ERR_ALERT | ERR_FATAL;
8001#endif
8002}
8003static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8004{
8005 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
8006}
8007
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008008/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008009static 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 +02008010{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008011#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Emeric Brun2b58d042012-09-20 17:10:03 +02008012 if (err)
8013 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
8014 return ERR_ALERT | ERR_FATAL;
8015#elif defined(OPENSSL_NO_ECDH)
8016 if (err)
8017 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
8018 return ERR_ALERT | ERR_FATAL;
8019#else
8020 if (!*args[cur_arg + 1]) {
8021 if (err)
8022 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
8023 return ERR_ALERT | ERR_FATAL;
8024 }
8025
8026 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008027
8028 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02008029#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008030}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008031static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8032{
8033 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
8034}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008035
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008036/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02008037static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8038{
8039 int code;
8040 char *p = args[cur_arg + 1];
8041 unsigned long long *ignerr = &conf->crt_ignerr;
8042
8043 if (!*p) {
8044 if (err)
8045 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
8046 return ERR_ALERT | ERR_FATAL;
8047 }
8048
8049 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
8050 ignerr = &conf->ca_ignerr;
8051
8052 if (strcmp(p, "all") == 0) {
8053 *ignerr = ~0ULL;
8054 return 0;
8055 }
8056
8057 while (p) {
8058 code = atoi(p);
8059 if ((code <= 0) || (code > 63)) {
8060 if (err)
8061 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
8062 args[cur_arg], code, args[cur_arg + 1]);
8063 return ERR_ALERT | ERR_FATAL;
8064 }
8065 *ignerr |= 1ULL << code;
8066 p = strchr(p, ',');
8067 if (p)
8068 p++;
8069 }
8070
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008071 return 0;
8072}
8073
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008074/* parse tls_method_options "no-xxx" and "force-xxx" */
8075static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008076{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008077 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008078 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008079 p = strchr(arg, '-');
8080 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008081 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008082 p++;
8083 if (!strcmp(p, "sslv3"))
8084 v = CONF_SSLV3;
8085 else if (!strcmp(p, "tlsv10"))
8086 v = CONF_TLSV10;
8087 else if (!strcmp(p, "tlsv11"))
8088 v = CONF_TLSV11;
8089 else if (!strcmp(p, "tlsv12"))
8090 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008091 else if (!strcmp(p, "tlsv13"))
8092 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008093 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008094 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008095 if (!strncmp(arg, "no-", 3))
8096 methods->flags |= methodVersions[v].flag;
8097 else if (!strncmp(arg, "force-", 6))
8098 methods->min = methods->max = v;
8099 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008100 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008101 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008102 fail:
8103 if (err)
8104 memprintf(err, "'%s' : option not implemented", arg);
8105 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008106}
8107
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008108static 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 +02008109{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008110 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008111}
8112
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008113static 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 +02008114{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008115 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8116}
8117
8118/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8119static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8120{
8121 uint16_t i, v = 0;
8122 char *argv = args[cur_arg + 1];
8123 if (!*argv) {
8124 if (err)
8125 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
8126 return ERR_ALERT | ERR_FATAL;
8127 }
8128 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8129 if (!strcmp(argv, methodVersions[i].name))
8130 v = i;
8131 if (!v) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008132 if (err)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008133 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008134 return ERR_ALERT | ERR_FATAL;
8135 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008136 if (!strcmp("ssl-min-ver", args[cur_arg]))
8137 methods->min = v;
8138 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8139 methods->max = v;
8140 else {
8141 if (err)
8142 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
8143 return ERR_ALERT | ERR_FATAL;
8144 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008145 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008146}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008147
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008148static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8149{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008150#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008151 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 +02008152#endif
8153 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8154}
8155
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008156static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8157{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008158 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008159}
8160
8161static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8162{
8163 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8164}
8165
Emeric Brun2d0c4822012-10-02 13:45:20 +02008166/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008167static 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 +02008168{
Emeric Brun89675492012-10-05 13:48:26 +02008169 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008170 return 0;
8171}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008172
Olivier Houchardc2aae742017-09-22 18:26:28 +02008173/* parse the "allow-0rtt" bind keyword */
8174static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8175{
8176 conf->early_data = 1;
8177 return 0;
8178}
8179
8180static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8181{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008182 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008183 return 0;
8184}
8185
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008186/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008187static 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 +02008188{
Bernard Spil13c53f82018-02-15 13:34:58 +01008189#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008190 char *p1, *p2;
8191
8192 if (!*args[cur_arg + 1]) {
8193 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8194 return ERR_ALERT | ERR_FATAL;
8195 }
8196
8197 free(conf->npn_str);
8198
Willy Tarreau3724da12016-02-12 17:11:12 +01008199 /* the NPN string is built as a suite of (<len> <name>)*,
8200 * so we reuse each comma to store the next <len> and need
8201 * one more for the end of the string.
8202 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008203 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008204 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008205 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8206
8207 /* replace commas with the name length */
8208 p1 = conf->npn_str;
8209 p2 = p1 + 1;
8210 while (1) {
8211 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8212 if (!p2)
8213 p2 = p1 + 1 + strlen(p1 + 1);
8214
8215 if (p2 - (p1 + 1) > 255) {
8216 *p2 = '\0';
8217 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8218 return ERR_ALERT | ERR_FATAL;
8219 }
8220
8221 *p1 = p2 - (p1 + 1);
8222 p1 = p2;
8223
8224 if (!*p2)
8225 break;
8226
8227 *(p2++) = '\0';
8228 }
8229 return 0;
8230#else
8231 if (err)
8232 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
8233 return ERR_ALERT | ERR_FATAL;
8234#endif
8235}
8236
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008237static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8238{
8239 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8240}
8241
Willy Tarreauab861d32013-04-02 02:30:41 +02008242/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008243static 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 +02008244{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008245#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008246 char *p1, *p2;
8247
8248 if (!*args[cur_arg + 1]) {
8249 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
8250 return ERR_ALERT | ERR_FATAL;
8251 }
8252
8253 free(conf->alpn_str);
8254
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008255 /* the ALPN string is built as a suite of (<len> <name>)*,
8256 * so we reuse each comma to store the next <len> and need
8257 * one more for the end of the string.
8258 */
Willy Tarreauab861d32013-04-02 02:30:41 +02008259 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008260 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02008261 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
8262
8263 /* replace commas with the name length */
8264 p1 = conf->alpn_str;
8265 p2 = p1 + 1;
8266 while (1) {
8267 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
8268 if (!p2)
8269 p2 = p1 + 1 + strlen(p1 + 1);
8270
8271 if (p2 - (p1 + 1) > 255) {
8272 *p2 = '\0';
8273 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8274 return ERR_ALERT | ERR_FATAL;
8275 }
8276
8277 *p1 = p2 - (p1 + 1);
8278 p1 = p2;
8279
8280 if (!*p2)
8281 break;
8282
8283 *(p2++) = '\0';
8284 }
8285 return 0;
8286#else
8287 if (err)
8288 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
8289 return ERR_ALERT | ERR_FATAL;
8290#endif
8291}
8292
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008293static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8294{
8295 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8296}
8297
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008298/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008299static 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 +02008300{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01008301 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02008302 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02008303
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008304 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
8305 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008306#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008307 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
8308 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
8309#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008310 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008311 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
8312 if (!conf->ssl_conf.ssl_methods.min)
8313 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
8314 if (!conf->ssl_conf.ssl_methods.max)
8315 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02008316
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008317 return 0;
8318}
8319
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008320/* parse the "prefer-client-ciphers" bind keyword */
8321static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8322{
8323 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
8324 return 0;
8325}
8326
Christopher Faulet31af49d2015-06-09 17:29:50 +02008327/* parse the "generate-certificates" bind keyword */
8328static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8329{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01008330#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02008331 conf->generate_certs = 1;
8332#else
8333 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
8334 err && *err ? *err : "");
8335#endif
8336 return 0;
8337}
8338
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008339/* parse the "strict-sni" bind keyword */
8340static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8341{
8342 conf->strict_sni = 1;
8343 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008344}
8345
8346/* parse the "tls-ticket-keys" bind keyword */
8347static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8348{
8349#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008350 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008351 int i = 0;
8352 char thisline[LINESIZE];
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008353 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008354
8355 if (!*args[cur_arg + 1]) {
8356 if (err)
8357 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008358 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008359 }
8360
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008361 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008362 if (keys_ref) {
8363 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008364 conf->keys_ref = keys_ref;
8365 return 0;
8366 }
8367
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008368 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01008369 if (!keys_ref) {
8370 if (err)
8371 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008372 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008373 }
8374
Emeric Brun9e754772019-01-10 17:51:55 +01008375 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01008376 if (!keys_ref->tlskeys) {
Emeric Brun09852f72019-01-10 10:51:13 +01008377 if (err)
8378 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008379 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008380 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008381
8382 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
8383 if (err)
8384 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008385 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008386 }
8387
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008388 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01008389 if (!keys_ref->filename) {
Emeric Brun09852f72019-01-10 10:51:13 +01008390 if (err)
8391 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008392 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008393 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008394
Emeric Brun9e754772019-01-10 17:51:55 +01008395 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008396 while (fgets(thisline, sizeof(thisline), f) != NULL) {
8397 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01008398 int dec_size;
8399
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008400 /* Strip newline characters from the end */
8401 if(thisline[len - 1] == '\n')
8402 thisline[--len] = 0;
8403
8404 if(thisline[len - 1] == '\r')
8405 thisline[--len] = 0;
8406
Emeric Brun9e754772019-01-10 17:51:55 +01008407 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
8408 if (dec_size < 0) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008409 if (err)
8410 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008411 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008412 }
Emeric Brun9e754772019-01-10 17:51:55 +01008413 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
8414 keys_ref->key_size_bits = 128;
8415 }
8416 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
8417 keys_ref->key_size_bits = 256;
8418 }
8419 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
8420 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
8421 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Emeric Brun9e754772019-01-10 17:51:55 +01008422 if (err)
8423 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008424 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01008425 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008426 i++;
8427 }
8428
8429 if (i < TLS_TICKETS_NO) {
8430 if (err)
8431 memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008432 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008433 }
8434
8435 fclose(f);
8436
8437 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01008438 i -= 2;
8439 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008440 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008441 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01008442 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008443 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008444
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008445 LIST_ADD(&tlskeys_reference, &keys_ref->list);
8446
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008447 return 0;
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008448
8449 fail:
8450 if (f)
8451 fclose(f);
8452 if (keys_ref) {
8453 free(keys_ref->filename);
8454 free(keys_ref->tlskeys);
8455 free(keys_ref);
8456 }
8457 return ERR_ALERT | ERR_FATAL;
8458
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008459#else
8460 if (err)
8461 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
8462 return ERR_ALERT | ERR_FATAL;
8463#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008464}
8465
Emeric Brund94b3fe2012-09-20 18:23:56 +02008466/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008467static 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 +02008468{
8469 if (!*args[cur_arg + 1]) {
8470 if (err)
8471 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
8472 return ERR_ALERT | ERR_FATAL;
8473 }
8474
8475 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008476 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008477 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008478 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008479 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008480 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008481 else {
8482 if (err)
8483 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
8484 args[cur_arg], args[cur_arg + 1]);
8485 return ERR_ALERT | ERR_FATAL;
8486 }
8487
8488 return 0;
8489}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008490static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8491{
8492 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
8493}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008494
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02008495/* parse the "no-ca-names" bind keyword */
8496static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8497{
8498 conf->no_ca_names = 1;
8499 return 0;
8500}
8501static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8502{
8503 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
8504}
8505
Willy Tarreau92faadf2012-10-10 23:04:25 +02008506/************** "server" keywords ****************/
8507
Olivier Houchardc7566002018-11-20 23:33:50 +01008508/* parse the "npn" bind keyword */
8509static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8510{
8511#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
8512 char *p1, *p2;
8513
8514 if (!*args[*cur_arg + 1]) {
8515 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
8516 return ERR_ALERT | ERR_FATAL;
8517 }
8518
8519 free(newsrv->ssl_ctx.npn_str);
8520
8521 /* the NPN string is built as a suite of (<len> <name>)*,
8522 * so we reuse each comma to store the next <len> and need
8523 * one more for the end of the string.
8524 */
8525 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
8526 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
8527 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
8528 newsrv->ssl_ctx.npn_len);
8529
8530 /* replace commas with the name length */
8531 p1 = newsrv->ssl_ctx.npn_str;
8532 p2 = p1 + 1;
8533 while (1) {
8534 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
8535 newsrv->ssl_ctx.npn_len - (p1 + 1));
8536 if (!p2)
8537 p2 = p1 + 1 + strlen(p1 + 1);
8538
8539 if (p2 - (p1 + 1) > 255) {
8540 *p2 = '\0';
8541 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
8542 return ERR_ALERT | ERR_FATAL;
8543 }
8544
8545 *p1 = p2 - (p1 + 1);
8546 p1 = p2;
8547
8548 if (!*p2)
8549 break;
8550
8551 *(p2++) = '\0';
8552 }
8553 return 0;
8554#else
8555 if (err)
8556 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
8557 return ERR_ALERT | ERR_FATAL;
8558#endif
8559}
8560
Olivier Houchard92150142018-12-21 19:47:01 +01008561/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01008562static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8563{
8564#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
8565 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01008566 char **alpn_str;
8567 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01008568
Olivier Houchard92150142018-12-21 19:47:01 +01008569 if (*args[*cur_arg] == 'c') {
8570 alpn_str = &newsrv->check.alpn_str;
8571 alpn_len = &newsrv->check.alpn_len;
8572 } else {
8573 alpn_str = &newsrv->ssl_ctx.alpn_str;
8574 alpn_len = &newsrv->ssl_ctx.alpn_len;
8575
8576 }
Olivier Houchardc7566002018-11-20 23:33:50 +01008577 if (!*args[*cur_arg + 1]) {
8578 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
8579 return ERR_ALERT | ERR_FATAL;
8580 }
8581
Olivier Houchard92150142018-12-21 19:47:01 +01008582 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01008583
8584 /* the ALPN string is built as a suite of (<len> <name>)*,
8585 * so we reuse each comma to store the next <len> and need
8586 * one more for the end of the string.
8587 */
Olivier Houchard92150142018-12-21 19:47:01 +01008588 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
8589 *alpn_str = calloc(1, *alpn_len + 1);
8590 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01008591
8592 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01008593 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01008594 p2 = p1 + 1;
8595 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01008596 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01008597 if (!p2)
8598 p2 = p1 + 1 + strlen(p1 + 1);
8599
8600 if (p2 - (p1 + 1) > 255) {
8601 *p2 = '\0';
8602 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
8603 return ERR_ALERT | ERR_FATAL;
8604 }
8605
8606 *p1 = p2 - (p1 + 1);
8607 p1 = p2;
8608
8609 if (!*p2)
8610 break;
8611
8612 *(p2++) = '\0';
8613 }
8614 return 0;
8615#else
8616 if (err)
8617 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
8618 return ERR_ALERT | ERR_FATAL;
8619#endif
8620}
8621
Emeric Brunef42d922012-10-11 16:11:36 +02008622/* parse the "ca-file" server keyword */
8623static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8624{
8625 if (!*args[*cur_arg + 1]) {
8626 if (err)
8627 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
8628 return ERR_ALERT | ERR_FATAL;
8629 }
8630
Willy Tarreauef934602016-12-22 23:12:01 +01008631 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
8632 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008633 else
8634 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
8635
8636 return 0;
8637}
8638
Olivier Houchard9130a962017-10-17 17:33:43 +02008639/* parse the "check-sni" server keyword */
8640static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8641{
8642 if (!*args[*cur_arg + 1]) {
8643 if (err)
8644 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
8645 return ERR_ALERT | ERR_FATAL;
8646 }
8647
8648 newsrv->check.sni = strdup(args[*cur_arg + 1]);
8649 if (!newsrv->check.sni) {
8650 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
8651 return ERR_ALERT | ERR_FATAL;
8652 }
8653 return 0;
8654
8655}
8656
Willy Tarreau92faadf2012-10-10 23:04:25 +02008657/* parse the "check-ssl" server keyword */
8658static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8659{
8660 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01008661 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
8662 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008663#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008664 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
8665 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
8666#endif
Willy Tarreauef934602016-12-22 23:12:01 +01008667 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008668 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
8669 if (!newsrv->ssl_ctx.methods.min)
8670 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
8671 if (!newsrv->ssl_ctx.methods.max)
8672 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
8673
Willy Tarreau92faadf2012-10-10 23:04:25 +02008674 return 0;
8675}
8676
8677/* parse the "ciphers" server keyword */
8678static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8679{
8680 if (!*args[*cur_arg + 1]) {
8681 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
8682 return ERR_ALERT | ERR_FATAL;
8683 }
8684
8685 free(newsrv->ssl_ctx.ciphers);
8686 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
8687 return 0;
8688}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008689
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008690#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008691/* parse the "ciphersuites" server keyword */
8692static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8693{
8694 if (!*args[*cur_arg + 1]) {
8695 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
8696 return ERR_ALERT | ERR_FATAL;
8697 }
8698
8699 free(newsrv->ssl_ctx.ciphersuites);
8700 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
8701 return 0;
8702}
8703#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02008704
Emeric Brunef42d922012-10-11 16:11:36 +02008705/* parse the "crl-file" server keyword */
8706static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8707{
8708#ifndef X509_V_FLAG_CRL_CHECK
8709 if (err)
8710 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
8711 return ERR_ALERT | ERR_FATAL;
8712#else
8713 if (!*args[*cur_arg + 1]) {
8714 if (err)
8715 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
8716 return ERR_ALERT | ERR_FATAL;
8717 }
8718
Willy Tarreauef934602016-12-22 23:12:01 +01008719 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
8720 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008721 else
8722 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
8723
8724 return 0;
8725#endif
8726}
8727
Emeric Bruna7aa3092012-10-26 12:58:00 +02008728/* parse the "crt" server keyword */
8729static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8730{
8731 if (!*args[*cur_arg + 1]) {
8732 if (err)
8733 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
8734 return ERR_ALERT | ERR_FATAL;
8735 }
8736
Willy Tarreauef934602016-12-22 23:12:01 +01008737 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01008738 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02008739 else
8740 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
8741
8742 return 0;
8743}
Emeric Brunef42d922012-10-11 16:11:36 +02008744
Frédéric Lécaille340ae602017-03-13 10:38:04 +01008745/* parse the "no-check-ssl" server keyword */
8746static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8747{
Christopher Faulet68d35ae2020-03-27 18:55:49 +01008748 newsrv->check.use_ssl = -1;
Frédéric Lécaille340ae602017-03-13 10:38:04 +01008749 free(newsrv->ssl_ctx.ciphers);
8750 newsrv->ssl_ctx.ciphers = NULL;
8751 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
8752 return 0;
8753}
8754
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01008755/* parse the "no-send-proxy-v2-ssl" server keyword */
8756static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8757{
8758 newsrv->pp_opts &= ~SRV_PP_V2;
8759 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
8760 return 0;
8761}
8762
8763/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
8764static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8765{
8766 newsrv->pp_opts &= ~SRV_PP_V2;
8767 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
8768 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
8769 return 0;
8770}
8771
Frédéric Lécaillee381d762017-03-13 11:54:17 +01008772/* parse the "no-ssl" server keyword */
8773static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8774{
Christopher Faulet68d35ae2020-03-27 18:55:49 +01008775 newsrv->use_ssl = -1;
Frédéric Lécaillee381d762017-03-13 11:54:17 +01008776 free(newsrv->ssl_ctx.ciphers);
8777 newsrv->ssl_ctx.ciphers = NULL;
8778 return 0;
8779}
8780
Olivier Houchard522eea72017-11-03 16:27:47 +01008781/* parse the "allow-0rtt" server keyword */
8782static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8783{
8784 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
8785 return 0;
8786}
8787
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01008788/* parse the "no-ssl-reuse" server keyword */
8789static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8790{
8791 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
8792 return 0;
8793}
8794
Emeric Brunf9c5c472012-10-11 15:28:34 +02008795/* parse the "no-tls-tickets" server keyword */
8796static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8797{
8798 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
8799 return 0;
8800}
David Safb76832014-05-08 23:42:08 -04008801/* parse the "send-proxy-v2-ssl" server keyword */
8802static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8803{
8804 newsrv->pp_opts |= SRV_PP_V2;
8805 newsrv->pp_opts |= SRV_PP_V2_SSL;
8806 return 0;
8807}
8808
8809/* parse the "send-proxy-v2-ssl-cn" server keyword */
8810static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8811{
8812 newsrv->pp_opts |= SRV_PP_V2;
8813 newsrv->pp_opts |= SRV_PP_V2_SSL;
8814 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
8815 return 0;
8816}
Emeric Brunf9c5c472012-10-11 15:28:34 +02008817
Willy Tarreau732eac42015-07-09 11:40:25 +02008818/* parse the "sni" server keyword */
8819static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8820{
8821#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
8822 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
8823 return ERR_ALERT | ERR_FATAL;
8824#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008825 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02008826
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008827 arg = args[*cur_arg + 1];
8828 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02008829 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
8830 return ERR_ALERT | ERR_FATAL;
8831 }
8832
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008833 free(newsrv->sni_expr);
8834 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02008835
Willy Tarreau732eac42015-07-09 11:40:25 +02008836 return 0;
8837#endif
8838}
8839
Willy Tarreau92faadf2012-10-10 23:04:25 +02008840/* parse the "ssl" server keyword */
8841static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8842{
8843 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01008844 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
8845 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008846#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008847 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
8848 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
8849#endif
Jerome Magnin770bc8a2020-04-22 11:40:18 +02008850 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
8851 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
8852
8853 if (!newsrv->ssl_ctx.methods.min)
8854 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
8855
8856 if (!newsrv->ssl_ctx.methods.max)
8857 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
8858
8859
Willy Tarreau92faadf2012-10-10 23:04:25 +02008860 return 0;
8861}
8862
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01008863/* parse the "ssl-reuse" server keyword */
8864static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8865{
8866 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
8867 return 0;
8868}
8869
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01008870/* parse the "tls-tickets" server keyword */
8871static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8872{
8873 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
8874 return 0;
8875}
8876
Emeric Brunef42d922012-10-11 16:11:36 +02008877/* parse the "verify" server keyword */
8878static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8879{
8880 if (!*args[*cur_arg + 1]) {
8881 if (err)
8882 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
8883 return ERR_ALERT | ERR_FATAL;
8884 }
8885
8886 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008887 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02008888 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008889 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02008890 else {
8891 if (err)
8892 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
8893 args[*cur_arg], args[*cur_arg + 1]);
8894 return ERR_ALERT | ERR_FATAL;
8895 }
8896
Evan Broderbe554312013-06-27 00:05:25 -07008897 return 0;
8898}
8899
8900/* parse the "verifyhost" server keyword */
8901static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8902{
8903 if (!*args[*cur_arg + 1]) {
8904 if (err)
8905 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
8906 return ERR_ALERT | ERR_FATAL;
8907 }
8908
Frédéric Lécaille273f3212017-03-13 15:52:01 +01008909 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07008910 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
8911
Emeric Brunef42d922012-10-11 16:11:36 +02008912 return 0;
8913}
8914
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008915/* parse the "ssl-default-bind-options" keyword in global section */
8916static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
8917 struct proxy *defpx, const char *file, int line,
8918 char **err) {
8919 int i = 1;
8920
8921 if (*(args[i]) == 0) {
8922 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
8923 return -1;
8924 }
8925 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008926 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01008927 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008928 else if (!strcmp(args[i], "prefer-client-ciphers"))
8929 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008930 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
8931 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
8932 i++;
8933 else {
8934 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
8935 return -1;
8936 }
8937 }
8938 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008939 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
8940 return -1;
8941 }
8942 i++;
8943 }
8944 return 0;
8945}
8946
8947/* parse the "ssl-default-server-options" keyword in global section */
8948static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
8949 struct proxy *defpx, const char *file, int line,
8950 char **err) {
8951 int i = 1;
8952
8953 if (*(args[i]) == 0) {
8954 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
8955 return -1;
8956 }
8957 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008958 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01008959 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008960 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
8961 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
8962 i++;
8963 else {
8964 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
8965 return -1;
8966 }
8967 }
8968 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008969 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
8970 return -1;
8971 }
8972 i++;
8973 }
8974 return 0;
8975}
8976
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008977/* parse the "ca-base" / "crt-base" keywords in global section.
8978 * Returns <0 on alert, >0 on warning, 0 on success.
8979 */
8980static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
8981 struct proxy *defpx, const char *file, int line,
8982 char **err)
8983{
8984 char **target;
8985
Willy Tarreauef934602016-12-22 23:12:01 +01008986 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008987
8988 if (too_many_args(1, args, err, NULL))
8989 return -1;
8990
8991 if (*target) {
8992 memprintf(err, "'%s' already specified.", args[0]);
8993 return -1;
8994 }
8995
8996 if (*(args[1]) == 0) {
8997 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
8998 return -1;
8999 }
9000 *target = strdup(args[1]);
9001 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009002}
9003
9004/* parse the "ssl-mode-async" keyword in global section.
9005 * Returns <0 on alert, >0 on warning, 0 on success.
9006 */
9007static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
9008 struct proxy *defpx, const char *file, int line,
9009 char **err)
9010{
Willy Tarreau5db847a2019-05-09 14:13:35 +02009011#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009012 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01009013 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009014 return 0;
9015#else
9016 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
9017 return -1;
9018#endif
9019}
9020
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009021#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009022static int ssl_check_async_engine_count(void) {
9023 int err_code = 0;
9024
Emeric Brun3854e012017-05-17 20:42:48 +02009025 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009026 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009027 err_code = ERR_ABORT;
9028 }
9029 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009030}
9031
Grant Zhang872f9c22017-01-21 01:10:18 +00009032/* parse the "ssl-engine" keyword in global section.
9033 * Returns <0 on alert, >0 on warning, 0 on success.
9034 */
9035static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9036 struct proxy *defpx, const char *file, int line,
9037 char **err)
9038{
9039 char *algo;
9040 int ret = -1;
9041
9042 if (*(args[1]) == 0) {
9043 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9044 return ret;
9045 }
9046
9047 if (*(args[2]) == 0) {
9048 /* if no list of algorithms is given, it defaults to ALL */
9049 algo = strdup("ALL");
9050 goto add_engine;
9051 }
9052
9053 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
9054 if (strcmp(args[2], "algo") != 0) {
9055 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
9056 return ret;
9057 }
9058
9059 if (*(args[3]) == 0) {
9060 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
9061 return ret;
9062 }
9063 algo = strdup(args[3]);
9064
9065add_engine:
9066 if (ssl_init_single_engine(args[1], algo)==0) {
9067 openssl_engines_initialized++;
9068 ret = 0;
9069 }
9070 free(algo);
9071 return ret;
9072}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009073#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00009074
Willy Tarreauf22e9682016-12-21 23:23:19 +01009075/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
9076 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9077 */
9078static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
9079 struct proxy *defpx, const char *file, int line,
9080 char **err)
9081{
9082 char **target;
9083
Willy Tarreauef934602016-12-22 23:12:01 +01009084 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01009085
9086 if (too_many_args(1, args, err, NULL))
9087 return -1;
9088
9089 if (*(args[1]) == 0) {
9090 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9091 return -1;
9092 }
9093
9094 free(*target);
9095 *target = strdup(args[1]);
9096 return 0;
9097}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009098
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009099#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009100/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9101 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9102 */
9103static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9104 struct proxy *defpx, const char *file, int line,
9105 char **err)
9106{
9107 char **target;
9108
9109 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9110
9111 if (too_many_args(1, args, err, NULL))
9112 return -1;
9113
9114 if (*(args[1]) == 0) {
9115 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9116 return -1;
9117 }
9118
9119 free(*target);
9120 *target = strdup(args[1]);
9121 return 0;
9122}
9123#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009124
Willy Tarreau9ceda382016-12-21 23:13:03 +01009125/* parse various global tune.ssl settings consisting in positive integers.
9126 * Returns <0 on alert, >0 on warning, 0 on success.
9127 */
9128static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9129 struct proxy *defpx, const char *file, int line,
9130 char **err)
9131{
9132 int *target;
9133
9134 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9135 target = &global.tune.sslcachesize;
9136 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009137 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009138 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009139 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009140 else if (strcmp(args[0], "maxsslconn") == 0)
9141 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009142 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9143 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009144 else {
9145 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9146 return -1;
9147 }
9148
9149 if (too_many_args(1, args, err, NULL))
9150 return -1;
9151
9152 if (*(args[1]) == 0) {
9153 memprintf(err, "'%s' expects an integer argument.", args[0]);
9154 return -1;
9155 }
9156
9157 *target = atoi(args[1]);
9158 if (*target < 0) {
9159 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9160 return -1;
9161 }
9162 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009163}
9164
9165static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9166 struct proxy *defpx, const char *file, int line,
9167 char **err)
9168{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009169 int ret;
9170
9171 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9172 if (ret != 0)
9173 return ret;
9174
Willy Tarreaubafbe012017-11-24 17:34:44 +01009175 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009176 memprintf(err, "'%s' is already configured.", args[0]);
9177 return -1;
9178 }
9179
Willy Tarreaubafbe012017-11-24 17:34:44 +01009180 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9181 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009182 memprintf(err, "Out of memory error.");
9183 return -1;
9184 }
9185 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009186}
9187
9188/* parse "ssl.force-private-cache".
9189 * Returns <0 on alert, >0 on warning, 0 on success.
9190 */
9191static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9192 struct proxy *defpx, const char *file, int line,
9193 char **err)
9194{
9195 if (too_many_args(0, args, err, NULL))
9196 return -1;
9197
Willy Tarreauef934602016-12-22 23:12:01 +01009198 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009199 return 0;
9200}
9201
9202/* parse "ssl.lifetime".
9203 * Returns <0 on alert, >0 on warning, 0 on success.
9204 */
9205static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9206 struct proxy *defpx, const char *file, int line,
9207 char **err)
9208{
9209 const char *res;
9210
9211 if (too_many_args(1, args, err, NULL))
9212 return -1;
9213
9214 if (*(args[1]) == 0) {
9215 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9216 return -1;
9217 }
9218
Willy Tarreauef934602016-12-22 23:12:01 +01009219 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009220 if (res == PARSE_TIME_OVER) {
9221 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9222 args[1], args[0]);
9223 return -1;
9224 }
9225 else if (res == PARSE_TIME_UNDER) {
9226 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9227 args[1], args[0]);
9228 return -1;
9229 }
9230 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009231 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9232 return -1;
9233 }
9234 return 0;
9235}
9236
9237#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009238/* parse "ssl-dh-param-file".
9239 * Returns <0 on alert, >0 on warning, 0 on success.
9240 */
9241static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9242 struct proxy *defpx, const char *file, int line,
9243 char **err)
9244{
9245 if (too_many_args(1, args, err, NULL))
9246 return -1;
9247
9248 if (*(args[1]) == 0) {
9249 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9250 return -1;
9251 }
9252
9253 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9254 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9255 return -1;
9256 }
9257 return 0;
9258}
9259
Willy Tarreau9ceda382016-12-21 23:13:03 +01009260/* parse "ssl.default-dh-param".
9261 * Returns <0 on alert, >0 on warning, 0 on success.
9262 */
9263static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9264 struct proxy *defpx, const char *file, int line,
9265 char **err)
9266{
9267 if (too_many_args(1, args, err, NULL))
9268 return -1;
9269
9270 if (*(args[1]) == 0) {
9271 memprintf(err, "'%s' expects an integer argument.", args[0]);
9272 return -1;
9273 }
9274
Willy Tarreauef934602016-12-22 23:12:01 +01009275 global_ssl.default_dh_param = atoi(args[1]);
9276 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009277 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9278 return -1;
9279 }
9280 return 0;
9281}
9282#endif
9283
9284
William Lallemand32af2032016-10-29 18:09:35 +02009285/* This function is used with TLS ticket keys management. It permits to browse
9286 * each reference. The variable <getnext> must contain the current node,
9287 * <end> point to the root node.
9288 */
9289#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9290static inline
9291struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
9292{
9293 struct tls_keys_ref *ref = getnext;
9294
9295 while (1) {
9296
9297 /* Get next list entry. */
9298 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
9299
9300 /* If the entry is the last of the list, return NULL. */
9301 if (&ref->list == end)
9302 return NULL;
9303
9304 return ref;
9305 }
9306}
9307
9308static inline
9309struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
9310{
9311 int id;
9312 char *error;
9313
9314 /* If the reference starts by a '#', this is numeric id. */
9315 if (reference[0] == '#') {
9316 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
9317 id = strtol(reference + 1, &error, 10);
9318 if (*error != '\0')
9319 return NULL;
9320
9321 /* Perform the unique id lookup. */
9322 return tlskeys_ref_lookupid(id);
9323 }
9324
9325 /* Perform the string lookup. */
9326 return tlskeys_ref_lookup(reference);
9327}
9328#endif
9329
9330
9331#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9332
9333static int cli_io_handler_tlskeys_files(struct appctx *appctx);
9334
9335static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
9336 return cli_io_handler_tlskeys_files(appctx);
9337}
9338
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009339/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
9340 * (next index to be dumped), and cli.p0 (next key reference).
9341 */
William Lallemand32af2032016-10-29 18:09:35 +02009342static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
9343
9344 struct stream_interface *si = appctx->owner;
9345
9346 switch (appctx->st2) {
9347 case STAT_ST_INIT:
9348 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08009349 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +02009350 * later and restart at the state "STAT_ST_INIT".
9351 */
9352 chunk_reset(&trash);
9353
9354 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
9355 chunk_appendf(&trash, "# id secret\n");
9356 else
9357 chunk_appendf(&trash, "# id (file)\n");
9358
Willy Tarreau06d80a92017-10-19 14:32:15 +02009359 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01009360 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009361 return 0;
9362 }
9363
William Lallemand32af2032016-10-29 18:09:35 +02009364 /* Now, we start the browsing of the references lists.
9365 * Note that the following call to LIST_ELEM return bad pointer. The only
9366 * available field of this pointer is <list>. It is used with the function
9367 * tlskeys_list_get_next() for retruning the first available entry
9368 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009369 if (appctx->ctx.cli.p0 == NULL) {
9370 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
9371 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009372 }
9373
9374 appctx->st2 = STAT_ST_LIST;
9375 /* fall through */
9376
9377 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009378 while (appctx->ctx.cli.p0) {
9379 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +02009380
9381 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009382 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +02009383 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009384
9385 if (appctx->ctx.cli.i1 == 0)
9386 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
9387
William Lallemand32af2032016-10-29 18:09:35 +02009388 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +01009389 int head;
9390
9391 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
9392 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009393 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +02009394 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +02009395
9396 chunk_reset(t2);
9397 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +01009398 if (ref->key_size_bits == 128) {
9399 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9400 sizeof(struct tls_sess_key_128),
9401 t2->area, t2->size);
9402 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9403 t2->area);
9404 }
9405 else if (ref->key_size_bits == 256) {
9406 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9407 sizeof(struct tls_sess_key_256),
9408 t2->area, t2->size);
9409 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9410 t2->area);
9411 }
9412 else {
9413 /* This case should never happen */
9414 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
9415 }
William Lallemand32af2032016-10-29 18:09:35 +02009416
Willy Tarreau06d80a92017-10-19 14:32:15 +02009417 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009418 /* let's try again later from this stream. We add ourselves into
9419 * this stream's users so that it can remove us upon termination.
9420 */
Christopher Faulet16f45c82018-02-16 11:23:49 +01009421 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +01009422 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009423 return 0;
9424 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009425 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +02009426 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01009427 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009428 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +02009429 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02009430 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009431 /* let's try again later from this stream. We add ourselves into
9432 * this stream's users so that it can remove us upon termination.
9433 */
Willy Tarreaudb398432018-11-15 11:08:52 +01009434 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009435 return 0;
9436 }
9437
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009438 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +02009439 break;
9440
9441 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009442 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009443 }
9444
9445 appctx->st2 = STAT_ST_FIN;
9446 /* fall through */
9447
9448 default:
9449 appctx->st2 = STAT_ST_FIN;
9450 return 1;
9451 }
9452 return 0;
9453}
9454
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009455/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009456static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009457{
William Lallemand32af2032016-10-29 18:09:35 +02009458 /* no parameter, shows only file list */
9459 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009460 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009461 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009462 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009463 }
9464
9465 if (args[2][0] == '*') {
9466 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009467 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009468 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009469 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
9470 if (!appctx->ctx.cli.p0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009471 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009472 appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009473 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009474 return 1;
9475 }
9476 }
William Lallemand32af2032016-10-29 18:09:35 +02009477 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009478 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009479}
9480
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009481static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009482{
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009483 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +02009484 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009485
William Lallemand32af2032016-10-29 18:09:35 +02009486 /* Expect two parameters: the filename and the new new TLS key in encoding */
9487 if (!*args[3] || !*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009488 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009489 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009490 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009491 return 1;
9492 }
9493
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009494 ref = tlskeys_ref_lookup_ref(args[3]);
9495 if (!ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009496 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009497 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009498 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009499 return 1;
9500 }
9501
Willy Tarreau1c913e42018-08-22 05:26:57 +02009502 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Emeric Brun9e754772019-01-10 17:51:55 +01009503 if (ret < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009504 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009505 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009506 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009507 return 1;
9508 }
Emeric Brun9e754772019-01-10 17:51:55 +01009509
Willy Tarreau1c913e42018-08-22 05:26:57 +02009510 trash.data = ret;
Emeric Brun9e754772019-01-10 17:51:55 +01009511 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) {
9512 appctx->ctx.cli.severity = LOG_ERR;
9513 appctx->ctx.cli.msg = "'set ssl tls-key' received a key of wrong size.\n";
9514 appctx->st0 = CLI_ST_PRINT;
9515 return 1;
9516 }
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009517 appctx->ctx.cli.severity = LOG_INFO;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01009518 appctx->ctx.cli.msg = "TLS ticket key updated!\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009519 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009520 return 1;
9521
9522}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009523#endif
William Lallemand32af2032016-10-29 18:09:35 +02009524
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009525static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009526{
9527#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
9528 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +02009529 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009530
9531 if (!payload)
9532 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +02009533
9534 /* Expect one parameter: the new response in base64 encoding */
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009535 if (!*payload) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009536 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009537 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009538 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009539 return 1;
9540 }
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009541
9542 /* remove \r and \n from the payload */
9543 for (i = 0, j = 0; payload[i]; i++) {
9544 if (payload[i] == '\r' || payload[i] == '\n')
9545 continue;
9546 payload[j++] = payload[i];
9547 }
9548 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +02009549
Willy Tarreau1c913e42018-08-22 05:26:57 +02009550 ret = base64dec(payload, j, trash.area, trash.size);
9551 if (ret < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009552 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009553 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009554 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009555 return 1;
9556 }
9557
Willy Tarreau1c913e42018-08-22 05:26:57 +02009558 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +02009559 if (ssl_sock_update_ocsp_response(&trash, &err)) {
9560 if (err) {
9561 memprintf(&err, "%s.\n", err);
9562 appctx->ctx.cli.err = err;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009563 appctx->st0 = CLI_ST_PRINT_FREE;
William Lallemand32af2032016-10-29 18:09:35 +02009564 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +02009565 else {
9566 appctx->ctx.cli.severity = LOG_ERR;
9567 appctx->ctx.cli.msg = "Failed to update OCSP response.\n";
9568 appctx->st0 = CLI_ST_PRINT;
9569 }
William Lallemand32af2032016-10-29 18:09:35 +02009570 return 1;
9571 }
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009572 appctx->ctx.cli.severity = LOG_INFO;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01009573 appctx->ctx.cli.msg = "OCSP Response updated!\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009574 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009575 return 1;
9576#else
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009577 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009578 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009579 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009580 return 1;
9581#endif
9582
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009583}
9584
Willy Tarreau86a394e2019-05-09 14:15:32 +02009585#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Dragan Dosene06e0642021-02-22 10:03:53 +01009586/* This function returns a sample struct filled with an <arg> content.
9587 * If the <arg> contains a string, it is returned in the sample flagged as
9588 * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is
9589 * filled with the content of the variable by using vars_get_by_desc().
9590 *
9591 * Keep in mind that the sample content may be written to a pre-allocated
9592 * trash chunk as returned by get_trash_chunk().
9593 *
9594 * This function returns 0 if an error occurs, otherwise it returns 1.
9595 */
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009596static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
9597{
9598 switch (arg->type) {
9599 case ARGT_STR:
9600 smp->data.type = SMP_T_STR;
9601 smp->data.u.str = arg->data.str;
Dragan Dosene06e0642021-02-22 10:03:53 +01009602 smp->flags = SMP_F_CONST;
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009603 return 1;
9604 case ARGT_VAR:
9605 if (!vars_get_by_desc(&arg->data.var, smp))
9606 return 0;
9607 if (!sample_casts[smp->data.type][SMP_T_STR])
9608 return 0;
9609 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
9610 return 0;
9611 return 1;
9612 default:
9613 return 0;
9614 }
9615}
9616
Dragan Dosene06e0642021-02-22 10:03:53 +01009617/* This function checks an <arg> and fills it with a variable type if the
9618 * <arg> string contains a valid variable name. If failed, the function
9619 * tries to perform a base64 decode operation on the same string, and
9620 * fills the <arg> with the decoded content.
9621 *
9622 * Validation is skipped if the <arg> string is empty.
9623 *
9624 * This function returns 0 if the variable lookup fails and the specified
9625 * <arg> string is not a valid base64 encoded string, as well if
9626 * unexpected argument type is specified or memory allocation error
9627 * occurs. Otherwise it returns 1.
9628 */
9629static inline int sample_check_arg_base64(struct arg *arg, char **err)
9630{
9631 char *dec = NULL;
9632 int dec_size;
9633
9634 if (arg->type != ARGT_STR) {
9635 memprintf(err, "unexpected argument type");
9636 return 0;
9637 }
9638
9639 if (arg->data.str.data == 0) /* empty */
9640 return 1;
9641
9642 if (vars_check_arg(arg, NULL))
9643 return 1;
9644
9645 if (arg->data.str.data % 4) {
9646 memprintf(err, "argument needs to be base64 encoded, and "
9647 "can either be a string or a variable");
9648 return 0;
9649 }
9650
9651 dec_size = (arg->data.str.data / 4 * 3)
9652 - (arg->data.str.area[arg->data.str.data-1] == '=' ? 1 : 0)
9653 - (arg->data.str.area[arg->data.str.data-2] == '=' ? 1 : 0);
9654
9655 if ((dec = malloc(dec_size)) == NULL) {
9656 memprintf(err, "memory allocation error");
9657 return 0;
9658 }
9659
9660 dec_size = base64dec(arg->data.str.area, arg->data.str.data, dec, dec_size);
9661 if (dec_size < 0) {
9662 memprintf(err, "argument needs to be base64 encoded, and "
9663 "can either be a string or a variable");
9664 free(dec);
9665 return 0;
9666 }
9667
9668 /* base64 decoded */
9669 chunk_destroy(&arg->data.str);
9670 arg->data.str.area = dec;
9671 arg->data.str.data = dec_size;
9672 return 1;
9673}
9674
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009675static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
9676 const char *file, int line, char **err)
9677{
9678 switch(args[0].data.sint) {
9679 case 128:
9680 case 192:
9681 case 256:
9682 break;
9683 default:
9684 memprintf(err, "key size must be 128, 192 or 256 (bits).");
9685 return 0;
9686 }
Dragan Dosene06e0642021-02-22 10:03:53 +01009687
9688 /* Try to decode variables. */
9689 if (!sample_check_arg_base64(&args[1], err)) {
9690 memprintf(err, "failed to parse nonce : %s", *err);
9691 return 0;
9692 }
9693 if (!sample_check_arg_base64(&args[2], err)) {
9694 memprintf(err, "failed to parse key : %s", *err);
9695 return 0;
9696 }
9697 if (!sample_check_arg_base64(&args[3], err)) {
9698 memprintf(err, "failed to parse aead_tag : %s", *err);
9699 return 0;
9700 }
9701
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009702 return 1;
9703}
9704
9705/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
9706static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
9707{
9708 struct sample nonce, key, aead_tag;
Dragan Dosene06e0642021-02-22 10:03:53 +01009709 struct buffer *smp_trash = NULL, *smp_trash_alloc = NULL;
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009710 EVP_CIPHER_CTX *ctx;
9711 int dec_size, ret;
9712
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009713 smp_trash_alloc = alloc_trash_chunk();
9714 if (!smp_trash_alloc)
9715 return 0;
9716
Dragan Dosene06e0642021-02-22 10:03:53 +01009717 /* smp copy */
9718 smp_trash_alloc->data = smp->data.u.str.data;
9719 if (unlikely(smp_trash_alloc->data > smp_trash_alloc->size))
9720 smp_trash_alloc->data = smp_trash_alloc->size;
9721 memcpy(smp_trash_alloc->area, smp->data.u.str.area, smp_trash_alloc->data);
9722
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009723 ctx = EVP_CIPHER_CTX_new();
9724
9725 if (!ctx)
9726 goto err;
9727
Dragan Dosene06e0642021-02-22 10:03:53 +01009728 smp_trash = alloc_trash_chunk();
9729 if (!smp_trash)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009730 goto err;
Dragan Dosene06e0642021-02-22 10:03:53 +01009731
9732 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
9733 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
9734 goto err;
9735
9736 if (arg_p[1].type == ARGT_VAR) {
9737 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
9738 if (dec_size < 0)
9739 goto err;
9740 smp_trash->data = dec_size;
9741 nonce.data.u.str = *smp_trash;
9742 }
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009743
9744 /* Set cipher type and mode */
9745 switch(arg_p[0].data.sint) {
9746 case 128:
9747 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
9748 break;
9749 case 192:
9750 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
9751 break;
9752 case 256:
9753 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
9754 break;
9755 }
9756
Dragan Dosene06e0642021-02-22 10:03:53 +01009757 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, nonce.data.u.str.data, NULL);
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009758
9759 /* Initialise IV */
Dragan Dosene06e0642021-02-22 10:03:53 +01009760 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) nonce.data.u.str.area))
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009761 goto err;
9762
Dragan Dosene06e0642021-02-22 10:03:53 +01009763 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
9764 if (!sample_conv_var2smp_str(&arg_p[2], &key))
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009765 goto err;
Dragan Dosene06e0642021-02-22 10:03:53 +01009766
9767 if (arg_p[2].type == ARGT_VAR) {
9768 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
9769 if (dec_size < 0)
9770 goto err;
9771 smp_trash->data = dec_size;
9772 key.data.u.str = *smp_trash;
9773 }
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009774
9775 /* Initialise key */
Dragan Dosene06e0642021-02-22 10:03:53 +01009776 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) key.data.u.str.area, NULL))
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009777 goto err;
9778
9779 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
Dragan Dosene06e0642021-02-22 10:03:53 +01009780 (unsigned char *) smp_trash_alloc->area, (int) smp_trash_alloc->data))
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009781 goto err;
9782
Dragan Dosene06e0642021-02-22 10:03:53 +01009783 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
9784 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009785 goto err;
Dragan Dosene06e0642021-02-22 10:03:53 +01009786
9787 if (arg_p[3].type == ARGT_VAR) {
9788 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
9789 if (dec_size < 0)
9790 goto err;
9791 smp_trash_alloc->data = dec_size;
9792 aead_tag.data.u.str = *smp_trash_alloc;
9793 }
9794
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009795 dec_size = smp_trash->data;
9796
Dragan Dosene06e0642021-02-22 10:03:53 +01009797 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, aead_tag.data.u.str.data, (void *) aead_tag.data.u.str.area);
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009798 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
9799
9800 if (ret <= 0)
9801 goto err;
9802
9803 smp->data.u.str.data = dec_size + smp_trash->data;
9804 smp->data.u.str.area = smp_trash->area;
9805 smp->data.type = SMP_T_BIN;
Dragan Dosene06e0642021-02-22 10:03:53 +01009806 smp_dup(smp);
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009807 free_trash_chunk(smp_trash_alloc);
Dragan Dosene06e0642021-02-22 10:03:53 +01009808 free_trash_chunk(smp_trash);
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009809 return 1;
9810
9811err:
9812 free_trash_chunk(smp_trash_alloc);
Dragan Dosene06e0642021-02-22 10:03:53 +01009813 free_trash_chunk(smp_trash);
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009814 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009815}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009816# endif
William Lallemand32af2032016-10-29 18:09:35 +02009817
9818/* register cli keywords */
9819static struct cli_kw_list cli_kws = {{ },{
9820#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9821 { { "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 +02009822 { { "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 +02009823#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009824 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemand32af2032016-10-29 18:09:35 +02009825 { { NULL }, NULL, NULL, NULL }
9826}};
9827
Willy Tarreau0108d902018-11-25 19:14:37 +01009828INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +02009829
Willy Tarreau7875d092012-09-10 08:20:03 +02009830/* Note: must not be declared <const> as its list will be overwritten.
9831 * Please take care of keeping this list alphabetically sorted.
9832 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009833static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +02009834 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009835 { "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 +01009836#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +01009837 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +01009838#endif
Emeric Brun645ae792014-04-30 14:21:06 +02009839 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +01009840#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9841 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
9842#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +01009843 { "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 +02009844 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +02009845 { "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 +02009846 { "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 +02009847#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +02009848 { "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 -04009849#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009850#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -04009851 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
9852 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -04009853 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
9854#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009855 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
9856 { "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 +01009857 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009858 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02009859 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9860 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9861 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9862 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9863 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9864 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9865 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9866 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009867 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009868 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
9869 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +01009870 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02009871 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9872 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9873 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9874 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9875 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9876 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9877 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +02009878 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009879 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009880 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009881 { "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 +01009882 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009883 { "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 +02009884 { "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 +01009885 { "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 +02009886 { "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 +01009887#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009888 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +02009889#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009890#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009891 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +02009892#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009893 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009894#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +02009895 { "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 -04009896#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009897 { "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 +02009898#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009899 { "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 -04009900#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009901#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -04009902 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9903 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -04009904 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9905#endif
Patrick Hemmer41966772018-04-28 19:15:48 -04009906#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009907 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -04009908#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009909 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9910 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9911 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9912 { "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 +02009913 { NULL, NULL, 0, 0, 0 },
9914}};
9915
Willy Tarreau0108d902018-11-25 19:14:37 +01009916INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
9917
Willy Tarreau7875d092012-09-10 08:20:03 +02009918/* Note: must not be declared <const> as its list will be overwritten.
9919 * Please take care of keeping this list alphabetically sorted.
9920 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009921static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +01009922 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
9923 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +01009924 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +02009925}};
9926
Willy Tarreau0108d902018-11-25 19:14:37 +01009927INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
9928
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009929/* Note: must not be declared <const> as its list will be overwritten.
9930 * Please take care of keeping this list alphabetically sorted, doing so helps
9931 * all code contributors.
9932 * Optional keywords are also declared with a NULL ->parse() function so that
9933 * the config parser can report an appropriate error when a known keyword was
9934 * not enabled.
9935 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009936static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +02009937 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009938 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
9939 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
9940 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009941#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009942 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
9943#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009944 { "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 +01009945 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009946 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009947 { "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 +01009948 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009949 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
9950 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009951 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
9952 { NULL, NULL, 0 },
9953};
9954
Willy Tarreau0108d902018-11-25 19:14:37 +01009955/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
9956
Willy Tarreau51fb7652012-09-18 18:24:39 +02009957static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +02009958 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009959 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
9960 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
9961 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
9962 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
9963 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
9964 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009965#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009966 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
9967#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009968 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
9969 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
9970 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
9971 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
9972 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
9973 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
9974 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
9975 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
9976 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
9977 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009978 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009979 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009980 { "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 +02009981 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
9982 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
9983 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
9984 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009985 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009986 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
9987 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009988 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
9989 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009990 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
9991 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
9992 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
9993 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
9994 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009995 { NULL, NULL, 0 },
9996}};
Emeric Brun46591952012-05-18 15:47:34 +02009997
Willy Tarreau0108d902018-11-25 19:14:37 +01009998INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
9999
Willy Tarreau92faadf2012-10-10 23:04:25 +020010000/* Note: must not be declared <const> as its list will be overwritten.
10001 * Please take care of keeping this list alphabetically sorted, doing so helps
10002 * all code contributors.
10003 * Optional keywords are also declared with a NULL ->parse() function so that
10004 * the config parser can report an appropriate error when a known keyword was
10005 * not enabled.
10006 */
10007static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010010008 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010010009 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010010 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010010011 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020010012 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010013 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
10014 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010015#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010016 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
10017#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010018 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
10019 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
10020 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
10021 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
10022 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
10023 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
10024 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
10025 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
10026 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
10027 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
10028 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
10029 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
10030 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
10031 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
10032 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
10033 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
10034 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
10035 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010010036 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010037 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
10038 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
10039 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
10040 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
10041 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
10042 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
10043 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
10044 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
10045 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
10046 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020010047 { NULL, NULL, 0, 0 },
10048}};
10049
Willy Tarreau0108d902018-11-25 19:14:37 +010010050INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
10051
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010052static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010053 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
10054 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010055 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010056 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
10057 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010010058#ifndef OPENSSL_NO_DH
10059 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
10060#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010061 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010062#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000010063 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010064#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010010065 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
10066#ifndef OPENSSL_NO_DH
10067 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
10068#endif
10069 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
10070 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
10071 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
10072 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010073 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010010074 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
10075 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010076#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010077 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
10078 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
10079#endif
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010080 { 0, NULL, NULL },
10081}};
10082
Willy Tarreau0108d902018-11-25 19:14:37 +010010083INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
10084
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010085/* Note: must not be declared <const> as its list will be overwritten */
10086static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020010087#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010088 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
10089#endif
10090 { NULL, NULL, 0, 0, 0 },
10091}};
10092
10093INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
10094
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020010095/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010010096static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020010097 .snd_buf = ssl_sock_from_buf,
10098 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010010099 .subscribe = ssl_subscribe,
10100 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020010101 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020010102 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020010103 .rcv_pipe = NULL,
10104 .snd_pipe = NULL,
10105 .shutr = NULL,
10106 .shutw = ssl_sock_shutw,
10107 .close = ssl_sock_close,
10108 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010010109 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010010110 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010010111 .prepare_srv = ssl_sock_prepare_srv_ctx,
10112 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010010113 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010010114 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020010115};
10116
Olivier Houchardccaa7de2017-10-02 11:51:03 +020010117enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
10118 struct session *sess, struct stream *s, int flags)
10119{
10120 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010010121 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020010122
10123 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010010124 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020010125
Olivier Houchard6fa63d92017-11-27 18:41:32 +010010126 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020010127 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010010128 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020010129 s->req.flags |= CF_READ_NULL;
10130 return ACT_RET_YIELD;
10131 }
10132 }
10133 return (ACT_RET_CONT);
10134}
10135
10136static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
10137{
10138 rule->action_ptr = ssl_action_wait_for_hs;
10139
10140 return ACT_RET_PRS_OK;
10141}
10142
10143static struct action_kw_list http_req_actions = {ILH, {
10144 { "wait-for-handshake", ssl_parse_wait_for_hs },
10145 { /* END */ }
10146}};
10147
Willy Tarreau0108d902018-11-25 19:14:37 +010010148INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
10149
Willy Tarreau5db847a2019-05-09 14:13:35 +020010150#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010010151
10152static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
10153{
10154 if (ptr) {
10155 chunk_destroy(ptr);
10156 free(ptr);
10157 }
10158}
10159
10160#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010010161static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
10162{
Willy Tarreaubafbe012017-11-24 17:34:44 +010010163 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010010164}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010010165
Emeric Brun46591952012-05-18 15:47:34 +020010166__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020010167static void __ssl_sock_init(void)
10168{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010169#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020010170 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050010171 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010172#endif
Emeric Brun46591952012-05-18 15:47:34 +020010173
Willy Tarreauef934602016-12-22 23:12:01 +010010174 if (global_ssl.listen_default_ciphers)
10175 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
10176 if (global_ssl.connect_default_ciphers)
10177 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010178#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010179 if (global_ssl.listen_default_ciphersuites)
10180 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
10181 if (global_ssl.connect_default_ciphersuites)
10182 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10183#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010010184
Willy Tarreau13e14102016-12-22 20:25:26 +010010185 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020010186#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020010187 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080010188#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010189#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020010190 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050010191 n = sk_SSL_COMP_num(cm);
10192 while (n--) {
10193 (void) sk_SSL_COMP_pop(cm);
10194 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010195#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050010196
Willy Tarreau5db847a2019-05-09 14:13:35 +020010197#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020010198 ssl_locking_init();
10199#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020010200#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010010201 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
10202#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020010203 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020010204 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 +010010205 ssl_pkey_info_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010206#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000010207 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010208 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010209#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010010210#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10211 hap_register_post_check(tlskeys_finalize_config);
10212#endif
Willy Tarreau80713382018-11-26 10:19:54 +010010213
10214 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
10215 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
10216
10217#ifndef OPENSSL_NO_DH
10218 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
10219 hap_register_post_deinit(ssl_free_dh);
10220#endif
10221#ifndef OPENSSL_NO_ENGINE
10222 hap_register_post_deinit(ssl_free_engines);
10223#endif
10224 /* Load SSL string for the verbose & debug mode. */
10225 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020010226 ha_meth = BIO_meth_new(0x666, "ha methods");
10227 BIO_meth_set_write(ha_meth, ha_ssl_write);
10228 BIO_meth_set_read(ha_meth, ha_ssl_read);
10229 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
10230 BIO_meth_set_create(ha_meth, ha_ssl_new);
10231 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
10232 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
10233 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
Willy Tarreau80713382018-11-26 10:19:54 +010010234}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010010235
Willy Tarreau80713382018-11-26 10:19:54 +010010236/* Compute and register the version string */
10237static void ssl_register_build_options()
10238{
10239 char *ptr = NULL;
10240 int i;
10241
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010242 memprintf(&ptr, "Built with OpenSSL version : "
10243#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010010244 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010245#else /* OPENSSL_IS_BORINGSSL */
10246 OPENSSL_VERSION_TEXT
10247 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080010248 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020010249 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010250#endif
10251 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020010252#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010253 "no (library version too old)"
10254#elif defined(OPENSSL_NO_TLSEXT)
10255 "no (disabled via OPENSSL_NO_TLSEXT)"
10256#else
10257 "yes"
10258#endif
10259 "", ptr);
10260
10261 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
10262#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10263 "yes"
10264#else
10265#ifdef OPENSSL_NO_TLSEXT
10266 "no (because of OPENSSL_NO_TLSEXT)"
10267#else
10268 "no (version might be too old, 0.9.8f min needed)"
10269#endif
10270#endif
10271 "", ptr);
10272
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020010273 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
10274 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
10275 if (methodVersions[i].option)
10276 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010010277
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010278 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010010279}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010280
Willy Tarreau80713382018-11-26 10:19:54 +010010281INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020010282
Emeric Brun46591952012-05-18 15:47:34 +020010283
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010284#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000010285void ssl_free_engines(void) {
10286 struct ssl_engine_list *wl, *wlb;
10287 /* free up engine list */
10288 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
10289 ENGINE_finish(wl->e);
10290 ENGINE_free(wl->e);
10291 LIST_DEL(&wl->list);
10292 free(wl);
10293 }
10294}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010295#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020010296
Remi Gacogned3a23c32015-05-28 16:39:47 +020010297#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000010298void ssl_free_dh(void) {
10299 if (local_dh_1024) {
10300 DH_free(local_dh_1024);
10301 local_dh_1024 = NULL;
10302 }
10303 if (local_dh_2048) {
10304 DH_free(local_dh_2048);
10305 local_dh_2048 = NULL;
10306 }
10307 if (local_dh_4096) {
10308 DH_free(local_dh_4096);
10309 local_dh_4096 = NULL;
10310 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020010311 if (global_dh) {
10312 DH_free(global_dh);
10313 global_dh = NULL;
10314 }
Grant Zhang872f9c22017-01-21 01:10:18 +000010315}
10316#endif
10317
10318__attribute__((destructor))
10319static void __ssl_sock_deinit(void)
10320{
10321#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020010322 if (ssl_ctx_lru_tree) {
10323 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010010324 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020010325 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020010326#endif
10327
Willy Tarreau5db847a2019-05-09 14:13:35 +020010328#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020010329 ERR_remove_state(0);
10330 ERR_free_strings();
10331
10332 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080010333#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020010334
Willy Tarreau5db847a2019-05-09 14:13:35 +020010335#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020010336 CRYPTO_cleanup_all_ex_data();
10337#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020010338 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020010339}
10340
10341
Emeric Brun46591952012-05-18 15:47:34 +020010342/*
10343 * Local variables:
10344 * c-indent-level: 8
10345 * c-basic-offset: 8
10346 * End:
10347 */