blob: 3dbab2be3dd7ed6afe8513af2881bc4122091711 [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;
458 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200459 (unsigned short)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));
492 el->e = engine;
493 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100494 nb_engines++;
495 if (global_ssl.async)
496 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000497 return 0;
498
499fail_set_method:
500 /* release the functional reference from ENGINE_init() */
501 ENGINE_finish(engine);
502
503fail_init:
504 /* release the structural reference from ENGINE_by_id() */
505 ENGINE_free(engine);
506
507fail_get:
508 return err_code;
509}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200510#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000511
Willy Tarreau5db847a2019-05-09 14:13:35 +0200512#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200513/*
514 * openssl async fd handler
515 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200516void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000517{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200518 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000519
Emeric Brun3854e012017-05-17 20:42:48 +0200520 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000521 * to poll this fd until it is requested
522 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000523 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000524 fd_cant_recv(fd);
525
526 /* crypto engine is available, let's notify the associated
527 * connection that it can pursue its processing.
528 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200529 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000530}
531
Emeric Brun3854e012017-05-17 20:42:48 +0200532/*
533 * openssl async delayed SSL_free handler
534 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200535void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000536{
537 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200538 OSSL_ASYNC_FD all_fd[32];
539 size_t num_all_fds = 0;
540 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000541
Emeric Brun3854e012017-05-17 20:42:48 +0200542 /* We suppose that the async job for a same SSL *
543 * are serialized. So if we are awake it is
544 * because the running job has just finished
545 * and we can remove all async fds safely
546 */
547 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
548 if (num_all_fds > 32) {
549 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
550 return;
551 }
552
553 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
554 for (i=0 ; i < num_all_fds ; i++)
555 fd_remove(all_fd[i]);
556
557 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000558 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100559 _HA_ATOMIC_SUB(&sslconns, 1);
560 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000561}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000562/*
Emeric Brun3854e012017-05-17 20:42:48 +0200563 * function used to manage a returned SSL_ERROR_WANT_ASYNC
564 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000565 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200566static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000567{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100568 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200569 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200570 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000571 size_t num_add_fds = 0;
572 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200573 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000574
575 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
576 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200577 if (num_add_fds > 32 || num_del_fds > 32) {
578 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 +0000579 return;
580 }
581
Emeric Brun3854e012017-05-17 20:42:48 +0200582 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000583
Emeric Brun3854e012017-05-17 20:42:48 +0200584 /* We remove unused fds from the fdtab */
585 for (i=0 ; i < num_del_fds ; i++)
586 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000587
Emeric Brun3854e012017-05-17 20:42:48 +0200588 /* We add new fds to the fdtab */
589 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200590 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000591 }
592
Emeric Brun3854e012017-05-17 20:42:48 +0200593 num_add_fds = 0;
594 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
595 if (num_add_fds > 32) {
596 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
597 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000598 }
Emeric Brun3854e012017-05-17 20:42:48 +0200599
600 /* We activate the polling for all known async fds */
601 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000602 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200603 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000604 /* To ensure that the fd cache won't be used
605 * We'll prefer to catch a real RD event
606 * because handling an EAGAIN on this fd will
607 * result in a context switch and also
608 * some engines uses a fd in blocking mode.
609 */
610 fd_cant_recv(add_fd[i]);
611 }
Emeric Brun3854e012017-05-17 20:42:48 +0200612
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000613}
614#endif
615
William Lallemandc33d83d2019-10-14 14:14:59 +0200616#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200617/*
618 * This function returns the number of seconds elapsed
619 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
620 * date presented un ASN1_GENERALIZEDTIME.
621 *
622 * In parsing error case, it returns -1.
623 */
624static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
625{
626 long epoch;
627 char *p, *end;
628 const unsigned short month_offset[12] = {
629 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
630 };
631 int year, month;
632
633 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
634
635 p = (char *)d->data;
636 end = p + d->length;
637
638 if (end - p < 4) return -1;
639 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
640 p += 4;
641 if (end - p < 2) return -1;
642 month = 10 * (p[0] - '0') + p[1] - '0';
643 if (month < 1 || month > 12) return -1;
644 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
645 We consider leap years and the current month (<marsh or not) */
646 epoch = ( ((year - 1970) * 365)
647 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
648 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
649 + month_offset[month-1]
650 ) * 24 * 60 * 60;
651 p += 2;
652 if (end - p < 2) return -1;
653 /* Add the number of seconds of completed days of current month */
654 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
655 p += 2;
656 if (end - p < 2) return -1;
657 /* Add the completed hours of the current day */
658 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
659 p += 2;
660 if (end - p < 2) return -1;
661 /* Add the completed minutes of the current hour */
662 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
663 p += 2;
664 if (p == end) return -1;
665 /* Test if there is available seconds */
666 if (p[0] < '0' || p[0] > '9')
667 goto nosec;
668 if (end - p < 2) return -1;
669 /* Add the seconds of the current minute */
670 epoch += 10 * (p[0] - '0') + p[1] - '0';
671 p += 2;
672 if (p == end) return -1;
673 /* Ignore seconds float part if present */
674 if (p[0] == '.') {
675 do {
676 if (++p == end) return -1;
677 } while (p[0] >= '0' && p[0] <= '9');
678 }
679
680nosec:
681 if (p[0] == 'Z') {
682 if (end - p != 1) return -1;
683 return epoch;
684 }
685 else if (p[0] == '+') {
686 if (end - p != 5) return -1;
687 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700688 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 +0200689 }
690 else if (p[0] == '-') {
691 if (end - p != 5) return -1;
692 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700693 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 +0200694 }
695
696 return -1;
697}
698
William Lallemandc33d83d2019-10-14 14:14:59 +0200699/*
700 * struct alignment works here such that the key.key is the same as key_data
701 * Do not change the placement of key_data
702 */
703struct certificate_ocsp {
704 struct ebmb_node key;
705 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
706 struct buffer response;
707 long expire;
708};
709
710struct ocsp_cbk_arg {
711 int is_single;
712 int single_kt;
713 union {
714 struct certificate_ocsp *s_ocsp;
715 /*
716 * m_ocsp will have multiple entries dependent on key type
717 * Entry 0 - DSA
718 * Entry 1 - ECDSA
719 * Entry 2 - RSA
720 */
721 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
722 };
723};
724
Emeric Brun1d3865b2014-06-20 15:37:32 +0200725static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200726
727/* This function starts to check if the OCSP response (in DER format) contained
728 * in chunk 'ocsp_response' is valid (else exits on error).
729 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
730 * contained in the OCSP Response and exits on error if no match.
731 * If it's a valid OCSP Response:
732 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
733 * pointed by 'ocsp'.
734 * If 'ocsp' is NULL, the function looks up into the OCSP response's
735 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
736 * from the response) and exits on error if not found. Finally, If an OCSP response is
737 * already present in the container, it will be overwritten.
738 *
739 * Note: OCSP response containing more than one OCSP Single response is not
740 * considered valid.
741 *
742 * Returns 0 on success, 1 in error case.
743 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200744static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
745 struct certificate_ocsp *ocsp,
746 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200747{
748 OCSP_RESPONSE *resp;
749 OCSP_BASICRESP *bs = NULL;
750 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200751 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200752 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200753 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200754 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200755 int reason;
756 int ret = 1;
757
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200758 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
759 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200760 if (!resp) {
761 memprintf(err, "Unable to parse OCSP response");
762 goto out;
763 }
764
765 rc = OCSP_response_status(resp);
766 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
767 memprintf(err, "OCSP response status not successful");
768 goto out;
769 }
770
771 bs = OCSP_response_get1_basic(resp);
772 if (!bs) {
773 memprintf(err, "Failed to get basic response from OCSP Response");
774 goto out;
775 }
776
777 count_sr = OCSP_resp_count(bs);
778 if (count_sr > 1) {
779 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
780 goto out;
781 }
782
783 sr = OCSP_resp_get0(bs, 0);
784 if (!sr) {
785 memprintf(err, "Failed to get OCSP single response");
786 goto out;
787 }
788
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200789 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
790
Emeric Brun4147b2e2014-06-16 18:36:30 +0200791 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200792 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200793 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200794 goto out;
795 }
796
Emeric Brun13a6b482014-06-20 15:44:34 +0200797 if (!nextupd) {
798 memprintf(err, "OCSP single response: missing nextupdate");
799 goto out;
800 }
801
Emeric Brunc8b27b62014-06-19 14:16:17 +0200802 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200803 if (!rc) {
804 memprintf(err, "OCSP single response: no longer valid.");
805 goto out;
806 }
807
808 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200809 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200810 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
811 goto out;
812 }
813 }
814
815 if (!ocsp) {
816 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
817 unsigned char *p;
818
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200819 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200820 if (!rc) {
821 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
822 goto out;
823 }
824
825 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
826 memprintf(err, "OCSP single response: Certificate ID too long");
827 goto out;
828 }
829
830 p = key;
831 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200832 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200833 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
834 if (!ocsp) {
835 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
836 goto out;
837 }
838 }
839
840 /* According to comments on "chunk_dup", the
841 previous chunk buffer will be freed */
842 if (!chunk_dup(&ocsp->response, ocsp_response)) {
843 memprintf(err, "OCSP response: Memory allocation error");
844 goto out;
845 }
846
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200847 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
848
Emeric Brun4147b2e2014-06-16 18:36:30 +0200849 ret = 0;
850out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +0100851 ERR_clear_error();
852
Emeric Brun4147b2e2014-06-16 18:36:30 +0200853 if (bs)
854 OCSP_BASICRESP_free(bs);
855
856 if (resp)
857 OCSP_RESPONSE_free(resp);
858
859 return ret;
860}
861/*
862 * External function use to update the OCSP response in the OCSP response's
863 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
864 * to update in DER format.
865 *
866 * Returns 0 on success, 1 in error case.
867 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200868int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200869{
870 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
871}
872
873/*
874 * This function load the OCSP Resonse in DER format contained in file at
875 * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response'
876 *
877 * Returns 0 on success, 1 in error case.
878 */
879static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err)
880{
881 int fd = -1;
882 int r = 0;
883 int ret = 1;
884
885 fd = open(ocsp_path, O_RDONLY);
886 if (fd == -1) {
887 memprintf(err, "Error opening OCSP response file");
888 goto end;
889 }
890
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200891 trash.data = 0;
892 while (trash.data < trash.size) {
893 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200894 if (r < 0) {
895 if (errno == EINTR)
896 continue;
897
898 memprintf(err, "Error reading OCSP response from file");
899 goto end;
900 }
901 else if (r == 0) {
902 break;
903 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200904 trash.data += r;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200905 }
906
907 close(fd);
908 fd = -1;
909
910 ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err);
911end:
912 if (fd != -1)
913 close(fd);
914
915 return ret;
916}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100917#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +0200918
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100919#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
920static 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)
921{
Christopher Faulet16f45c82018-02-16 11:23:49 +0100922 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +0100923 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100924 struct connection *conn;
925 int head;
926 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100927 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100928
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200929 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +0200930 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100931 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
932
933 keys = ref->tlskeys;
934 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100935
936 if (enc) {
937 memcpy(key_name, keys[head].name, 16);
938
939 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +0100940 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100941
Emeric Brun9e754772019-01-10 17:51:55 +0100942 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100943
Emeric Brun9e754772019-01-10 17:51:55 +0100944 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
945 goto end;
946
Willy Tarreau9356dac2019-05-10 09:22:53 +0200947 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100948 ret = 1;
949 }
950 else if (ref->key_size_bits == 256 ) {
951
952 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
953 goto end;
954
Willy Tarreau9356dac2019-05-10 09:22:53 +0200955 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100956 ret = 1;
957 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100958 } else {
959 for (i = 0; i < TLS_TICKETS_NO; i++) {
960 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
961 goto found;
962 }
Christopher Faulet16f45c82018-02-16 11:23:49 +0100963 ret = 0;
964 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100965
Christopher Faulet16f45c82018-02-16 11:23:49 +0100966 found:
Emeric Brun9e754772019-01-10 17:51:55 +0100967 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +0200968 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 +0100969 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
970 goto end;
971 /* 2 for key renewal, 1 if current key is still valid */
972 ret = i ? 2 : 1;
973 }
974 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +0200975 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 +0100976 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
977 goto end;
978 /* 2 for key renewal, 1 if current key is still valid */
979 ret = i ? 2 : 1;
980 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100981 }
Emeric Brun9e754772019-01-10 17:51:55 +0100982
Christopher Faulet16f45c82018-02-16 11:23:49 +0100983 end:
984 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
985 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200986}
987
988struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
989{
990 struct tls_keys_ref *ref;
991
992 list_for_each_entry(ref, &tlskeys_reference, list)
993 if (ref->filename && strcmp(filename, ref->filename) == 0)
994 return ref;
995 return NULL;
996}
997
998struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
999{
1000 struct tls_keys_ref *ref;
1001
1002 list_for_each_entry(ref, &tlskeys_reference, list)
1003 if (ref->unique_id == unique_id)
1004 return ref;
1005 return NULL;
1006}
1007
Emeric Brun9e754772019-01-10 17:51:55 +01001008/* Update the key into ref: if keysize doesnt
1009 * match existing ones, this function returns -1
1010 * else it returns 0 on success.
1011 */
1012int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001013 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001014{
Emeric Brun9e754772019-01-10 17:51:55 +01001015 if (ref->key_size_bits == 128) {
1016 if (tlskey->data != sizeof(struct tls_sess_key_128))
1017 return -1;
1018 }
1019 else if (ref->key_size_bits == 256) {
1020 if (tlskey->data != sizeof(struct tls_sess_key_256))
1021 return -1;
1022 }
1023 else
1024 return -1;
1025
Christopher Faulet16f45c82018-02-16 11:23:49 +01001026 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001027 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1028 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001029 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1030 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001031
1032 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001033}
1034
Willy Tarreau83061a82018-07-13 11:56:34 +02001035int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001036{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001037 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1038
1039 if(!ref) {
1040 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1041 return 1;
1042 }
Emeric Brun9e754772019-01-10 17:51:55 +01001043 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1044 memprintf(err, "Invalid key size");
1045 return 1;
1046 }
1047
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001048 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001049}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001050
1051/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001052 * automatic ids. It's called just after the basic checks. It returns
1053 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001054 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001055static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001056{
1057 int i = 0;
1058 struct tls_keys_ref *ref, *ref2, *ref3;
1059 struct list tkr = LIST_HEAD_INIT(tkr);
1060
1061 list_for_each_entry(ref, &tlskeys_reference, list) {
1062 if (ref->unique_id == -1) {
1063 /* Look for the first free id. */
1064 while (1) {
1065 list_for_each_entry(ref2, &tlskeys_reference, list) {
1066 if (ref2->unique_id == i) {
1067 i++;
1068 break;
1069 }
1070 }
1071 if (&ref2->list == &tlskeys_reference)
1072 break;
1073 }
1074
1075 /* Uses the unique id and increment it for the next entry. */
1076 ref->unique_id = i;
1077 i++;
1078 }
1079 }
1080
1081 /* This sort the reference list by id. */
1082 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1083 LIST_DEL(&ref->list);
1084 list_for_each_entry(ref3, &tkr, list) {
1085 if (ref->unique_id < ref3->unique_id) {
1086 LIST_ADDQ(&ref3->list, &ref->list);
1087 break;
1088 }
1089 }
1090 if (&ref3->list == &tkr)
1091 LIST_ADDQ(&tkr, &ref->list);
1092 }
1093
1094 /* swap root */
1095 LIST_ADD(&tkr, &tlskeys_reference);
1096 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001097 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001098}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001099#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1100
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001101#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001102int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1103{
1104 switch (evp_keytype) {
1105 case EVP_PKEY_RSA:
1106 return 2;
1107 case EVP_PKEY_DSA:
1108 return 0;
1109 case EVP_PKEY_EC:
1110 return 1;
1111 }
1112
1113 return -1;
1114}
1115
Emeric Brun4147b2e2014-06-16 18:36:30 +02001116/*
1117 * Callback used to set OCSP status extension content in server hello.
1118 */
1119int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1120{
yanbzhube2774d2015-12-10 15:07:30 -05001121 struct certificate_ocsp *ocsp;
1122 struct ocsp_cbk_arg *ocsp_arg;
1123 char *ssl_buf;
1124 EVP_PKEY *ssl_pkey;
1125 int key_type;
1126 int index;
1127
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001128 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001129
1130 ssl_pkey = SSL_get_privatekey(ssl);
1131 if (!ssl_pkey)
1132 return SSL_TLSEXT_ERR_NOACK;
1133
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001134 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001135
1136 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1137 ocsp = ocsp_arg->s_ocsp;
1138 else {
1139 /* For multiple certs per context, we have to find the correct OCSP response based on
1140 * the certificate type
1141 */
1142 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1143
1144 if (index < 0)
1145 return SSL_TLSEXT_ERR_NOACK;
1146
1147 ocsp = ocsp_arg->m_ocsp[index];
1148
1149 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001150
1151 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001152 !ocsp->response.area ||
1153 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001154 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001155 return SSL_TLSEXT_ERR_NOACK;
1156
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001157 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001158 if (!ssl_buf)
1159 return SSL_TLSEXT_ERR_NOACK;
1160
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001161 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1162 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001163
1164 return SSL_TLSEXT_ERR_OK;
1165}
1166
1167/*
1168 * This function enables the handling of OCSP status extension on 'ctx' if a
1169 * file name 'cert_path' suffixed using ".ocsp" is present.
1170 * To enable OCSP status extension, the issuer's certificate is mandatory.
1171 * It should be present in the certificate's extra chain builded from file
1172 * 'cert_path'. If not found, the issuer certificate is loaded from a file
1173 * named 'cert_path' suffixed using '.issuer'.
1174 *
1175 * In addition, ".ocsp" file content is loaded as a DER format of an OCSP
1176 * response. If file is empty or content is not a valid OCSP response,
1177 * OCSP status extension is enabled but OCSP response is ignored (a warning
1178 * is displayed).
1179 *
1180 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001181 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001182 */
1183static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path)
1184{
1185
1186 BIO *in = NULL;
1187 X509 *x, *xi = NULL, *issuer = NULL;
1188 STACK_OF(X509) *chain = NULL;
1189 OCSP_CERTID *cid = NULL;
1190 SSL *ssl;
1191 char ocsp_path[MAXPATHLEN+1];
1192 int i, ret = -1;
1193 struct stat st;
1194 struct certificate_ocsp *ocsp = NULL, *iocsp;
1195 char *warn = NULL;
1196 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001197 pem_password_cb *passwd_cb;
1198 void *passwd_cb_userdata;
1199 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001200
1201 snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path);
1202
1203 if (stat(ocsp_path, &st))
1204 return 1;
1205
1206 ssl = SSL_new(ctx);
1207 if (!ssl)
1208 goto out;
1209
1210 x = SSL_get_certificate(ssl);
1211 if (!x)
1212 goto out;
1213
1214 /* Try to lookup for issuer in certificate extra chain */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001215 SSL_CTX_get_extra_chain_certs(ctx, &chain);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001216 for (i = 0; i < sk_X509_num(chain); i++) {
1217 issuer = sk_X509_value(chain, i);
1218 if (X509_check_issued(issuer, x) == X509_V_OK)
1219 break;
1220 else
1221 issuer = NULL;
1222 }
1223
1224 /* If not found try to load issuer from a suffixed file */
1225 if (!issuer) {
1226 char issuer_path[MAXPATHLEN+1];
1227
1228 in = BIO_new(BIO_s_file());
1229 if (!in)
1230 goto out;
1231
1232 snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path);
1233 if (BIO_read_filename(in, issuer_path) <= 0)
1234 goto out;
1235
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001236 passwd_cb = SSL_CTX_get_default_passwd_cb(ctx);
1237 passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx);
1238
1239 xi = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001240 if (!xi)
1241 goto out;
1242
1243 if (X509_check_issued(xi, x) != X509_V_OK)
1244 goto out;
1245
1246 issuer = xi;
1247 }
1248
1249 cid = OCSP_cert_to_id(0, x, issuer);
1250 if (!cid)
1251 goto out;
1252
1253 i = i2d_OCSP_CERTID(cid, NULL);
1254 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1255 goto out;
1256
Vincent Bernat02779b62016-04-03 13:48:43 +02001257 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001258 if (!ocsp)
1259 goto out;
1260
1261 p = ocsp->key_data;
1262 i2d_OCSP_CERTID(cid, &p);
1263
1264 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1265 if (iocsp == ocsp)
1266 ocsp = NULL;
1267
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001268#ifndef SSL_CTX_get_tlsext_status_cb
1269# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1270 *cb = (void (*) (void))ctx->tlsext_status_cb;
1271#endif
1272 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1273
1274 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001275 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001276 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001277
1278 cb_arg->is_single = 1;
1279 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001280
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001281 pkey = X509_get_pubkey(x);
1282 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1283 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001284
1285 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1286 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1287 } else {
1288 /*
1289 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1290 * Update that cb_arg with the new cert's staple
1291 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001292 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001293 struct certificate_ocsp *tmp_ocsp;
1294 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001295 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001296 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001297
1298#ifdef SSL_CTX_get_tlsext_status_arg
1299 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1300#else
1301 cb_arg = ctx->tlsext_status_arg;
1302#endif
yanbzhube2774d2015-12-10 15:07:30 -05001303
1304 /*
1305 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1306 * the order of operations below matter, take care when changing it
1307 */
1308 tmp_ocsp = cb_arg->s_ocsp;
1309 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1310 cb_arg->s_ocsp = NULL;
1311 cb_arg->m_ocsp[index] = tmp_ocsp;
1312 cb_arg->is_single = 0;
1313 cb_arg->single_kt = 0;
1314
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001315 pkey = X509_get_pubkey(x);
1316 key_type = EVP_PKEY_base_id(pkey);
1317 EVP_PKEY_free(pkey);
1318
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001319 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001320 if (index >= 0 && !cb_arg->m_ocsp[index])
1321 cb_arg->m_ocsp[index] = iocsp;
1322
1323 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001324
1325 ret = 0;
1326
1327 warn = NULL;
1328 if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) {
1329 memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001330 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001331 }
1332
1333out:
1334 if (ssl)
1335 SSL_free(ssl);
1336
1337 if (in)
1338 BIO_free(in);
1339
1340 if (xi)
1341 X509_free(xi);
1342
1343 if (cid)
1344 OCSP_CERTID_free(cid);
1345
1346 if (ocsp)
1347 free(ocsp);
1348
1349 if (warn)
1350 free(warn);
1351
1352
1353 return ret;
1354}
1355
1356#endif
1357
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001358#ifdef OPENSSL_IS_BORINGSSL
1359static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path)
1360{
1361 char ocsp_path[MAXPATHLEN+1];
1362 struct stat st;
1363 int fd = -1, r = 0;
1364
1365 snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path);
1366 if (stat(ocsp_path, &st))
1367 return 0;
1368
1369 fd = open(ocsp_path, O_RDONLY);
1370 if (fd == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001371 ha_warning("Error opening OCSP response file %s.\n", ocsp_path);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001372 return -1;
1373 }
1374
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001375 trash.data = 0;
1376 while (trash.data < trash.size) {
1377 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001378 if (r < 0) {
1379 if (errno == EINTR)
1380 continue;
Christopher Faulet767a84b2017-11-24 16:50:31 +01001381 ha_warning("Error reading OCSP response from file %s.\n", ocsp_path);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001382 close(fd);
1383 return -1;
1384 }
1385 else if (r == 0) {
1386 break;
1387 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001388 trash.data += r;
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001389 }
1390 close(fd);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001391 return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *) trash.area,
1392 trash.data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001393}
1394#endif
1395
Willy Tarreau5db847a2019-05-09 14:13:35 +02001396#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001397
1398#define CT_EXTENSION_TYPE 18
1399
1400static int sctl_ex_index = -1;
1401
1402/*
1403 * Try to parse Signed Certificate Timestamp List structure. This function
1404 * makes only basic test if the data seems like SCTL. No signature validation
1405 * is performed.
1406 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001407static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001408{
1409 int ret = 1;
1410 int len, pos, sct_len;
1411 unsigned char *data;
1412
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001413 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001414 goto out;
1415
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001416 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001417 len = (data[0] << 8) | data[1];
1418
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001419 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001420 goto out;
1421
1422 data = data + 2;
1423 pos = 0;
1424 while (pos < len) {
1425 if (len - pos < 2)
1426 goto out;
1427
1428 sct_len = (data[pos] << 8) | data[pos + 1];
1429 if (pos + sct_len + 2 > len)
1430 goto out;
1431
1432 pos += sct_len + 2;
1433 }
1434
1435 ret = 0;
1436
1437out:
1438 return ret;
1439}
1440
Willy Tarreau83061a82018-07-13 11:56:34 +02001441static int ssl_sock_load_sctl_from_file(const char *sctl_path,
1442 struct buffer **sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001443{
1444 int fd = -1;
1445 int r = 0;
1446 int ret = 1;
1447
1448 *sctl = NULL;
1449
1450 fd = open(sctl_path, O_RDONLY);
1451 if (fd == -1)
1452 goto end;
1453
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001454 trash.data = 0;
1455 while (trash.data < trash.size) {
1456 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001457 if (r < 0) {
1458 if (errno == EINTR)
1459 continue;
1460
1461 goto end;
1462 }
1463 else if (r == 0) {
1464 break;
1465 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001466 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001467 }
1468
1469 ret = ssl_sock_parse_sctl(&trash);
1470 if (ret)
1471 goto end;
1472
Vincent Bernat02779b62016-04-03 13:48:43 +02001473 *sctl = calloc(1, sizeof(**sctl));
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001474 if (!chunk_dup(*sctl, &trash)) {
1475 free(*sctl);
1476 *sctl = NULL;
1477 goto end;
1478 }
1479
1480end:
1481 if (fd != -1)
1482 close(fd);
1483
1484 return ret;
1485}
1486
1487int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1488{
Willy Tarreau83061a82018-07-13 11:56:34 +02001489 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001490
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001491 *out = (unsigned char *) sctl->area;
1492 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001493
1494 return 1;
1495}
1496
1497int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1498{
1499 return 1;
1500}
1501
1502static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path)
1503{
1504 char sctl_path[MAXPATHLEN+1];
1505 int ret = -1;
1506 struct stat st;
Willy Tarreau83061a82018-07-13 11:56:34 +02001507 struct buffer *sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001508
1509 snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path);
1510
1511 if (stat(sctl_path, &st))
1512 return 1;
1513
1514 if (ssl_sock_load_sctl_from_file(sctl_path, &sctl))
1515 goto out;
1516
1517 if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) {
1518 free(sctl);
1519 goto out;
1520 }
1521
1522 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1523
1524 ret = 0;
1525
1526out:
1527 return ret;
1528}
1529
1530#endif
1531
Emeric Brune1f38db2012-09-03 20:36:47 +02001532void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1533{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001534 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001535 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001536 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001537 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001538
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001539#ifndef SSL_OP_NO_RENEGOTIATION
1540 /* Please note that BoringSSL defines this macro to zero so don't
1541 * change this to #if and do not assign a default value to this macro!
1542 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001543 if (where & SSL_CB_HANDSHAKE_START) {
1544 /* Disable renegotiation (CVE-2009-3555) */
Olivier Houchard90084a12017-11-23 18:21:29 +01001545 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 +02001546 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001547 conn->err_code = CO_ER_SSL_RENEG;
1548 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001549 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001550#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001551
1552 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001553 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001554 /* Long certificate chains optimz
1555 If write and read bios are differents, we
1556 consider that the buffering was activated,
1557 so we rise the output buffer size from 4k
1558 to 16k */
1559 write_bio = SSL_get_wbio(ssl);
1560 if (write_bio != SSL_get_rbio(ssl)) {
1561 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001562 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001563 }
1564 }
1565 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001566}
1567
Emeric Brune64aef12012-09-21 13:15:06 +02001568/* Callback is called for each certificate of the chain during a verify
1569 ok is set to 1 if preverify detect no error on current certificate.
1570 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001571int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001572{
1573 SSL *ssl;
1574 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001575 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001576 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001577
1578 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001579 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001580
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001581 ctx = conn->xprt_ctx;
1582
1583 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001584
Emeric Brun81c00f02012-09-21 14:31:21 +02001585 if (ok) /* no errors */
1586 return ok;
1587
1588 depth = X509_STORE_CTX_get_error_depth(x_store);
1589 err = X509_STORE_CTX_get_error(x_store);
1590
1591 /* check if CA error needs to be ignored */
1592 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001593 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1594 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1595 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001596 }
1597
Willy Tarreaua9d299f2020-02-04 14:02:02 +01001598 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001599 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001600 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001601 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001602 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001603
Willy Tarreau20879a02012-12-03 16:32:10 +01001604 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001605 return 0;
1606 }
1607
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001608 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1609 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001610
Emeric Brun81c00f02012-09-21 14:31:21 +02001611 /* check if certificate error needs to be ignored */
Willy Tarreaua9d299f2020-02-04 14:02:02 +01001612 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001613 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001614 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001615 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001616 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001617
Willy Tarreau20879a02012-12-03 16:32:10 +01001618 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001619 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001620}
1621
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001622static inline
1623void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001624 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001625{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001626 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001627 unsigned char *msg;
1628 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001629 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001630
1631 /* This function is called for "from client" and "to server"
1632 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001633 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001634 */
1635
1636 /* "write_p" is set to 0 is the bytes are received messages,
1637 * otherwise it is set to 1.
1638 */
1639 if (write_p != 0)
1640 return;
1641
1642 /* content_type contains the type of message received or sent
1643 * according with the SSL/TLS protocol spec. This message is
1644 * encoded with one byte. The value 256 (two bytes) is used
1645 * for designing the SSL/TLS record layer. According with the
1646 * rfc6101, the expected message (other than 256) are:
1647 * - change_cipher_spec(20)
1648 * - alert(21)
1649 * - handshake(22)
1650 * - application_data(23)
1651 * - (255)
1652 * We are interessed by the handshake and specially the client
1653 * hello.
1654 */
1655 if (content_type != 22)
1656 return;
1657
1658 /* The message length is at least 4 bytes, containing the
1659 * message type and the message length.
1660 */
1661 if (len < 4)
1662 return;
1663
1664 /* First byte of the handshake message id the type of
1665 * message. The konwn types are:
1666 * - hello_request(0)
1667 * - client_hello(1)
1668 * - server_hello(2)
1669 * - certificate(11)
1670 * - server_key_exchange (12)
1671 * - certificate_request(13)
1672 * - server_hello_done(14)
1673 * We are interested by the client hello.
1674 */
1675 msg = (unsigned char *)buf;
1676 if (msg[0] != 1)
1677 return;
1678
1679 /* Next three bytes are the length of the message. The total length
1680 * must be this decoded length + 4. If the length given as argument
1681 * is not the same, we abort the protocol dissector.
1682 */
1683 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1684 if (len < rec_len + 4)
1685 return;
1686 msg += 4;
1687 end = msg + rec_len;
1688 if (end < msg)
1689 return;
1690
1691 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1692 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001693 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1694 */
1695 msg += 1 + 1 + 4 + 28;
1696 if (msg > end)
1697 return;
1698
1699 /* Next, is session id:
1700 * if present, we have to jump by length + 1 for the size information
1701 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001702 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001703 if (msg[0] > 0)
1704 msg += msg[0];
1705 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001706 if (msg > end)
1707 return;
1708
1709 /* Next two bytes are the ciphersuite length. */
1710 if (msg + 2 > end)
1711 return;
1712 rec_len = (msg[0] << 8) + msg[1];
1713 msg += 2;
1714 if (msg + rec_len > end || msg + rec_len < msg)
1715 return;
1716
Willy Tarreaubafbe012017-11-24 17:34:44 +01001717 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001718 if (!capture)
1719 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001720 /* Compute the xxh64 of the ciphersuite. */
1721 capture->xxh64 = XXH64(msg, rec_len, 0);
1722
1723 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001724 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1725 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001726 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001727
1728 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001729}
1730
Emeric Brun29f037d2014-04-25 19:05:36 +02001731/* Callback is called for ssl protocol analyse */
1732void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1733{
Emeric Brun29f037d2014-04-25 19:05:36 +02001734#ifdef TLS1_RT_HEARTBEAT
1735 /* test heartbeat received (write_p is set to 0
1736 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001737 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001738 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001739 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001740 const unsigned char *p = buf;
1741 unsigned int payload;
1742
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001743 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001744
1745 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1746 if (*p != TLS1_HB_REQUEST)
1747 return;
1748
Willy Tarreauaeed6722014-04-25 23:59:58 +02001749 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001750 goto kill_it;
1751
1752 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001753 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001754 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001755 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001756 /* We have a clear heartbleed attack (CVE-2014-0160), the
1757 * advertised payload is larger than the advertised packet
1758 * length, so we have garbage in the buffer between the
1759 * payload and the end of the buffer (p+len). We can't know
1760 * if the SSL stack is patched, and we don't know if we can
1761 * safely wipe out the area between p+3+len and payload.
1762 * So instead, we prevent the response from being sent by
1763 * setting the max_send_fragment to 0 and we report an SSL
1764 * error, which will kill this connection. It will be reported
1765 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001766 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1767 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001768 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001769 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1770 return;
1771 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001772#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001773 if (global_ssl.capture_cipherlist > 0)
1774 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001775}
1776
Bernard Spil13c53f82018-02-15 13:34:58 +01001777#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001778static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1779 const unsigned char *in, unsigned int inlen,
1780 void *arg)
1781{
1782 struct server *srv = arg;
1783
1784 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1785 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1786 return SSL_TLSEXT_ERR_OK;
1787 return SSL_TLSEXT_ERR_NOACK;
1788}
1789#endif
1790
1791#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001792/* This callback is used so that the server advertises the list of
1793 * negociable protocols for NPN.
1794 */
1795static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1796 unsigned int *len, void *arg)
1797{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001798 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001799
1800 *data = (const unsigned char *)conf->npn_str;
1801 *len = conf->npn_len;
1802 return SSL_TLSEXT_ERR_OK;
1803}
1804#endif
1805
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001806#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001807/* This callback is used so that the server advertises the list of
1808 * negociable protocols for ALPN.
1809 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001810static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1811 unsigned char *outlen,
1812 const unsigned char *server,
1813 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001814{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001815 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001816
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001817 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1818 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1819 return SSL_TLSEXT_ERR_NOACK;
1820 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001821 return SSL_TLSEXT_ERR_OK;
1822}
1823#endif
1824
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001825#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001826#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001827
Christopher Faulet30548802015-06-11 13:39:32 +02001828/* Create a X509 certificate with the specified servername and serial. This
1829 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001830static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001831ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001832{
Christopher Faulet7969a332015-10-09 11:15:03 +02001833 X509 *cacert = bind_conf->ca_sign_cert;
1834 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001835 SSL_CTX *ssl_ctx = NULL;
1836 X509 *newcrt = NULL;
1837 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001838 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001839 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001840 X509_NAME *name;
1841 const EVP_MD *digest;
1842 X509V3_CTX ctx;
1843 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001844 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001845
Christopher Faulet48a83322017-07-28 16:56:09 +02001846 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001847#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001848 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1849#else
1850 tmp_ssl = SSL_new(bind_conf->default_ctx);
1851 if (tmp_ssl)
1852 pkey = SSL_get_privatekey(tmp_ssl);
1853#endif
1854 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001855 goto mkcert_error;
1856
1857 /* Create the certificate */
1858 if (!(newcrt = X509_new()))
1859 goto mkcert_error;
1860
1861 /* Set version number for the certificate (X509v3) and the serial
1862 * number */
1863 if (X509_set_version(newcrt, 2L) != 1)
1864 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001865 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001866
1867 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001868 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1869 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001870 goto mkcert_error;
1871
1872 /* set public key in the certificate */
1873 if (X509_set_pubkey(newcrt, pkey) != 1)
1874 goto mkcert_error;
1875
1876 /* Set issuer name from the CA */
1877 if (!(name = X509_get_subject_name(cacert)))
1878 goto mkcert_error;
1879 if (X509_set_issuer_name(newcrt, name) != 1)
1880 goto mkcert_error;
1881
1882 /* Set the subject name using the same, but the CN */
1883 name = X509_NAME_dup(name);
1884 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
1885 (const unsigned char *)servername,
1886 -1, -1, 0) != 1) {
1887 X509_NAME_free(name);
1888 goto mkcert_error;
1889 }
1890 if (X509_set_subject_name(newcrt, name) != 1) {
1891 X509_NAME_free(name);
1892 goto mkcert_error;
1893 }
1894 X509_NAME_free(name);
1895
1896 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001897 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001898 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
1899 for (i = 0; i < X509V3_EXT_SIZE; i++) {
1900 X509_EXTENSION *ext;
1901
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001902 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001903 goto mkcert_error;
1904 if (!X509_add_ext(newcrt, ext, -1)) {
1905 X509_EXTENSION_free(ext);
1906 goto mkcert_error;
1907 }
1908 X509_EXTENSION_free(ext);
1909 }
1910
1911 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001912
1913 key_type = EVP_PKEY_base_id(capkey);
1914
1915 if (key_type == EVP_PKEY_DSA)
1916 digest = EVP_sha1();
1917 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001918 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001919 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02001920 digest = EVP_sha256();
1921 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02001922#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02001923 int nid;
1924
1925 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
1926 goto mkcert_error;
1927 if (!(digest = EVP_get_digestbynid(nid)))
1928 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02001929#else
1930 goto mkcert_error;
1931#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02001932 }
1933
Christopher Faulet31af49d2015-06-09 17:29:50 +02001934 if (!(X509_sign(newcrt, capkey, digest)))
1935 goto mkcert_error;
1936
1937 /* Create and set the new SSL_CTX */
1938 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
1939 goto mkcert_error;
1940 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1941 goto mkcert_error;
1942 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
1943 goto mkcert_error;
1944 if (!SSL_CTX_check_private_key(ssl_ctx))
1945 goto mkcert_error;
1946
1947 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02001948
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001949#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001950 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001951#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001952#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
1953 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001954 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001955 EC_KEY *ecc;
1956 int nid;
1957
1958 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
1959 goto end;
1960 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
1961 goto end;
1962 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
1963 EC_KEY_free(ecc);
1964 }
1965#endif
1966 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02001967 return ssl_ctx;
1968
1969 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001970 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001971 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001972 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
1973 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001974 return NULL;
1975}
1976
Christopher Faulet7969a332015-10-09 11:15:03 +02001977SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001978ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02001979{
Willy Tarreau07d94e42018-09-20 10:57:52 +02001980 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01001981 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001982
Olivier Houchard66ab4982019-02-26 18:37:15 +01001983 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02001984}
1985
Christopher Faulet30548802015-06-11 13:39:32 +02001986/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02001987 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02001988SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02001989ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02001990{
1991 struct lru64 *lru = NULL;
1992
1993 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001994 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001995 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02001996 if (lru && lru->domain) {
1997 if (ssl)
1998 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001999 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002000 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002001 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002002 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002003 }
2004 return NULL;
2005}
2006
Emeric Brun821bb9b2017-06-15 16:37:39 +02002007/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2008 * function is not thread-safe, it should only be used to check if a certificate
2009 * exists in the lru cache (with no warranty it will not be removed by another
2010 * thread). It is kept for backward compatibility. */
2011SSL_CTX *
2012ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2013{
2014 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2015}
2016
Christopher Fauletd2cab922015-07-28 16:03:47 +02002017/* Set a certificate int the LRU cache used to store generated
2018 * certificate. Return 0 on success, otherwise -1 */
2019int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002020ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002021{
2022 struct lru64 *lru = NULL;
2023
2024 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002025 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002026 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002027 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002028 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002029 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002030 }
Christopher Faulet30548802015-06-11 13:39:32 +02002031 if (lru->domain && lru->data)
2032 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002033 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002034 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002035 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002036 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002037 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002038}
2039
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002040/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002041unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002042ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002043{
2044 return XXH32(data, len, ssl_ctx_lru_seed);
2045}
2046
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002047/* Generate a cert and immediately assign it to the SSL session so that the cert's
2048 * refcount is maintained regardless of the cert's presence in the LRU cache.
2049 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002050static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002051ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002052{
2053 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002054 SSL_CTX *ssl_ctx = NULL;
2055 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002056 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002057
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002058 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002059 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002060 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002061 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002062 if (lru && lru->domain)
2063 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002064 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002065 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002066 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002067 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002068 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002069 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002070 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002071 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002072 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002073 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002074 SSL_set_SSL_CTX(ssl, ssl_ctx);
2075 /* No LRU cache, this CTX will be released as soon as the session dies */
2076 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002077 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002078 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002079 return 0;
2080}
2081static int
2082ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2083{
2084 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002085 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002086
2087 conn_get_to_addr(conn);
2088 if (conn->flags & CO_FL_ADDR_TO_SET) {
2089 key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002090 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002091 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002092 }
2093 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002094}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002095#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002096
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002097#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002098typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2099
2100static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002101{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002102#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002103 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002104 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2105#endif
2106}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002107static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2108 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002109 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2110}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002111static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002112#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002113 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002114 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2115#endif
2116}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002117static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002118#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002119 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002120 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2121#endif
2122}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002123/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002124static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2125/* Unusable in this context. */
2126static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2127static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2128static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2129static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2130static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002131#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002132typedef enum { SET_MIN, SET_MAX } set_context_func;
2133
2134static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2135 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002136 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2137}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002138static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2139 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2140 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2141}
2142static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2143 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002144 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2145}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002146static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2147 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2148 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2149}
2150static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2151 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002152 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2153}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002154static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2155 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2156 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2157}
2158static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2159 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002160 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2161}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002162static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2163 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2164 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2165}
2166static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002167#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002168 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002169 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2170#endif
2171}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002172static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2173#if SSL_OP_NO_TLSv1_3
2174 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2175 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002176#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002177}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002178#endif
2179static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2180static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002181
2182static struct {
2183 int option;
2184 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002185 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2186 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002187 const char *name;
2188} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002189 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2190 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2191 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2192 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2193 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2194 {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 +02002195};
2196
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002197static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2198{
2199 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2200 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2201 SSL_set_SSL_CTX(ssl, ctx);
2202}
2203
Willy Tarreau5db847a2019-05-09 14:13:35 +02002204#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002205
2206static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2207{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002208 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002209 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002210
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002211 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2212 return SSL_TLSEXT_ERR_OK;
2213 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002214}
2215
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002216#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002217static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2218{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002219 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002220#else
2221static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2222{
2223#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002224 struct connection *conn;
2225 struct bind_conf *s;
2226 const uint8_t *extension_data;
2227 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002228 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002229
2230 char *wildp = NULL;
2231 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002232 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002233 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002234 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002235 int i;
2236
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002237 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002238 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002239
Olivier Houchard9679ac92017-10-27 14:58:08 +02002240 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002241 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002242#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002243 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2244 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002245#else
2246 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2247#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002248 /*
2249 * The server_name extension was given too much extensibility when it
2250 * was written, so parsing the normal case is a bit complex.
2251 */
2252 size_t len;
2253 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002254 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002255 /* Extract the length of the supplied list of names. */
2256 len = (*extension_data++) << 8;
2257 len |= *extension_data++;
2258 if (len + 2 != extension_len)
2259 goto abort;
2260 /*
2261 * The list in practice only has a single element, so we only consider
2262 * the first one.
2263 */
2264 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2265 goto abort;
2266 extension_len = len - 1;
2267 /* Now we can finally pull out the byte array with the actual hostname. */
2268 if (extension_len <= 2)
2269 goto abort;
2270 len = (*extension_data++) << 8;
2271 len |= *extension_data++;
2272 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2273 || memchr(extension_data, 0, len) != NULL)
2274 goto abort;
2275 servername = extension_data;
2276 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002277 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002278#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2279 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002280 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002281 }
2282#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002283 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002284 if (!s->strict_sni) {
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002285 ssl_sock_switchctx_set(ssl, s->default_ctx);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002286 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002287 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002288 goto abort;
2289 }
2290
2291 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002292#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002293 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002294#else
2295 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2296#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002297 uint8_t sign;
2298 size_t len;
2299 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002300 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002301 len = (*extension_data++) << 8;
2302 len |= *extension_data++;
2303 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002304 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002305 if (len % 2 != 0)
2306 goto abort;
2307 for (; len > 0; len -= 2) {
2308 extension_data++; /* hash */
2309 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002310 switch (sign) {
2311 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002312 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002313 break;
2314 case TLSEXT_signature_ecdsa:
2315 has_ecdsa_sig = 1;
2316 break;
2317 default:
2318 continue;
2319 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002320 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002321 break;
2322 }
2323 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002324 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002325 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002326 }
2327 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002328 const SSL_CIPHER *cipher;
2329 size_t len;
2330 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002331 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002332#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002333 len = ctx->cipher_suites_len;
2334 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002335#else
2336 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2337#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002338 if (len % 2 != 0)
2339 goto abort;
2340 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002341#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002342 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002343 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002344#else
2345 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2346#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002347 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002348 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002349 break;
2350 }
2351 }
2352 }
2353
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002354 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002355 trash.area[i] = tolower(servername[i]);
2356 if (!wildp && (trash.area[i] == '.'))
2357 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002358 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002359 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002360
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002361
Emmanuel Hocdetcc957c32019-11-06 16:05:34 +01002362 for (i = 0; i < 2; i++) {
2363 if (i == 0) /* lookup in full qualified names */
2364 node = ebst_lookup(&s->sni_ctx, trash.area);
2365 else if (i == 1 && wildp) /* lookup in wildcards names */
2366 node = ebst_lookup(&s->sni_w_ctx, wildp);
2367 else
2368 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002369 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdetcc957c32019-11-06 16:05:34 +01002370 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002371 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002372 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002373 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002374 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002375 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002376 break;
2377 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002378 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002379 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002380 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002381 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002382 if (!node_anonymous)
2383 node_anonymous = n;
2384 break;
2385 }
2386 }
2387 }
Emmanuel Hocdetcc957c32019-11-06 16:05:34 +01002388 /* select by key_signature priority order */
2389 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2390 : ((has_rsa_sig && node_rsa) ? node_rsa
2391 : (node_anonymous ? node_anonymous
2392 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2393 : node_rsa /* no rsa signature case (far far away) */
2394 )));
2395 if (node) {
2396 /* switch ctx */
2397 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2398 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002399 if (conf) {
2400 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2401 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2402 if (conf->early_data)
2403 allow_early = 1;
2404 }
2405 goto allow_early;
Emmanuel Hocdetcc957c32019-11-06 16:05:34 +01002406 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002407 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002408#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002409 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002410 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002411 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002412 }
2413#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002414 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002415 /* no certificate match, is the default_ctx */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002416 ssl_sock_switchctx_set(ssl, s->default_ctx);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002417 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002418allow_early:
2419#ifdef OPENSSL_IS_BORINGSSL
2420 if (allow_early)
2421 SSL_set_early_data_enabled(ssl, 1);
2422#else
2423 if (!allow_early)
2424 SSL_set_max_early_data(ssl, 0);
2425#endif
2426 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002427 abort:
2428 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2429 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002430#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002431 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002432#else
2433 *al = SSL_AD_UNRECOGNIZED_NAME;
2434 return 0;
2435#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002436}
2437
2438#else /* OPENSSL_IS_BORINGSSL */
2439
Emeric Brunfc0421f2012-09-07 17:30:07 +02002440/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2441 * warning when no match is found, which implies the default (first) cert
2442 * will keep being used.
2443 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002444static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002445{
2446 const char *servername;
2447 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002448 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002449 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002450 int i;
2451 (void)al; /* shut gcc stupid warning */
2452
2453 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002454 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002455#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002456 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2457 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002458#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002459 if (s->strict_sni)
2460 return SSL_TLSEXT_ERR_ALERT_FATAL;
2461 ssl_sock_switchctx_set(ssl, s->default_ctx);
2462 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002463 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002464
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002465 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002466 if (!servername[i])
2467 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002468 trash.area[i] = tolower(servername[i]);
2469 if (!wildp && (trash.area[i] == '.'))
2470 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002471 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002472 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002473
Emmanuel Hocdet26b7b802019-11-04 15:49:46 +01002474 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002475 /* lookup in full qualified names */
Emmanuel Hocdet26b7b802019-11-04 15:49:46 +01002476 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2477 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002478 if (!container_of(n, struct sni_ctx, name)->neg) {
2479 node = n;
2480 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002481 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002482 }
2483 if (!node && wildp) {
2484 /* lookup in wildcards names */
Emmanuel Hocdet26b7b802019-11-04 15:49:46 +01002485 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2486 /* lookup a not neg filter */
2487 if (!container_of(n, struct sni_ctx, name)->neg) {
2488 node = n;
2489 break;
2490 }
2491 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002492 }
Emmanuel Hocdet26b7b802019-11-04 15:49:46 +01002493 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002494#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002495 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2496 /* switch ctx done in ssl_sock_generate_certificate */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002497 return SSL_TLSEXT_ERR_OK;
2498 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002499#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002500 if (s->strict_sni)
2501 return SSL_TLSEXT_ERR_ALERT_FATAL;
2502 ssl_sock_switchctx_set(ssl, s->default_ctx);
2503 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002504 }
2505
2506 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002507 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002508 return SSL_TLSEXT_ERR_OK;
2509}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002510#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002511#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2512
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002513#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002514
2515static DH * ssl_get_dh_1024(void)
2516{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002517 static unsigned char dh1024_p[]={
2518 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2519 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2520 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2521 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2522 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2523 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2524 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2525 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2526 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2527 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2528 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2529 };
2530 static unsigned char dh1024_g[]={
2531 0x02,
2532 };
2533
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002534 BIGNUM *p;
2535 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002536 DH *dh = DH_new();
2537 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002538 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2539 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002540
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002541 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002542 DH_free(dh);
2543 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002544 } else {
2545 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002546 }
2547 }
2548 return dh;
2549}
2550
2551static DH *ssl_get_dh_2048(void)
2552{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002553 static unsigned char dh2048_p[]={
2554 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2555 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2556 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2557 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2558 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2559 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2560 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2561 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2562 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2563 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2564 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2565 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2566 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2567 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2568 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2569 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2570 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2571 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2572 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2573 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2574 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2575 0xB7,0x1F,0x77,0xF3,
2576 };
2577 static unsigned char dh2048_g[]={
2578 0x02,
2579 };
2580
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002581 BIGNUM *p;
2582 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002583 DH *dh = DH_new();
2584 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002585 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2586 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002587
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002588 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002589 DH_free(dh);
2590 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002591 } else {
2592 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002593 }
2594 }
2595 return dh;
2596}
2597
2598static DH *ssl_get_dh_4096(void)
2599{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002600 static unsigned char dh4096_p[]={
2601 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2602 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2603 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2604 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2605 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2606 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2607 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2608 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2609 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2610 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2611 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2612 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2613 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2614 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2615 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2616 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2617 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2618 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2619 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2620 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2621 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2622 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2623 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2624 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2625 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2626 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2627 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2628 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2629 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2630 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2631 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2632 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2633 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2634 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2635 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2636 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2637 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2638 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2639 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2640 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2641 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2642 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2643 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002644 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002645 static unsigned char dh4096_g[]={
2646 0x02,
2647 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002648
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002649 BIGNUM *p;
2650 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002651 DH *dh = DH_new();
2652 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002653 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2654 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002655
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002656 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002657 DH_free(dh);
2658 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002659 } else {
2660 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002661 }
2662 }
2663 return dh;
2664}
2665
2666/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002667 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002668static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2669{
2670 DH *dh = NULL;
2671 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002672 int type;
2673
2674 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002675
2676 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2677 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2678 */
2679 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2680 keylen = EVP_PKEY_bits(pkey);
2681 }
2682
Willy Tarreauef934602016-12-22 23:12:01 +01002683 if (keylen > global_ssl.default_dh_param) {
2684 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002685 }
2686
Remi Gacogned3a341a2015-05-29 16:26:17 +02002687 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002688 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002689 }
2690 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002691 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002692 }
2693 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002694 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002695 }
2696
2697 return dh;
2698}
2699
Remi Gacogne47783ef2015-05-29 15:53:22 +02002700static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002701{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002702 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002703 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002704
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002705 if (in == NULL)
2706 goto end;
2707
Remi Gacogne47783ef2015-05-29 15:53:22 +02002708 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002709 goto end;
2710
Remi Gacogne47783ef2015-05-29 15:53:22 +02002711 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2712
2713end:
2714 if (in)
2715 BIO_free(in);
2716
Emeric Brune1b4ed42018-08-16 15:14:12 +02002717 ERR_clear_error();
2718
Remi Gacogne47783ef2015-05-29 15:53:22 +02002719 return dh;
2720}
2721
2722int ssl_sock_load_global_dh_param_from_file(const char *filename)
2723{
2724 global_dh = ssl_sock_get_dh_from_file(filename);
2725
2726 if (global_dh) {
2727 return 0;
2728 }
2729
2730 return -1;
2731}
2732
Emeric Bruncfc1afe2019-10-17 13:27:40 +02002733/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
2734 * If there is no DH paramater availaible in the ckchs, the global
2735 * DH parameter is loaded into the SSL_CTX and if there is no
2736 * DH parameter available in ckchs nor in global, the default
2737 * DH parameters are applied on the SSL_CTX.
2738 * Returns a bitfield containing the flags:
2739 * ERR_FATAL in any fatal error case
2740 * ERR_ALERT if a reason of the error is availabine in err
2741 * ERR_WARN if a warning is available into err
2742 * The value 0 means there is no error nor warning and
2743 * the operation succeed.
2744 */
2745static int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file, char **err)
2746 {
2747 int ret = 0;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002748 DH *dh = ssl_sock_get_dh_from_file(file);
2749
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002750 if (dh) {
Emeric Brund6de1512019-10-17 14:53:03 +02002751 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
2752 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
2753 err && *err ? *err : "", file);
2754#if defined(SSL_CTX_set_dh_auto)
2755 SSL_CTX_set_dh_auto(ctx, 1);
2756 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2757 err && *err ? *err : "");
2758#else
2759 memprintf(err, "%s, DH ciphers won't be available.\n",
2760 err && *err ? *err : "");
2761#endif
2762 ret |= ERR_WARN;
2763 goto end;
2764 }
Remi Gacogne4f902b82015-05-28 16:23:00 +02002765
2766 if (ssl_dh_ptr_index >= 0) {
2767 /* store a pointer to the DH params to avoid complaining about
2768 ssl-default-dh-param not being set for this SSL_CTX */
2769 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
2770 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002771 }
Remi Gacogne47783ef2015-05-29 15:53:22 +02002772 else if (global_dh) {
Emeric Brund6de1512019-10-17 14:53:03 +02002773 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
2774 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
2775 err && *err ? *err : "", file);
2776#if defined(SSL_CTX_set_dh_auto)
2777 SSL_CTX_set_dh_auto(ctx, 1);
2778 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2779 err && *err ? *err : "");
2780#else
2781 memprintf(err, "%s, DH ciphers won't be available.\n",
2782 err && *err ? *err : "");
2783#endif
2784 ret |= ERR_WARN;
2785 goto end;
2786 }
Remi Gacogne47783ef2015-05-29 15:53:22 +02002787 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002788 else {
Emeric Brun41fdb3c2013-04-26 11:05:44 +02002789 /* Clear openssl global errors stack */
2790 ERR_clear_error();
2791
Willy Tarreauef934602016-12-22 23:12:01 +01002792 if (global_ssl.default_dh_param <= 1024) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002793 /* we are limited to DH parameter of 1024 bits anyway */
Remi Gacognec7e12632016-07-02 16:26:10 +02002794 if (local_dh_1024 == NULL)
2795 local_dh_1024 = ssl_get_dh_1024();
2796
Emeric Bruncfc1afe2019-10-17 13:27:40 +02002797 if (local_dh_1024 == NULL) {
2798 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
2799 err && *err ? *err : "", file);
2800 ret |= ERR_ALERT | ERR_FATAL;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002801 goto end;
Emeric Bruncfc1afe2019-10-17 13:27:40 +02002802 }
Willy Tarreau6e774b42014-04-25 21:35:23 +02002803
Emeric Brund6de1512019-10-17 14:53:03 +02002804 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
2805 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
2806 err && *err ? *err : "", file);
2807#if defined(SSL_CTX_set_dh_auto)
2808 SSL_CTX_set_dh_auto(ctx, 1);
2809 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2810 err && *err ? *err : "");
2811#else
2812 memprintf(err, "%s, DH ciphers won't be available.\n",
2813 err && *err ? *err : "");
2814#endif
2815 ret |= ERR_WARN;
2816 goto end;
2817 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002818 }
2819 else {
2820 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
2821 }
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002822 }
Emeric Brun644cde02012-12-14 11:21:13 +01002823
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002824end:
2825 if (dh)
2826 DH_free(dh);
2827
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002828 return ret;
2829}
2830#endif
2831
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002832static 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 +02002833 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002834{
2835 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002836 int wild = 0, neg = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002837 struct ebmb_node *node;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002838
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002839 if (*name == '!') {
2840 neg = 1;
2841 name++;
2842 }
2843 if (*name == '*') {
2844 wild = 1;
2845 name++;
2846 }
2847 /* !* filter is a nop */
2848 if (neg && wild)
2849 return order;
2850 if (*name) {
2851 int j, len;
2852 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002853 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002854 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002855 if (j >= trash.size)
William Lallemand24e292c2019-10-03 23:46:33 +02002856 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002857 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002858
2859 /* Check for duplicates. */
2860 if (wild)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002861 node = ebst_lookup(&s->sni_w_ctx, trash.area);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002862 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002863 node = ebst_lookup(&s->sni_ctx, trash.area);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002864 for (; node; node = ebmb_next_dup(node)) {
2865 sc = ebmb_entry(node, struct sni_ctx, name);
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002866 if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg)
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002867 return order;
2868 }
2869
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002870 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002871 if (!sc)
William Lallemand24e292c2019-10-03 23:46:33 +02002872 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002873 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002874 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002875 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002876 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002877 sc->order = order++;
2878 sc->neg = neg;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01002879 if (kinfo.sig != TLSEXT_signature_anonymous)
2880 SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002881 if (wild)
2882 ebst_insert(&s->sni_w_ctx, &sc->name);
2883 else
2884 ebst_insert(&s->sni_ctx, &sc->name);
2885 }
2886 return order;
2887}
2888
yanbzhu488a4d22015-12-01 15:16:07 -05002889
2890/* The following code is used for loading multiple crt files into
2891 * SSL_CTX's based on CN/SAN
2892 */
Willy Tarreau5db847a2019-05-09 14:13:35 +02002893#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05002894/* This is used to preload the certifcate, private key
2895 * and Cert Chain of a file passed in via the crt
2896 * argument
2897 *
2898 * This way, we do not have to read the file multiple times
2899 */
2900struct cert_key_and_chain {
2901 X509 *cert;
2902 EVP_PKEY *key;
2903 unsigned int num_chain_certs;
2904 /* This is an array of X509 pointers */
2905 X509 **chain_certs;
2906};
2907
yanbzhu08ce6ab2015-12-02 13:01:29 -05002908#define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES))
2909
2910struct key_combo_ctx {
2911 SSL_CTX *ctx;
2912 int order;
2913};
2914
2915/* Map used for processing multiple keypairs for a single purpose
2916 *
2917 * This maps CN/SNI name to certificate type
2918 */
2919struct sni_keytype {
2920 int keytypes; /* BITMASK for keytypes */
2921 struct ebmb_node name; /* node holding the servername value */
2922};
2923
2924
yanbzhu488a4d22015-12-01 15:16:07 -05002925/* Frees the contents of a cert_key_and_chain
2926 */
2927static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
2928{
2929 int i;
2930
2931 if (!ckch)
2932 return;
2933
2934 /* Free the certificate and set pointer to NULL */
2935 if (ckch->cert)
2936 X509_free(ckch->cert);
2937 ckch->cert = NULL;
2938
2939 /* Free the key and set pointer to NULL */
2940 if (ckch->key)
2941 EVP_PKEY_free(ckch->key);
2942 ckch->key = NULL;
2943
2944 /* Free each certificate in the chain */
2945 for (i = 0; i < ckch->num_chain_certs; i++) {
2946 if (ckch->chain_certs[i])
2947 X509_free(ckch->chain_certs[i]);
2948 }
2949
2950 /* Free the chain obj itself and set to NULL */
2951 if (ckch->num_chain_certs > 0) {
2952 free(ckch->chain_certs);
2953 ckch->num_chain_certs = 0;
2954 ckch->chain_certs = NULL;
2955 }
2956
2957}
2958
2959/* checks if a key and cert exists in the ckch
2960 */
2961static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
2962{
2963 return (ckch->cert != NULL && ckch->key != NULL);
2964}
2965
2966
2967/* Loads the contents of a crt file (path) into a cert_key_and_chain
2968 * This allows us to carry the contents of the file without having to
2969 * read the file multiple times.
2970 *
2971 * returns:
2972 * 0 on Success
2973 * 1 on SSL Failure
2974 * 2 on file not found
2975 */
2976static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
2977{
2978
2979 BIO *in;
2980 X509 *ca = NULL;
2981 int ret = 1;
2982
2983 ssl_sock_free_cert_key_and_chain_contents(ckch);
2984
2985 in = BIO_new(BIO_s_file());
2986 if (in == NULL)
2987 goto end;
2988
2989 if (BIO_read_filename(in, path) <= 0)
2990 goto end;
2991
yanbzhu488a4d22015-12-01 15:16:07 -05002992 /* Read Private Key */
2993 ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
2994 if (ckch->key == NULL) {
2995 memprintf(err, "%sunable to load private key from file '%s'.\n",
2996 err && *err ? *err : "", path);
2997 goto end;
2998 }
2999
Willy Tarreaubb137a82016-04-06 19:02:38 +02003000 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003001 if (BIO_reset(in) == -1) {
3002 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3003 err && *err ? *err : "", path);
3004 goto end;
3005 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003006
3007 /* Read Certificate */
3008 ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3009 if (ckch->cert == NULL) {
3010 memprintf(err, "%sunable to load certificate from file '%s'.\n",
3011 err && *err ? *err : "", path);
3012 goto end;
3013 }
3014
yanbzhu488a4d22015-12-01 15:16:07 -05003015 /* Read Certificate Chain */
3016 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3017 /* Grow the chain certs */
3018 ckch->num_chain_certs++;
3019 ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *)));
3020
3021 /* use - 1 here since we just incremented it above */
3022 ckch->chain_certs[ckch->num_chain_certs - 1] = ca;
3023 }
3024 ret = ERR_get_error();
3025 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3026 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
3027 err && *err ? *err : "", path);
3028 ret = 1;
3029 goto end;
3030 }
3031
3032 ret = 0;
3033
3034end:
3035
3036 ERR_clear_error();
3037 if (in)
3038 BIO_free(in);
3039
3040 /* Something went wrong in one of the reads */
3041 if (ret != 0)
3042 ssl_sock_free_cert_key_and_chain_contents(ckch);
3043
3044 return ret;
3045}
3046
3047/* Loads the info in ckch into ctx
Emeric Brun394701d2019-10-17 13:25:14 +02003048 * Returns a bitfield containing the flags:
3049 * ERR_FATAL in any fatal error case
3050 * ERR_ALERT if the reason of the error is available in err
3051 * ERR_WARN if a warning is available into err
3052 * The value 0 means there is no error nor warning and
3053 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003054 */
3055static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3056{
3057 int i = 0;
Emeric Brun394701d2019-10-17 13:25:14 +02003058 int errcode = 0;
yanbzhu488a4d22015-12-01 15:16:07 -05003059
3060 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3061 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3062 err && *err ? *err : "", path);
Emeric Brun394701d2019-10-17 13:25:14 +02003063 errcode |= ERR_ALERT | ERR_FATAL;
3064 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003065 }
3066
3067 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3068 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3069 err && *err ? *err : "", path);
Emeric Brun394701d2019-10-17 13:25:14 +02003070 errcode |= ERR_ALERT | ERR_FATAL;
3071 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003072 }
3073
yanbzhu488a4d22015-12-01 15:16:07 -05003074 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
3075 for (i = 0; i < ckch->num_chain_certs; i++) {
3076 if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05003077 memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3078 err && *err ? *err : "", (i+1), path);
Emeric Brun394701d2019-10-17 13:25:14 +02003079 errcode |= ERR_ALERT | ERR_FATAL;
3080 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003081 }
3082 }
3083
3084 if (SSL_CTX_check_private_key(ctx) <= 0) {
3085 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
3086 err && *err ? *err : "", path);
Emeric Brun394701d2019-10-17 13:25:14 +02003087 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu488a4d22015-12-01 15:16:07 -05003088 }
3089
Emeric Brun394701d2019-10-17 13:25:14 +02003090 end:
3091 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003092}
3093
yanbzhu08ce6ab2015-12-02 13:01:29 -05003094
William Lallemand4801c702019-10-04 17:36:55 +02003095static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003096{
3097 struct sni_keytype *s_kt = NULL;
3098 struct ebmb_node *node;
3099 int i;
3100
3101 for (i = 0; i < trash.size; i++) {
3102 if (!str[i])
3103 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003104 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003105 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003106 trash.area[i] = 0;
3107 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003108 if (!node) {
3109 /* CN not found in tree */
3110 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3111 /* Using memcpy here instead of strncpy.
3112 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3113 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3114 */
William Lallemand4801c702019-10-04 17:36:55 +02003115 if (!s_kt)
3116 return -1;
3117
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003118 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003119 s_kt->keytypes = 0;
3120 ebst_insert(sni_keytypes, &s_kt->name);
3121 } else {
3122 /* CN found in tree */
3123 s_kt = container_of(node, struct sni_keytype, name);
3124 }
3125
3126 /* Mark that this CN has the keytype of key_index via keytypes mask */
3127 s_kt->keytypes |= 1<<key_index;
3128
William Lallemand4801c702019-10-04 17:36:55 +02003129 return 0;
3130
yanbzhu08ce6ab2015-12-02 13:01:29 -05003131}
3132
3133
3134/* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files.
3135 * If any are found, group these files into a set of SSL_CTX*
3136 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3137 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003138 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003139 *
Willy Tarreaub131c872019-10-16 16:42:19 +02003140 * Returns a set of ERR_* flags possibly with an error in <err>.
3141 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003142 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003143static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3144 char **sni_filter, int fcount, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003145{
3146 char fp[MAXPATHLEN+1] = {0};
3147 int n = 0;
3148 int i = 0;
3149 struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} };
3150 struct eb_root sni_keytypes_map = { {0} };
3151 struct ebmb_node *node;
3152 struct ebmb_node *next;
3153 /* Array of SSL_CTX pointers corresponding to each possible combo
3154 * of keytypes
3155 */
3156 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Willy Tarreaub131c872019-10-16 16:42:19 +02003157 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003158 X509_NAME *xname = NULL;
3159 char *str = NULL;
3160#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3161 STACK_OF(GENERAL_NAME) *names = NULL;
3162#endif
3163
3164 /* Load all possible certs and keys */
3165 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3166 struct stat buf;
3167
3168 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3169 if (stat(fp, &buf) == 0) {
3170 if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) {
Willy Tarreaub131c872019-10-16 16:42:19 +02003171 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003172 goto end;
3173 }
3174 }
3175 }
3176
3177 /* Process each ckch and update keytypes for each CN/SAN
3178 * for example, if CN/SAN www.a.com is associated with
3179 * certs with keytype 0 and 2, then at the end of the loop,
3180 * www.a.com will have:
3181 * keyindex = 0 | 1 | 4 = 5
3182 */
3183 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand4801c702019-10-04 17:36:55 +02003184 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003185
3186 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3187 continue;
3188
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003189 if (fcount) {
William Lallemand4801c702019-10-04 17:36:55 +02003190 for (i = 0; i < fcount; i++) {
3191 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3192 if (ret < 0) {
3193 memprintf(err, "%sunable to allocate SSL context.\n",
3194 err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003195 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand4801c702019-10-04 17:36:55 +02003196 goto end;
3197 }
3198 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003199 } else {
3200 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3201 * so the line that contains logic is marked via comments
3202 */
3203 xname = X509_get_subject_name(certs_and_keys[n].cert);
3204 i = -1;
3205 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3206 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003207 ASN1_STRING *value;
3208 value = X509_NAME_ENTRY_get_data(entry);
3209 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003210 /* Important line is here */
William Lallemand4801c702019-10-04 17:36:55 +02003211 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003212
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003213 OPENSSL_free(str);
3214 str = NULL;
William Lallemand4801c702019-10-04 17:36:55 +02003215 if (ret < 0) {
3216 memprintf(err, "%sunable to allocate SSL context.\n",
3217 err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003218 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand4801c702019-10-04 17:36:55 +02003219 goto end;
3220 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003221 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003222 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003223
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003224 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003225#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003226 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3227 if (names) {
3228 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3229 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003230
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003231 if (name->type == GEN_DNS) {
3232 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3233 /* Important line is here */
William Lallemand4801c702019-10-04 17:36:55 +02003234 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003235
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003236 OPENSSL_free(str);
3237 str = NULL;
William Lallemand4801c702019-10-04 17:36:55 +02003238 if (ret < 0) {
3239 memprintf(err, "%sunable to allocate SSL context.\n",
3240 err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003241 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand4801c702019-10-04 17:36:55 +02003242 goto end;
3243 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003244 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003245 }
3246 }
3247 }
3248 }
3249#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3250 }
3251
3252 /* If no files found, return error */
3253 if (eb_is_empty(&sni_keytypes_map)) {
3254 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3255 err && *err ? *err : "", path);
Willy Tarreaub131c872019-10-16 16:42:19 +02003256 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003257 goto end;
3258 }
3259
3260 /* We now have a map of CN/SAN to keytypes that are loaded in
3261 * Iterate through the map to create the SSL_CTX's (if needed)
3262 * and add each CTX to the SNI tree
3263 *
3264 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003265 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003266 * combination is denoted by the key in the map. Each key
3267 * has a value between 1 and 2^n - 1. Conveniently, the array
3268 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3269 * entry in the array to correspond to the unique combo (key)
3270 * associated with i. This unique key combo (i) will be associated
3271 * with combos[i-1]
3272 */
3273
3274 node = ebmb_first(&sni_keytypes_map);
3275 while (node) {
3276 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003277 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003278 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003279
3280 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3281 i = container_of(node, struct sni_keytype, name)->keytypes;
3282 cur_ctx = key_combos[i-1].ctx;
3283
3284 if (cur_ctx == NULL) {
3285 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003286 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003287 if (cur_ctx == NULL) {
3288 memprintf(err, "%sunable to allocate SSL context.\n",
3289 err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003290 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003291 goto end;
3292 }
3293
yanbzhube2774d2015-12-10 15:07:30 -05003294 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003295 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3296 if (i & (1<<n)) {
3297 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003298 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Brun394701d2019-10-17 13:25:14 +02003299 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3300 if (errcode & ERR_CODE) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05003301 SSL_CTX_free(cur_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003302 goto end;
3303 }
yanbzhube2774d2015-12-10 15:07:30 -05003304
3305#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
3306 /* Load OCSP Info into context */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003307 if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) {
yanbzhube2774d2015-12-10 15:07:30 -05003308 if (err)
3309 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 +00003310 *err ? *err : "", cur_file);
yanbzhube2774d2015-12-10 15:07:30 -05003311 SSL_CTX_free(cur_ctx);
Willy Tarreaub131c872019-10-16 16:42:19 +02003312 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhube2774d2015-12-10 15:07:30 -05003313 goto end;
3314 }
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02003315#elif (defined OPENSSL_IS_BORINGSSL)
3316 ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file);
yanbzhube2774d2015-12-10 15:07:30 -05003317#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003318 }
3319 }
3320
3321 /* Load DH params into the ctx to support DHE keys */
3322#ifndef OPENSSL_NO_DH
3323 if (ssl_dh_ptr_index >= 0)
3324 SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL);
3325
Emeric Bruncfc1afe2019-10-17 13:27:40 +02003326 errcode |= ssl_sock_load_dh_params(cur_ctx, NULL, err);
3327 if (errcode & ERR_CODE) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05003328 if (err)
3329 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3330 *err ? *err : "", path);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003331 goto end;
3332 }
3333#endif
3334
3335 /* Update key_combos */
3336 key_combos[i-1].ctx = cur_ctx;
3337 }
3338
3339 /* Update SNI Tree */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003340 key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf,
William Lallemand24e292c2019-10-03 23:46:33 +02003341 kinfo, str, key_combos[i-1].order);
3342 if (key_combos[i-1].order < 0) {
3343 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003344 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand24e292c2019-10-03 23:46:33 +02003345 goto end;
3346 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003347 node = ebmb_next(node);
3348 }
3349
3350
3351 /* Mark a default context if none exists, using the ctx that has the most shared keys */
3352 if (!bind_conf->default_ctx) {
3353 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
3354 if (key_combos[i].ctx) {
3355 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003356 bind_conf->default_ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003357 break;
3358 }
3359 }
3360 }
3361
3362end:
3363
3364 if (names)
3365 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
3366
3367 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3368 ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]);
3369
3370 node = ebmb_first(&sni_keytypes_map);
3371 while (node) {
3372 next = ebmb_next(node);
3373 ebmb_delete(node);
William Lallemande92c0302019-10-04 17:24:39 +02003374 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05003375 node = next;
3376 }
3377
Willy Tarreaub131c872019-10-16 16:42:19 +02003378 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003379}
3380#else
3381/* This is a dummy, that just logs an error and returns error */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003382static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3383 char **sni_filter, int fcount, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003384{
3385 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3386 err && *err ? *err : "", path, strerror(errno));
3387 return 1;
3388}
3389
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003390#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05003391
Emeric Brunfc0421f2012-09-07 17:30:07 +02003392/* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if
3393 * an early error happens and the caller must call SSL_CTX_free() by itelf.
3394 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003395static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s,
3396 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003397{
3398 BIO *in;
3399 X509 *x = NULL, *ca;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003400 int i, err;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003401 int ret = -1;
3402 int order = 0;
3403 X509_NAME *xname;
3404 char *str;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003405 pem_password_cb *passwd_cb;
3406 void *passwd_cb_userdata;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003407 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003408 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003409
Emeric Brunfc0421f2012-09-07 17:30:07 +02003410#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3411 STACK_OF(GENERAL_NAME) *names;
3412#endif
3413
3414 in = BIO_new(BIO_s_file());
3415 if (in == NULL)
3416 goto end;
3417
3418 if (BIO_read_filename(in, file) <= 0)
3419 goto end;
3420
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003421
3422 passwd_cb = SSL_CTX_get_default_passwd_cb(ctx);
3423 passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx);
3424
3425 x = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003426 if (x == NULL)
3427 goto end;
3428
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003429 pkey = X509_get_pubkey(x);
3430 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003431 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003432 switch(EVP_PKEY_base_id(pkey)) {
3433 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003434 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003435 break;
3436 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003437 kinfo.sig = TLSEXT_signature_ecdsa;
3438 break;
3439 case EVP_PKEY_DSA:
3440 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003441 break;
3442 }
3443 EVP_PKEY_free(pkey);
3444 }
3445
Emeric Brun50bcecc2013-04-22 13:05:23 +02003446 if (fcount) {
William Lallemand24e292c2019-10-03 23:46:33 +02003447 while (fcount--) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003448 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, sni_filter[fcount], order);
William Lallemand24e292c2019-10-03 23:46:33 +02003449 if (order < 0)
3450 goto end;
3451 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003452 }
3453 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003454#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003455 names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
3456 if (names) {
3457 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3458 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
3459 if (name->type == GEN_DNS) {
3460 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003461 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003462 OPENSSL_free(str);
William Lallemand24e292c2019-10-03 23:46:33 +02003463 if (order < 0)
3464 goto end;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003465 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003466 }
3467 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003468 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003469 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003470#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003471 xname = X509_get_subject_name(x);
3472 i = -1;
3473 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3474 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003475 ASN1_STRING *value;
3476
3477 value = X509_NAME_ENTRY_get_data(entry);
3478 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003479 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003480 OPENSSL_free(str);
William Lallemand24e292c2019-10-03 23:46:33 +02003481 if (order < 0)
3482 goto end;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003483 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003484 }
3485 }
3486
3487 ret = 0; /* the caller must not free the SSL_CTX argument anymore */
3488 if (!SSL_CTX_use_certificate(ctx, x))
3489 goto end;
3490
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003491#ifdef SSL_CTX_clear_extra_chain_certs
3492 SSL_CTX_clear_extra_chain_certs(ctx);
3493#else
Emeric Brunfc0421f2012-09-07 17:30:07 +02003494 if (ctx->extra_certs != NULL) {
3495 sk_X509_pop_free(ctx->extra_certs, X509_free);
3496 ctx->extra_certs = NULL;
3497 }
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003498#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02003499
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003500 while ((ca = PEM_read_bio_X509(in, NULL, passwd_cb, passwd_cb_userdata))) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003501 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3502 X509_free(ca);
3503 goto end;
3504 }
3505 }
3506
3507 err = ERR_get_error();
3508 if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
3509 /* we successfully reached the last cert in the file */
3510 ret = 1;
3511 }
3512 ERR_clear_error();
3513
3514end:
3515 if (x)
3516 X509_free(x);
3517
3518 if (in)
3519 BIO_free(in);
3520
3521 return ret;
3522}
3523
Willy Tarreaub131c872019-10-16 16:42:19 +02003524/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003525static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3526 char **sni_filter, int fcount, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003527{
Emeric Bruncfc1afe2019-10-17 13:27:40 +02003528 int errcode = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003529 int ret;
3530 SSL_CTX *ctx;
3531
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003532 ctx = SSL_CTX_new(SSLv23_server_method());
Emeric Brunfc0421f2012-09-07 17:30:07 +02003533 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003534 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3535 err && *err ? *err : "", path);
Willy Tarreaub131c872019-10-16 16:42:19 +02003536 return ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003537 }
3538
3539 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003540 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
3541 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003542 SSL_CTX_free(ctx);
Willy Tarreaub131c872019-10-16 16:42:19 +02003543 return ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003544 }
3545
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003546 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf, ssl_conf, sni_filter, fcount);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003547 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003548 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
3549 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003550 if (ret < 0) /* serious error, must do that ourselves */
3551 SSL_CTX_free(ctx);
Willy Tarreaub131c872019-10-16 16:42:19 +02003552 return ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003553 }
Emeric Brun61694ab2012-10-26 13:35:33 +02003554
3555 if (SSL_CTX_check_private_key(ctx) <= 0) {
3556 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
3557 err && *err ? *err : "", path);
Willy Tarreaub131c872019-10-16 16:42:19 +02003558 return ERR_ALERT | ERR_FATAL;
Emeric Brun61694ab2012-10-26 13:35:33 +02003559 }
3560
Emeric Brunfc0421f2012-09-07 17:30:07 +02003561 /* we must not free the SSL_CTX anymore below, since it's already in
3562 * the tree, so it will be discovered and cleaned in time.
3563 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003564#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +02003565 /* store a NULL pointer to indicate we have not yet loaded
3566 a custom DH param file */
3567 if (ssl_dh_ptr_index >= 0) {
3568 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3569 }
3570
Emeric Bruncfc1afe2019-10-17 13:27:40 +02003571 errcode |= ssl_sock_load_dh_params(ctx, path, err);
3572 if (errcode & ERR_CODE) {
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003573 if (err)
3574 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3575 *err ? *err : "", path);
Emeric Bruncfc1afe2019-10-17 13:27:40 +02003576 return errcode;
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003577 }
3578#endif
3579
Lukas Tribuse4e30f72014-12-09 16:32:51 +01003580#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02003581 ret = ssl_sock_load_ocsp(ctx, path);
3582 if (ret < 0) {
3583 if (err)
3584 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",
3585 *err ? *err : "", path);
Willy Tarreaub131c872019-10-16 16:42:19 +02003586 return ERR_ALERT | ERR_FATAL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02003587 }
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02003588#elif (defined OPENSSL_IS_BORINGSSL)
3589 ssl_sock_set_ocsp_response_from_file(ctx, path);
Emeric Brun4147b2e2014-06-16 18:36:30 +02003590#endif
3591
Willy Tarreau5db847a2019-05-09 14:13:35 +02003592#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01003593 if (sctl_ex_index >= 0) {
3594 ret = ssl_sock_load_sctl(ctx, path);
3595 if (ret < 0) {
3596 if (err)
3597 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
3598 *err ? *err : "", path);
Willy Tarreaub131c872019-10-16 16:42:19 +02003599 return ERR_ALERT | ERR_FATAL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01003600 }
3601 }
3602#endif
3603
Emeric Brunfc0421f2012-09-07 17:30:07 +02003604#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003605 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003606 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
3607 err && *err ? *err : "");
Willy Tarreaub131c872019-10-16 16:42:19 +02003608 return ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003609 }
3610#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003611 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003612 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003613 bind_conf->default_ssl_conf = ssl_conf;
3614 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003615
Emeric Bruncfc1afe2019-10-17 13:27:40 +02003616 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003617}
3618
Willy Tarreaub131c872019-10-16 16:42:19 +02003619
3620/* Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau03209342016-12-22 17:08:28 +01003621int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003622{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003623 struct dirent **de_list;
3624 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003625 DIR *dir;
3626 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01003627 char *end;
3628 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02003629 int cfgerr = 0;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003630#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05003631 int is_bundle;
3632 int j;
3633#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02003634
yanbzhu08ce6ab2015-12-02 13:01:29 -05003635 if (stat(path, &buf) == 0) {
3636 dir = opendir(path);
3637 if (!dir)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003638 return ssl_sock_load_cert_file(path, bind_conf, NULL, NULL, 0, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003639
yanbzhu08ce6ab2015-12-02 13:01:29 -05003640 /* strip trailing slashes, including first one */
3641 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
3642 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003643
yanbzhu08ce6ab2015-12-02 13:01:29 -05003644 n = scandir(path, &de_list, 0, alphasort);
3645 if (n < 0) {
3646 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
3647 err && *err ? *err : "", path, strerror(errno));
Willy Tarreaub131c872019-10-16 16:42:19 +02003648 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003649 }
3650 else {
3651 for (i = 0; i < n; i++) {
3652 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02003653
yanbzhu08ce6ab2015-12-02 13:01:29 -05003654 end = strrchr(de->d_name, '.');
3655 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
3656 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003657
yanbzhu08ce6ab2015-12-02 13:01:29 -05003658 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
3659 if (stat(fp, &buf) != 0) {
3660 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3661 err && *err ? *err : "", fp, strerror(errno));
Willy Tarreaub131c872019-10-16 16:42:19 +02003662 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003663 goto ignore_entry;
3664 }
3665 if (!S_ISREG(buf.st_mode))
3666 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05003667
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003668#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05003669 is_bundle = 0;
3670 /* Check if current entry in directory is part of a multi-cert bundle */
3671
3672 if (end) {
3673 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
3674 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
3675 is_bundle = 1;
3676 break;
3677 }
3678 }
3679
3680 if (is_bundle) {
yanbzhu63ea8462015-12-09 13:35:14 -05003681 int dp_len;
3682
3683 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05003684
3685 /* increment i and free de until we get to a non-bundle cert
3686 * Note here that we look at de_list[i + 1] before freeing de
Willy Tarreau5b8a8652019-10-29 10:48:50 +01003687 * this is important since ignore_entry will free de. This also
3688 * guarantees that de->d_name continues to hold the same prefix.
yanbzhu63ea8462015-12-09 13:35:14 -05003689 */
Willy Tarreau5b8a8652019-10-29 10:48:50 +01003690 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
yanbzhu63ea8462015-12-09 13:35:14 -05003691 free(de);
3692 i++;
3693 de = de_list[i];
3694 }
3695
Willy Tarreau5b8a8652019-10-29 10:48:50 +01003696 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
Willy Tarreaub131c872019-10-16 16:42:19 +02003697 cfgerr |= ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05003698 /* Successfully processed the bundle */
3699 goto ignore_entry;
3700 }
3701 }
3702
3703#endif
Willy Tarreaub131c872019-10-16 16:42:19 +02003704 cfgerr |= ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003705ignore_entry:
3706 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003707 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003708 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003709 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003710 closedir(dir);
3711 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003712 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003713
Willy Tarreaub131c872019-10-16 16:42:19 +02003714 cfgerr |= ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003715
Emeric Brunfc0421f2012-09-07 17:30:07 +02003716 return cfgerr;
3717}
3718
Thierry Fournier383085f2013-01-24 14:15:43 +01003719/* Make sure openssl opens /dev/urandom before the chroot. The work is only
3720 * done once. Zero is returned if the operation fails. No error is returned
3721 * if the random is said as not implemented, because we expect that openssl
3722 * will use another method once needed.
3723 */
3724static int ssl_initialize_random()
3725{
3726 unsigned char random;
3727 static int random_initialized = 0;
3728
3729 if (!random_initialized && RAND_bytes(&random, 1) != 0)
3730 random_initialized = 1;
3731
3732 return random_initialized;
3733}
3734
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003735/* release ssl bind conf */
3736void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003737{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003738 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01003739#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003740 free(conf->npn_str);
3741 conf->npn_str = NULL;
3742#endif
3743#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
3744 free(conf->alpn_str);
3745 conf->alpn_str = NULL;
3746#endif
3747 free(conf->ca_file);
3748 conf->ca_file = NULL;
3749 free(conf->crl_file);
3750 conf->crl_file = NULL;
3751 free(conf->ciphers);
3752 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02003753#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02003754 free(conf->ciphersuites);
3755 conf->ciphersuites = NULL;
3756#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01003757 free(conf->curves);
3758 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003759 free(conf->ecdhe);
3760 conf->ecdhe = NULL;
3761 }
3762}
3763
Willy Tarreaub131c872019-10-16 16:42:19 +02003764/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003765int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
3766{
3767 char thisline[CRT_LINESIZE];
3768 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003769 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05003770 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003771 int linenum = 0;
3772 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003773
Willy Tarreauad1731d2013-04-02 17:35:58 +02003774 if ((f = fopen(file, "r")) == NULL) {
3775 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaub131c872019-10-16 16:42:19 +02003776 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003777 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003778
3779 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003780 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003781 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003782 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003783 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003784 char *crt_path;
3785 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003786
3787 linenum++;
3788 end = line + strlen(line);
3789 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
3790 /* Check if we reached the limit and the last char is not \n.
3791 * Watch out for the last line without the terminating '\n'!
3792 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02003793 memprintf(err, "line %d too long in file '%s', limit is %d characters",
3794 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaub131c872019-10-16 16:42:19 +02003795 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003796 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003797 }
3798
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003799 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02003800 newarg = 1;
3801 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003802 if (*line == '#' || *line == '\n' || *line == '\r') {
3803 /* end of string, end of loop */
3804 *line = 0;
3805 break;
Willy Tarreau26e4ab42020-02-25 07:51:59 +01003806 } else if (isspace((unsigned char)*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02003807 newarg = 1;
3808 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003809 } else if (*line == '[') {
3810 if (ssl_b) {
3811 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003812 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003813 break;
3814 }
3815 if (!arg) {
3816 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003817 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003818 break;
3819 }
3820 ssl_b = arg;
3821 newarg = 1;
3822 *line = 0;
3823 } else if (*line == ']') {
3824 if (ssl_e) {
3825 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003826 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02003827 break;
3828 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003829 if (!ssl_b) {
3830 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003831 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003832 break;
3833 }
3834 ssl_e = arg;
3835 newarg = 1;
3836 *line = 0;
3837 } else if (newarg) {
3838 if (arg == MAX_CRT_ARGS) {
3839 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003840 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003841 break;
3842 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02003843 newarg = 0;
3844 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003845 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02003846 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003847 }
Willy Tarreaub131c872019-10-16 16:42:19 +02003848 if (cfgerr & ERR_CODE)
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003849 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003850 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003851
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003852 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003853 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003854 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003855
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003856 crt_path = args[0];
3857 if (*crt_path != '/' && global_ssl.crt_base) {
3858 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
3859 memprintf(err, "'%s' : path too long on line %d in file '%s'",
3860 crt_path, linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003861 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003862 break;
3863 }
3864 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
3865 crt_path = path;
3866 }
3867
3868 ssl_conf = calloc(1, sizeof *ssl_conf);
3869 cur_arg = ssl_b ? ssl_b : 1;
3870 while (cur_arg < ssl_e) {
3871 newarg = 0;
3872 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
3873 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
3874 newarg = 1;
Willy Tarreaub131c872019-10-16 16:42:19 +02003875 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003876 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
3877 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
3878 args[cur_arg], linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003879 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003880 }
3881 cur_arg += 1 + ssl_bind_kws[i].skip;
3882 break;
3883 }
3884 }
3885 if (!cfgerr && !newarg) {
3886 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
3887 args[cur_arg], linenum, file);
Willy Tarreaub131c872019-10-16 16:42:19 +02003888 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003889 break;
3890 }
3891 }
Willy Tarreaub131c872019-10-16 16:42:19 +02003892
3893 if (cfgerr & ERR_CODE) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003894 ssl_sock_free_ssl_conf(ssl_conf);
3895 free(ssl_conf);
3896 ssl_conf = NULL;
3897 break;
3898 }
3899
3900 if (stat(crt_path, &buf) == 0) {
Willy Tarreaub131c872019-10-16 16:42:19 +02003901 cfgerr |= ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf,
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003902 &args[cur_arg], arg - cur_arg - 1, err);
Willy Tarreaub131c872019-10-16 16:42:19 +02003903 } else {
3904 cfgerr |= ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf,
3905 &args[cur_arg], arg - cur_arg - 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05003906 }
3907
Willy Tarreaub131c872019-10-16 16:42:19 +02003908 if (cfgerr & ERR_CODE) {
Willy Tarreauad1731d2013-04-02 17:35:58 +02003909 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003910 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003911 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003912 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003913 fclose(f);
3914 return cfgerr;
3915}
3916
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003917/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003918static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003919ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003920{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003921 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003922 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003923 SSL_OP_ALL | /* all known workarounds for bugs */
3924 SSL_OP_NO_SSLv2 |
3925 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003926 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02003927 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003928 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02003929 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003930 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003931 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003932 SSL_MODE_ENABLE_PARTIAL_WRITE |
3933 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01003934 SSL_MODE_RELEASE_BUFFERS |
3935 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02003936 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003937 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003938 int flags = MC_SSL_O_ALL;
3939 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003940
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003941 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003942 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003943
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003944 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01003945 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
3946 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3947 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003948 else
3949 flags = conf_ssl_methods->flags;
3950
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003951 min = conf_ssl_methods->min;
3952 max = conf_ssl_methods->max;
3953 /* start with TLSv10 to remove SSLv3 per default */
3954 if (!min && (!max || max >= CONF_TLSV10))
3955 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003956 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003957 if (min)
3958 flags |= (methodVersions[min].flag - 1);
3959 if (max)
3960 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003961 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003962 min = max = CONF_TLSV_NONE;
3963 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003964 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003965 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003966 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003967 if (min) {
3968 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003969 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
3970 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3971 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
3972 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003973 hole = 0;
3974 }
3975 max = i;
3976 }
3977 else {
3978 min = max = i;
3979 }
3980 }
3981 else {
3982 if (min)
3983 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003984 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003985 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003986 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
3987 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003988 cfgerr += 1;
3989 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02003990 /* save real min/max in bind_conf */
3991 conf_ssl_methods->min = min;
3992 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003993
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003994#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003995 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08003996 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003997 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003998 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003999 else
4000 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4001 if (flags & methodVersions[i].flag)
4002 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004003#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004004 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004005 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4006 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004007#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004008
4009 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4010 options |= SSL_OP_NO_TICKET;
4011 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4012 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004013
4014#ifdef SSL_OP_NO_RENEGOTIATION
4015 options |= SSL_OP_NO_RENEGOTIATION;
4016#endif
4017
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004018 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004019
Willy Tarreau5db847a2019-05-09 14:13:35 +02004020#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004021 if (global_ssl.async)
4022 mode |= SSL_MODE_ASYNC;
4023#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004024 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004025 if (global_ssl.life_time)
4026 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004027
4028#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4029#ifdef OPENSSL_IS_BORINGSSL
4030 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4031 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Willy Tarreau5db847a2019-05-09 14:13:35 +02004032#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houcharde82c1d42019-12-17 15:39:54 +01004033 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004034 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004035 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4036 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004037#else
4038 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004039#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004040 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004041#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004042 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004043}
4044
William Lallemand4f45bb92017-10-30 20:08:51 +01004045
4046static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4047{
4048 if (first == block) {
4049 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4050 if (first->len > 0)
4051 sh_ssl_sess_tree_delete(sh_ssl_sess);
4052 }
4053}
4054
4055/* return first block from sh_ssl_sess */
4056static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4057{
4058 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4059
4060}
4061
4062/* store a session into the cache
4063 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4064 * data: asn1 encoded session
4065 * data_len: asn1 encoded session length
4066 * Returns 1 id session was stored (else 0)
4067 */
4068static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4069{
4070 struct shared_block *first;
4071 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4072
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004073 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004074 if (!first) {
4075 /* Could not retrieve enough free blocks to store that session */
4076 return 0;
4077 }
4078
4079 /* STORE the key in the first elem */
4080 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4081 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4082 first->len = sizeof(struct sh_ssl_sess_hdr);
4083
4084 /* it returns the already existing node
4085 or current node if none, never returns null */
4086 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4087 if (oldsh_ssl_sess != sh_ssl_sess) {
4088 /* NOTE: Row couldn't be in use because we lock read & write function */
4089 /* release the reserved row */
4090 shctx_row_dec_hot(ssl_shctx, first);
4091 /* replace the previous session already in the tree */
4092 sh_ssl_sess = oldsh_ssl_sess;
4093 /* ignore the previous session data, only use the header */
4094 first = sh_ssl_sess_first_block(sh_ssl_sess);
4095 shctx_row_inc_hot(ssl_shctx, first);
4096 first->len = sizeof(struct sh_ssl_sess_hdr);
4097 }
4098
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004099 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004100 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004101 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004102 }
4103
4104 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004105
4106 return 1;
4107}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004108
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004109/* SSL callback used when a new session is created while connecting to a server */
4110static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4111{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004112 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004113 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004114
Willy Tarreau07d94e42018-09-20 10:57:52 +02004115 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004116
Olivier Houcharde6060c52017-11-16 17:42:52 +01004117 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4118 int len;
4119 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004120
Olivier Houcharde6060c52017-11-16 17:42:52 +01004121 len = i2d_SSL_SESSION(sess, NULL);
4122 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4123 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4124 } else {
4125 free(s->ssl_ctx.reused_sess[tid].ptr);
4126 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4127 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4128 }
4129 if (s->ssl_ctx.reused_sess[tid].ptr) {
4130 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4131 &ptr);
4132 }
4133 } else {
4134 free(s->ssl_ctx.reused_sess[tid].ptr);
4135 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4136 }
4137
4138 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004139}
4140
Olivier Houcharde6060c52017-11-16 17:42:52 +01004141
William Lallemanded0b5ad2017-10-30 19:36:36 +01004142/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004143int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004144{
4145 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4146 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4147 unsigned char *p;
4148 int data_len;
Emeric Brun7b34de32019-10-08 18:27:37 +02004149 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004150 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004151
4152 /* Session id is already stored in to key and session id is known
4153 * so we dont store it to keep size.
Emeric Brun7b34de32019-10-08 18:27:37 +02004154 * note: SSL_SESSION_set1_id is using
4155 * a memcpy so we need to use a different pointer
4156 * than sid_data or sid_ctx_data to avoid valgrind
4157 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004158 */
4159
4160 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Brun7b34de32019-10-08 18:27:37 +02004161
4162 /* copy value in an other buffer */
4163 memcpy(encid, sid_data, sid_length);
4164
4165 /* pad with 0 */
4166 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4167 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4168
4169 /* force length to zero to avoid ASN1 encoding */
4170 SSL_SESSION_set1_id(sess, encid, 0);
4171
4172 /* force length to zero to avoid ASN1 encoding */
4173 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004174
4175 /* check if buffer is large enough for the ASN1 encoded session */
4176 data_len = i2d_SSL_SESSION(sess, NULL);
4177 if (data_len > SHSESS_MAX_DATA_LEN)
4178 goto err;
4179
4180 p = encsess;
4181
4182 /* process ASN1 session encoding before the lock */
4183 i2d_SSL_SESSION(sess, &p);
4184
William Lallemanded0b5ad2017-10-30 19:36:36 +01004185
William Lallemanda3c77cf2017-10-30 23:44:40 +01004186 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004187 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004188 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004189 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004190err:
4191 /* reset original length values */
Emeric Brun7b34de32019-10-08 18:27:37 +02004192 SSL_SESSION_set1_id(sess, encid, sid_length);
4193 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004194
4195 return 0; /* do not increment session reference count */
4196}
4197
4198/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004199SSL_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 +01004200{
William Lallemand4f45bb92017-10-30 20:08:51 +01004201 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004202 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4203 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004204 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004205 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004206
4207 global.shctx_lookups++;
4208
4209 /* allow the session to be freed automatically by openssl */
4210 *do_copy = 0;
4211
4212 /* tree key is zeros padded sessionid */
4213 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4214 memcpy(tmpkey, key, key_len);
4215 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4216 key = tmpkey;
4217 }
4218
4219 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004220 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004221
4222 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004223 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4224 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004225 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004226 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004227 global.shctx_misses++;
4228 return NULL;
4229 }
4230
William Lallemand4f45bb92017-10-30 20:08:51 +01004231 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4232 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004233
William Lallemand4f45bb92017-10-30 20:08:51 +01004234 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 +01004235
William Lallemanda3c77cf2017-10-30 23:44:40 +01004236 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004237
4238 /* decode ASN1 session */
4239 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004240 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004241 /* Reset session id and session id contenxt */
4242 if (sess) {
4243 SSL_SESSION_set1_id(sess, key, key_len);
4244 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4245 }
4246
4247 return sess;
4248}
4249
William Lallemand4f45bb92017-10-30 20:08:51 +01004250
William Lallemanded0b5ad2017-10-30 19:36:36 +01004251/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004252void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004253{
William Lallemand4f45bb92017-10-30 20:08:51 +01004254 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004255 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4256 unsigned int sid_length;
4257 const unsigned char *sid_data;
4258 (void)ctx;
4259
4260 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4261 /* tree key is zeros padded sessionid */
4262 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4263 memcpy(tmpkey, sid_data, sid_length);
4264 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4265 sid_data = tmpkey;
4266 }
4267
William Lallemanda3c77cf2017-10-30 23:44:40 +01004268 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004269
4270 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004271 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4272 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004273 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004274 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004275 }
4276
4277 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004278 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004279}
4280
4281/* Set session cache mode to server and disable openssl internal cache.
4282 * Set shared cache callbacks on an ssl context.
4283 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004284void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004285{
4286 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4287
4288 if (!ssl_shctx) {
4289 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4290 return;
4291 }
4292
4293 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4294 SSL_SESS_CACHE_NO_INTERNAL |
4295 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4296
4297 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004298 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4299 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4300 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004301}
4302
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004303int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx)
4304{
4305 struct proxy *curproxy = bind_conf->frontend;
4306 int cfgerr = 0;
4307 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004308 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004309 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004310#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004311 const char *conf_ciphersuites;
4312#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004313 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004314
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004315 if (ssl_conf) {
4316 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4317 int i, min, max;
4318 int flags = MC_SSL_O_ALL;
4319
4320 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004321 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4322 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004323 if (min)
4324 flags |= (methodVersions[min].flag - 1);
4325 if (max)
4326 flags |= ~((methodVersions[max].flag << 1) - 1);
4327 min = max = CONF_TLSV_NONE;
4328 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4329 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4330 if (min)
4331 max = i;
4332 else
4333 min = max = i;
4334 }
4335 /* save real min/max */
4336 conf_ssl_methods->min = min;
4337 conf_ssl_methods->max = max;
4338 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004339 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4340 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004341 cfgerr += 1;
4342 }
4343 }
4344
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004345 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01004346 case SSL_SOCK_VERIFY_NONE:
4347 verify = SSL_VERIFY_NONE;
4348 break;
4349 case SSL_SOCK_VERIFY_OPTIONAL:
4350 verify = SSL_VERIFY_PEER;
4351 break;
4352 case SSL_SOCK_VERIFY_REQUIRED:
4353 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
4354 break;
4355 }
4356 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
4357 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004358 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
4359 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
4360 if (ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004361 /* load CAfile to verify */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004362 if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004363 ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
4364 curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02004365 cfgerr++;
4366 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02004367 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
4368 /* set CA names for client cert request, function returns void */
4369 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file));
4370 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004371 }
Emeric Brun850efd52014-01-29 12:24:34 +01004372 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004373 ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
4374 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun850efd52014-01-29 12:24:34 +01004375 cfgerr++;
4376 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004377#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004378 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004379 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
4380
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004381 if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004382 ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
4383 curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02004384 cfgerr++;
4385 }
Emeric Brun561e5742012-10-02 15:20:55 +02004386 else {
4387 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4388 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004389 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004390#endif
Emeric Brun644cde02012-12-14 11:21:13 +01004391 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02004392 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004393#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02004394 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004395 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004396 ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
4397 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004398 cfgerr++;
4399 }
4400 }
4401#endif
4402
William Lallemand4f45bb92017-10-30 20:08:51 +01004403 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004404 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
4405 if (conf_ciphers &&
4406 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004407 ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
4408 curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004409 cfgerr++;
4410 }
4411
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004412#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004413 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
4414 if (conf_ciphersuites &&
4415 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
4416 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
4417 curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
4418 cfgerr++;
4419 }
4420#endif
4421
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004422#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02004423 /* If tune.ssl.default-dh-param has not been set,
4424 neither has ssl-default-dh-file and no static DH
4425 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01004426 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02004427 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02004428 (ssl_dh_ptr_index == -1 ||
4429 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004430 STACK_OF(SSL_CIPHER) * ciphers = NULL;
4431 const SSL_CIPHER * cipher = NULL;
4432 char cipher_description[128];
4433 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
4434 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
4435 which is not ephemeral DH. */
4436 const char dhe_description[] = " Kx=DH ";
4437 const char dhe_export_description[] = " Kx=DH(";
4438 int idx = 0;
4439 int dhe_found = 0;
4440 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02004441
Remi Gacogne23d5d372014-10-10 17:04:26 +02004442 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004443
Remi Gacogne23d5d372014-10-10 17:04:26 +02004444 if (ssl) {
4445 ciphers = SSL_get_ciphers(ssl);
4446
4447 if (ciphers) {
4448 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
4449 cipher = sk_SSL_CIPHER_value(ciphers, idx);
4450 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
4451 if (strstr(cipher_description, dhe_description) != NULL ||
4452 strstr(cipher_description, dhe_export_description) != NULL) {
4453 dhe_found = 1;
4454 break;
4455 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02004456 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004457 }
4458 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02004459 SSL_free(ssl);
4460 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02004461 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004462
Lukas Tribus90132722014-08-18 00:56:33 +02004463 if (dhe_found) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004464 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 +02004465 }
4466
Willy Tarreauef934602016-12-22 23:12:01 +01004467 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004468 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004469
Willy Tarreauef934602016-12-22 23:12:01 +01004470 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004471 if (local_dh_1024 == NULL) {
4472 local_dh_1024 = ssl_get_dh_1024();
4473 }
Willy Tarreauef934602016-12-22 23:12:01 +01004474 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004475 if (local_dh_2048 == NULL) {
4476 local_dh_2048 = ssl_get_dh_2048();
4477 }
Willy Tarreauef934602016-12-22 23:12:01 +01004478 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004479 if (local_dh_4096 == NULL) {
4480 local_dh_4096 = ssl_get_dh_4096();
4481 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004482 }
4483 }
4484 }
4485#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004486
Emeric Brunfc0421f2012-09-07 17:30:07 +02004487 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004488#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02004489 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02004490#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02004491
Bernard Spil13c53f82018-02-15 13:34:58 +01004492#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004493 ssl_conf_cur = NULL;
4494 if (ssl_conf && ssl_conf->npn_str)
4495 ssl_conf_cur = ssl_conf;
4496 else if (bind_conf->ssl_conf.npn_str)
4497 ssl_conf_cur = &bind_conf->ssl_conf;
4498 if (ssl_conf_cur)
4499 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02004500#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01004501#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004502 ssl_conf_cur = NULL;
4503 if (ssl_conf && ssl_conf->alpn_str)
4504 ssl_conf_cur = ssl_conf;
4505 else if (bind_conf->ssl_conf.alpn_str)
4506 ssl_conf_cur = &bind_conf->ssl_conf;
4507 if (ssl_conf_cur)
4508 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02004509#endif
Lukas Tribusd13e9252019-11-24 18:20:40 +01004510#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004511 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
4512 if (conf_curves) {
4513 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004514 ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
4515 curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004516 cfgerr++;
4517 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01004518 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004519 }
4520#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004521#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004522 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02004523 int i;
4524 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004525#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004526 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02004527 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4528 NULL);
4529
4530 if (ecdhe == NULL) {
Eric Salama27c21cd2019-11-20 11:33:40 +01004531 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02004532 return cfgerr;
4533 }
4534#else
4535 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
4536 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4537 ECDHE_DEFAULT_CURVE);
4538#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004539
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004540 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02004541 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004542 ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
4543 curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun2b58d042012-09-20 17:10:03 +02004544 cfgerr++;
4545 }
4546 else {
4547 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
4548 EC_KEY_free(ecdh);
4549 }
4550 }
4551#endif
4552
Emeric Brunfc0421f2012-09-07 17:30:07 +02004553 return cfgerr;
4554}
4555
Evan Broderbe554312013-06-27 00:05:25 -07004556static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
4557{
4558 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
4559 size_t prefixlen, suffixlen;
4560
4561 /* Trivial case */
4562 if (strcmp(pattern, hostname) == 0)
4563 return 1;
4564
Evan Broderbe554312013-06-27 00:05:25 -07004565 /* The rest of this logic is based on RFC 6125, section 6.4.3
4566 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
4567
Emeric Bruna848dae2013-10-08 11:27:28 +02004568 pattern_wildcard = NULL;
4569 pattern_left_label_end = pattern;
4570 while (*pattern_left_label_end != '.') {
4571 switch (*pattern_left_label_end) {
4572 case 0:
4573 /* End of label not found */
4574 return 0;
4575 case '*':
4576 /* If there is more than one wildcards */
4577 if (pattern_wildcard)
4578 return 0;
4579 pattern_wildcard = pattern_left_label_end;
4580 break;
4581 }
4582 pattern_left_label_end++;
4583 }
4584
4585 /* If it's not trivial and there is no wildcard, it can't
4586 * match */
4587 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07004588 return 0;
4589
4590 /* Make sure all labels match except the leftmost */
4591 hostname_left_label_end = strchr(hostname, '.');
4592 if (!hostname_left_label_end
4593 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
4594 return 0;
4595
4596 /* Make sure the leftmost label of the hostname is long enough
4597 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02004598 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07004599 return 0;
4600
4601 /* Finally compare the string on either side of the
4602 * wildcard */
4603 prefixlen = pattern_wildcard - pattern;
4604 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02004605 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
4606 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07004607 return 0;
4608
4609 return 1;
4610}
4611
4612static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
4613{
4614 SSL *ssl;
4615 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004616 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004617 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02004618 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07004619
4620 int depth;
4621 X509 *cert;
4622 STACK_OF(GENERAL_NAME) *alt_names;
4623 int i;
4624 X509_NAME *cert_subject;
4625 char *str;
4626
4627 if (ok == 0)
4628 return ok;
4629
4630 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004631 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01004632 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07004633
Willy Tarreauad92a9a2017-07-28 11:38:41 +02004634 /* We're checking if the provided hostnames match the desired one. The
4635 * desired hostname comes from the SNI we presented if any, or if not
4636 * provided then it may have been explicitly stated using a "verifyhost"
4637 * directive. If neither is set, we don't care about the name so the
4638 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02004639 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004640 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02004641 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004642 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02004643 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004644 if (!servername)
4645 return ok;
4646 }
Evan Broderbe554312013-06-27 00:05:25 -07004647
4648 /* We only need to verify the CN on the actual server cert,
4649 * not the indirect CAs */
4650 depth = X509_STORE_CTX_get_error_depth(ctx);
4651 if (depth != 0)
4652 return ok;
4653
4654 /* At this point, the cert is *not* OK unless we can find a
4655 * hostname match */
4656 ok = 0;
4657
4658 cert = X509_STORE_CTX_get_current_cert(ctx);
4659 /* It seems like this might happen if verify peer isn't set */
4660 if (!cert)
4661 return ok;
4662
4663 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
4664 if (alt_names) {
4665 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
4666 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
4667 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004668#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02004669 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
4670#else
Evan Broderbe554312013-06-27 00:05:25 -07004671 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02004672#endif
Evan Broderbe554312013-06-27 00:05:25 -07004673 ok = ssl_sock_srv_hostcheck(str, servername);
4674 OPENSSL_free(str);
4675 }
4676 }
4677 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02004678 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07004679 }
4680
4681 cert_subject = X509_get_subject_name(cert);
4682 i = -1;
4683 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
4684 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004685 ASN1_STRING *value;
4686 value = X509_NAME_ENTRY_get_data(entry);
4687 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07004688 ok = ssl_sock_srv_hostcheck(str, servername);
4689 OPENSSL_free(str);
4690 }
4691 }
4692
Willy Tarreau71d058c2017-07-26 20:09:56 +02004693 /* report the mismatch and indicate if SNI was used or not */
4694 if (!ok && !conn->err_code)
4695 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07004696 return ok;
4697}
4698
Emeric Brun94324a42012-10-11 14:00:19 +02004699/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01004700int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02004701{
Willy Tarreau03209342016-12-22 17:08:28 +01004702 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02004703 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004704 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02004705 SSL_OP_ALL | /* all known workarounds for bugs */
4706 SSL_OP_NO_SSLv2 |
4707 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004708 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02004709 SSL_MODE_ENABLE_PARTIAL_WRITE |
4710 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004711 SSL_MODE_RELEASE_BUFFERS |
4712 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01004713 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004714 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004715 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004716 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004717 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02004718
Thierry Fournier383085f2013-01-24 14:15:43 +01004719 /* Make sure openssl opens /dev/urandom before the chroot */
4720 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004721 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01004722 cfgerr++;
4723 }
4724
Willy Tarreaufce03112015-01-15 21:32:40 +01004725 /* Automatic memory computations need to know we use SSL there */
4726 global.ssl_used_backend = 1;
4727
4728 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02004729 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01004730 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004731 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
4732 curproxy->id, srv->id,
4733 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02004734 cfgerr++;
4735 return cfgerr;
4736 }
4737 }
Christopher Faulet68d35ae2020-03-27 18:55:49 +01004738 if (srv->use_ssl == 1)
Emeric Brun94324a42012-10-11 14:00:19 +02004739 srv->xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02004740
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004741 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004742 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004743 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
4744 proxy_type_str(curproxy), curproxy->id,
4745 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02004746 cfgerr++;
4747 return cfgerr;
4748 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004749
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004750 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004751 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
4752 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4753 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004754 else
4755 flags = conf_ssl_methods->flags;
4756
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004757 /* Real min and max should be determinate with configuration and openssl's capabilities */
4758 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004759 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004760 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004761 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004762
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004763 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004764 min = max = CONF_TLSV_NONE;
4765 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004766 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004767 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004768 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004769 if (min) {
4770 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004771 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
4772 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4773 proxy_type_str(curproxy), curproxy->id, srv->id,
4774 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004775 hole = 0;
4776 }
4777 max = i;
4778 }
4779 else {
4780 min = max = i;
4781 }
4782 }
4783 else {
4784 if (min)
4785 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004786 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004787 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004788 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
4789 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004790 cfgerr += 1;
4791 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004792
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004793#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004794 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004795 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004796 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004797 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004798 else
4799 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4800 if (flags & methodVersions[i].flag)
4801 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004802#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004803 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004804 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4805 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004806#endif
4807
4808 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
4809 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004810 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004811
Willy Tarreau5db847a2019-05-09 14:13:35 +02004812#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004813 if (global_ssl.async)
4814 mode |= SSL_MODE_ASYNC;
4815#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004816 SSL_CTX_set_mode(ctx, mode);
4817 srv->ssl_ctx.ctx = ctx;
4818
Emeric Bruna7aa3092012-10-26 12:58:00 +02004819 if (srv->ssl_ctx.client_crt) {
4820 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 +01004821 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
4822 proxy_type_str(curproxy), curproxy->id,
4823 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004824 cfgerr++;
4825 }
4826 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 +01004827 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
4828 proxy_type_str(curproxy), curproxy->id,
4829 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004830 cfgerr++;
4831 }
4832 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004833 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
4834 proxy_type_str(curproxy), curproxy->id,
4835 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004836 cfgerr++;
4837 }
4838 }
Emeric Brun94324a42012-10-11 14:00:19 +02004839
Emeric Brun850efd52014-01-29 12:24:34 +01004840 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
4841 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01004842 switch (srv->ssl_ctx.verify) {
4843 case SSL_SOCK_VERIFY_NONE:
4844 verify = SSL_VERIFY_NONE;
4845 break;
4846 case SSL_SOCK_VERIFY_REQUIRED:
4847 verify = SSL_VERIFY_PEER;
4848 break;
4849 }
Evan Broderbe554312013-06-27 00:05:25 -07004850 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01004851 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02004852 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01004853 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02004854 if (srv->ssl_ctx.ca_file) {
4855 /* load CAfile to verify */
4856 if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004857 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n",
4858 curproxy->id, srv->id,
4859 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004860 cfgerr++;
4861 }
4862 }
Emeric Brun850efd52014-01-29 12:24:34 +01004863 else {
4864 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01004865 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",
4866 curproxy->id, srv->id,
4867 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004868 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01004869 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
4870 curproxy->id, srv->id,
4871 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004872 cfgerr++;
4873 }
Emeric Brunef42d922012-10-11 16:11:36 +02004874#ifdef X509_V_FLAG_CRL_CHECK
4875 if (srv->ssl_ctx.crl_file) {
4876 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
4877
4878 if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004879 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
4880 curproxy->id, srv->id,
4881 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004882 cfgerr++;
4883 }
4884 else {
4885 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4886 }
4887 }
4888#endif
4889 }
4890
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004891 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
4892 SSL_SESS_CACHE_NO_INTERNAL_STORE);
4893 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02004894 if (srv->ssl_ctx.ciphers &&
4895 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004896 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
4897 curproxy->id, srv->id,
4898 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02004899 cfgerr++;
4900 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004901
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004902#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004903 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00004904 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004905 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
4906 curproxy->id, srv->id,
4907 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
4908 cfgerr++;
4909 }
4910#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01004911#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
4912 if (srv->ssl_ctx.npn_str)
4913 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
4914#endif
4915#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4916 if (srv->ssl_ctx.alpn_str)
4917 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
4918#endif
4919
Emeric Brun94324a42012-10-11 14:00:19 +02004920
4921 return cfgerr;
4922}
4923
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004924/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004925 * be NULL, in which case nothing is done. Returns the number of errors
4926 * encountered.
4927 */
Willy Tarreau03209342016-12-22 17:08:28 +01004928int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004929{
4930 struct ebmb_node *node;
4931 struct sni_ctx *sni;
4932 int err = 0;
4933
Willy Tarreaufce03112015-01-15 21:32:40 +01004934 /* Automatic memory computations need to know we use SSL there */
4935 global.ssl_used_frontend = 1;
4936
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004937 /* Make sure openssl opens /dev/urandom before the chroot */
4938 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004939 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004940 err++;
4941 }
4942 /* Create initial_ctx used to start the ssl connection before do switchctx */
4943 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004944 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004945 /* It should not be necessary to call this function, but it's
4946 necessary first to check and move all initialisation related
4947 to initial_ctx in ssl_sock_initial_ctx. */
4948 err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx);
4949 }
Emeric Brun0bed9942014-10-30 19:25:24 +01004950 if (bind_conf->default_ctx)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004951 err += ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx);
Emeric Brun0bed9942014-10-30 19:25:24 +01004952
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004953 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004954 while (node) {
4955 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004956 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4957 /* only initialize the CTX on its first occurrence and
4958 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004959 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004960 node = ebmb_next(node);
4961 }
4962
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004963 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004964 while (node) {
4965 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004966 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4967 /* only initialize the CTX on its first occurrence and
4968 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004969 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004970 node = ebmb_next(node);
4971 }
4972 return err;
4973}
4974
Willy Tarreau55d37912016-12-21 23:38:39 +01004975/* Prepares all the contexts for a bind_conf and allocates the shared SSL
4976 * context if needed. Returns < 0 on error, 0 on success. The warnings and
4977 * alerts are directly emitted since the rest of the stack does it below.
4978 */
4979int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
4980{
4981 struct proxy *px = bind_conf->frontend;
4982 int alloc_ctx;
4983 int err;
4984
4985 if (!bind_conf->is_ssl) {
4986 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004987 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
4988 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01004989 }
4990 return 0;
4991 }
4992 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004993 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004994 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
4995 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004996 }
4997 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004998 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
4999 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005000 return -1;
5001 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005002 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005003 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005004 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005005 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01005006 sizeof(*sh_ssl_sess_tree),
5007 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005008 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005009 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5010 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");
5011 else
5012 ha_alert("Unable to allocate SSL session cache.\n");
5013 return -1;
5014 }
5015 /* free block callback */
5016 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5017 /* init the root tree within the extra space */
5018 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5019 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005020 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005021 err = 0;
5022 /* initialize all certificate contexts */
5023 err += ssl_sock_prepare_all_ctx(bind_conf);
5024
5025 /* initialize CA variables if the certificates generation is enabled */
5026 err += ssl_sock_load_ca(bind_conf);
5027
5028 return -err;
5029}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005030
5031/* release ssl context allocated for servers. */
5032void ssl_sock_free_srv_ctx(struct server *srv)
5033{
Olivier Houchardc7566002018-11-20 23:33:50 +01005034#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5035 if (srv->ssl_ctx.alpn_str)
5036 free(srv->ssl_ctx.alpn_str);
5037#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005038#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01005039 if (srv->ssl_ctx.npn_str)
5040 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005041#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005042 if (srv->ssl_ctx.ctx)
5043 SSL_CTX_free(srv->ssl_ctx.ctx);
5044}
5045
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005046/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005047 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5048 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005049void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005050{
5051 struct ebmb_node *node, *back;
5052 struct sni_ctx *sni;
5053
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005054 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005055 while (node) {
5056 sni = ebmb_entry(node, struct sni_ctx, name);
5057 back = ebmb_next(node);
5058 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005059 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005060 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005061 ssl_sock_free_ssl_conf(sni->conf);
5062 free(sni->conf);
5063 sni->conf = NULL;
5064 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005065 free(sni);
5066 node = back;
5067 }
5068
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005069 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005070 while (node) {
5071 sni = ebmb_entry(node, struct sni_ctx, name);
5072 back = ebmb_next(node);
5073 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005074 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005075 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005076 ssl_sock_free_ssl_conf(sni->conf);
5077 free(sni->conf);
5078 sni->conf = NULL;
5079 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005080 free(sni);
5081 node = back;
5082 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005083 SSL_CTX_free(bind_conf->initial_ctx);
5084 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005085 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005086 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005087}
5088
Willy Tarreau795cdab2016-12-22 17:30:54 +01005089/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5090void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5091{
5092 ssl_sock_free_ca(bind_conf);
5093 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005094 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005095 free(bind_conf->ca_sign_file);
5096 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005097 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005098 free(bind_conf->keys_ref->filename);
5099 free(bind_conf->keys_ref->tlskeys);
5100 LIST_DEL(&bind_conf->keys_ref->list);
5101 free(bind_conf->keys_ref);
5102 }
5103 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005104 bind_conf->ca_sign_pass = NULL;
5105 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005106}
5107
Christopher Faulet31af49d2015-06-09 17:29:50 +02005108/* Load CA cert file and private key used to generate certificates */
5109int
Willy Tarreau03209342016-12-22 17:08:28 +01005110ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005111{
Willy Tarreau03209342016-12-22 17:08:28 +01005112 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005113 FILE *fp;
5114 X509 *cacert = NULL;
5115 EVP_PKEY *capkey = NULL;
5116 int err = 0;
5117
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005118 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005119 return err;
5120
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005121#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005122 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005123 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005124 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005125 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005126 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005127#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005128
Christopher Faulet31af49d2015-06-09 17:29:50 +02005129 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005130 ha_alert("Proxy '%s': cannot enable certificate generation, "
5131 "no CA certificate File configured at [%s:%d].\n",
5132 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005133 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005134 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005135
5136 /* read in the CA certificate */
5137 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005138 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5139 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005140 goto load_error;
5141 }
5142 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005143 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5144 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005145 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005146 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005147 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005148 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005149 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5150 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005151 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005152 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005153
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005154 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005155 bind_conf->ca_sign_cert = cacert;
5156 bind_conf->ca_sign_pkey = capkey;
5157 return err;
5158
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005159 read_error:
5160 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005161 if (capkey) EVP_PKEY_free(capkey);
5162 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005163 load_error:
5164 bind_conf->generate_certs = 0;
5165 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005166 return err;
5167}
5168
5169/* Release CA cert and private key used to generate certificated */
5170void
5171ssl_sock_free_ca(struct bind_conf *bind_conf)
5172{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005173 if (bind_conf->ca_sign_pkey)
5174 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5175 if (bind_conf->ca_sign_cert)
5176 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005177 bind_conf->ca_sign_pkey = NULL;
5178 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005179}
5180
Emeric Brun46591952012-05-18 15:47:34 +02005181/*
5182 * This function is called if SSL * context is not yet allocated. The function
5183 * is designed to be called before any other data-layer operation and sets the
5184 * handshake flag on the connection. It is safe to call it multiple times.
5185 * It returns 0 on success and -1 in error case.
5186 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005187static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005188{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005189 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005190 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005191 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005192 return 0;
5193
Willy Tarreau3c728722014-01-23 13:50:42 +01005194 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005195 return 0;
5196
Olivier Houchard66ab4982019-02-26 18:37:15 +01005197 ctx = pool_alloc(ssl_sock_ctx_pool);
5198 if (!ctx) {
5199 conn->err_code = CO_ER_SSL_NO_MEM;
5200 return -1;
5201 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005202 ctx->wait_event.tasklet = tasklet_new();
5203 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005204 conn->err_code = CO_ER_SSL_NO_MEM;
5205 pool_free(ssl_sock_ctx_pool, ctx);
5206 return -1;
5207 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005208 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5209 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005210 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005211 ctx->sent_early_data = 0;
Olivier Houchardf6715e72019-12-19 15:02:39 +01005212 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005213 ctx->conn = conn;
Olivier Houchard81284e62019-06-06 13:21:23 +02005214 ctx->send_wait = NULL;
5215 ctx->recv_wait = NULL;
Emeric Brun87cfd662019-09-06 15:36:02 +02005216 ctx->xprt_st = 0;
5217 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005218
5219 /* Only work with sockets for now, this should be adapted when we'll
5220 * add QUIC support.
5221 */
5222 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005223 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005224 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5225 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005226 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005227
Willy Tarreau20879a02012-12-03 16:32:10 +01005228 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5229 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005230 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005231 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005232
Emeric Brun46591952012-05-18 15:47:34 +02005233 /* If it is in client mode initiate SSL session
5234 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005235 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005236 int may_retry = 1;
5237
5238 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005239 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005240 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5241 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005242 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005243 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005244 goto retry_connect;
5245 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005246 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005247 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005248 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005249 ctx->bio = BIO_new(ha_meth);
5250 if (!ctx->bio) {
Olivier Houchardb451b972020-01-24 15:17:38 +01005251 SSL_free(ctx->ssl);
5252 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005253 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005254 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005255 goto retry_connect;
5256 }
Emeric Brun55476152014-11-12 17:35:37 +01005257 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005258 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005259 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005260 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005261 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005262
Evan Broderbe554312013-06-27 00:05:25 -07005263 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005264 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5265 SSL_free(ctx->ssl);
5266 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005267 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005268 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005269 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005270 goto retry_connect;
5271 }
Emeric Brun55476152014-11-12 17:35:37 +01005272 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005273 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005274 }
5275
Olivier Houchard66ab4982019-02-26 18:37:15 +01005276 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005277 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5278 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5279 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 +01005280 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005281 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005282 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5283 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005284 } else if (sess) {
5285 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005286 }
5287 }
Evan Broderbe554312013-06-27 00:05:25 -07005288
Emeric Brun46591952012-05-18 15:47:34 +02005289 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005290 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005291
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005292 _HA_ATOMIC_ADD(&sslconns, 1);
5293 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005294 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005295 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005296 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005297 if (conn->flags & CO_FL_ERROR)
5298 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005299 return 0;
5300 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005301 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005302 int may_retry = 1;
5303
5304 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005305 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005306 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5307 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005308 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005309 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005310 goto retry_accept;
5311 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005312 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005313 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005314 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005315 ctx->bio = BIO_new(ha_meth);
5316 if (!ctx->bio) {
Olivier Houchardb451b972020-01-24 15:17:38 +01005317 SSL_free(ctx->ssl);
5318 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005319 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005320 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005321 goto retry_accept;
5322 }
Emeric Brun55476152014-11-12 17:35:37 +01005323 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005324 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005325 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005326 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005327 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005328
Emeric Brune1f38db2012-09-03 20:36:47 +02005329 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005330 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5331 SSL_free(ctx->ssl);
5332 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005333 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005334 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005335 goto retry_accept;
5336 }
Emeric Brun55476152014-11-12 17:35:37 +01005337 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005338 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005339 }
5340
Frédéric Lécaille583362f2020-01-24 14:56:18 +01005341#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5342 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
5343 b_alloc(&ctx->early_buf);
5344 SSL_set_max_early_data(ctx->ssl,
5345 /* Only allow early data if we managed to allocate
5346 * a buffer.
5347 */
5348 (!b_is_null(&ctx->early_buf)) ?
5349 global.tune.bufsize - global.tune.maxrewrite : 0);
5350 }
5351#endif
5352
Olivier Houchard66ab4982019-02-26 18:37:15 +01005353 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02005354
Emeric Brun46591952012-05-18 15:47:34 +02005355 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005356 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdet510fce52019-08-05 18:04:16 +02005357#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005358 conn->flags |= CO_FL_EARLY_SSL_HS;
5359#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02005360
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005361 _HA_ATOMIC_ADD(&sslconns, 1);
5362 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005363 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005364 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005365 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005366 if (conn->flags & CO_FL_ERROR)
5367 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005368 return 0;
5369 }
5370 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01005371 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005372err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005373 if (ctx && ctx->wait_event.tasklet)
5374 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005375 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02005376 return -1;
5377}
5378
5379
5380/* This is the callback which is used when an SSL handshake is pending. It
5381 * updates the FD status if it wants some polling before being called again.
5382 * It returns 0 if it fails in a fatal way or needs to poll to go further,
5383 * otherwise it returns non-zero and removes itself from the connection's
5384 * flags (the bit is provided in <flag> by the caller).
5385 */
Olivier Houchard000694c2019-05-23 14:45:12 +02005386static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02005387{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005388 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005389 int ret;
5390
Willy Tarreau3c728722014-01-23 13:50:42 +01005391 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005392 return 0;
5393
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005394 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005395 goto out_error;
5396
Willy Tarreau5db847a2019-05-09 14:13:35 +02005397#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02005398 /*
5399 * Check if we have early data. If we do, we have to read them
5400 * before SSL_do_handshake() is called, And there's no way to
5401 * detect early data, except to try to read them
5402 */
5403 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchardf6715e72019-12-19 15:02:39 +01005404 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005405
Olivier Houchardf6715e72019-12-19 15:02:39 +01005406 while (1) {
5407 ret = SSL_read_early_data(ctx->ssl,
5408 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
5409 &read_data);
5410 if (ret == SSL_READ_EARLY_DATA_ERROR)
5411 goto check_error;
5412 if (read_data > 0) {
5413 conn->flags |= CO_FL_EARLY_DATA;
5414 b_add(&ctx->early_buf, read_data);
5415 }
5416 if (ret == SSL_READ_EARLY_DATA_FINISH) {
5417 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5418 if (!b_data(&ctx->early_buf))
5419 b_free(&ctx->early_buf);
5420 break;
5421 }
5422 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005423 }
5424#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005425 /* If we use SSL_do_handshake to process a reneg initiated by
5426 * the remote peer, it sometimes returns SSL_ERROR_SSL.
5427 * Usually SSL_write and SSL_read are used and process implicitly
5428 * the reneg handshake.
5429 * Here we use SSL_peek as a workaround for reneg.
5430 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005431 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005432 char c;
5433
Olivier Houchard66ab4982019-02-26 18:37:15 +01005434 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01005435 if (ret <= 0) {
5436 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005437 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005438
Emeric Brun674b7432012-11-08 19:21:55 +01005439 if (ret == SSL_ERROR_WANT_WRITE) {
5440 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005441 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005442 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005443 return 0;
5444 }
5445 else if (ret == SSL_ERROR_WANT_READ) {
5446 /* handshake may have been completed but we have
5447 * no more data to read.
5448 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005449 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005450 ret = 1;
5451 goto reneg_ok;
5452 }
5453 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005454 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005455 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005456 return 0;
5457 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005458#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005459 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005460 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005461 return 0;
5462 }
5463#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005464 else if (ret == SSL_ERROR_SYSCALL) {
5465 /* if errno is null, then connection was successfully established */
5466 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5467 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01005468 if (!conn->err_code) {
Lukas Tribus5db881f2019-07-08 14:29:15 +02005469#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5470 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005471 conn->err_code = CO_ER_SSL_HANDSHAKE;
5472#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005473 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005474#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus5db881f2019-07-08 14:29:15 +02005475 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005476 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005477 empty_handshake = state == TLS_ST_BEFORE;
5478#else
Lukas Tribus5db881f2019-07-08 14:29:15 +02005479 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5480 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005481#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005482 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02005483 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005484 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005485 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5486 else
5487 conn->err_code = CO_ER_SSL_EMPTY;
5488 }
5489 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005490 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005491 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5492 else
5493 conn->err_code = CO_ER_SSL_ABORT;
5494 }
5495 }
5496 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005497 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005498 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01005499 else
Emeric Brun29f037d2014-04-25 19:05:36 +02005500 conn->err_code = CO_ER_SSL_HANDSHAKE;
5501 }
Lukas Tribus5db881f2019-07-08 14:29:15 +02005502#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01005503 }
Emeric Brun674b7432012-11-08 19:21:55 +01005504 goto out_error;
5505 }
5506 else {
5507 /* Fail on all other handshake errors */
5508 /* Note: OpenSSL may leave unread bytes in the socket's
5509 * buffer, causing an RST to be emitted upon close() on
5510 * TCP sockets. We first try to drain possibly pending
5511 * data to avoid this as much as possible.
5512 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005513 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005514 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005515 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005516 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01005517 goto out_error;
5518 }
5519 }
5520 /* read some data: consider handshake completed */
5521 goto reneg_ok;
5522 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005523 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005524check_error:
Emeric Brun46591952012-05-18 15:47:34 +02005525 if (ret != 1) {
5526 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005527 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005528
5529 if (ret == SSL_ERROR_WANT_WRITE) {
5530 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005531 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005532 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005533 return 0;
5534 }
5535 else if (ret == SSL_ERROR_WANT_READ) {
5536 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02005537 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005538 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5539 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005540 return 0;
5541 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005542#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005543 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005544 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005545 return 0;
5546 }
5547#endif
Willy Tarreau89230192012-09-28 20:22:13 +02005548 else if (ret == SSL_ERROR_SYSCALL) {
5549 /* if errno is null, then connection was successfully established */
5550 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5551 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005552 if (!conn->err_code) {
Lukas Tribus5db881f2019-07-08 14:29:15 +02005553#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5554 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005555 conn->err_code = CO_ER_SSL_HANDSHAKE;
5556#else
5557 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005558#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus5db881f2019-07-08 14:29:15 +02005559 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005560 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005561 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005562#else
Lukas Tribus5db881f2019-07-08 14:29:15 +02005563 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5564 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005565#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005566 if (empty_handshake) {
5567 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005568 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005569 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5570 else
5571 conn->err_code = CO_ER_SSL_EMPTY;
5572 }
5573 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005574 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005575 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5576 else
5577 conn->err_code = CO_ER_SSL_ABORT;
5578 }
Emeric Brun29f037d2014-04-25 19:05:36 +02005579 }
5580 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005581 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005582 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5583 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005584 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02005585 }
Lukas Tribus5db881f2019-07-08 14:29:15 +02005586#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02005587 }
Willy Tarreau89230192012-09-28 20:22:13 +02005588 goto out_error;
5589 }
Emeric Brun46591952012-05-18 15:47:34 +02005590 else {
5591 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02005592 /* Note: OpenSSL may leave unread bytes in the socket's
5593 * buffer, causing an RST to be emitted upon close() on
5594 * TCP sockets. We first try to drain possibly pending
5595 * data to avoid this as much as possible.
5596 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005597 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005598 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005599 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005600 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005601 goto out_error;
5602 }
5603 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005604#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01005605 else {
5606 /*
5607 * If the server refused the early data, we have to send a
5608 * 425 to the client, as we no longer have the data to sent
5609 * them again.
5610 */
5611 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005612 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01005613 conn->err_code = CO_ER_SSL_EARLY_FAILED;
5614 goto out_error;
5615 }
5616 }
5617 }
5618#endif
5619
Emeric Brun46591952012-05-18 15:47:34 +02005620
Emeric Brun674b7432012-11-08 19:21:55 +01005621reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00005622
Willy Tarreau5db847a2019-05-09 14:13:35 +02005623#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005624 /* ASYNC engine API doesn't support moving read/write
5625 * buffers. So we disable ASYNC mode right after
5626 * the handshake to avoid buffer oveflows.
5627 */
5628 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005629 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005630#endif
Emeric Brun46591952012-05-18 15:47:34 +02005631 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005632 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005633 if (objt_server(conn->target)) {
5634 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
5635 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
5636 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02005637 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005638 else {
5639 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
5640 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
5641 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
5642 }
Emeric Brun46591952012-05-18 15:47:34 +02005643 }
5644
5645 /* The connection is now established at both layers, it's time to leave */
5646 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
5647 return 1;
5648
5649 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01005650 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005651 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005652 ERR_clear_error();
5653
Emeric Brun9fa89732012-10-04 17:09:56 +02005654 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02005655 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5656 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5657 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02005658 }
5659
Emeric Brun46591952012-05-18 15:47:34 +02005660 /* Fail on all other handshake errors */
5661 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01005662 if (!conn->err_code)
5663 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005664 return 0;
5665}
5666
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005667static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01005668{
Olivier Houchardea8dd942019-05-20 14:02:16 +02005669 struct wait_event *sw;
5670 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005671
Olivier Houcharda37eb6a2019-06-24 18:57:39 +02005672 if (!ctx)
5673 return -1;
5674
Olivier Houchardea8dd942019-05-20 14:02:16 +02005675 if (event_type & SUB_RETRY_RECV) {
5676 sw = param;
5677 BUG_ON(ctx->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5678 sw->events |= SUB_RETRY_RECV;
5679 ctx->recv_wait = sw;
5680 if (!(conn->flags & CO_FL_SSL_WAIT_HS) &&
5681 !(ctx->wait_event.events & SUB_RETRY_RECV))
5682 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
5683 event_type &= ~SUB_RETRY_RECV;
5684 }
5685 if (event_type & SUB_RETRY_SEND) {
5686sw = param;
5687 BUG_ON(ctx->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5688 sw->events |= SUB_RETRY_SEND;
5689 ctx->send_wait = sw;
5690 if (!(conn->flags & CO_FL_SSL_WAIT_HS) &&
5691 !(ctx->wait_event.events & SUB_RETRY_SEND))
5692 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
5693 event_type &= ~SUB_RETRY_SEND;
5694
5695 }
5696 if (event_type != 0)
5697 return -1;
5698 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01005699}
5700
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005701static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01005702{
Olivier Houchardea8dd942019-05-20 14:02:16 +02005703 struct wait_event *sw;
5704 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005705
Olivier Houchardea8dd942019-05-20 14:02:16 +02005706 if (event_type & SUB_RETRY_RECV) {
5707 sw = param;
5708 BUG_ON(ctx->recv_wait != sw);
5709 ctx->recv_wait = NULL;
5710 sw->events &= ~SUB_RETRY_RECV;
5711 /* If we subscribed, and we're not doing the handshake,
5712 * then we subscribed because the upper layer asked for it,
5713 * as the upper layer is no longer interested, we can
5714 * unsubscribe too.
5715 */
5716 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) &&
5717 (ctx->wait_event.events & SUB_RETRY_RECV))
5718 conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV,
5719 &ctx->wait_event);
5720 }
5721 if (event_type & SUB_RETRY_SEND) {
5722 sw = param;
5723 BUG_ON(ctx->send_wait != sw);
5724 ctx->send_wait = NULL;
5725 sw->events &= ~SUB_RETRY_SEND;
5726 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) &&
5727 (ctx->wait_event.events & SUB_RETRY_SEND))
5728 conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND,
5729 &ctx->wait_event);
5730
5731 }
5732
5733 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01005734}
5735
Olivier Houchard2e055482019-05-27 19:50:12 +02005736/* Use the provided XPRT as an underlying XPRT, and provide the old one.
5737 * Returns 0 on success, and non-zero on failure.
5738 */
5739static 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)
5740{
5741 struct ssl_sock_ctx *ctx = xprt_ctx;
5742
5743 if (oldxprt_ops != NULL)
5744 *oldxprt_ops = ctx->xprt;
5745 if (oldxprt_ctx != NULL)
5746 *oldxprt_ctx = ctx->xprt_ctx;
5747 ctx->xprt = toadd_ops;
5748 ctx->xprt_ctx = toadd_ctx;
5749 return 0;
5750}
5751
Olivier Houchard5149b592019-05-23 17:47:36 +02005752/* Remove the specified xprt. If if it our underlying XPRT, remove it and
5753 * return 0, otherwise just call the remove_xprt method from the underlying
5754 * XPRT.
5755 */
5756static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
5757{
5758 struct ssl_sock_ctx *ctx = xprt_ctx;
5759
5760 if (ctx->xprt_ctx == toremove_ctx) {
5761 ctx->xprt_ctx = newctx;
5762 ctx->xprt = newops;
5763 return 0;
5764 }
5765 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
5766}
5767
Olivier Houchardea8dd942019-05-20 14:02:16 +02005768static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
5769{
5770 struct ssl_sock_ctx *ctx = context;
5771
5772 /* First if we're doing an handshake, try that */
5773 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
5774 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
5775 /* If we had an error, or the handshake is done and I/O is available,
5776 * let the upper layer know.
5777 * If no mux was set up yet, and nobody subscribed, then call
5778 * xprt_done_cb() ourself if it's set, or destroy the connection,
5779 * we can't be sure conn_fd_handler() will be called again.
5780 */
5781 if ((ctx->conn->flags & CO_FL_ERROR) ||
5782 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
5783 int ret = 0;
5784 int woke = 0;
5785
5786 /* On error, wake any waiter */
5787 if (ctx->recv_wait) {
5788 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005789 tasklet_wakeup(ctx->recv_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005790 ctx->recv_wait = NULL;
5791 woke = 1;
5792 }
5793 if (ctx->send_wait) {
5794 ctx->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005795 tasklet_wakeup(ctx->send_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005796 ctx->send_wait = NULL;
5797 woke = 1;
5798 }
5799 /* If we're the first xprt for the connection, let the
5800 * upper layers know. If xprt_done_cb() is set, call it,
5801 * otherwise, we should have a mux, so call its wake
5802 * method if we didn't woke a tasklet already.
5803 */
5804 if (ctx->conn->xprt_ctx == ctx) {
5805 if (ctx->conn->xprt_done_cb)
5806 ret = ctx->conn->xprt_done_cb(ctx->conn);
5807 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
5808 ctx->conn->mux->wake(ctx->conn);
5809 return NULL;
5810 }
5811 }
Olivier Houchardf6715e72019-12-19 15:02:39 +01005812#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5813 /* If we have early data and somebody wants to receive, let them */
5814 else if (b_data(&ctx->early_buf) && ctx->recv_wait) {
5815 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
5816 tasklet_wakeup(ctx->recv_wait->tasklet);
5817 ctx->recv_wait = NULL;
5818
5819 }
5820#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02005821 return NULL;
5822}
5823
Emeric Brun46591952012-05-18 15:47:34 +02005824/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01005825 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02005826 * buffer wraps, in which case a second call may be performed. The connection's
5827 * flags are updated with whatever special event is detected (error, read0,
5828 * empty). The caller is responsible for taking care of those events and
5829 * avoiding the call if inappropriate. The function does not call the
5830 * connection's polling update function, so the caller is responsible for this.
5831 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005832static 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 +02005833{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005834 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02005835 ssize_t ret;
5836 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02005837
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005838 conn_refresh_polling_flags(conn);
5839
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005840 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005841 goto out_error;
5842
Olivier Houchardf6715e72019-12-19 15:02:39 +01005843#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5844 if (b_data(&ctx->early_buf)) {
5845 try = b_contig_space(buf);
5846 if (try > b_data(&ctx->early_buf))
5847 try = b_data(&ctx->early_buf);
5848 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
5849 b_add(buf, try);
5850 b_del(&ctx->early_buf, try);
5851 if (b_data(&ctx->early_buf) == 0)
5852 b_free(&ctx->early_buf);
5853 return try;
5854 }
5855#endif
5856
Emeric Brun46591952012-05-18 15:47:34 +02005857 if (conn->flags & CO_FL_HANDSHAKE)
5858 /* a handshake was requested */
5859 return 0;
5860
Emeric Brun46591952012-05-18 15:47:34 +02005861 /* read the largest possible block. For this, we perform only one call
5862 * to recv() unless the buffer wraps and we exactly fill the first hunk,
5863 * in which case we accept to do it once again. A new attempt is made on
5864 * EINTR too.
5865 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01005866 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02005867
Willy Tarreau591d4452018-06-15 17:21:00 +02005868 try = b_contig_space(buf);
5869 if (!try)
5870 break;
5871
Willy Tarreauabf08d92014-01-14 11:31:27 +01005872 if (try > count)
5873 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02005874
Olivier Houchard66ab4982019-02-26 18:37:15 +01005875 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdet510fce52019-08-05 18:04:16 +02005876
Emeric Brune1f38db2012-09-03 20:36:47 +02005877 if (conn->flags & CO_FL_ERROR) {
5878 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01005879 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02005880 }
Emeric Brun46591952012-05-18 15:47:34 +02005881 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02005882 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005883 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02005884 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02005885 }
Emeric Brun46591952012-05-18 15:47:34 +02005886 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005887 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005888 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01005889 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02005890 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005891 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005892#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005893 /* Async mode can be re-enabled, because we're leaving data state.*/
5894 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005895 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005896#endif
Emeric Brun46591952012-05-18 15:47:34 +02005897 break;
5898 }
5899 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005900 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005901 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5902 SUB_RETRY_RECV,
5903 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01005904 /* handshake is running, and it may need to re-enable read */
5905 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005906#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005907 /* Async mode can be re-enabled, because we're leaving data state.*/
5908 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005909 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005910#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01005911 break;
5912 }
Emeric Brun46591952012-05-18 15:47:34 +02005913 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005914 } else if (ret == SSL_ERROR_ZERO_RETURN)
5915 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005916 /* For SSL_ERROR_SYSCALL, make sure to clear the error
5917 * stack before shutting down the connection for
5918 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005919 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
5920 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02005921 /* otherwise it's a real error */
5922 goto out_error;
5923 }
5924 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005925 leave:
Emeric Brun46591952012-05-18 15:47:34 +02005926 return done;
5927
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005928 clear_ssl_error:
5929 /* Clear openssl global errors stack */
5930 ssl_sock_dump_errors(conn);
5931 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02005932 read0:
5933 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005934 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005935
Emeric Brun46591952012-05-18 15:47:34 +02005936 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005937 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01005938 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005939 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005940 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005941 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02005942}
5943
5944
Willy Tarreau787db9a2018-06-14 18:31:46 +02005945/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5946 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5947 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02005948 * Only one call to send() is performed, unless the buffer wraps, in which case
5949 * a second call may be performed. The connection's flags are updated with
5950 * whatever special event is detected (error, empty). The caller is responsible
5951 * for taking care of those events and avoiding the call if inappropriate. The
5952 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02005953 * is responsible for this. The buffer's output is not adjusted, it's up to the
5954 * caller to take care of this. It's up to the caller to update the buffer's
5955 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02005956 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005957static 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 +02005958{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005959 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02005960 ssize_t ret;
5961 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02005962
5963 done = 0;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005964 conn_refresh_polling_flags(conn);
Emeric Brun46591952012-05-18 15:47:34 +02005965
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005966 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005967 goto out_error;
5968
Olivier Houchard010941f2019-05-03 20:56:19 +02005969 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02005970 /* a handshake was requested */
5971 return 0;
5972
5973 /* send the largest possible block. For this we perform only one call
5974 * to send() unless the buffer wraps and we exactly fill the first hunk,
5975 * in which case we accept to do it once again.
5976 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02005977 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02005978#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005979 size_t written_data;
5980#endif
5981
Willy Tarreau787db9a2018-06-14 18:31:46 +02005982 try = b_contig_data(buf, done);
5983 if (try > count)
5984 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01005985
Willy Tarreau7bed9452014-02-02 02:00:24 +01005986 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005987 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01005988 global_ssl.max_record && try > global_ssl.max_record) {
5989 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01005990 }
5991 else {
5992 /* we need to keep the information about the fact that
5993 * we're not limiting the upcoming send(), because if it
5994 * fails, we'll have to retry with at least as many data.
5995 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005996 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01005997 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01005998
Willy Tarreau5db847a2019-05-09 14:13:35 +02005999#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02006000 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006001 unsigned int max_early;
6002
Olivier Houchard522eea72017-11-03 16:27:47 +01006003 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006004 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006005 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006006 if (SSL_get0_session(ctx->ssl))
6007 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006008 else
6009 max_early = 0;
6010 }
6011
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006012 if (try + ctx->sent_early_data > max_early) {
6013 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006014 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006015 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006016 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006017 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006018 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006019 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006020 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006021 if (ret == 1) {
6022 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006023 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006024 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006025 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006026 /* Initiate the handshake, now */
6027 tasklet_wakeup(ctx->wait_event.tasklet);
6028 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006029
Olivier Houchardc2aae742017-09-22 18:26:28 +02006030 }
6031
6032 } else
6033#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006034 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006035
Emeric Brune1f38db2012-09-03 20:36:47 +02006036 if (conn->flags & CO_FL_ERROR) {
6037 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006038 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006039 }
Emeric Brun46591952012-05-18 15:47:34 +02006040 if (ret > 0) {
Olivier Houchardf24502b2019-01-17 19:09:11 +01006041 /* A send succeeded, so we can consier ourself connected */
6042 conn->flags |= CO_FL_CONNECTED;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006043 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006044 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006045 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006046 }
6047 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006048 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006049
Emeric Brun46591952012-05-18 15:47:34 +02006050 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006051 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006052 /* handshake is running, and it may need to re-enable write */
6053 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006054 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006055#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006056 /* Async mode can be re-enabled, because we're leaving data state.*/
6057 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006058 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006059#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006060 break;
6061 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006062
Emeric Brun46591952012-05-18 15:47:34 +02006063 break;
6064 }
6065 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006066 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006067 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006068 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6069 SUB_RETRY_RECV,
6070 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006071#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006072 /* Async mode can be re-enabled, because we're leaving data state.*/
6073 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006074 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006075#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006076 break;
6077 }
Emeric Brun46591952012-05-18 15:47:34 +02006078 goto out_error;
6079 }
6080 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006081 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006082 return done;
6083
6084 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006085 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006086 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006087 ERR_clear_error();
6088
Emeric Brun46591952012-05-18 15:47:34 +02006089 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006090 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006091}
6092
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006093static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006094
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006095 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006096
Olivier Houchardea8dd942019-05-20 14:02:16 +02006097
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006098 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006099 if (ctx->wait_event.events != 0)
6100 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6101 ctx->wait_event.events,
6102 &ctx->wait_event);
6103 if (ctx->send_wait) {
6104 ctx->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006105 tasklet_wakeup(ctx->send_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006106 }
6107 if (ctx->recv_wait) {
6108 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006109 tasklet_wakeup(ctx->recv_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006110 }
Olivier Houchard692c1d02019-05-23 18:41:47 +02006111 if (ctx->xprt->close)
6112 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006113#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006114 if (global_ssl.async) {
6115 OSSL_ASYNC_FD all_fd[32], afd;
6116 size_t num_all_fds = 0;
6117 int i;
6118
Olivier Houchard66ab4982019-02-26 18:37:15 +01006119 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006120 if (num_all_fds > 32) {
6121 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6122 return;
6123 }
6124
Olivier Houchard66ab4982019-02-26 18:37:15 +01006125 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006126
6127 /* If an async job is pending, we must try to
6128 to catch the end using polling before calling
6129 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006130 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006131 for (i=0 ; i < num_all_fds ; i++) {
6132 /* switch on an handler designed to
6133 * handle the SSL_free
6134 */
6135 afd = all_fd[i];
6136 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006137 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006138 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006139 /* To ensure that the fd cache won't be used
6140 * and we'll catch a real RD event.
6141 */
6142 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006143 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006144 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006145 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006146 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006147 return;
6148 }
Emeric Brun3854e012017-05-17 20:42:48 +02006149 /* Else we can remove the fds from the fdtab
6150 * and call SSL_free.
6151 * note: we do a fd_remove and not a delete
6152 * because the fd is owned by the engine.
6153 * the engine is responsible to close
6154 */
6155 for (i=0 ; i < num_all_fds ; i++)
6156 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006157 }
6158#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006159 SSL_free(ctx->ssl);
Olivier Houchardf6715e72019-12-19 15:02:39 +01006160 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006161 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006162 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006163 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006164 }
Emeric Brun46591952012-05-18 15:47:34 +02006165}
6166
6167/* This function tries to perform a clean shutdown on an SSL connection, and in
6168 * any case, flags the connection as reusable if no handshake was in progress.
6169 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006170static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006171{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006172 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006173
Emeric Brun46591952012-05-18 15:47:34 +02006174 if (conn->flags & CO_FL_HANDSHAKE)
6175 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006176 if (!clean)
6177 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006178 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006179 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006180 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006181 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006182 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006183 ERR_clear_error();
6184 }
Emeric Brun46591952012-05-18 15:47:34 +02006185}
6186
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006187/* used for ppv2 pkey alog (can be used for logging) */
Willy Tarreau83061a82018-07-13 11:56:34 +02006188int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006189{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006190 struct ssl_sock_ctx *ctx;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006191 struct pkey_info *pkinfo;
6192 int bits = 0;
6193 int sig = TLSEXT_signature_anonymous;
6194 int len = -1;
6195
6196 if (!ssl_sock_is_ssl(conn))
6197 return 0;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006198 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006199 pkinfo = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ctx->ssl), ssl_pkey_info_index);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006200 if (pkinfo) {
6201 sig = pkinfo->sig;
6202 bits = pkinfo->bits;
6203 } else {
6204 /* multicert and generated cert have no pkey info */
6205 X509 *crt;
6206 EVP_PKEY *pkey;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006207 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006208 if (!crt)
6209 return 0;
6210 pkey = X509_get_pubkey(crt);
6211 if (pkey) {
6212 bits = EVP_PKEY_bits(pkey);
6213 switch(EVP_PKEY_base_id(pkey)) {
6214 case EVP_PKEY_RSA:
6215 sig = TLSEXT_signature_rsa;
6216 break;
6217 case EVP_PKEY_EC:
6218 sig = TLSEXT_signature_ecdsa;
6219 break;
6220 case EVP_PKEY_DSA:
6221 sig = TLSEXT_signature_dsa;
6222 break;
6223 }
6224 EVP_PKEY_free(pkey);
6225 }
6226 }
6227
6228 switch(sig) {
6229 case TLSEXT_signature_rsa:
6230 len = chunk_printf(out, "RSA%d", bits);
6231 break;
6232 case TLSEXT_signature_ecdsa:
6233 len = chunk_printf(out, "EC%d", bits);
6234 break;
6235 case TLSEXT_signature_dsa:
6236 len = chunk_printf(out, "DSA%d", bits);
6237 break;
6238 default:
6239 return 0;
6240 }
6241 if (len < 0)
6242 return 0;
6243 return 1;
6244}
6245
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006246/* used for ppv2 cert signature (can be used for logging) */
6247const char *ssl_sock_get_cert_sig(struct connection *conn)
6248{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006249 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006250
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006251 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6252 X509 *crt;
6253
6254 if (!ssl_sock_is_ssl(conn))
6255 return NULL;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006256 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006257 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006258 if (!crt)
6259 return NULL;
6260 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6261 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6262}
6263
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006264/* used for ppv2 authority */
6265const char *ssl_sock_get_sni(struct connection *conn)
6266{
6267#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006268 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006269
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006270 if (!ssl_sock_is_ssl(conn))
6271 return NULL;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006272 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006273 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006274#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006275 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006276#endif
6277}
6278
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006279/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006280const char *ssl_sock_get_cipher_name(struct connection *conn)
6281{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006282 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006283
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006284 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006285 return NULL;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006286 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006287 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006288}
6289
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006290/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006291const char *ssl_sock_get_proto_version(struct connection *conn)
6292{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006293 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006294
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006295 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006296 return NULL;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006297 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006298 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006299}
6300
Willy Tarreau8d598402012-10-22 17:58:39 +02006301/* Extract a serial from a cert, and copy it to a chunk.
6302 * Returns 1 if serial is found and copied, 0 if no serial found and
6303 * -1 if output is not large enough.
6304 */
6305static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006306ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006307{
6308 ASN1_INTEGER *serial;
6309
6310 serial = X509_get_serialNumber(crt);
6311 if (!serial)
6312 return 0;
6313
6314 if (out->size < serial->length)
6315 return -1;
6316
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006317 memcpy(out->area, serial->data, serial->length);
6318 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006319 return 1;
6320}
6321
Emeric Brun43e79582014-10-29 19:03:26 +01006322/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006323 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6324 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006325 */
6326static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006327ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006328{
6329 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006330 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01006331
6332 len =i2d_X509(crt, NULL);
6333 if (len <= 0)
6334 return 1;
6335
6336 if (out->size < len)
6337 return -1;
6338
6339 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006340 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01006341 return 1;
6342}
6343
Emeric Brunce5ad802012-10-22 14:11:22 +02006344
Willy Tarreau83061a82018-07-13 11:56:34 +02006345/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02006346 * Returns 1 if serial is found and copied, 0 if no valid time found
6347 * and -1 if output is not large enough.
6348 */
6349static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006350ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02006351{
6352 if (tm->type == V_ASN1_GENERALIZEDTIME) {
6353 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
6354
6355 if (gentm->length < 12)
6356 return 0;
6357 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
6358 return 0;
6359 if (out->size < gentm->length-2)
6360 return -1;
6361
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006362 memcpy(out->area, gentm->data+2, gentm->length-2);
6363 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02006364 return 1;
6365 }
6366 else if (tm->type == V_ASN1_UTCTIME) {
6367 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
6368
6369 if (utctm->length < 10)
6370 return 0;
6371 if (utctm->data[0] >= 0x35)
6372 return 0;
6373 if (out->size < utctm->length)
6374 return -1;
6375
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006376 memcpy(out->area, utctm->data, utctm->length);
6377 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02006378 return 1;
6379 }
6380
6381 return 0;
6382}
6383
Emeric Brun87855892012-10-17 17:39:35 +02006384/* Extract an entry from a X509_NAME and copy its value to an output chunk.
6385 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
6386 */
6387static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006388ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
6389 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006390{
6391 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006392 ASN1_OBJECT *obj;
6393 ASN1_STRING *data;
6394 const unsigned char *data_ptr;
6395 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006396 int i, j, n;
6397 int cur = 0;
6398 const char *s;
6399 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006400 int name_count;
6401
6402 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006403
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006404 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006405 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02006406 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006407 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02006408 else
6409 j = i;
6410
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006411 ne = X509_NAME_get_entry(a, j);
6412 obj = X509_NAME_ENTRY_get_object(ne);
6413 data = X509_NAME_ENTRY_get_data(ne);
6414 data_ptr = ASN1_STRING_get0_data(data);
6415 data_len = ASN1_STRING_length(data);
6416 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006417 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006418 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006419 s = tmp;
6420 }
6421
6422 if (chunk_strcasecmp(entry, s) != 0)
6423 continue;
6424
6425 if (pos < 0)
6426 cur--;
6427 else
6428 cur++;
6429
6430 if (cur != pos)
6431 continue;
6432
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006433 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02006434 return -1;
6435
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006436 memcpy(out->area, data_ptr, data_len);
6437 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006438 return 1;
6439 }
6440
6441 return 0;
6442
6443}
6444
6445/* Extract and format full DN from a X509_NAME and copy result into a chunk
6446 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
6447 */
6448static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006449ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006450{
6451 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006452 ASN1_OBJECT *obj;
6453 ASN1_STRING *data;
6454 const unsigned char *data_ptr;
6455 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006456 int i, n, ln;
6457 int l = 0;
6458 const char *s;
6459 char *p;
6460 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006461 int name_count;
6462
6463
6464 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006465
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006466 out->data = 0;
6467 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006468 for (i = 0; i < name_count; i++) {
6469 ne = X509_NAME_get_entry(a, i);
6470 obj = X509_NAME_ENTRY_get_object(ne);
6471 data = X509_NAME_ENTRY_get_data(ne);
6472 data_ptr = ASN1_STRING_get0_data(data);
6473 data_len = ASN1_STRING_length(data);
6474 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006475 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006476 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006477 s = tmp;
6478 }
6479 ln = strlen(s);
6480
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006481 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006482 if (l > out->size)
6483 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006484 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02006485
6486 *(p++)='/';
6487 memcpy(p, s, ln);
6488 p += ln;
6489 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006490 memcpy(p, data_ptr, data_len);
6491 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006492 }
6493
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006494 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02006495 return 0;
6496
6497 return 1;
6498}
6499
Olivier Houchardab28a322018-12-21 19:45:40 +01006500void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
6501{
6502#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006503 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006504
Olivier Houchardaa2ecea2019-06-28 14:10:33 +02006505 if (!ssl_sock_is_ssl(conn))
6506 return;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006507 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006508 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01006509#endif
6510}
6511
Willy Tarreau119a4082016-12-22 21:58:38 +01006512/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
6513 * to disable SNI.
6514 */
Willy Tarreau63076412015-07-10 11:33:32 +02006515void ssl_sock_set_servername(struct connection *conn, const char *hostname)
6516{
6517#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006518 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006519
Willy Tarreau119a4082016-12-22 21:58:38 +01006520 char *prev_name;
6521
Willy Tarreau63076412015-07-10 11:33:32 +02006522 if (!ssl_sock_is_ssl(conn))
6523 return;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006524 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02006525
Willy Tarreau119a4082016-12-22 21:58:38 +01006526 /* if the SNI changes, we must destroy the reusable context so that a
6527 * new connection will present a new SNI. As an optimization we could
6528 * later imagine having a small cache of ssl_ctx to hold a few SNI per
6529 * server.
6530 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006531 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01006532 if ((!prev_name && hostname) ||
6533 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006534 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01006535
Olivier Houchard66ab4982019-02-26 18:37:15 +01006536 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02006537#endif
6538}
6539
Emeric Brun0abf8362014-06-24 18:26:41 +02006540/* Extract peer certificate's common name into the chunk dest
6541 * Returns
6542 * the len of the extracted common name
6543 * or 0 if no CN found in DN
6544 * or -1 on error case (i.e. no peer certificate)
6545 */
Willy Tarreau83061a82018-07-13 11:56:34 +02006546int ssl_sock_get_remote_common_name(struct connection *conn,
6547 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04006548{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006549 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04006550 X509 *crt = NULL;
6551 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04006552 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02006553 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006554 .area = (char *)&find_cn,
6555 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04006556 };
Emeric Brun0abf8362014-06-24 18:26:41 +02006557 int result = -1;
David Safb76832014-05-08 23:42:08 -04006558
6559 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02006560 goto out;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006561 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04006562
6563 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006564 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006565 if (!crt)
6566 goto out;
6567
6568 name = X509_get_subject_name(crt);
6569 if (!name)
6570 goto out;
David Safb76832014-05-08 23:42:08 -04006571
Emeric Brun0abf8362014-06-24 18:26:41 +02006572 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
6573out:
David Safb76832014-05-08 23:42:08 -04006574 if (crt)
6575 X509_free(crt);
6576
6577 return result;
6578}
6579
Dave McCowan328fb582014-07-30 10:39:13 -04006580/* returns 1 if client passed a certificate for this session, 0 if not */
6581int ssl_sock_get_cert_used_sess(struct connection *conn)
6582{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006583 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006584 X509 *crt = NULL;
6585
6586 if (!ssl_sock_is_ssl(conn))
6587 return 0;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006588 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006589
6590 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006591 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04006592 if (!crt)
6593 return 0;
6594
6595 X509_free(crt);
6596 return 1;
6597}
6598
6599/* returns 1 if client passed a certificate for this connection, 0 if not */
6600int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04006601{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006602 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006603
David Safb76832014-05-08 23:42:08 -04006604 if (!ssl_sock_is_ssl(conn))
6605 return 0;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006606 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006607 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04006608}
6609
6610/* returns result from SSL verify */
6611unsigned int ssl_sock_get_verify_result(struct connection *conn)
6612{
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006613 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006614
David Safb76832014-05-08 23:42:08 -04006615 if (!ssl_sock_is_ssl(conn))
6616 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet4517b0c2019-09-10 10:12:03 +02006617 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006618 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006619}
6620
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006621/* Returns the application layer protocol name in <str> and <len> when known.
6622 * Zero is returned if the protocol name was not found, otherwise non-zero is
6623 * returned. The string is allocated in the SSL context and doesn't have to be
6624 * freed by the caller. NPN is also checked if available since older versions
6625 * of openssl (1.0.1) which are more common in field only support this one.
6626 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006627static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006628{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006629#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
6630 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006631 struct ssl_sock_ctx *ctx = xprt_ctx;
6632 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006633 return 0;
6634
6635 *str = NULL;
6636
6637#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01006638 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006639 if (*str)
6640 return 1;
6641#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01006642#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006643 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006644 if (*str)
6645 return 1;
6646#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006647#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006648 return 0;
6649}
6650
Willy Tarreau7875d092012-09-10 08:20:03 +02006651/***** Below are some sample fetching functions for ACL/patterns *****/
6652
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006653static int
6654smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
6655{
6656 struct connection *conn;
6657
6658 conn = objt_conn(smp->sess->origin);
6659 if (!conn || conn->xprt != &ssl_sock)
6660 return 0;
6661
6662 smp->flags = 0;
6663 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetbb8643c2019-08-07 14:44:49 +02006664#ifdef OPENSSL_IS_BORINGSSL
6665 {
6666 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6667 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
6668 SSL_early_data_accepted(ctx->ssl));
6669 }
6670#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01006671 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard629693f2020-01-23 14:57:36 +01006672 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetbb8643c2019-08-07 14:44:49 +02006673#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006674 return 1;
6675}
6676
Emeric Brune64aef12012-09-21 13:15:06 +02006677/* boolean, returns true if client cert was present */
6678static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006679smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02006680{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006681 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006682 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006683
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006684 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006685 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02006686 return 0;
6687
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006688 ctx = conn->xprt_ctx;
6689
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006690 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02006691 smp->flags |= SMP_F_MAY_CHANGE;
6692 return 0;
6693 }
6694
6695 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006696 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006697 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02006698
6699 return 1;
6700}
6701
Emeric Brun43e79582014-10-29 19:03:26 +01006702/* binary, returns a certificate in a binary chunk (der/raw).
6703 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6704 * should be use.
6705 */
6706static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006707smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01006708{
6709 int cert_peer = (kw[4] == 'c') ? 1 : 0;
6710 X509 *crt = NULL;
6711 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006712 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01006713 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006714 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01006715
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006716 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01006717 if (!conn || conn->xprt != &ssl_sock)
6718 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006719 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01006720
6721 if (!(conn->flags & CO_FL_CONNECTED)) {
6722 smp->flags |= SMP_F_MAY_CHANGE;
6723 return 0;
6724 }
6725
6726 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006727 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01006728 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006729 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01006730
6731 if (!crt)
6732 goto out;
6733
6734 smp_trash = get_trash_chunk();
6735 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
6736 goto out;
6737
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006738 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006739 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01006740 ret = 1;
6741out:
6742 /* SSL_get_peer_certificate, it increase X509 * ref count */
6743 if (cert_peer && crt)
6744 X509_free(crt);
6745 return ret;
6746}
6747
Emeric Brunba841a12014-04-30 17:05:08 +02006748/* binary, returns serial of certificate in a binary chunk.
6749 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6750 * should be use.
6751 */
Willy Tarreau8d598402012-10-22 17:58:39 +02006752static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006753smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02006754{
Emeric Brunba841a12014-04-30 17:05:08 +02006755 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02006756 X509 *crt = NULL;
6757 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006758 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006759 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006760 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006761
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006762 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006763 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02006764 return 0;
6765
Olivier Houchard66ab4982019-02-26 18:37:15 +01006766 ctx = conn->xprt_ctx;
6767
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006768 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02006769 smp->flags |= SMP_F_MAY_CHANGE;
6770 return 0;
6771 }
6772
Emeric Brunba841a12014-04-30 17:05:08 +02006773 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006774 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006775 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006776 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006777
Willy Tarreau8d598402012-10-22 17:58:39 +02006778 if (!crt)
6779 goto out;
6780
Willy Tarreau47ca5452012-12-23 20:22:19 +01006781 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02006782 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
6783 goto out;
6784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006785 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006786 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02006787 ret = 1;
6788out:
Emeric Brunba841a12014-04-30 17:05:08 +02006789 /* SSL_get_peer_certificate, it increase X509 * ref count */
6790 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02006791 X509_free(crt);
6792 return ret;
6793}
Emeric Brune64aef12012-09-21 13:15:06 +02006794
Emeric Brunba841a12014-04-30 17:05:08 +02006795/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
6796 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6797 * should be use.
6798 */
James Votha051b4a2013-05-14 20:37:59 +02006799static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006800smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02006801{
Emeric Brunba841a12014-04-30 17:05:08 +02006802 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02006803 X509 *crt = NULL;
6804 const EVP_MD *digest;
6805 int ret = 0;
Willy Tarreau767e8ad2020-02-25 08:59:23 +01006806 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006807 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006808 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006809 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02006810
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006811 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006812 if (!conn || conn->xprt != &ssl_sock)
6813 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006814 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006815
6816 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02006817 smp->flags |= SMP_F_MAY_CHANGE;
6818 return 0;
6819 }
6820
Emeric Brunba841a12014-04-30 17:05:08 +02006821 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006822 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006823 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006824 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02006825 if (!crt)
6826 goto out;
6827
6828 smp_trash = get_trash_chunk();
6829 digest = EVP_sha1();
Willy Tarreau767e8ad2020-02-25 08:59:23 +01006830 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
6831 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006832 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006833 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02006834 ret = 1;
6835out:
Emeric Brunba841a12014-04-30 17:05:08 +02006836 /* SSL_get_peer_certificate, it increase X509 * ref count */
6837 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02006838 X509_free(crt);
6839 return ret;
6840}
6841
Emeric Brunba841a12014-04-30 17:05:08 +02006842/* string, returns certificate's notafter date in ASN1_UTCTIME format.
6843 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6844 * should be use.
6845 */
Emeric Brunce5ad802012-10-22 14:11:22 +02006846static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006847smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02006848{
Emeric Brunba841a12014-04-30 17:05:08 +02006849 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02006850 X509 *crt = NULL;
6851 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006852 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006853 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006854 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02006855
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006856 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006857 if (!conn || conn->xprt != &ssl_sock)
6858 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006859 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006860
6861 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02006862 smp->flags |= SMP_F_MAY_CHANGE;
6863 return 0;
6864 }
6865
Emeric Brunba841a12014-04-30 17:05:08 +02006866 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006867 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006868 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006869 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02006870 if (!crt)
6871 goto out;
6872
Willy Tarreau47ca5452012-12-23 20:22:19 +01006873 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08006874 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02006875 goto out;
6876
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006877 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006878 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02006879 ret = 1;
6880out:
Emeric Brunba841a12014-04-30 17:05:08 +02006881 /* SSL_get_peer_certificate, it increase X509 * ref count */
6882 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02006883 X509_free(crt);
6884 return ret;
6885}
6886
Emeric Brunba841a12014-04-30 17:05:08 +02006887/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
6888 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6889 * should be use.
6890 */
Emeric Brun87855892012-10-17 17:39:35 +02006891static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006892smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02006893{
Emeric Brunba841a12014-04-30 17:05:08 +02006894 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02006895 X509 *crt = NULL;
6896 X509_NAME *name;
6897 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006898 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006899 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006900 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02006901
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006902 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006903 if (!conn || conn->xprt != &ssl_sock)
6904 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006905 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006906
6907 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02006908 smp->flags |= SMP_F_MAY_CHANGE;
6909 return 0;
6910 }
6911
Emeric Brunba841a12014-04-30 17:05:08 +02006912 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006913 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006914 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006915 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02006916 if (!crt)
6917 goto out;
6918
6919 name = X509_get_issuer_name(crt);
6920 if (!name)
6921 goto out;
6922
Willy Tarreau47ca5452012-12-23 20:22:19 +01006923 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02006924 if (args && args[0].type == ARGT_STR) {
6925 int pos = 1;
6926
6927 if (args[1].type == ARGT_SINT)
6928 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02006929
6930 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
6931 goto out;
6932 }
6933 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
6934 goto out;
6935
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006936 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006937 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02006938 ret = 1;
6939out:
Emeric Brunba841a12014-04-30 17:05:08 +02006940 /* SSL_get_peer_certificate, it increase X509 * ref count */
6941 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02006942 X509_free(crt);
6943 return ret;
6944}
6945
Emeric Brunba841a12014-04-30 17:05:08 +02006946/* string, returns notbefore date in ASN1_UTCTIME format.
6947 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6948 * should be use.
6949 */
Emeric Brunce5ad802012-10-22 14:11:22 +02006950static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006951smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02006952{
Emeric Brunba841a12014-04-30 17:05:08 +02006953 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02006954 X509 *crt = NULL;
6955 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006956 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006957 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006958 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006959
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006960 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006961 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02006962 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006963 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02006964
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006965 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02006966 smp->flags |= SMP_F_MAY_CHANGE;
6967 return 0;
6968 }
6969
Emeric Brunba841a12014-04-30 17:05:08 +02006970 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006971 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006972 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006973 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02006974 if (!crt)
6975 goto out;
6976
Willy Tarreau47ca5452012-12-23 20:22:19 +01006977 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08006978 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02006979 goto out;
6980
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006981 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006982 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02006983 ret = 1;
6984out:
Emeric Brunba841a12014-04-30 17:05:08 +02006985 /* SSL_get_peer_certificate, it increase X509 * ref count */
6986 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02006987 X509_free(crt);
6988 return ret;
6989}
6990
Emeric Brunba841a12014-04-30 17:05:08 +02006991/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
6992 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6993 * should be use.
6994 */
Emeric Brun87855892012-10-17 17:39:35 +02006995static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006996smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02006997{
Emeric Brunba841a12014-04-30 17:05:08 +02006998 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02006999 X509 *crt = NULL;
7000 X509_NAME *name;
7001 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007002 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007003 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007004 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007005
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007006 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007007 if (!conn || conn->xprt != &ssl_sock)
7008 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007009 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007010
7011 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007012 smp->flags |= SMP_F_MAY_CHANGE;
7013 return 0;
7014 }
7015
Emeric Brunba841a12014-04-30 17:05:08 +02007016 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007017 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007018 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007019 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007020 if (!crt)
7021 goto out;
7022
7023 name = X509_get_subject_name(crt);
7024 if (!name)
7025 goto out;
7026
Willy Tarreau47ca5452012-12-23 20:22:19 +01007027 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02007028 if (args && args[0].type == ARGT_STR) {
7029 int pos = 1;
7030
7031 if (args[1].type == ARGT_SINT)
7032 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007033
7034 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7035 goto out;
7036 }
7037 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7038 goto out;
7039
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007040 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007041 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007042 ret = 1;
7043out:
Emeric Brunba841a12014-04-30 17:05:08 +02007044 /* SSL_get_peer_certificate, it increase X509 * ref count */
7045 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007046 X509_free(crt);
7047 return ret;
7048}
Emeric Brun9143d372012-12-20 15:44:16 +01007049
7050/* integer, returns true if current session use a client certificate */
7051static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007052smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007053{
7054 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007055 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007056 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007057
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007058 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007059 if (!conn || conn->xprt != &ssl_sock)
7060 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007061 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007062
7063 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01007064 smp->flags |= SMP_F_MAY_CHANGE;
7065 return 0;
7066 }
7067
7068 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007069 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007070 if (crt) {
7071 X509_free(crt);
7072 }
7073
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007074 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007075 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007076 return 1;
7077}
7078
Emeric Brunba841a12014-04-30 17:05:08 +02007079/* integer, returns the certificate version
7080 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7081 * should be use.
7082 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007083static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007084smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007085{
Emeric Brunba841a12014-04-30 17:05:08 +02007086 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007087 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007088 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007089 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007090
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007091 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007092 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007093 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007094 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007095
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007096 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007097 smp->flags |= SMP_F_MAY_CHANGE;
7098 return 0;
7099 }
7100
Emeric Brunba841a12014-04-30 17:05:08 +02007101 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007102 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007103 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007104 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007105 if (!crt)
7106 return 0;
7107
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007108 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007109 /* SSL_get_peer_certificate increase X509 * ref count */
7110 if (cert_peer)
7111 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007112 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007113
7114 return 1;
7115}
7116
Emeric Brunba841a12014-04-30 17:05:08 +02007117/* string, returns the certificate's signature algorithm.
7118 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7119 * should be use.
7120 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007121static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007122smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007123{
Emeric Brunba841a12014-04-30 17:05:08 +02007124 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007125 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007126 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007127 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007128 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007129 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007130
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007131 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007132 if (!conn || conn->xprt != &ssl_sock)
7133 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007134 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007135
7136 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007137 smp->flags |= SMP_F_MAY_CHANGE;
7138 return 0;
7139 }
7140
Emeric Brunba841a12014-04-30 17:05:08 +02007141 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007142 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007143 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007144 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007145 if (!crt)
7146 return 0;
7147
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007148 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7149 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007150
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007151 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7152 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007153 /* SSL_get_peer_certificate increase X509 * ref count */
7154 if (cert_peer)
7155 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007156 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007157 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007158
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007159 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007160 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007161 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007162 /* SSL_get_peer_certificate increase X509 * ref count */
7163 if (cert_peer)
7164 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007165
7166 return 1;
7167}
7168
Emeric Brunba841a12014-04-30 17:05:08 +02007169/* string, returns the certificate's key algorithm.
7170 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7171 * should be use.
7172 */
Emeric Brun521a0112012-10-22 12:22:55 +02007173static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007174smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02007175{
Emeric Brunba841a12014-04-30 17:05:08 +02007176 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02007177 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007178 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02007179 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007180 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007181 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02007182
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007183 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007184 if (!conn || conn->xprt != &ssl_sock)
7185 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007186 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007187
7188 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02007189 smp->flags |= SMP_F_MAY_CHANGE;
7190 return 0;
7191 }
7192
Emeric Brunba841a12014-04-30 17:05:08 +02007193 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007194 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007195 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007196 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02007197 if (!crt)
7198 return 0;
7199
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007200 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
7201 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02007202
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007203 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7204 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007205 /* SSL_get_peer_certificate increase X509 * ref count */
7206 if (cert_peer)
7207 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007208 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007209 }
Emeric Brun521a0112012-10-22 12:22:55 +02007210
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007211 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007212 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007213 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007214 if (cert_peer)
7215 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007216
7217 return 1;
7218}
7219
Emeric Brun645ae792014-04-30 14:21:06 +02007220/* boolean, returns true if front conn. transport layer is SSL.
7221 * This function is also usable on backend conn if the fetch keyword 5th
7222 * char is 'b'.
7223 */
Willy Tarreau7875d092012-09-10 08:20:03 +02007224static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007225smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007226{
Emeric Bruneb8def92018-02-19 15:59:48 +01007227 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7228 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007229
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007230 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007231 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007232 return 1;
7233}
7234
Emeric Brun2525b6b2012-10-18 15:59:43 +02007235/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007236static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007237smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007238{
7239#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007240 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007241 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007242
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007243 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007244 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007245 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007246 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007247 return 1;
7248#else
7249 return 0;
7250#endif
7251}
7252
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007253/* boolean, returns true if client session has been resumed.
7254 * This function is also usable on backend conn if the fetch keyword 5th
7255 * char is 'b'.
7256 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007257static int
7258smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
7259{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007260 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7261 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007262 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007263
Nenad Merdanovic26ea8222015-05-18 02:28:57 +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) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007267 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007268 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007269 return 1;
7270}
7271
Emeric Brun645ae792014-04-30 14:21:06 +02007272/* string, returns the used cipher if front conn. transport layer is SSL.
7273 * This function is also usable on backend conn if the fetch keyword 5th
7274 * char is 'b'.
7275 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007276static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007277smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007278{
Emeric Bruneb8def92018-02-19 15:59:48 +01007279 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7280 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007281 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007282
Willy Tarreaube508f12016-03-10 11:47:01 +01007283 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007284 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007285 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007286 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007287
Olivier Houchard66ab4982019-02-26 18:37:15 +01007288 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007289 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007290 return 0;
7291
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007292 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007293 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007294 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007295
7296 return 1;
7297}
7298
Emeric Brun645ae792014-04-30 14:21:06 +02007299/* integer, returns the algoritm's keysize if front conn. transport layer
7300 * is SSL.
7301 * This function is also usable on backend conn if the fetch keyword 5th
7302 * char is 'b'.
7303 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007304static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007305smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007306{
Emeric Bruneb8def92018-02-19 15:59:48 +01007307 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7308 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007309 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007310 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01007311
Emeric Brun589fcad2012-10-16 14:13:26 +02007312 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007313 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007314 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007315 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007316
Olivier Houchard66ab4982019-02-26 18:37:15 +01007317 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007318 return 0;
7319
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007320 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007321 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007322
7323 return 1;
7324}
7325
Emeric Brun645ae792014-04-30 14:21:06 +02007326/* integer, returns the used keysize if front conn. transport layer is SSL.
7327 * This function is also usable on backend conn if the fetch keyword 5th
7328 * char is 'b'.
7329 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007330static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007331smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007332{
Emeric Bruneb8def92018-02-19 15:59:48 +01007333 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7334 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007335 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007336
Emeric Brun589fcad2012-10-16 14:13:26 +02007337 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007338 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7339 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007340 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007341
Olivier Houchard66ab4982019-02-26 18:37:15 +01007342 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007343 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02007344 return 0;
7345
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007346 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007347
7348 return 1;
7349}
7350
Bernard Spil13c53f82018-02-15 13:34:58 +01007351#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02007352static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007353smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007354{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007355 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007356 struct ssl_sock_ctx *ctx;
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007357 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007358
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007359 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007360 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007361
Olivier Houchard6b77f492018-11-22 18:18:29 +01007362 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
7363 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007364 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7365 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007366 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007367
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007368 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007369 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007370 (const unsigned char **)&smp->data.u.str.area,
7371 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02007372
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007373 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007374 return 0;
7375
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007376 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007377 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007378}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007379#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02007380
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01007381#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02007382static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007383smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02007384{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007385 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007386 struct ssl_sock_ctx *ctx;
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007387 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007388
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007389 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007390 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02007391
Olivier Houchard6b77f492018-11-22 18:18:29 +01007392 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
7393 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7394
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007395 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02007396 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007397 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02007398
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007399 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007400 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007401 (const unsigned char **)&smp->data.u.str.area,
7402 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02007403
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007404 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02007405 return 0;
7406
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007407 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02007408 return 1;
7409}
7410#endif
7411
Emeric Brun645ae792014-04-30 14:21:06 +02007412/* string, returns the used protocol if front conn. transport layer is SSL.
7413 * This function is also usable on backend conn if the fetch keyword 5th
7414 * char is 'b'.
7415 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02007416static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007417smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007418{
Emeric Bruneb8def92018-02-19 15:59:48 +01007419 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7420 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007421 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007422
Emeric Brun589fcad2012-10-16 14:13:26 +02007423 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007424 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7425 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007426 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007427
Olivier Houchard66ab4982019-02-26 18:37:15 +01007428 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007429 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007430 return 0;
7431
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007432 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007433 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007434 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007435
7436 return 1;
7437}
7438
Willy Tarreau87b09662015-04-03 00:22:06 +02007439/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02007440 * This function is also usable on backend conn if the fetch keyword 5th
7441 * char is 'b'.
7442 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007443#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02007444static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007445smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02007446{
Emeric Bruneb8def92018-02-19 15:59:48 +01007447 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7448 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007449 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007450 struct ssl_sock_ctx *ctx;
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007451 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01007452
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007453 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007454 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02007455
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007456 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7457 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007458 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007459
Olivier Houchard66ab4982019-02-26 18:37:15 +01007460 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02007461 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02007462 return 0;
7463
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007464 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Dragan Dosen9dc47c42020-05-04 09:07:28 +02007465 if (!smp->data.u.str.area || !len)
Emeric Brunfe68f682012-10-16 14:59:28 +02007466 return 0;
7467
Willy Tarreau767e8ad2020-02-25 08:59:23 +01007468 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02007469 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02007470}
Patrick Hemmer41966772018-04-28 19:15:48 -04007471#endif
7472
Emeric Brunfe68f682012-10-16 14:59:28 +02007473
Emmanuel Hocdet839af572019-05-14 16:27:35 +02007474#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04007475static int
Patrick Hemmer65674662019-06-04 08:13:03 -04007476smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
7477{
7478 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7479 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7480 struct buffer *data;
7481 struct ssl_sock_ctx *ctx;
7482
7483 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7484 return 0;
7485 ctx = conn->xprt_ctx;
7486
7487 data = get_trash_chunk();
7488 if (kw[7] == 'c')
7489 data->data = SSL_get_client_random(ctx->ssl,
7490 (unsigned char *) data->area,
7491 data->size);
7492 else
7493 data->data = SSL_get_server_random(ctx->ssl,
7494 (unsigned char *) data->area,
7495 data->size);
7496 if (!data->data)
7497 return 0;
7498
7499 smp->flags = 0;
7500 smp->data.type = SMP_T_BIN;
7501 smp->data.u.str = *data;
7502
7503 return 1;
7504}
7505
7506static int
Patrick Hemmere0275472018-04-28 19:15:51 -04007507smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
7508{
7509 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7510 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7511 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02007512 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007513 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04007514
7515 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7516 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007517 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04007518
Olivier Houchard66ab4982019-02-26 18:37:15 +01007519 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04007520 if (!ssl_sess)
7521 return 0;
7522
7523 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007524 data->data = SSL_SESSION_get_master_key(ssl_sess,
7525 (unsigned char *) data->area,
7526 data->size);
7527 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04007528 return 0;
7529
7530 smp->flags = 0;
7531 smp->data.type = SMP_T_BIN;
7532 smp->data.u.str = *data;
7533
7534 return 1;
7535}
7536#endif
7537
Patrick Hemmer41966772018-04-28 19:15:48 -04007538#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02007539static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007540smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007541{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007542 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007543 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007544
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007545 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007546 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02007547
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007548 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007549 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7550 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007551 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007552
Olivier Houchard66ab4982019-02-26 18:37:15 +01007553 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007554 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02007555 return 0;
7556
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007557 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02007558 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02007559}
Patrick Hemmer41966772018-04-28 19:15:48 -04007560#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02007561
David Sc1ad52e2014-04-08 18:48:47 -04007562static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007563smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
7564{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007565 struct connection *conn;
7566 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007567 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007568
7569 conn = objt_conn(smp->sess->origin);
7570 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7571 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007572 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007573
Olivier Houchard66ab4982019-02-26 18:37:15 +01007574 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007575 if (!capture)
7576 return 0;
7577
7578 smp->flags = SMP_F_CONST;
7579 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007580 smp->data.u.str.area = capture->ciphersuite;
7581 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007582 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007583}
7584
7585static int
7586smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
7587{
Willy Tarreau83061a82018-07-13 11:56:34 +02007588 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007589
7590 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
7591 return 0;
7592
7593 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007594 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007595 smp->data.type = SMP_T_BIN;
7596 smp->data.u.str = *data;
7597 return 1;
7598}
7599
7600static int
7601smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
7602{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007603 struct connection *conn;
7604 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007605 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007606
7607 conn = objt_conn(smp->sess->origin);
7608 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7609 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007610 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007611
Olivier Houchard66ab4982019-02-26 18:37:15 +01007612 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007613 if (!capture)
7614 return 0;
7615
7616 smp->data.type = SMP_T_SINT;
7617 smp->data.u.sint = capture->xxh64;
7618 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007619}
7620
7621static int
7622smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
7623{
Willy Tarreau5db847a2019-05-09 14:13:35 +02007624#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02007625 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007626 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007627
7628 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
7629 return 0;
7630
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007631 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007632 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007633 const char *str;
7634 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007635 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007636 uint16_t id = (bin[0] << 8) | bin[1];
7637#if defined(OPENSSL_IS_BORINGSSL)
7638 cipher = SSL_get_cipher_by_value(id);
7639#else
Willy Tarreaub7290772018-10-15 11:01:59 +02007640 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007641 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7642 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007643#endif
7644 str = SSL_CIPHER_get_name(cipher);
7645 if (!str || strcmp(str, "(NONE)") == 0)
7646 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007647 else
7648 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
7649 }
7650 smp->data.type = SMP_T_STR;
7651 smp->data.u.str = *data;
7652 return 1;
7653#else
7654 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
7655#endif
7656}
7657
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007658#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007659static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007660smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04007661{
Emeric Bruneb8def92018-02-19 15:59:48 +01007662 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7663 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04007664 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02007665 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007666 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04007667
7668 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04007669 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7670 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007671 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04007672
7673 if (!(conn->flags & CO_FL_CONNECTED)) {
7674 smp->flags |= SMP_F_MAY_CHANGE;
7675 return 0;
7676 }
7677
7678 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01007679 if (!SSL_session_reused(ctx->ssl))
7680 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007681 finished_trash->area,
7682 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04007683 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007684 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007685 finished_trash->area,
7686 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04007687
7688 if (!finished_len)
7689 return 0;
7690
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007691 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007692 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007693 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04007694
7695 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04007696}
Patrick Hemmer41966772018-04-28 19:15:48 -04007697#endif
David Sc1ad52e2014-04-08 18:48:47 -04007698
Emeric Brun2525b6b2012-10-18 15:59:43 +02007699/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02007700static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007701smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02007702{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007703 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007704 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007705
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007706 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007707 if (!conn || conn->xprt != &ssl_sock)
7708 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007709 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007710
7711 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007712 smp->flags = SMP_F_MAY_CHANGE;
7713 return 0;
7714 }
7715
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007716 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007717 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007718 smp->flags = 0;
7719
7720 return 1;
7721}
7722
Emeric Brun2525b6b2012-10-18 15:59:43 +02007723/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02007724static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007725smp_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 +02007726{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007727 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007728 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007729
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007730 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007731 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02007732 return 0;
7733
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007734 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007735 smp->flags = SMP_F_MAY_CHANGE;
7736 return 0;
7737 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007738 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02007739
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007740 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007741 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007742 smp->flags = 0;
7743
7744 return 1;
7745}
7746
Emeric Brun2525b6b2012-10-18 15:59:43 +02007747/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02007748static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007749smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02007750{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007751 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007752 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007753
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007754 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007755 if (!conn || conn->xprt != &ssl_sock)
7756 return 0;
7757
7758 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007759 smp->flags = SMP_F_MAY_CHANGE;
7760 return 0;
7761 }
7762
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007763 ctx = conn->xprt_ctx;
7764
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007765 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007766 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007767 smp->flags = 0;
7768
7769 return 1;
7770}
7771
Emeric Brun2525b6b2012-10-18 15:59:43 +02007772/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007773static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007774smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007775{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007776 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007777 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007778
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007779 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007780 if (!conn || conn->xprt != &ssl_sock)
7781 return 0;
7782
7783 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007784 smp->flags = SMP_F_MAY_CHANGE;
7785 return 0;
7786 }
7787
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007788 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007789 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007790 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007791
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007792 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007793 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007794 smp->flags = 0;
7795
7796 return 1;
7797}
7798
Emeric Brunfb510ea2012-10-05 12:00:26 +02007799/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007800static 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 +02007801{
7802 if (!*args[cur_arg + 1]) {
7803 if (err)
7804 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
7805 return ERR_ALERT | ERR_FATAL;
7806 }
7807
Willy Tarreauef934602016-12-22 23:12:01 +01007808 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7809 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007810 else
7811 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007812
Emeric Brund94b3fe2012-09-20 18:23:56 +02007813 return 0;
7814}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007815static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7816{
7817 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
7818}
Emeric Brund94b3fe2012-09-20 18:23:56 +02007819
Christopher Faulet31af49d2015-06-09 17:29:50 +02007820/* parse the "ca-sign-file" bind keyword */
7821static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7822{
7823 if (!*args[cur_arg + 1]) {
7824 if (err)
7825 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
7826 return ERR_ALERT | ERR_FATAL;
7827 }
7828
Willy Tarreauef934602016-12-22 23:12:01 +01007829 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7830 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02007831 else
7832 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
7833
7834 return 0;
7835}
7836
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007837/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02007838static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7839{
7840 if (!*args[cur_arg + 1]) {
7841 if (err)
7842 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
7843 return ERR_ALERT | ERR_FATAL;
7844 }
7845 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
7846 return 0;
7847}
7848
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007849/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007850static 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 +02007851{
7852 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007853 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007854 return ERR_ALERT | ERR_FATAL;
7855 }
7856
Emeric Brun76d88952012-10-05 15:47:31 +02007857 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02007858 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007859 return 0;
7860}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007861static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7862{
7863 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
7864}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007865
Emmanuel Hocdet839af572019-05-14 16:27:35 +02007866#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007867/* parse the "ciphersuites" bind keyword */
7868static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7869{
7870 if (!*args[cur_arg + 1]) {
7871 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
7872 return ERR_ALERT | ERR_FATAL;
7873 }
7874
7875 free(conf->ciphersuites);
7876 conf->ciphersuites = strdup(args[cur_arg + 1]);
7877 return 0;
7878}
7879static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7880{
7881 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
7882}
7883#endif
7884
Willy Tarreaub131c872019-10-16 16:42:19 +02007885/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02007886static 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 +02007887{
Willy Tarreau38011032013-08-13 16:59:39 +02007888 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02007889
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007890 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007891 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007892 return ERR_ALERT | ERR_FATAL;
7893 }
7894
Willy Tarreauef934602016-12-22 23:12:01 +01007895 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
7896 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02007897 memprintf(err, "'%s' : path too long", args[cur_arg]);
7898 return ERR_ALERT | ERR_FATAL;
7899 }
Willy Tarreauef934602016-12-22 23:12:01 +01007900 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaub131c872019-10-16 16:42:19 +02007901 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007902 }
7903
Willy Tarreaub131c872019-10-16 16:42:19 +02007904 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02007905}
7906
Willy Tarreaub131c872019-10-16 16:42:19 +02007907/* 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 +01007908static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7909{
Willy Tarreaub131c872019-10-16 16:42:19 +02007910 int err_code;
7911
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007912 if (!*args[cur_arg + 1]) {
7913 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
7914 return ERR_ALERT | ERR_FATAL;
7915 }
7916
Willy Tarreaub131c872019-10-16 16:42:19 +02007917 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err);
7918 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02007919 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007920
Willy Tarreaub131c872019-10-16 16:42:19 +02007921 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007922}
7923
Emeric Brunfb510ea2012-10-05 12:00:26 +02007924/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007925static 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 +02007926{
Emeric Brun051cdab2012-10-02 19:25:50 +02007927#ifndef X509_V_FLAG_CRL_CHECK
7928 if (err)
7929 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
7930 return ERR_ALERT | ERR_FATAL;
7931#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02007932 if (!*args[cur_arg + 1]) {
7933 if (err)
7934 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
7935 return ERR_ALERT | ERR_FATAL;
7936 }
Emeric Brun2b58d042012-09-20 17:10:03 +02007937
Willy Tarreauef934602016-12-22 23:12:01 +01007938 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7939 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007940 else
7941 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007942
Emeric Brun2b58d042012-09-20 17:10:03 +02007943 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02007944#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02007945}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007946static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7947{
7948 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
7949}
Emeric Brun2b58d042012-09-20 17:10:03 +02007950
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007951/* parse the "curves" bind keyword keyword */
7952static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7953{
Lukas Tribusd13e9252019-11-24 18:20:40 +01007954#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007955 if (!*args[cur_arg + 1]) {
7956 if (err)
7957 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
7958 return ERR_ALERT | ERR_FATAL;
7959 }
7960 conf->curves = strdup(args[cur_arg + 1]);
7961 return 0;
7962#else
7963 if (err)
7964 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
7965 return ERR_ALERT | ERR_FATAL;
7966#endif
7967}
7968static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7969{
7970 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
7971}
7972
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007973/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007974static 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 +02007975{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007976#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Emeric Brun2b58d042012-09-20 17:10:03 +02007977 if (err)
7978 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
7979 return ERR_ALERT | ERR_FATAL;
7980#elif defined(OPENSSL_NO_ECDH)
7981 if (err)
7982 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
7983 return ERR_ALERT | ERR_FATAL;
7984#else
7985 if (!*args[cur_arg + 1]) {
7986 if (err)
7987 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
7988 return ERR_ALERT | ERR_FATAL;
7989 }
7990
7991 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007992
7993 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02007994#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007995}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007996static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7997{
7998 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
7999}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008000
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008001/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02008002static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8003{
8004 int code;
8005 char *p = args[cur_arg + 1];
8006 unsigned long long *ignerr = &conf->crt_ignerr;
8007
8008 if (!*p) {
8009 if (err)
8010 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
8011 return ERR_ALERT | ERR_FATAL;
8012 }
8013
8014 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
8015 ignerr = &conf->ca_ignerr;
8016
8017 if (strcmp(p, "all") == 0) {
8018 *ignerr = ~0ULL;
8019 return 0;
8020 }
8021
8022 while (p) {
8023 code = atoi(p);
8024 if ((code <= 0) || (code > 63)) {
8025 if (err)
8026 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
8027 args[cur_arg], code, args[cur_arg + 1]);
8028 return ERR_ALERT | ERR_FATAL;
8029 }
8030 *ignerr |= 1ULL << code;
8031 p = strchr(p, ',');
8032 if (p)
8033 p++;
8034 }
8035
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008036 return 0;
8037}
8038
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008039/* parse tls_method_options "no-xxx" and "force-xxx" */
8040static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008041{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008042 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008043 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008044 p = strchr(arg, '-');
8045 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008046 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008047 p++;
8048 if (!strcmp(p, "sslv3"))
8049 v = CONF_SSLV3;
8050 else if (!strcmp(p, "tlsv10"))
8051 v = CONF_TLSV10;
8052 else if (!strcmp(p, "tlsv11"))
8053 v = CONF_TLSV11;
8054 else if (!strcmp(p, "tlsv12"))
8055 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008056 else if (!strcmp(p, "tlsv13"))
8057 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008058 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008059 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008060 if (!strncmp(arg, "no-", 3))
8061 methods->flags |= methodVersions[v].flag;
8062 else if (!strncmp(arg, "force-", 6))
8063 methods->min = methods->max = v;
8064 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008065 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008066 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008067 fail:
8068 if (err)
8069 memprintf(err, "'%s' : option not implemented", arg);
8070 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008071}
8072
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008073static 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 +02008074{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008075 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008076}
8077
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008078static 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 +02008079{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008080 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8081}
8082
8083/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8084static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8085{
8086 uint16_t i, v = 0;
8087 char *argv = args[cur_arg + 1];
8088 if (!*argv) {
8089 if (err)
8090 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
8091 return ERR_ALERT | ERR_FATAL;
8092 }
8093 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8094 if (!strcmp(argv, methodVersions[i].name))
8095 v = i;
8096 if (!v) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008097 if (err)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008098 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008099 return ERR_ALERT | ERR_FATAL;
8100 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008101 if (!strcmp("ssl-min-ver", args[cur_arg]))
8102 methods->min = v;
8103 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8104 methods->max = v;
8105 else {
8106 if (err)
8107 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
8108 return ERR_ALERT | ERR_FATAL;
8109 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008110 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008111}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008112
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008113static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8114{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008115#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008116 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 +02008117#endif
8118 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8119}
8120
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008121static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8122{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008123 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008124}
8125
8126static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8127{
8128 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8129}
8130
Emeric Brun2d0c4822012-10-02 13:45:20 +02008131/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008132static 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 +02008133{
Emeric Brun89675492012-10-05 13:48:26 +02008134 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008135 return 0;
8136}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008137
Olivier Houchardc2aae742017-09-22 18:26:28 +02008138/* parse the "allow-0rtt" bind keyword */
8139static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8140{
8141 conf->early_data = 1;
8142 return 0;
8143}
8144
8145static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8146{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008147 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008148 return 0;
8149}
8150
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008151/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008152static 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 +02008153{
Bernard Spil13c53f82018-02-15 13:34:58 +01008154#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008155 char *p1, *p2;
8156
8157 if (!*args[cur_arg + 1]) {
8158 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8159 return ERR_ALERT | ERR_FATAL;
8160 }
8161
8162 free(conf->npn_str);
8163
Willy Tarreau3724da12016-02-12 17:11:12 +01008164 /* the NPN string is built as a suite of (<len> <name>)*,
8165 * so we reuse each comma to store the next <len> and need
8166 * one more for the end of the string.
8167 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008168 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008169 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008170 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8171
8172 /* replace commas with the name length */
8173 p1 = conf->npn_str;
8174 p2 = p1 + 1;
8175 while (1) {
8176 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8177 if (!p2)
8178 p2 = p1 + 1 + strlen(p1 + 1);
8179
8180 if (p2 - (p1 + 1) > 255) {
8181 *p2 = '\0';
8182 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8183 return ERR_ALERT | ERR_FATAL;
8184 }
8185
8186 *p1 = p2 - (p1 + 1);
8187 p1 = p2;
8188
8189 if (!*p2)
8190 break;
8191
8192 *(p2++) = '\0';
8193 }
8194 return 0;
8195#else
8196 if (err)
8197 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
8198 return ERR_ALERT | ERR_FATAL;
8199#endif
8200}
8201
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008202static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8203{
8204 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8205}
8206
Willy Tarreauab861d32013-04-02 02:30:41 +02008207/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008208static 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 +02008209{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008210#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008211 char *p1, *p2;
8212
8213 if (!*args[cur_arg + 1]) {
8214 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
8215 return ERR_ALERT | ERR_FATAL;
8216 }
8217
8218 free(conf->alpn_str);
8219
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008220 /* the ALPN string is built as a suite of (<len> <name>)*,
8221 * so we reuse each comma to store the next <len> and need
8222 * one more for the end of the string.
8223 */
Willy Tarreauab861d32013-04-02 02:30:41 +02008224 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008225 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02008226 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
8227
8228 /* replace commas with the name length */
8229 p1 = conf->alpn_str;
8230 p2 = p1 + 1;
8231 while (1) {
8232 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
8233 if (!p2)
8234 p2 = p1 + 1 + strlen(p1 + 1);
8235
8236 if (p2 - (p1 + 1) > 255) {
8237 *p2 = '\0';
8238 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8239 return ERR_ALERT | ERR_FATAL;
8240 }
8241
8242 *p1 = p2 - (p1 + 1);
8243 p1 = p2;
8244
8245 if (!*p2)
8246 break;
8247
8248 *(p2++) = '\0';
8249 }
8250 return 0;
8251#else
8252 if (err)
8253 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
8254 return ERR_ALERT | ERR_FATAL;
8255#endif
8256}
8257
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008258static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8259{
8260 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8261}
8262
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008263/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008264static 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 +02008265{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01008266 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02008267 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02008268
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008269 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
8270 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008271#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008272 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
8273 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
8274#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008275 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008276 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
8277 if (!conf->ssl_conf.ssl_methods.min)
8278 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
8279 if (!conf->ssl_conf.ssl_methods.max)
8280 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02008281
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008282 return 0;
8283}
8284
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008285/* parse the "prefer-client-ciphers" bind keyword */
8286static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8287{
8288 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
8289 return 0;
8290}
8291
Christopher Faulet31af49d2015-06-09 17:29:50 +02008292/* parse the "generate-certificates" bind keyword */
8293static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8294{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01008295#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02008296 conf->generate_certs = 1;
8297#else
8298 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
8299 err && *err ? *err : "");
8300#endif
8301 return 0;
8302}
8303
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008304/* parse the "strict-sni" bind keyword */
8305static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8306{
8307 conf->strict_sni = 1;
8308 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008309}
8310
8311/* parse the "tls-ticket-keys" bind keyword */
8312static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8313{
8314#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008315 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008316 int i = 0;
8317 char thisline[LINESIZE];
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008318 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008319
8320 if (!*args[cur_arg + 1]) {
8321 if (err)
8322 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008323 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008324 }
8325
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008326 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008327 if (keys_ref) {
8328 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008329 conf->keys_ref = keys_ref;
8330 return 0;
8331 }
8332
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008333 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01008334 if (!keys_ref) {
8335 if (err)
8336 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008337 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008338 }
8339
Emeric Brun9e754772019-01-10 17:51:55 +01008340 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01008341 if (!keys_ref->tlskeys) {
Emeric Brun09852f72019-01-10 10:51:13 +01008342 if (err)
8343 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008344 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008345 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008346
8347 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
8348 if (err)
8349 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008350 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008351 }
8352
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008353 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01008354 if (!keys_ref->filename) {
Emeric Brun09852f72019-01-10 10:51:13 +01008355 if (err)
8356 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008357 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008358 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008359
Emeric Brun9e754772019-01-10 17:51:55 +01008360 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008361 while (fgets(thisline, sizeof(thisline), f) != NULL) {
8362 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01008363 int dec_size;
8364
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008365 /* Strip newline characters from the end */
8366 if(thisline[len - 1] == '\n')
8367 thisline[--len] = 0;
8368
8369 if(thisline[len - 1] == '\r')
8370 thisline[--len] = 0;
8371
Emeric Brun9e754772019-01-10 17:51:55 +01008372 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
8373 if (dec_size < 0) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008374 if (err)
8375 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008376 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008377 }
Emeric Brun9e754772019-01-10 17:51:55 +01008378 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
8379 keys_ref->key_size_bits = 128;
8380 }
8381 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
8382 keys_ref->key_size_bits = 256;
8383 }
8384 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
8385 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
8386 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Emeric Brun9e754772019-01-10 17:51:55 +01008387 if (err)
8388 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008389 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01008390 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008391 i++;
8392 }
8393
8394 if (i < TLS_TICKETS_NO) {
8395 if (err)
8396 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 +02008397 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008398 }
8399
8400 fclose(f);
8401
8402 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01008403 i -= 2;
8404 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008405 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008406 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01008407 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008408 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008409
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008410 LIST_ADD(&tlskeys_reference, &keys_ref->list);
8411
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008412 return 0;
Christopher Faulet2bbc80d2019-10-21 09:55:49 +02008413
8414 fail:
8415 if (f)
8416 fclose(f);
8417 if (keys_ref) {
8418 free(keys_ref->filename);
8419 free(keys_ref->tlskeys);
8420 free(keys_ref);
8421 }
8422 return ERR_ALERT | ERR_FATAL;
8423
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008424#else
8425 if (err)
8426 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
8427 return ERR_ALERT | ERR_FATAL;
8428#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008429}
8430
Emeric Brund94b3fe2012-09-20 18:23:56 +02008431/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008432static 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 +02008433{
8434 if (!*args[cur_arg + 1]) {
8435 if (err)
8436 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
8437 return ERR_ALERT | ERR_FATAL;
8438 }
8439
8440 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008441 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008442 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008443 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008444 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008445 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008446 else {
8447 if (err)
8448 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
8449 args[cur_arg], args[cur_arg + 1]);
8450 return ERR_ALERT | ERR_FATAL;
8451 }
8452
8453 return 0;
8454}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008455static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8456{
8457 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
8458}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008459
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02008460/* parse the "no-ca-names" bind keyword */
8461static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8462{
8463 conf->no_ca_names = 1;
8464 return 0;
8465}
8466static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8467{
8468 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
8469}
8470
Willy Tarreau92faadf2012-10-10 23:04:25 +02008471/************** "server" keywords ****************/
8472
Olivier Houchardc7566002018-11-20 23:33:50 +01008473/* parse the "npn" bind keyword */
8474static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8475{
8476#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
8477 char *p1, *p2;
8478
8479 if (!*args[*cur_arg + 1]) {
8480 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
8481 return ERR_ALERT | ERR_FATAL;
8482 }
8483
8484 free(newsrv->ssl_ctx.npn_str);
8485
8486 /* the NPN string is built as a suite of (<len> <name>)*,
8487 * so we reuse each comma to store the next <len> and need
8488 * one more for the end of the string.
8489 */
8490 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
8491 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
8492 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
8493 newsrv->ssl_ctx.npn_len);
8494
8495 /* replace commas with the name length */
8496 p1 = newsrv->ssl_ctx.npn_str;
8497 p2 = p1 + 1;
8498 while (1) {
8499 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
8500 newsrv->ssl_ctx.npn_len - (p1 + 1));
8501 if (!p2)
8502 p2 = p1 + 1 + strlen(p1 + 1);
8503
8504 if (p2 - (p1 + 1) > 255) {
8505 *p2 = '\0';
8506 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
8507 return ERR_ALERT | ERR_FATAL;
8508 }
8509
8510 *p1 = p2 - (p1 + 1);
8511 p1 = p2;
8512
8513 if (!*p2)
8514 break;
8515
8516 *(p2++) = '\0';
8517 }
8518 return 0;
8519#else
8520 if (err)
8521 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
8522 return ERR_ALERT | ERR_FATAL;
8523#endif
8524}
8525
Olivier Houchard92150142018-12-21 19:47:01 +01008526/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01008527static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8528{
8529#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
8530 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01008531 char **alpn_str;
8532 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01008533
Olivier Houchard92150142018-12-21 19:47:01 +01008534 if (*args[*cur_arg] == 'c') {
8535 alpn_str = &newsrv->check.alpn_str;
8536 alpn_len = &newsrv->check.alpn_len;
8537 } else {
8538 alpn_str = &newsrv->ssl_ctx.alpn_str;
8539 alpn_len = &newsrv->ssl_ctx.alpn_len;
8540
8541 }
Olivier Houchardc7566002018-11-20 23:33:50 +01008542 if (!*args[*cur_arg + 1]) {
8543 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
8544 return ERR_ALERT | ERR_FATAL;
8545 }
8546
Olivier Houchard92150142018-12-21 19:47:01 +01008547 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01008548
8549 /* the ALPN string is built as a suite of (<len> <name>)*,
8550 * so we reuse each comma to store the next <len> and need
8551 * one more for the end of the string.
8552 */
Olivier Houchard92150142018-12-21 19:47:01 +01008553 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
8554 *alpn_str = calloc(1, *alpn_len + 1);
8555 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01008556
8557 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01008558 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01008559 p2 = p1 + 1;
8560 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01008561 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01008562 if (!p2)
8563 p2 = p1 + 1 + strlen(p1 + 1);
8564
8565 if (p2 - (p1 + 1) > 255) {
8566 *p2 = '\0';
8567 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
8568 return ERR_ALERT | ERR_FATAL;
8569 }
8570
8571 *p1 = p2 - (p1 + 1);
8572 p1 = p2;
8573
8574 if (!*p2)
8575 break;
8576
8577 *(p2++) = '\0';
8578 }
8579 return 0;
8580#else
8581 if (err)
8582 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
8583 return ERR_ALERT | ERR_FATAL;
8584#endif
8585}
8586
Emeric Brunef42d922012-10-11 16:11:36 +02008587/* parse the "ca-file" server keyword */
8588static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8589{
8590 if (!*args[*cur_arg + 1]) {
8591 if (err)
8592 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
8593 return ERR_ALERT | ERR_FATAL;
8594 }
8595
Willy Tarreauef934602016-12-22 23:12:01 +01008596 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
8597 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008598 else
8599 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
8600
8601 return 0;
8602}
8603
Olivier Houchard9130a962017-10-17 17:33:43 +02008604/* parse the "check-sni" server keyword */
8605static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8606{
8607 if (!*args[*cur_arg + 1]) {
8608 if (err)
8609 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
8610 return ERR_ALERT | ERR_FATAL;
8611 }
8612
8613 newsrv->check.sni = strdup(args[*cur_arg + 1]);
8614 if (!newsrv->check.sni) {
8615 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
8616 return ERR_ALERT | ERR_FATAL;
8617 }
8618 return 0;
8619
8620}
8621
Willy Tarreau92faadf2012-10-10 23:04:25 +02008622/* parse the "check-ssl" server keyword */
8623static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8624{
8625 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01008626 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
8627 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008628#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008629 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
8630 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
8631#endif
Willy Tarreauef934602016-12-22 23:12:01 +01008632 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008633 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
8634 if (!newsrv->ssl_ctx.methods.min)
8635 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
8636 if (!newsrv->ssl_ctx.methods.max)
8637 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
8638
Willy Tarreau92faadf2012-10-10 23:04:25 +02008639 return 0;
8640}
8641
8642/* parse the "ciphers" server keyword */
8643static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8644{
8645 if (!*args[*cur_arg + 1]) {
8646 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
8647 return ERR_ALERT | ERR_FATAL;
8648 }
8649
8650 free(newsrv->ssl_ctx.ciphers);
8651 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
8652 return 0;
8653}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008654
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008655#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008656/* parse the "ciphersuites" server keyword */
8657static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8658{
8659 if (!*args[*cur_arg + 1]) {
8660 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
8661 return ERR_ALERT | ERR_FATAL;
8662 }
8663
8664 free(newsrv->ssl_ctx.ciphersuites);
8665 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
8666 return 0;
8667}
8668#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02008669
Emeric Brunef42d922012-10-11 16:11:36 +02008670/* parse the "crl-file" server keyword */
8671static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8672{
8673#ifndef X509_V_FLAG_CRL_CHECK
8674 if (err)
8675 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
8676 return ERR_ALERT | ERR_FATAL;
8677#else
8678 if (!*args[*cur_arg + 1]) {
8679 if (err)
8680 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
8681 return ERR_ALERT | ERR_FATAL;
8682 }
8683
Willy Tarreauef934602016-12-22 23:12:01 +01008684 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
8685 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008686 else
8687 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
8688
8689 return 0;
8690#endif
8691}
8692
Emeric Bruna7aa3092012-10-26 12:58:00 +02008693/* parse the "crt" server keyword */
8694static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8695{
8696 if (!*args[*cur_arg + 1]) {
8697 if (err)
8698 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
8699 return ERR_ALERT | ERR_FATAL;
8700 }
8701
Willy Tarreauef934602016-12-22 23:12:01 +01008702 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01008703 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02008704 else
8705 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
8706
8707 return 0;
8708}
Emeric Brunef42d922012-10-11 16:11:36 +02008709
Frédéric Lécaille340ae602017-03-13 10:38:04 +01008710/* parse the "no-check-ssl" server keyword */
8711static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8712{
Christopher Faulet68d35ae2020-03-27 18:55:49 +01008713 newsrv->check.use_ssl = -1;
Frédéric Lécaille340ae602017-03-13 10:38:04 +01008714 free(newsrv->ssl_ctx.ciphers);
8715 newsrv->ssl_ctx.ciphers = NULL;
8716 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
8717 return 0;
8718}
8719
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01008720/* parse the "no-send-proxy-v2-ssl" server keyword */
8721static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8722{
8723 newsrv->pp_opts &= ~SRV_PP_V2;
8724 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
8725 return 0;
8726}
8727
8728/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
8729static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8730{
8731 newsrv->pp_opts &= ~SRV_PP_V2;
8732 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
8733 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
8734 return 0;
8735}
8736
Frédéric Lécaillee381d762017-03-13 11:54:17 +01008737/* parse the "no-ssl" server keyword */
8738static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8739{
Christopher Faulet68d35ae2020-03-27 18:55:49 +01008740 newsrv->use_ssl = -1;
Frédéric Lécaillee381d762017-03-13 11:54:17 +01008741 free(newsrv->ssl_ctx.ciphers);
8742 newsrv->ssl_ctx.ciphers = NULL;
8743 return 0;
8744}
8745
Olivier Houchard522eea72017-11-03 16:27:47 +01008746/* parse the "allow-0rtt" server keyword */
8747static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8748{
8749 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
8750 return 0;
8751}
8752
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01008753/* parse the "no-ssl-reuse" server keyword */
8754static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8755{
8756 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
8757 return 0;
8758}
8759
Emeric Brunf9c5c472012-10-11 15:28:34 +02008760/* parse the "no-tls-tickets" server keyword */
8761static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8762{
8763 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
8764 return 0;
8765}
David Safb76832014-05-08 23:42:08 -04008766/* parse the "send-proxy-v2-ssl" server keyword */
8767static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8768{
8769 newsrv->pp_opts |= SRV_PP_V2;
8770 newsrv->pp_opts |= SRV_PP_V2_SSL;
8771 return 0;
8772}
8773
8774/* parse the "send-proxy-v2-ssl-cn" server keyword */
8775static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8776{
8777 newsrv->pp_opts |= SRV_PP_V2;
8778 newsrv->pp_opts |= SRV_PP_V2_SSL;
8779 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
8780 return 0;
8781}
Emeric Brunf9c5c472012-10-11 15:28:34 +02008782
Willy Tarreau732eac42015-07-09 11:40:25 +02008783/* parse the "sni" server keyword */
8784static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8785{
8786#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
8787 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
8788 return ERR_ALERT | ERR_FATAL;
8789#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008790 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02008791
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008792 arg = args[*cur_arg + 1];
8793 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02008794 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
8795 return ERR_ALERT | ERR_FATAL;
8796 }
8797
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008798 free(newsrv->sni_expr);
8799 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02008800
Willy Tarreau732eac42015-07-09 11:40:25 +02008801 return 0;
8802#endif
8803}
8804
Willy Tarreau92faadf2012-10-10 23:04:25 +02008805/* parse the "ssl" server keyword */
8806static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8807{
8808 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01008809 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
8810 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008811#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008812 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
8813 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
8814#endif
Jerome Magnin770bc8a2020-04-22 11:40:18 +02008815 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
8816 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
8817
8818 if (!newsrv->ssl_ctx.methods.min)
8819 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
8820
8821 if (!newsrv->ssl_ctx.methods.max)
8822 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
8823
8824
Willy Tarreau92faadf2012-10-10 23:04:25 +02008825 return 0;
8826}
8827
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01008828/* parse the "ssl-reuse" server keyword */
8829static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8830{
8831 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
8832 return 0;
8833}
8834
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01008835/* parse the "tls-tickets" server keyword */
8836static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8837{
8838 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
8839 return 0;
8840}
8841
Emeric Brunef42d922012-10-11 16:11:36 +02008842/* parse the "verify" server keyword */
8843static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8844{
8845 if (!*args[*cur_arg + 1]) {
8846 if (err)
8847 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
8848 return ERR_ALERT | ERR_FATAL;
8849 }
8850
8851 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008852 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02008853 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008854 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02008855 else {
8856 if (err)
8857 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
8858 args[*cur_arg], args[*cur_arg + 1]);
8859 return ERR_ALERT | ERR_FATAL;
8860 }
8861
Evan Broderbe554312013-06-27 00:05:25 -07008862 return 0;
8863}
8864
8865/* parse the "verifyhost" server keyword */
8866static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8867{
8868 if (!*args[*cur_arg + 1]) {
8869 if (err)
8870 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
8871 return ERR_ALERT | ERR_FATAL;
8872 }
8873
Frédéric Lécaille273f3212017-03-13 15:52:01 +01008874 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07008875 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
8876
Emeric Brunef42d922012-10-11 16:11:36 +02008877 return 0;
8878}
8879
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008880/* parse the "ssl-default-bind-options" keyword in global section */
8881static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
8882 struct proxy *defpx, const char *file, int line,
8883 char **err) {
8884 int i = 1;
8885
8886 if (*(args[i]) == 0) {
8887 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
8888 return -1;
8889 }
8890 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008891 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01008892 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008893 else if (!strcmp(args[i], "prefer-client-ciphers"))
8894 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008895 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
8896 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
8897 i++;
8898 else {
8899 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
8900 return -1;
8901 }
8902 }
8903 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008904 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
8905 return -1;
8906 }
8907 i++;
8908 }
8909 return 0;
8910}
8911
8912/* parse the "ssl-default-server-options" keyword in global section */
8913static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
8914 struct proxy *defpx, const char *file, int line,
8915 char **err) {
8916 int i = 1;
8917
8918 if (*(args[i]) == 0) {
8919 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
8920 return -1;
8921 }
8922 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008923 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01008924 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008925 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
8926 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
8927 i++;
8928 else {
8929 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
8930 return -1;
8931 }
8932 }
8933 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008934 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
8935 return -1;
8936 }
8937 i++;
8938 }
8939 return 0;
8940}
8941
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008942/* parse the "ca-base" / "crt-base" keywords in global section.
8943 * Returns <0 on alert, >0 on warning, 0 on success.
8944 */
8945static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
8946 struct proxy *defpx, const char *file, int line,
8947 char **err)
8948{
8949 char **target;
8950
Willy Tarreauef934602016-12-22 23:12:01 +01008951 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008952
8953 if (too_many_args(1, args, err, NULL))
8954 return -1;
8955
8956 if (*target) {
8957 memprintf(err, "'%s' already specified.", args[0]);
8958 return -1;
8959 }
8960
8961 if (*(args[1]) == 0) {
8962 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
8963 return -1;
8964 }
8965 *target = strdup(args[1]);
8966 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008967}
8968
8969/* parse the "ssl-mode-async" keyword in global section.
8970 * Returns <0 on alert, >0 on warning, 0 on success.
8971 */
8972static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
8973 struct proxy *defpx, const char *file, int line,
8974 char **err)
8975{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008976#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008977 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01008978 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008979 return 0;
8980#else
8981 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
8982 return -1;
8983#endif
8984}
8985
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008986#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008987static int ssl_check_async_engine_count(void) {
8988 int err_code = 0;
8989
Emeric Brun3854e012017-05-17 20:42:48 +02008990 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008991 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008992 err_code = ERR_ABORT;
8993 }
8994 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008995}
8996
Grant Zhang872f9c22017-01-21 01:10:18 +00008997/* parse the "ssl-engine" keyword in global section.
8998 * Returns <0 on alert, >0 on warning, 0 on success.
8999 */
9000static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9001 struct proxy *defpx, const char *file, int line,
9002 char **err)
9003{
9004 char *algo;
9005 int ret = -1;
9006
9007 if (*(args[1]) == 0) {
9008 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9009 return ret;
9010 }
9011
9012 if (*(args[2]) == 0) {
9013 /* if no list of algorithms is given, it defaults to ALL */
9014 algo = strdup("ALL");
9015 goto add_engine;
9016 }
9017
9018 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
9019 if (strcmp(args[2], "algo") != 0) {
9020 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
9021 return ret;
9022 }
9023
9024 if (*(args[3]) == 0) {
9025 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
9026 return ret;
9027 }
9028 algo = strdup(args[3]);
9029
9030add_engine:
9031 if (ssl_init_single_engine(args[1], algo)==0) {
9032 openssl_engines_initialized++;
9033 ret = 0;
9034 }
9035 free(algo);
9036 return ret;
9037}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009038#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00009039
Willy Tarreauf22e9682016-12-21 23:23:19 +01009040/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
9041 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9042 */
9043static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
9044 struct proxy *defpx, const char *file, int line,
9045 char **err)
9046{
9047 char **target;
9048
Willy Tarreauef934602016-12-22 23:12:01 +01009049 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01009050
9051 if (too_many_args(1, args, err, NULL))
9052 return -1;
9053
9054 if (*(args[1]) == 0) {
9055 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9056 return -1;
9057 }
9058
9059 free(*target);
9060 *target = strdup(args[1]);
9061 return 0;
9062}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009063
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009064#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009065/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9066 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9067 */
9068static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9069 struct proxy *defpx, const char *file, int line,
9070 char **err)
9071{
9072 char **target;
9073
9074 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9075
9076 if (too_many_args(1, args, err, NULL))
9077 return -1;
9078
9079 if (*(args[1]) == 0) {
9080 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9081 return -1;
9082 }
9083
9084 free(*target);
9085 *target = strdup(args[1]);
9086 return 0;
9087}
9088#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009089
Willy Tarreau9ceda382016-12-21 23:13:03 +01009090/* parse various global tune.ssl settings consisting in positive integers.
9091 * Returns <0 on alert, >0 on warning, 0 on success.
9092 */
9093static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9094 struct proxy *defpx, const char *file, int line,
9095 char **err)
9096{
9097 int *target;
9098
9099 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9100 target = &global.tune.sslcachesize;
9101 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009102 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009103 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009104 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009105 else if (strcmp(args[0], "maxsslconn") == 0)
9106 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009107 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9108 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009109 else {
9110 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9111 return -1;
9112 }
9113
9114 if (too_many_args(1, args, err, NULL))
9115 return -1;
9116
9117 if (*(args[1]) == 0) {
9118 memprintf(err, "'%s' expects an integer argument.", args[0]);
9119 return -1;
9120 }
9121
9122 *target = atoi(args[1]);
9123 if (*target < 0) {
9124 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9125 return -1;
9126 }
9127 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009128}
9129
9130static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9131 struct proxy *defpx, const char *file, int line,
9132 char **err)
9133{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009134 int ret;
9135
9136 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9137 if (ret != 0)
9138 return ret;
9139
Willy Tarreaubafbe012017-11-24 17:34:44 +01009140 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009141 memprintf(err, "'%s' is already configured.", args[0]);
9142 return -1;
9143 }
9144
Willy Tarreaubafbe012017-11-24 17:34:44 +01009145 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9146 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009147 memprintf(err, "Out of memory error.");
9148 return -1;
9149 }
9150 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009151}
9152
9153/* parse "ssl.force-private-cache".
9154 * Returns <0 on alert, >0 on warning, 0 on success.
9155 */
9156static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9157 struct proxy *defpx, const char *file, int line,
9158 char **err)
9159{
9160 if (too_many_args(0, args, err, NULL))
9161 return -1;
9162
Willy Tarreauef934602016-12-22 23:12:01 +01009163 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009164 return 0;
9165}
9166
9167/* parse "ssl.lifetime".
9168 * Returns <0 on alert, >0 on warning, 0 on success.
9169 */
9170static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9171 struct proxy *defpx, const char *file, int line,
9172 char **err)
9173{
9174 const char *res;
9175
9176 if (too_many_args(1, args, err, NULL))
9177 return -1;
9178
9179 if (*(args[1]) == 0) {
9180 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9181 return -1;
9182 }
9183
Willy Tarreauef934602016-12-22 23:12:01 +01009184 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009185 if (res == PARSE_TIME_OVER) {
9186 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9187 args[1], args[0]);
9188 return -1;
9189 }
9190 else if (res == PARSE_TIME_UNDER) {
9191 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9192 args[1], args[0]);
9193 return -1;
9194 }
9195 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009196 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9197 return -1;
9198 }
9199 return 0;
9200}
9201
9202#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009203/* parse "ssl-dh-param-file".
9204 * Returns <0 on alert, >0 on warning, 0 on success.
9205 */
9206static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9207 struct proxy *defpx, const char *file, int line,
9208 char **err)
9209{
9210 if (too_many_args(1, args, err, NULL))
9211 return -1;
9212
9213 if (*(args[1]) == 0) {
9214 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9215 return -1;
9216 }
9217
9218 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9219 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9220 return -1;
9221 }
9222 return 0;
9223}
9224
Willy Tarreau9ceda382016-12-21 23:13:03 +01009225/* parse "ssl.default-dh-param".
9226 * Returns <0 on alert, >0 on warning, 0 on success.
9227 */
9228static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9229 struct proxy *defpx, const char *file, int line,
9230 char **err)
9231{
9232 if (too_many_args(1, args, err, NULL))
9233 return -1;
9234
9235 if (*(args[1]) == 0) {
9236 memprintf(err, "'%s' expects an integer argument.", args[0]);
9237 return -1;
9238 }
9239
Willy Tarreauef934602016-12-22 23:12:01 +01009240 global_ssl.default_dh_param = atoi(args[1]);
9241 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009242 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9243 return -1;
9244 }
9245 return 0;
9246}
9247#endif
9248
9249
William Lallemand32af2032016-10-29 18:09:35 +02009250/* This function is used with TLS ticket keys management. It permits to browse
9251 * each reference. The variable <getnext> must contain the current node,
9252 * <end> point to the root node.
9253 */
9254#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9255static inline
9256struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
9257{
9258 struct tls_keys_ref *ref = getnext;
9259
9260 while (1) {
9261
9262 /* Get next list entry. */
9263 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
9264
9265 /* If the entry is the last of the list, return NULL. */
9266 if (&ref->list == end)
9267 return NULL;
9268
9269 return ref;
9270 }
9271}
9272
9273static inline
9274struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
9275{
9276 int id;
9277 char *error;
9278
9279 /* If the reference starts by a '#', this is numeric id. */
9280 if (reference[0] == '#') {
9281 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
9282 id = strtol(reference + 1, &error, 10);
9283 if (*error != '\0')
9284 return NULL;
9285
9286 /* Perform the unique id lookup. */
9287 return tlskeys_ref_lookupid(id);
9288 }
9289
9290 /* Perform the string lookup. */
9291 return tlskeys_ref_lookup(reference);
9292}
9293#endif
9294
9295
9296#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9297
9298static int cli_io_handler_tlskeys_files(struct appctx *appctx);
9299
9300static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
9301 return cli_io_handler_tlskeys_files(appctx);
9302}
9303
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009304/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
9305 * (next index to be dumped), and cli.p0 (next key reference).
9306 */
William Lallemand32af2032016-10-29 18:09:35 +02009307static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
9308
9309 struct stream_interface *si = appctx->owner;
9310
9311 switch (appctx->st2) {
9312 case STAT_ST_INIT:
9313 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08009314 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +02009315 * later and restart at the state "STAT_ST_INIT".
9316 */
9317 chunk_reset(&trash);
9318
9319 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
9320 chunk_appendf(&trash, "# id secret\n");
9321 else
9322 chunk_appendf(&trash, "# id (file)\n");
9323
Willy Tarreau06d80a92017-10-19 14:32:15 +02009324 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01009325 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009326 return 0;
9327 }
9328
William Lallemand32af2032016-10-29 18:09:35 +02009329 /* Now, we start the browsing of the references lists.
9330 * Note that the following call to LIST_ELEM return bad pointer. The only
9331 * available field of this pointer is <list>. It is used with the function
9332 * tlskeys_list_get_next() for retruning the first available entry
9333 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009334 if (appctx->ctx.cli.p0 == NULL) {
9335 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
9336 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009337 }
9338
9339 appctx->st2 = STAT_ST_LIST;
9340 /* fall through */
9341
9342 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009343 while (appctx->ctx.cli.p0) {
9344 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +02009345
9346 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009347 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +02009348 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009349
9350 if (appctx->ctx.cli.i1 == 0)
9351 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
9352
William Lallemand32af2032016-10-29 18:09:35 +02009353 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +01009354 int head;
9355
9356 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
9357 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009358 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +02009359 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +02009360
9361 chunk_reset(t2);
9362 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +01009363 if (ref->key_size_bits == 128) {
9364 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9365 sizeof(struct tls_sess_key_128),
9366 t2->area, t2->size);
9367 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9368 t2->area);
9369 }
9370 else if (ref->key_size_bits == 256) {
9371 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9372 sizeof(struct tls_sess_key_256),
9373 t2->area, t2->size);
9374 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9375 t2->area);
9376 }
9377 else {
9378 /* This case should never happen */
9379 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
9380 }
William Lallemand32af2032016-10-29 18:09:35 +02009381
Willy Tarreau06d80a92017-10-19 14:32:15 +02009382 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009383 /* let's try again later from this stream. We add ourselves into
9384 * this stream's users so that it can remove us upon termination.
9385 */
Christopher Faulet16f45c82018-02-16 11:23:49 +01009386 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +01009387 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009388 return 0;
9389 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009390 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +02009391 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01009392 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009393 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +02009394 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02009395 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009396 /* let's try again later from this stream. We add ourselves into
9397 * this stream's users so that it can remove us upon termination.
9398 */
Willy Tarreaudb398432018-11-15 11:08:52 +01009399 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009400 return 0;
9401 }
9402
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009403 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +02009404 break;
9405
9406 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009407 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009408 }
9409
9410 appctx->st2 = STAT_ST_FIN;
9411 /* fall through */
9412
9413 default:
9414 appctx->st2 = STAT_ST_FIN;
9415 return 1;
9416 }
9417 return 0;
9418}
9419
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009420/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009421static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009422{
William Lallemand32af2032016-10-29 18:09:35 +02009423 /* no parameter, shows only file list */
9424 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009425 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009426 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009427 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009428 }
9429
9430 if (args[2][0] == '*') {
9431 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009432 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009433 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009434 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
9435 if (!appctx->ctx.cli.p0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009436 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009437 appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009438 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009439 return 1;
9440 }
9441 }
William Lallemand32af2032016-10-29 18:09:35 +02009442 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009443 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009444}
9445
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009446static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009447{
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009448 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +02009449 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009450
William Lallemand32af2032016-10-29 18:09:35 +02009451 /* Expect two parameters: the filename and the new new TLS key in encoding */
9452 if (!*args[3] || !*args[4]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009453 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009454 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 +01009455 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009456 return 1;
9457 }
9458
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009459 ref = tlskeys_ref_lookup_ref(args[3]);
9460 if (!ref) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009461 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009462 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009463 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009464 return 1;
9465 }
9466
Willy Tarreau1c913e42018-08-22 05:26:57 +02009467 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Emeric Brun9e754772019-01-10 17:51:55 +01009468 if (ret < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009469 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009470 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009471 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009472 return 1;
9473 }
Emeric Brun9e754772019-01-10 17:51:55 +01009474
Willy Tarreau1c913e42018-08-22 05:26:57 +02009475 trash.data = ret;
Emeric Brun9e754772019-01-10 17:51:55 +01009476 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) {
9477 appctx->ctx.cli.severity = LOG_ERR;
9478 appctx->ctx.cli.msg = "'set ssl tls-key' received a key of wrong size.\n";
9479 appctx->st0 = CLI_ST_PRINT;
9480 return 1;
9481 }
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009482 appctx->ctx.cli.severity = LOG_INFO;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01009483 appctx->ctx.cli.msg = "TLS ticket key updated!\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009484 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009485 return 1;
9486
9487}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009488#endif
William Lallemand32af2032016-10-29 18:09:35 +02009489
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009490static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009491{
9492#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
9493 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +02009494 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009495
9496 if (!payload)
9497 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +02009498
9499 /* Expect one parameter: the new response in base64 encoding */
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009500 if (!*payload) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009501 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009502 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009503 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009504 return 1;
9505 }
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009506
9507 /* remove \r and \n from the payload */
9508 for (i = 0, j = 0; payload[i]; i++) {
9509 if (payload[i] == '\r' || payload[i] == '\n')
9510 continue;
9511 payload[j++] = payload[i];
9512 }
9513 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +02009514
Willy Tarreau1c913e42018-08-22 05:26:57 +02009515 ret = base64dec(payload, j, trash.area, trash.size);
9516 if (ret < 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009517 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009518 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\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
Willy Tarreau1c913e42018-08-22 05:26:57 +02009523 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +02009524 if (ssl_sock_update_ocsp_response(&trash, &err)) {
9525 if (err) {
9526 memprintf(&err, "%s.\n", err);
9527 appctx->ctx.cli.err = err;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009528 appctx->st0 = CLI_ST_PRINT_FREE;
William Lallemand32af2032016-10-29 18:09:35 +02009529 }
Aurélien Nephtali9a4da682018-04-16 19:02:42 +02009530 else {
9531 appctx->ctx.cli.severity = LOG_ERR;
9532 appctx->ctx.cli.msg = "Failed to update OCSP response.\n";
9533 appctx->st0 = CLI_ST_PRINT;
9534 }
William Lallemand32af2032016-10-29 18:09:35 +02009535 return 1;
9536 }
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009537 appctx->ctx.cli.severity = LOG_INFO;
Aurélien Nephtali6e8a41d2018-03-15 21:48:50 +01009538 appctx->ctx.cli.msg = "OCSP Response updated!\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01009539 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009540 return 1;
9541#else
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02009542 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand32af2032016-10-29 18:09:35 +02009543 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 +01009544 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02009545 return 1;
9546#endif
9547
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009548}
9549
Willy Tarreau86a394e2019-05-09 14:15:32 +02009550#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009551static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
9552{
9553 switch (arg->type) {
9554 case ARGT_STR:
9555 smp->data.type = SMP_T_STR;
9556 smp->data.u.str = arg->data.str;
9557 return 1;
9558 case ARGT_VAR:
9559 if (!vars_get_by_desc(&arg->data.var, smp))
9560 return 0;
9561 if (!sample_casts[smp->data.type][SMP_T_STR])
9562 return 0;
9563 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
9564 return 0;
9565 return 1;
9566 default:
9567 return 0;
9568 }
9569}
9570
9571static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
9572 const char *file, int line, char **err)
9573{
9574 switch(args[0].data.sint) {
9575 case 128:
9576 case 192:
9577 case 256:
9578 break;
9579 default:
9580 memprintf(err, "key size must be 128, 192 or 256 (bits).");
9581 return 0;
9582 }
9583 /* Try to decode a variable. */
9584 vars_check_arg(&args[1], NULL);
9585 vars_check_arg(&args[2], NULL);
9586 vars_check_arg(&args[3], NULL);
9587 return 1;
9588}
9589
9590/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
9591static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
9592{
9593 struct sample nonce, key, aead_tag;
9594 struct buffer *smp_trash, *smp_trash_alloc;
9595 EVP_CIPHER_CTX *ctx;
9596 int dec_size, ret;
9597
9598 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
9599 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
9600 return 0;
9601
9602 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
9603 if (!sample_conv_var2smp_str(&arg_p[2], &key))
9604 return 0;
9605
9606 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
9607 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
9608 return 0;
9609
9610 smp_trash = get_trash_chunk();
9611 smp_trash_alloc = alloc_trash_chunk();
9612 if (!smp_trash_alloc)
9613 return 0;
9614
9615 ctx = EVP_CIPHER_CTX_new();
9616
9617 if (!ctx)
9618 goto err;
9619
9620 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
9621 if (dec_size < 0)
9622 goto err;
9623 smp_trash->data = dec_size;
9624
9625 /* Set cipher type and mode */
9626 switch(arg_p[0].data.sint) {
9627 case 128:
9628 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
9629 break;
9630 case 192:
9631 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
9632 break;
9633 case 256:
9634 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
9635 break;
9636 }
9637
9638 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
9639
9640 /* Initialise IV */
9641 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
9642 goto err;
9643
9644 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
9645 if (dec_size < 0)
9646 goto err;
9647 smp_trash->data = dec_size;
9648
9649 /* Initialise key */
9650 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
9651 goto err;
9652
9653 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
9654 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
9655 goto err;
9656
9657 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
9658 if (dec_size < 0)
9659 goto err;
9660 smp_trash_alloc->data = dec_size;
9661 dec_size = smp_trash->data;
9662
9663 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
9664 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
9665
9666 if (ret <= 0)
9667 goto err;
9668
9669 smp->data.u.str.data = dec_size + smp_trash->data;
9670 smp->data.u.str.area = smp_trash->area;
9671 smp->data.type = SMP_T_BIN;
9672 smp->flags &= ~SMP_F_CONST;
9673 free_trash_chunk(smp_trash_alloc);
9674 return 1;
9675
9676err:
9677 free_trash_chunk(smp_trash_alloc);
9678 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009679}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009680# endif
William Lallemand32af2032016-10-29 18:09:35 +02009681
9682/* register cli keywords */
9683static struct cli_kw_list cli_kws = {{ },{
9684#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9685 { { "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 +02009686 { { "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 +02009687#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009688 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemand32af2032016-10-29 18:09:35 +02009689 { { NULL }, NULL, NULL, NULL }
9690}};
9691
Willy Tarreau0108d902018-11-25 19:14:37 +01009692INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +02009693
Willy Tarreau7875d092012-09-10 08:20:03 +02009694/* Note: must not be declared <const> as its list will be overwritten.
9695 * Please take care of keeping this list alphabetically sorted.
9696 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009697static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +02009698 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009699 { "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 +01009700#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +01009701 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +01009702#endif
Emeric Brun645ae792014-04-30 14:21:06 +02009703 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +01009704#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9705 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
9706#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +01009707 { "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 +02009708 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +02009709 { "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 +02009710 { "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 +02009711#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +02009712 { "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 -04009713#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009714#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -04009715 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
9716 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -04009717 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
9718#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009719 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
9720 { "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 +01009721 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009722 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02009723 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9724 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9725 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9726 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9727 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9728 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9729 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9730 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009731 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009732 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
9733 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +01009734 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02009735 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9736 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9737 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9738 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9739 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9740 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9741 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +02009742 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009743 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009744 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009745 { "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 +01009746 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009747 { "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 +02009748 { "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 +01009749 { "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 +02009750 { "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 +01009751#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009752 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +02009753#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009754#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009755 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +02009756#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009757 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009758#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +02009759 { "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 -04009760#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009761 { "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 +02009762#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009763 { "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 -04009764#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009765#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -04009766 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9767 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -04009768 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9769#endif
Patrick Hemmer41966772018-04-28 19:15:48 -04009770#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009771 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -04009772#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009773 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9774 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9775 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9776 { "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 +02009777 { NULL, NULL, 0, 0, 0 },
9778}};
9779
Willy Tarreau0108d902018-11-25 19:14:37 +01009780INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
9781
Willy Tarreau7875d092012-09-10 08:20:03 +02009782/* Note: must not be declared <const> as its list will be overwritten.
9783 * Please take care of keeping this list alphabetically sorted.
9784 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009785static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +01009786 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
9787 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +01009788 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +02009789}};
9790
Willy Tarreau0108d902018-11-25 19:14:37 +01009791INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
9792
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009793/* Note: must not be declared <const> as its list will be overwritten.
9794 * Please take care of keeping this list alphabetically sorted, doing so helps
9795 * all code contributors.
9796 * Optional keywords are also declared with a NULL ->parse() function so that
9797 * the config parser can report an appropriate error when a known keyword was
9798 * not enabled.
9799 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009800static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +02009801 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009802 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
9803 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
9804 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009805#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009806 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
9807#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009808 { "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 +01009809 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009810 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009811 { "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 +01009812 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009813 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
9814 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009815 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
9816 { NULL, NULL, 0 },
9817};
9818
Willy Tarreau0108d902018-11-25 19:14:37 +01009819/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
9820
Willy Tarreau51fb7652012-09-18 18:24:39 +02009821static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +02009822 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009823 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
9824 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
9825 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
9826 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
9827 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
9828 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009829#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009830 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
9831#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009832 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
9833 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
9834 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
9835 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
9836 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
9837 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
9838 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
9839 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
9840 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
9841 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009842 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009843 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009844 { "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 +02009845 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
9846 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
9847 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
9848 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009849 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009850 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
9851 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009852 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
9853 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009854 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
9855 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
9856 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
9857 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
9858 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009859 { NULL, NULL, 0 },
9860}};
Emeric Brun46591952012-05-18 15:47:34 +02009861
Willy Tarreau0108d902018-11-25 19:14:37 +01009862INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
9863
Willy Tarreau92faadf2012-10-10 23:04:25 +02009864/* Note: must not be declared <const> as its list will be overwritten.
9865 * Please take care of keeping this list alphabetically sorted, doing so helps
9866 * all code contributors.
9867 * Optional keywords are also declared with a NULL ->parse() function so that
9868 * the config parser can report an appropriate error when a known keyword was
9869 * not enabled.
9870 */
9871static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +01009872 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +01009873 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009874 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +01009875 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +02009876 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009877 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
9878 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009879#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009880 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
9881#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009882 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
9883 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
9884 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
9885 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
9886 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
9887 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
9888 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
9889 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
9890 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
9891 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
9892 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
9893 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
9894 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
9895 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
9896 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
9897 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
9898 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
9899 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +01009900 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009901 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
9902 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
9903 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
9904 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
9905 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
9906 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
9907 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
9908 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
9909 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
9910 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +02009911 { NULL, NULL, 0, 0 },
9912}};
9913
Willy Tarreau0108d902018-11-25 19:14:37 +01009914INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
9915
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009916static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009917 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
9918 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009919 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009920 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
9921 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +01009922#ifndef OPENSSL_NO_DH
9923 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
9924#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009925 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009926#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00009927 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009928#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +01009929 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
9930#ifndef OPENSSL_NO_DH
9931 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
9932#endif
9933 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
9934 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
9935 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
9936 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009937 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +01009938 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
9939 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009940#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009941 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
9942 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
9943#endif
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009944 { 0, NULL, NULL },
9945}};
9946
Willy Tarreau0108d902018-11-25 19:14:37 +01009947INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
9948
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009949/* Note: must not be declared <const> as its list will be overwritten */
9950static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +02009951#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009952 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
9953#endif
9954 { NULL, NULL, 0, 0, 0 },
9955}};
9956
9957INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
9958
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02009959/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +01009960static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02009961 .snd_buf = ssl_sock_from_buf,
9962 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +01009963 .subscribe = ssl_subscribe,
9964 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +02009965 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +02009966 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +02009967 .rcv_pipe = NULL,
9968 .snd_pipe = NULL,
9969 .shutr = NULL,
9970 .shutw = ssl_sock_shutw,
9971 .close = ssl_sock_close,
9972 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +01009973 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +01009974 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +01009975 .prepare_srv = ssl_sock_prepare_srv_ctx,
9976 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +01009977 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01009978 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +02009979};
9980
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009981enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
9982 struct session *sess, struct stream *s, int flags)
9983{
9984 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009985 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009986
9987 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009988 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009989
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009990 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009991 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009992 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009993 s->req.flags |= CF_READ_NULL;
9994 return ACT_RET_YIELD;
9995 }
9996 }
9997 return (ACT_RET_CONT);
9998}
9999
10000static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
10001{
10002 rule->action_ptr = ssl_action_wait_for_hs;
10003
10004 return ACT_RET_PRS_OK;
10005}
10006
10007static struct action_kw_list http_req_actions = {ILH, {
10008 { "wait-for-handshake", ssl_parse_wait_for_hs },
10009 { /* END */ }
10010}};
10011
Willy Tarreau0108d902018-11-25 19:14:37 +010010012INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
10013
Willy Tarreau5db847a2019-05-09 14:13:35 +020010014#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010010015
10016static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
10017{
10018 if (ptr) {
10019 chunk_destroy(ptr);
10020 free(ptr);
10021 }
10022}
10023
10024#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010010025static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
10026{
Willy Tarreaubafbe012017-11-24 17:34:44 +010010027 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010010028}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010010029
Emeric Brun46591952012-05-18 15:47:34 +020010030__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020010031static void __ssl_sock_init(void)
10032{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010033#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020010034 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050010035 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010036#endif
Emeric Brun46591952012-05-18 15:47:34 +020010037
Willy Tarreauef934602016-12-22 23:12:01 +010010038 if (global_ssl.listen_default_ciphers)
10039 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
10040 if (global_ssl.connect_default_ciphers)
10041 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010042#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010043 if (global_ssl.listen_default_ciphersuites)
10044 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
10045 if (global_ssl.connect_default_ciphersuites)
10046 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10047#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010010048
Willy Tarreau13e14102016-12-22 20:25:26 +010010049 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020010050#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020010051 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080010052#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010053#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020010054 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050010055 n = sk_SSL_COMP_num(cm);
10056 while (n--) {
10057 (void) sk_SSL_COMP_pop(cm);
10058 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050010059#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050010060
Willy Tarreau5db847a2019-05-09 14:13:35 +020010061#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020010062 ssl_locking_init();
10063#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020010064#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010010065 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
10066#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020010067 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020010068 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 +010010069 ssl_pkey_info_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010070#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000010071 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010072 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010073#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010010074#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10075 hap_register_post_check(tlskeys_finalize_config);
10076#endif
Willy Tarreau80713382018-11-26 10:19:54 +010010077
10078 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
10079 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
10080
10081#ifndef OPENSSL_NO_DH
10082 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
10083 hap_register_post_deinit(ssl_free_dh);
10084#endif
10085#ifndef OPENSSL_NO_ENGINE
10086 hap_register_post_deinit(ssl_free_engines);
10087#endif
10088 /* Load SSL string for the verbose & debug mode. */
10089 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020010090 ha_meth = BIO_meth_new(0x666, "ha methods");
10091 BIO_meth_set_write(ha_meth, ha_ssl_write);
10092 BIO_meth_set_read(ha_meth, ha_ssl_read);
10093 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
10094 BIO_meth_set_create(ha_meth, ha_ssl_new);
10095 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
10096 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
10097 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
Willy Tarreau80713382018-11-26 10:19:54 +010010098}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010010099
Willy Tarreau80713382018-11-26 10:19:54 +010010100/* Compute and register the version string */
10101static void ssl_register_build_options()
10102{
10103 char *ptr = NULL;
10104 int i;
10105
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010106 memprintf(&ptr, "Built with OpenSSL version : "
10107#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010010108 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010109#else /* OPENSSL_IS_BORINGSSL */
10110 OPENSSL_VERSION_TEXT
10111 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080010112 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020010113 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010114#endif
10115 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020010116#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010117 "no (library version too old)"
10118#elif defined(OPENSSL_NO_TLSEXT)
10119 "no (disabled via OPENSSL_NO_TLSEXT)"
10120#else
10121 "yes"
10122#endif
10123 "", ptr);
10124
10125 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
10126#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10127 "yes"
10128#else
10129#ifdef OPENSSL_NO_TLSEXT
10130 "no (because of OPENSSL_NO_TLSEXT)"
10131#else
10132 "no (version might be too old, 0.9.8f min needed)"
10133#endif
10134#endif
10135 "", ptr);
10136
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020010137 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
10138 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
10139 if (methodVersions[i].option)
10140 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010010141
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010142 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010010143}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010144
Willy Tarreau80713382018-11-26 10:19:54 +010010145INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020010146
Emeric Brun46591952012-05-18 15:47:34 +020010147
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010148#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000010149void ssl_free_engines(void) {
10150 struct ssl_engine_list *wl, *wlb;
10151 /* free up engine list */
10152 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
10153 ENGINE_finish(wl->e);
10154 ENGINE_free(wl->e);
10155 LIST_DEL(&wl->list);
10156 free(wl);
10157 }
10158}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010159#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020010160
Remi Gacogned3a23c32015-05-28 16:39:47 +020010161#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000010162void ssl_free_dh(void) {
10163 if (local_dh_1024) {
10164 DH_free(local_dh_1024);
10165 local_dh_1024 = NULL;
10166 }
10167 if (local_dh_2048) {
10168 DH_free(local_dh_2048);
10169 local_dh_2048 = NULL;
10170 }
10171 if (local_dh_4096) {
10172 DH_free(local_dh_4096);
10173 local_dh_4096 = NULL;
10174 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020010175 if (global_dh) {
10176 DH_free(global_dh);
10177 global_dh = NULL;
10178 }
Grant Zhang872f9c22017-01-21 01:10:18 +000010179}
10180#endif
10181
10182__attribute__((destructor))
10183static void __ssl_sock_deinit(void)
10184{
10185#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020010186 if (ssl_ctx_lru_tree) {
10187 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010010188 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020010189 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020010190#endif
10191
Willy Tarreau5db847a2019-05-09 14:13:35 +020010192#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020010193 ERR_remove_state(0);
10194 ERR_free_strings();
10195
10196 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080010197#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020010198
Willy Tarreau5db847a2019-05-09 14:13:35 +020010199#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020010200 CRYPTO_cleanup_all_ex_data();
10201#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020010202 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020010203}
10204
10205
Emeric Brun46591952012-05-18 15:47:34 +020010206/*
10207 * Local variables:
10208 * c-indent-level: 8
10209 * c-basic-offset: 8
10210 * End:
10211 */