blob: ca48a65ea61d83fe142c1e0a269fe5a8a02de9a7 [file] [log] [blame]
yanbzhu08ce6ab2015-12-02 13:01:29 -05001
Emeric Brun46591952012-05-18 15:47:34 +02002/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003 * SSL/TLS transport layer over SOCK_STREAM sockets
Emeric Brun46591952012-05-18 15:47:34 +02004 *
5 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Willy Tarreau69845df2012-09-10 09:43:09 +020012 * Acknowledgement:
13 * We'd like to specially thank the Stud project authors for a very clean
14 * and well documented code which helped us understand how the OpenSSL API
15 * ought to be used in non-blocking mode. This is one difficult part which
16 * is not easy to get from the OpenSSL doc, and reading the Stud code made
17 * it much more obvious than the examples in the OpenSSL package. Keep up
18 * the good works, guys !
19 *
20 * Stud is an extremely efficient and scalable SSL/TLS proxy which combines
21 * particularly well with haproxy. For more info about this project, visit :
22 * https://github.com/bumptech/stud
23 *
Emeric Brun46591952012-05-18 15:47:34 +020024 */
25
Willy Tarreau8d164dc2019-05-10 09:35:00 +020026/* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */
Emeric Brun46591952012-05-18 15:47:34 +020027#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020028#include <ctype.h>
29#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020030#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020034#include <string.h>
35#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020036
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020040#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020041#include <netinet/tcp.h>
42
Christopher Faulet31af49d2015-06-09 17:29:50 +020043#include <import/lru.h>
44#include <import/xxhash.h>
45
Emeric Brun46591952012-05-18 15:47:34 +020046#include <common/buffer.h>
Willy Tarreau843b7cb2018-07-13 10:54:26 +020047#include <common/chunk.h>
Emeric Brun46591952012-05-18 15:47:34 +020048#include <common/compat.h>
49#include <common/config.h>
50#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020051#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010052#include <common/initcall.h>
Willy Tarreau55994562019-05-09 14:52:44 +020053#include <common/openssl-compat.h>
Emeric Brun46591952012-05-18 15:47:34 +020054#include <common/standard.h>
55#include <common/ticks.h>
56#include <common/time.h>
Emeric Brun2c86cbf2014-10-30 15:56:50 +010057#include <common/cfgparse.h>
Nenad Merdanovic05552d42015-02-27 19:56:49 +010058#include <common/base64.h>
Emeric Brun46591952012-05-18 15:47:34 +020059
Emeric Brunfc0421f2012-09-07 17:30:07 +020060#include <ebsttree.h>
61
William Lallemand32af2032016-10-29 18:09:35 +020062#include <types/applet.h>
63#include <types/cli.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020064#include <types/global.h>
65#include <types/ssl_sock.h>
William Lallemand32af2032016-10-29 18:09:35 +020066#include <types/stats.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020067
Willy Tarreau7875d092012-09-10 08:20:03 +020068#include <proto/acl.h>
69#include <proto/arg.h>
William Lallemand32af2032016-10-29 18:09:35 +020070#include <proto/channel.h>
Emeric Brun46591952012-05-18 15:47:34 +020071#include <proto/connection.h>
William Lallemand32af2032016-10-29 18:09:35 +020072#include <proto/cli.h>
Emeric Brun46591952012-05-18 15:47:34 +020073#include <proto/fd.h>
74#include <proto/freq_ctr.h>
75#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020076#include <proto/http_rules.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020077#include <proto/listener.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010078#include <proto/pattern.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020079#include <proto/proto_tcp.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020080#include <proto/http_ana.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020081#include <proto/server.h>
William Lallemand32af2032016-10-29 18:09:35 +020082#include <proto/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020083#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020084#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020085#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020086#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020087#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020088#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010089#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020090
Willy Tarreau9356dac2019-05-10 09:22:53 +020091/* ***** READ THIS before adding code here! *****
92 *
93 * Due to API incompatibilities between multiple OpenSSL versions and their
94 * derivatives, it's often tempting to add macros to (re-)define certain
95 * symbols. Please do not do this here, and do it in common/openssl-compat.h
96 * exclusively so that the whole code consistently uses the same macros.
97 *
98 * Whenever possible if a macro is missing in certain versions, it's better
99 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
100 */
101
Willy Tarreau518cedd2014-02-17 15:43:01 +0100102/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +0200103#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +0100104#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +0100105#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +0200106#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
107
Emeric Brunf282a812012-09-21 15:27:54 +0200108/* bits 0xFFFF0000 are reserved to store verify errors */
109
110/* Verify errors macros */
111#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
112#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
113#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
114
115#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
116#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
117#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +0200118
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200119/* ssl_methods flags for ssl options */
120#define MC_SSL_O_ALL 0x0000
121#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
122#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
123#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
124#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200125#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200126
127/* ssl_methods versions */
128enum {
129 CONF_TLSV_NONE = 0,
130 CONF_TLSV_MIN = 1,
131 CONF_SSLV3 = 1,
132 CONF_TLSV10 = 2,
133 CONF_TLSV11 = 3,
134 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200135 CONF_TLSV13 = 5,
136 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200137};
138
Emeric Brun850efd52014-01-29 12:24:34 +0100139/* server and bind verify method, it uses a global value as default */
140enum {
141 SSL_SOCK_VERIFY_DEFAULT = 0,
142 SSL_SOCK_VERIFY_REQUIRED = 1,
143 SSL_SOCK_VERIFY_OPTIONAL = 2,
144 SSL_SOCK_VERIFY_NONE = 3,
145};
146
William Lallemand3f85c9a2017-10-09 16:30:50 +0200147
Willy Tarreau71b734c2014-01-28 15:19:44 +0100148int sslconns = 0;
149int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100150static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100151int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200152
Willy Tarreauef934602016-12-22 23:12:01 +0100153static struct {
154 char *crt_base; /* base directory path for certificates */
155 char *ca_base; /* base directory path for CAs and CRLs */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000156 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100157
158 char *listen_default_ciphers;
159 char *connect_default_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200160#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200161 char *listen_default_ciphersuites;
162 char *connect_default_ciphersuites;
163#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100164 int listen_default_ssloptions;
165 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200166 struct tls_version_filter listen_default_sslmethods;
167 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100168
169 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
170 unsigned int life_time; /* SSL session lifetime in seconds */
171 unsigned int max_record; /* SSL max record size */
172 unsigned int default_dh_param; /* SSL maximum DH parameter size */
173 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100174 int capture_cipherlist; /* Size of the cipherlist buffer. */
Willy Tarreauef934602016-12-22 23:12:01 +0100175} global_ssl = {
176#ifdef LISTEN_DEFAULT_CIPHERS
177 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
178#endif
179#ifdef CONNECT_DEFAULT_CIPHERS
180 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
181#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200182#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200183#ifdef LISTEN_DEFAULT_CIPHERSUITES
184 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
185#endif
186#ifdef CONNECT_DEFAULT_CIPHERSUITES
187 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
188#endif
189#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100190 .listen_default_ssloptions = BC_SSL_O_NONE,
191 .connect_default_ssloptions = SRV_SSL_O_NONE,
192
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200193 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
194 .listen_default_sslmethods.min = CONF_TLSV_NONE,
195 .listen_default_sslmethods.max = CONF_TLSV_NONE,
196 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
197 .connect_default_sslmethods.min = CONF_TLSV_NONE,
198 .connect_default_sslmethods.max = CONF_TLSV_NONE,
199
Willy Tarreauef934602016-12-22 23:12:01 +0100200#ifdef DEFAULT_SSL_MAX_RECORD
201 .max_record = DEFAULT_SSL_MAX_RECORD,
202#endif
203 .default_dh_param = SSL_DEFAULT_DH_PARAM,
204 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100205 .capture_cipherlist = 0,
Willy Tarreauef934602016-12-22 23:12:01 +0100206};
207
Olivier Houcharda8955d52019-04-07 22:00:38 +0200208static BIO_METHOD *ha_meth;
209
Olivier Houchard66ab4982019-02-26 18:37:15 +0100210struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200211 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100212 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200213 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200214 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100215 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200216 struct wait_event wait_event;
217 struct wait_event *recv_wait;
218 struct wait_event *send_wait;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100219 int xprt_st; /* transport layer state, initialized to zero */
220 int tmp_early_data; /* 1st byte of early data, if any */
221 int sent_early_data; /* Amount of early data we sent so far */
222
Olivier Houchard66ab4982019-02-26 18:37:15 +0100223};
224
225DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
226
Olivier Houchardea8dd942019-05-20 14:02:16 +0200227static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200228static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200229
Olivier Houcharda8955d52019-04-07 22:00:38 +0200230/* Methods to implement OpenSSL BIO */
231static int ha_ssl_write(BIO *h, const char *buf, int num)
232{
233 struct buffer tmpbuf;
234 struct ssl_sock_ctx *ctx;
235 int ret;
236
237 ctx = BIO_get_data(h);
238 tmpbuf.size = num;
239 tmpbuf.area = (void *)(uintptr_t)buf;
240 tmpbuf.data = num;
241 tmpbuf.head = 0;
242 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200243 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200244 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200245 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200246 } else if (ret == 0)
247 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200248 return ret;
249}
250
251static int ha_ssl_gets(BIO *h, char *buf, int size)
252{
253
254 return 0;
255}
256
257static int ha_ssl_puts(BIO *h, const char *str)
258{
259
260 return ha_ssl_write(h, str, strlen(str));
261}
262
263static int ha_ssl_read(BIO *h, char *buf, int size)
264{
265 struct buffer tmpbuf;
266 struct ssl_sock_ctx *ctx;
267 int ret;
268
269 ctx = BIO_get_data(h);
270 tmpbuf.size = size;
271 tmpbuf.area = buf;
272 tmpbuf.data = 0;
273 tmpbuf.head = 0;
274 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200275 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200276 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200277 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200278 } else if (ret == 0)
279 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200280
281 return ret;
282}
283
284static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
285{
286 int ret = 0;
287 switch (cmd) {
288 case BIO_CTRL_DUP:
289 case BIO_CTRL_FLUSH:
290 ret = 1;
291 break;
292 }
293 return ret;
294}
295
296static int ha_ssl_new(BIO *h)
297{
298 BIO_set_init(h, 1);
299 BIO_set_data(h, NULL);
300 BIO_clear_flags(h, ~0);
301 return 1;
302}
303
304static int ha_ssl_free(BIO *data)
305{
306
307 return 1;
308}
309
310
Willy Tarreau5db847a2019-05-09 14:13:35 +0200311#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100312
Emeric Brun821bb9b2017-06-15 16:37:39 +0200313static HA_RWLOCK_T *ssl_rwlocks;
314
315
316unsigned long ssl_id_function(void)
317{
318 return (unsigned long)tid;
319}
320
321void ssl_locking_function(int mode, int n, const char * file, int line)
322{
323 if (mode & CRYPTO_LOCK) {
324 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100325 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200326 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100327 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200328 }
329 else {
330 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200332 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100333 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200334 }
335}
336
337static int ssl_locking_init(void)
338{
339 int i;
340
341 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
342 if (!ssl_rwlocks)
343 return -1;
344
345 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100346 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200347
348 CRYPTO_set_id_callback(ssl_id_function);
349 CRYPTO_set_locking_callback(ssl_locking_function);
350
351 return 0;
352}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100353
Emeric Brun821bb9b2017-06-15 16:37:39 +0200354#endif
355
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
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200465#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
yanbzhube2774d2015-12-10 15:07:30 -0500466/*
467 * struct alignment works here such that the key.key is the same as key_data
468 * Do not change the placement of key_data
469 */
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200470struct certificate_ocsp {
471 struct ebmb_node key;
472 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
Willy Tarreau83061a82018-07-13 11:56:34 +0200473 struct buffer response;
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200474 long expire;
475};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200476
yanbzhube2774d2015-12-10 15:07:30 -0500477struct ocsp_cbk_arg {
478 int is_single;
479 int single_kt;
480 union {
481 struct certificate_ocsp *s_ocsp;
482 /*
483 * m_ocsp will have multiple entries dependent on key type
484 * Entry 0 - DSA
485 * Entry 1 - ECDSA
486 * Entry 2 - RSA
487 */
488 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
489 };
490};
491
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200492#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000493static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
494{
495 int err_code = ERR_ABORT;
496 ENGINE *engine;
497 struct ssl_engine_list *el;
498
499 /* grab the structural reference to the engine */
500 engine = ENGINE_by_id(engine_id);
501 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100502 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000503 goto fail_get;
504 }
505
506 if (!ENGINE_init(engine)) {
507 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100508 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000509 goto fail_init;
510 }
511
512 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100513 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000514 goto fail_set_method;
515 }
516
517 el = calloc(1, sizeof(*el));
518 el->e = engine;
519 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100520 nb_engines++;
521 if (global_ssl.async)
522 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000523 return 0;
524
525fail_set_method:
526 /* release the functional reference from ENGINE_init() */
527 ENGINE_finish(engine);
528
529fail_init:
530 /* release the structural reference from ENGINE_by_id() */
531 ENGINE_free(engine);
532
533fail_get:
534 return err_code;
535}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200536#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000537
Willy Tarreau5db847a2019-05-09 14:13:35 +0200538#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200539/*
540 * openssl async fd handler
541 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200542void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000543{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200544 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000545
Emeric Brun3854e012017-05-17 20:42:48 +0200546 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000547 * to poll this fd until it is requested
548 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000549 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000550 fd_cant_recv(fd);
551
552 /* crypto engine is available, let's notify the associated
553 * connection that it can pursue its processing.
554 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200555 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000556}
557
Emeric Brun3854e012017-05-17 20:42:48 +0200558/*
559 * openssl async delayed SSL_free handler
560 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200561void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000562{
563 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200564 OSSL_ASYNC_FD all_fd[32];
565 size_t num_all_fds = 0;
566 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000567
Emeric Brun3854e012017-05-17 20:42:48 +0200568 /* We suppose that the async job for a same SSL *
569 * are serialized. So if we are awake it is
570 * because the running job has just finished
571 * and we can remove all async fds safely
572 */
573 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
574 if (num_all_fds > 32) {
575 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
576 return;
577 }
578
579 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
580 for (i=0 ; i < num_all_fds ; i++)
581 fd_remove(all_fd[i]);
582
583 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000584 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100585 _HA_ATOMIC_SUB(&sslconns, 1);
586 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000587}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000588/*
Emeric Brun3854e012017-05-17 20:42:48 +0200589 * function used to manage a returned SSL_ERROR_WANT_ASYNC
590 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000591 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200592static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000593{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100594 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200595 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200596 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000597 size_t num_add_fds = 0;
598 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200599 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000600
601 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
602 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200603 if (num_add_fds > 32 || num_del_fds > 32) {
604 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 +0000605 return;
606 }
607
Emeric Brun3854e012017-05-17 20:42:48 +0200608 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000609
Emeric Brun3854e012017-05-17 20:42:48 +0200610 /* We remove unused fds from the fdtab */
611 for (i=0 ; i < num_del_fds ; i++)
612 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000613
Emeric Brun3854e012017-05-17 20:42:48 +0200614 /* We add new fds to the fdtab */
615 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200616 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000617 }
618
Emeric Brun3854e012017-05-17 20:42:48 +0200619 num_add_fds = 0;
620 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
621 if (num_add_fds > 32) {
622 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
623 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000624 }
Emeric Brun3854e012017-05-17 20:42:48 +0200625
626 /* We activate the polling for all known async fds */
627 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000628 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200629 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000630 /* To ensure that the fd cache won't be used
631 * We'll prefer to catch a real RD event
632 * because handling an EAGAIN on this fd will
633 * result in a context switch and also
634 * some engines uses a fd in blocking mode.
635 */
636 fd_cant_recv(add_fd[i]);
637 }
Emeric Brun3854e012017-05-17 20:42:48 +0200638
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000639}
640#endif
641
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200642/*
643 * This function returns the number of seconds elapsed
644 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
645 * date presented un ASN1_GENERALIZEDTIME.
646 *
647 * In parsing error case, it returns -1.
648 */
649static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
650{
651 long epoch;
652 char *p, *end;
653 const unsigned short month_offset[12] = {
654 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
655 };
656 int year, month;
657
658 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
659
660 p = (char *)d->data;
661 end = p + d->length;
662
663 if (end - p < 4) return -1;
664 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
665 p += 4;
666 if (end - p < 2) return -1;
667 month = 10 * (p[0] - '0') + p[1] - '0';
668 if (month < 1 || month > 12) return -1;
669 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
670 We consider leap years and the current month (<marsh or not) */
671 epoch = ( ((year - 1970) * 365)
672 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
673 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
674 + month_offset[month-1]
675 ) * 24 * 60 * 60;
676 p += 2;
677 if (end - p < 2) return -1;
678 /* Add the number of seconds of completed days of current month */
679 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
680 p += 2;
681 if (end - p < 2) return -1;
682 /* Add the completed hours of the current day */
683 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
684 p += 2;
685 if (end - p < 2) return -1;
686 /* Add the completed minutes of the current hour */
687 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
688 p += 2;
689 if (p == end) return -1;
690 /* Test if there is available seconds */
691 if (p[0] < '0' || p[0] > '9')
692 goto nosec;
693 if (end - p < 2) return -1;
694 /* Add the seconds of the current minute */
695 epoch += 10 * (p[0] - '0') + p[1] - '0';
696 p += 2;
697 if (p == end) return -1;
698 /* Ignore seconds float part if present */
699 if (p[0] == '.') {
700 do {
701 if (++p == end) return -1;
702 } while (p[0] >= '0' && p[0] <= '9');
703 }
704
705nosec:
706 if (p[0] == 'Z') {
707 if (end - p != 1) return -1;
708 return epoch;
709 }
710 else if (p[0] == '+') {
711 if (end - p != 5) return -1;
712 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700713 return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200714 }
715 else if (p[0] == '-') {
716 if (end - p != 5) return -1;
717 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700718 return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200719 }
720
721 return -1;
722}
723
Emeric Brun1d3865b2014-06-20 15:37:32 +0200724static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200725
726/* This function starts to check if the OCSP response (in DER format) contained
727 * in chunk 'ocsp_response' is valid (else exits on error).
728 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
729 * contained in the OCSP Response and exits on error if no match.
730 * If it's a valid OCSP Response:
731 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
732 * pointed by 'ocsp'.
733 * If 'ocsp' is NULL, the function looks up into the OCSP response's
734 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
735 * from the response) and exits on error if not found. Finally, If an OCSP response is
736 * already present in the container, it will be overwritten.
737 *
738 * Note: OCSP response containing more than one OCSP Single response is not
739 * considered valid.
740 *
741 * Returns 0 on success, 1 in error case.
742 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200743static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
744 struct certificate_ocsp *ocsp,
745 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200746{
747 OCSP_RESPONSE *resp;
748 OCSP_BASICRESP *bs = NULL;
749 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200750 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200751 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200752 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200753 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200754 int reason;
755 int ret = 1;
756
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200757 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
758 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200759 if (!resp) {
760 memprintf(err, "Unable to parse OCSP response");
761 goto out;
762 }
763
764 rc = OCSP_response_status(resp);
765 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
766 memprintf(err, "OCSP response status not successful");
767 goto out;
768 }
769
770 bs = OCSP_response_get1_basic(resp);
771 if (!bs) {
772 memprintf(err, "Failed to get basic response from OCSP Response");
773 goto out;
774 }
775
776 count_sr = OCSP_resp_count(bs);
777 if (count_sr > 1) {
778 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
779 goto out;
780 }
781
782 sr = OCSP_resp_get0(bs, 0);
783 if (!sr) {
784 memprintf(err, "Failed to get OCSP single response");
785 goto out;
786 }
787
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200788 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
789
Emeric Brun4147b2e2014-06-16 18:36:30 +0200790 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200791 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200792 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200793 goto out;
794 }
795
Emeric Brun13a6b482014-06-20 15:44:34 +0200796 if (!nextupd) {
797 memprintf(err, "OCSP single response: missing nextupdate");
798 goto out;
799 }
800
Emeric Brunc8b27b62014-06-19 14:16:17 +0200801 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200802 if (!rc) {
803 memprintf(err, "OCSP single response: no longer valid.");
804 goto out;
805 }
806
807 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200808 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200809 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
810 goto out;
811 }
812 }
813
814 if (!ocsp) {
815 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
816 unsigned char *p;
817
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200818 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200819 if (!rc) {
820 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
821 goto out;
822 }
823
824 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
825 memprintf(err, "OCSP single response: Certificate ID too long");
826 goto out;
827 }
828
829 p = key;
830 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200831 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200832 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
833 if (!ocsp) {
834 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
835 goto out;
836 }
837 }
838
839 /* According to comments on "chunk_dup", the
840 previous chunk buffer will be freed */
841 if (!chunk_dup(&ocsp->response, ocsp_response)) {
842 memprintf(err, "OCSP response: Memory allocation error");
843 goto out;
844 }
845
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200846 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
847
Emeric Brun4147b2e2014-06-16 18:36:30 +0200848 ret = 0;
849out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +0100850 ERR_clear_error();
851
Emeric Brun4147b2e2014-06-16 18:36:30 +0200852 if (bs)
853 OCSP_BASICRESP_free(bs);
854
855 if (resp)
856 OCSP_RESPONSE_free(resp);
857
858 return ret;
859}
860/*
861 * External function use to update the OCSP response in the OCSP response's
862 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
863 * to update in DER format.
864 *
865 * Returns 0 on success, 1 in error case.
866 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200867int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200868{
869 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
870}
871
872/*
873 * This function load the OCSP Resonse in DER format contained in file at
874 * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response'
875 *
876 * Returns 0 on success, 1 in error case.
877 */
878static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err)
879{
880 int fd = -1;
881 int r = 0;
882 int ret = 1;
883
884 fd = open(ocsp_path, O_RDONLY);
885 if (fd == -1) {
886 memprintf(err, "Error opening OCSP response file");
887 goto end;
888 }
889
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200890 trash.data = 0;
891 while (trash.data < trash.size) {
892 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200893 if (r < 0) {
894 if (errno == EINTR)
895 continue;
896
897 memprintf(err, "Error reading OCSP response from file");
898 goto end;
899 }
900 else if (r == 0) {
901 break;
902 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200903 trash.data += r;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200904 }
905
906 close(fd);
907 fd = -1;
908
909 ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err);
910end:
911 if (fd != -1)
912 close(fd);
913
914 return ret;
915}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100916#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +0200917
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100918#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
919static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc)
920{
Christopher Faulet16f45c82018-02-16 11:23:49 +0100921 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +0100922 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100923 struct connection *conn;
924 int head;
925 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100926 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100927
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200928 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +0200929 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100930 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
931
932 keys = ref->tlskeys;
933 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100934
935 if (enc) {
936 memcpy(key_name, keys[head].name, 16);
937
938 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +0100939 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100940
Emeric Brun9e754772019-01-10 17:51:55 +0100941 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100942
Emeric Brun9e754772019-01-10 17:51:55 +0100943 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
944 goto end;
945
Willy Tarreau9356dac2019-05-10 09:22:53 +0200946 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100947 ret = 1;
948 }
949 else if (ref->key_size_bits == 256 ) {
950
951 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
952 goto end;
953
Willy Tarreau9356dac2019-05-10 09:22:53 +0200954 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100955 ret = 1;
956 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100957 } else {
958 for (i = 0; i < TLS_TICKETS_NO; i++) {
959 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
960 goto found;
961 }
Christopher Faulet16f45c82018-02-16 11:23:49 +0100962 ret = 0;
963 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100964
Christopher Faulet16f45c82018-02-16 11:23:49 +0100965 found:
Emeric Brun9e754772019-01-10 17:51:55 +0100966 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +0200967 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100968 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
969 goto end;
970 /* 2 for key renewal, 1 if current key is still valid */
971 ret = i ? 2 : 1;
972 }
973 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +0200974 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +0100975 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
976 goto end;
977 /* 2 for key renewal, 1 if current key is still valid */
978 ret = i ? 2 : 1;
979 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100980 }
Emeric Brun9e754772019-01-10 17:51:55 +0100981
Christopher Faulet16f45c82018-02-16 11:23:49 +0100982 end:
983 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
984 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200985}
986
987struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
988{
989 struct tls_keys_ref *ref;
990
991 list_for_each_entry(ref, &tlskeys_reference, list)
992 if (ref->filename && strcmp(filename, ref->filename) == 0)
993 return ref;
994 return NULL;
995}
996
997struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
998{
999 struct tls_keys_ref *ref;
1000
1001 list_for_each_entry(ref, &tlskeys_reference, list)
1002 if (ref->unique_id == unique_id)
1003 return ref;
1004 return NULL;
1005}
1006
Emeric Brun9e754772019-01-10 17:51:55 +01001007/* Update the key into ref: if keysize doesnt
1008 * match existing ones, this function returns -1
1009 * else it returns 0 on success.
1010 */
1011int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001012 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001013{
Emeric Brun9e754772019-01-10 17:51:55 +01001014 if (ref->key_size_bits == 128) {
1015 if (tlskey->data != sizeof(struct tls_sess_key_128))
1016 return -1;
1017 }
1018 else if (ref->key_size_bits == 256) {
1019 if (tlskey->data != sizeof(struct tls_sess_key_256))
1020 return -1;
1021 }
1022 else
1023 return -1;
1024
Christopher Faulet16f45c82018-02-16 11:23:49 +01001025 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001026 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1027 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001028 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1029 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001030
1031 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001032}
1033
Willy Tarreau83061a82018-07-13 11:56:34 +02001034int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001035{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001036 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1037
1038 if(!ref) {
1039 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1040 return 1;
1041 }
Emeric Brun9e754772019-01-10 17:51:55 +01001042 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1043 memprintf(err, "Invalid key size");
1044 return 1;
1045 }
1046
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001047 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001048}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001049
1050/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001051 * automatic ids. It's called just after the basic checks. It returns
1052 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001053 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001054static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001055{
1056 int i = 0;
1057 struct tls_keys_ref *ref, *ref2, *ref3;
1058 struct list tkr = LIST_HEAD_INIT(tkr);
1059
1060 list_for_each_entry(ref, &tlskeys_reference, list) {
1061 if (ref->unique_id == -1) {
1062 /* Look for the first free id. */
1063 while (1) {
1064 list_for_each_entry(ref2, &tlskeys_reference, list) {
1065 if (ref2->unique_id == i) {
1066 i++;
1067 break;
1068 }
1069 }
1070 if (&ref2->list == &tlskeys_reference)
1071 break;
1072 }
1073
1074 /* Uses the unique id and increment it for the next entry. */
1075 ref->unique_id = i;
1076 i++;
1077 }
1078 }
1079
1080 /* This sort the reference list by id. */
1081 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1082 LIST_DEL(&ref->list);
1083 list_for_each_entry(ref3, &tkr, list) {
1084 if (ref->unique_id < ref3->unique_id) {
1085 LIST_ADDQ(&ref3->list, &ref->list);
1086 break;
1087 }
1088 }
1089 if (&ref3->list == &tkr)
1090 LIST_ADDQ(&tkr, &ref->list);
1091 }
1092
1093 /* swap root */
1094 LIST_ADD(&tkr, &tlskeys_reference);
1095 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001096 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001097}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001098#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1099
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001100#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001101int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1102{
1103 switch (evp_keytype) {
1104 case EVP_PKEY_RSA:
1105 return 2;
1106 case EVP_PKEY_DSA:
1107 return 0;
1108 case EVP_PKEY_EC:
1109 return 1;
1110 }
1111
1112 return -1;
1113}
1114
Emeric Brun4147b2e2014-06-16 18:36:30 +02001115/*
1116 * Callback used to set OCSP status extension content in server hello.
1117 */
1118int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1119{
yanbzhube2774d2015-12-10 15:07:30 -05001120 struct certificate_ocsp *ocsp;
1121 struct ocsp_cbk_arg *ocsp_arg;
1122 char *ssl_buf;
1123 EVP_PKEY *ssl_pkey;
1124 int key_type;
1125 int index;
1126
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001127 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001128
1129 ssl_pkey = SSL_get_privatekey(ssl);
1130 if (!ssl_pkey)
1131 return SSL_TLSEXT_ERR_NOACK;
1132
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001133 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001134
1135 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1136 ocsp = ocsp_arg->s_ocsp;
1137 else {
1138 /* For multiple certs per context, we have to find the correct OCSP response based on
1139 * the certificate type
1140 */
1141 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1142
1143 if (index < 0)
1144 return SSL_TLSEXT_ERR_NOACK;
1145
1146 ocsp = ocsp_arg->m_ocsp[index];
1147
1148 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001149
1150 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001151 !ocsp->response.area ||
1152 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001153 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001154 return SSL_TLSEXT_ERR_NOACK;
1155
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001156 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001157 if (!ssl_buf)
1158 return SSL_TLSEXT_ERR_NOACK;
1159
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001160 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1161 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001162
1163 return SSL_TLSEXT_ERR_OK;
1164}
1165
1166/*
1167 * This function enables the handling of OCSP status extension on 'ctx' if a
1168 * file name 'cert_path' suffixed using ".ocsp" is present.
1169 * To enable OCSP status extension, the issuer's certificate is mandatory.
1170 * It should be present in the certificate's extra chain builded from file
1171 * 'cert_path'. If not found, the issuer certificate is loaded from a file
1172 * named 'cert_path' suffixed using '.issuer'.
1173 *
1174 * In addition, ".ocsp" file content is loaded as a DER format of an OCSP
1175 * response. If file is empty or content is not a valid OCSP response,
1176 * OCSP status extension is enabled but OCSP response is ignored (a warning
1177 * is displayed).
1178 *
1179 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001180 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001181 */
1182static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path)
1183{
1184
1185 BIO *in = NULL;
1186 X509 *x, *xi = NULL, *issuer = NULL;
1187 STACK_OF(X509) *chain = NULL;
1188 OCSP_CERTID *cid = NULL;
1189 SSL *ssl;
1190 char ocsp_path[MAXPATHLEN+1];
1191 int i, ret = -1;
1192 struct stat st;
1193 struct certificate_ocsp *ocsp = NULL, *iocsp;
1194 char *warn = NULL;
1195 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001196 pem_password_cb *passwd_cb;
1197 void *passwd_cb_userdata;
1198 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001199
1200 snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path);
1201
1202 if (stat(ocsp_path, &st))
1203 return 1;
1204
1205 ssl = SSL_new(ctx);
1206 if (!ssl)
1207 goto out;
1208
1209 x = SSL_get_certificate(ssl);
1210 if (!x)
1211 goto out;
1212
1213 /* Try to lookup for issuer in certificate extra chain */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001214 SSL_CTX_get_extra_chain_certs(ctx, &chain);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001215 for (i = 0; i < sk_X509_num(chain); i++) {
1216 issuer = sk_X509_value(chain, i);
1217 if (X509_check_issued(issuer, x) == X509_V_OK)
1218 break;
1219 else
1220 issuer = NULL;
1221 }
1222
1223 /* If not found try to load issuer from a suffixed file */
1224 if (!issuer) {
1225 char issuer_path[MAXPATHLEN+1];
1226
1227 in = BIO_new(BIO_s_file());
1228 if (!in)
1229 goto out;
1230
1231 snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path);
1232 if (BIO_read_filename(in, issuer_path) <= 0)
1233 goto out;
1234
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001235 passwd_cb = SSL_CTX_get_default_passwd_cb(ctx);
1236 passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx);
1237
1238 xi = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001239 if (!xi)
1240 goto out;
1241
1242 if (X509_check_issued(xi, x) != X509_V_OK)
1243 goto out;
1244
1245 issuer = xi;
1246 }
1247
1248 cid = OCSP_cert_to_id(0, x, issuer);
1249 if (!cid)
1250 goto out;
1251
1252 i = i2d_OCSP_CERTID(cid, NULL);
1253 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1254 goto out;
1255
Vincent Bernat02779b62016-04-03 13:48:43 +02001256 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001257 if (!ocsp)
1258 goto out;
1259
1260 p = ocsp->key_data;
1261 i2d_OCSP_CERTID(cid, &p);
1262
1263 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1264 if (iocsp == ocsp)
1265 ocsp = NULL;
1266
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001267#ifndef SSL_CTX_get_tlsext_status_cb
1268# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1269 *cb = (void (*) (void))ctx->tlsext_status_cb;
1270#endif
1271 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1272
1273 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001274 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001275 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001276
1277 cb_arg->is_single = 1;
1278 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001279
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001280 pkey = X509_get_pubkey(x);
1281 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1282 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001283
1284 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1285 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1286 } else {
1287 /*
1288 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1289 * Update that cb_arg with the new cert's staple
1290 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001291 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001292 struct certificate_ocsp *tmp_ocsp;
1293 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001294 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001295 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001296
1297#ifdef SSL_CTX_get_tlsext_status_arg
1298 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1299#else
1300 cb_arg = ctx->tlsext_status_arg;
1301#endif
yanbzhube2774d2015-12-10 15:07:30 -05001302
1303 /*
1304 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1305 * the order of operations below matter, take care when changing it
1306 */
1307 tmp_ocsp = cb_arg->s_ocsp;
1308 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1309 cb_arg->s_ocsp = NULL;
1310 cb_arg->m_ocsp[index] = tmp_ocsp;
1311 cb_arg->is_single = 0;
1312 cb_arg->single_kt = 0;
1313
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001314 pkey = X509_get_pubkey(x);
1315 key_type = EVP_PKEY_base_id(pkey);
1316 EVP_PKEY_free(pkey);
1317
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001318 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001319 if (index >= 0 && !cb_arg->m_ocsp[index])
1320 cb_arg->m_ocsp[index] = iocsp;
1321
1322 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001323
1324 ret = 0;
1325
1326 warn = NULL;
1327 if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) {
1328 memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001329 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001330 }
1331
1332out:
1333 if (ssl)
1334 SSL_free(ssl);
1335
1336 if (in)
1337 BIO_free(in);
1338
1339 if (xi)
1340 X509_free(xi);
1341
1342 if (cid)
1343 OCSP_CERTID_free(cid);
1344
1345 if (ocsp)
1346 free(ocsp);
1347
1348 if (warn)
1349 free(warn);
1350
1351
1352 return ret;
1353}
1354
1355#endif
1356
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001357#ifdef OPENSSL_IS_BORINGSSL
1358static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path)
1359{
1360 char ocsp_path[MAXPATHLEN+1];
1361 struct stat st;
1362 int fd = -1, r = 0;
1363
1364 snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path);
1365 if (stat(ocsp_path, &st))
1366 return 0;
1367
1368 fd = open(ocsp_path, O_RDONLY);
1369 if (fd == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001370 ha_warning("Error opening OCSP response file %s.\n", ocsp_path);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001371 return -1;
1372 }
1373
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001374 trash.data = 0;
1375 while (trash.data < trash.size) {
1376 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001377 if (r < 0) {
1378 if (errno == EINTR)
1379 continue;
Christopher Faulet767a84b2017-11-24 16:50:31 +01001380 ha_warning("Error reading OCSP response from file %s.\n", ocsp_path);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001381 close(fd);
1382 return -1;
1383 }
1384 else if (r == 0) {
1385 break;
1386 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001387 trash.data += r;
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001388 }
1389 close(fd);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001390 return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *) trash.area,
1391 trash.data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001392}
1393#endif
1394
Willy Tarreau5db847a2019-05-09 14:13:35 +02001395#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001396
1397#define CT_EXTENSION_TYPE 18
1398
1399static int sctl_ex_index = -1;
1400
1401/*
1402 * Try to parse Signed Certificate Timestamp List structure. This function
1403 * makes only basic test if the data seems like SCTL. No signature validation
1404 * is performed.
1405 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001406static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001407{
1408 int ret = 1;
1409 int len, pos, sct_len;
1410 unsigned char *data;
1411
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001412 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001413 goto out;
1414
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001415 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001416 len = (data[0] << 8) | data[1];
1417
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001418 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001419 goto out;
1420
1421 data = data + 2;
1422 pos = 0;
1423 while (pos < len) {
1424 if (len - pos < 2)
1425 goto out;
1426
1427 sct_len = (data[pos] << 8) | data[pos + 1];
1428 if (pos + sct_len + 2 > len)
1429 goto out;
1430
1431 pos += sct_len + 2;
1432 }
1433
1434 ret = 0;
1435
1436out:
1437 return ret;
1438}
1439
Willy Tarreau83061a82018-07-13 11:56:34 +02001440static int ssl_sock_load_sctl_from_file(const char *sctl_path,
1441 struct buffer **sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001442{
1443 int fd = -1;
1444 int r = 0;
1445 int ret = 1;
1446
1447 *sctl = NULL;
1448
1449 fd = open(sctl_path, O_RDONLY);
1450 if (fd == -1)
1451 goto end;
1452
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001453 trash.data = 0;
1454 while (trash.data < trash.size) {
1455 r = read(fd, trash.area + trash.data, trash.size - trash.data);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001456 if (r < 0) {
1457 if (errno == EINTR)
1458 continue;
1459
1460 goto end;
1461 }
1462 else if (r == 0) {
1463 break;
1464 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001465 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001466 }
1467
1468 ret = ssl_sock_parse_sctl(&trash);
1469 if (ret)
1470 goto end;
1471
Vincent Bernat02779b62016-04-03 13:48:43 +02001472 *sctl = calloc(1, sizeof(**sctl));
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001473 if (!chunk_dup(*sctl, &trash)) {
1474 free(*sctl);
1475 *sctl = NULL;
1476 goto end;
1477 }
1478
1479end:
1480 if (fd != -1)
1481 close(fd);
1482
1483 return ret;
1484}
1485
1486int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1487{
Willy Tarreau83061a82018-07-13 11:56:34 +02001488 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001489
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001490 *out = (unsigned char *) sctl->area;
1491 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001492
1493 return 1;
1494}
1495
1496int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1497{
1498 return 1;
1499}
1500
1501static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path)
1502{
1503 char sctl_path[MAXPATHLEN+1];
1504 int ret = -1;
1505 struct stat st;
Willy Tarreau83061a82018-07-13 11:56:34 +02001506 struct buffer *sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001507
1508 snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path);
1509
1510 if (stat(sctl_path, &st))
1511 return 1;
1512
1513 if (ssl_sock_load_sctl_from_file(sctl_path, &sctl))
1514 goto out;
1515
1516 if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) {
1517 free(sctl);
1518 goto out;
1519 }
1520
1521 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1522
1523 ret = 0;
1524
1525out:
1526 return ret;
1527}
1528
1529#endif
1530
Emeric Brune1f38db2012-09-03 20:36:47 +02001531void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1532{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001533 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001534 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001535 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001536 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001537
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001538#ifndef SSL_OP_NO_RENEGOTIATION
1539 /* Please note that BoringSSL defines this macro to zero so don't
1540 * change this to #if and do not assign a default value to this macro!
1541 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001542 if (where & SSL_CB_HANDSHAKE_START) {
1543 /* Disable renegotiation (CVE-2009-3555) */
Olivier Houchard90084a12017-11-23 18:21:29 +01001544 if ((conn->flags & (CO_FL_CONNECTED | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_CONNECTED) {
Emeric Brune1f38db2012-09-03 20:36:47 +02001545 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001546 conn->err_code = CO_ER_SSL_RENEG;
1547 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001548 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001549#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001550
1551 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001552 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001553 /* Long certificate chains optimz
1554 If write and read bios are differents, we
1555 consider that the buffering was activated,
1556 so we rise the output buffer size from 4k
1557 to 16k */
1558 write_bio = SSL_get_wbio(ssl);
1559 if (write_bio != SSL_get_rbio(ssl)) {
1560 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001561 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001562 }
1563 }
1564 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001565}
1566
Emeric Brune64aef12012-09-21 13:15:06 +02001567/* Callback is called for each certificate of the chain during a verify
1568 ok is set to 1 if preverify detect no error on current certificate.
1569 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001570int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001571{
1572 SSL *ssl;
1573 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001574 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001575 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001576
1577 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001578 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001579
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001580 ctx = conn->xprt_ctx;
1581
1582 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001583
Emeric Brun81c00f02012-09-21 14:31:21 +02001584 if (ok) /* no errors */
1585 return ok;
1586
1587 depth = X509_STORE_CTX_get_error_depth(x_store);
1588 err = X509_STORE_CTX_get_error(x_store);
1589
1590 /* check if CA error needs to be ignored */
1591 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001592 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1593 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1594 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001595 }
1596
Willy Tarreau07d94e42018-09-20 10:57:52 +02001597 if (__objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001598 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001599 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001600 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001601 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001602
Willy Tarreau20879a02012-12-03 16:32:10 +01001603 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001604 return 0;
1605 }
1606
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001607 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1608 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001609
Emeric Brun81c00f02012-09-21 14:31:21 +02001610 /* check if certificate error needs to be ignored */
Willy Tarreau07d94e42018-09-20 10:57:52 +02001611 if (__objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001612 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001613 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001614 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001615 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001616
Willy Tarreau20879a02012-12-03 16:32:10 +01001617 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001618 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001619}
1620
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001621static inline
1622void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001623 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001624{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001625 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001626 unsigned char *msg;
1627 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001628 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001629
1630 /* This function is called for "from client" and "to server"
1631 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001632 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001633 */
1634
1635 /* "write_p" is set to 0 is the bytes are received messages,
1636 * otherwise it is set to 1.
1637 */
1638 if (write_p != 0)
1639 return;
1640
1641 /* content_type contains the type of message received or sent
1642 * according with the SSL/TLS protocol spec. This message is
1643 * encoded with one byte. The value 256 (two bytes) is used
1644 * for designing the SSL/TLS record layer. According with the
1645 * rfc6101, the expected message (other than 256) are:
1646 * - change_cipher_spec(20)
1647 * - alert(21)
1648 * - handshake(22)
1649 * - application_data(23)
1650 * - (255)
1651 * We are interessed by the handshake and specially the client
1652 * hello.
1653 */
1654 if (content_type != 22)
1655 return;
1656
1657 /* The message length is at least 4 bytes, containing the
1658 * message type and the message length.
1659 */
1660 if (len < 4)
1661 return;
1662
1663 /* First byte of the handshake message id the type of
1664 * message. The konwn types are:
1665 * - hello_request(0)
1666 * - client_hello(1)
1667 * - server_hello(2)
1668 * - certificate(11)
1669 * - server_key_exchange (12)
1670 * - certificate_request(13)
1671 * - server_hello_done(14)
1672 * We are interested by the client hello.
1673 */
1674 msg = (unsigned char *)buf;
1675 if (msg[0] != 1)
1676 return;
1677
1678 /* Next three bytes are the length of the message. The total length
1679 * must be this decoded length + 4. If the length given as argument
1680 * is not the same, we abort the protocol dissector.
1681 */
1682 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1683 if (len < rec_len + 4)
1684 return;
1685 msg += 4;
1686 end = msg + rec_len;
1687 if (end < msg)
1688 return;
1689
1690 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1691 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001692 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1693 */
1694 msg += 1 + 1 + 4 + 28;
1695 if (msg > end)
1696 return;
1697
1698 /* Next, is session id:
1699 * if present, we have to jump by length + 1 for the size information
1700 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001701 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001702 if (msg[0] > 0)
1703 msg += msg[0];
1704 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001705 if (msg > end)
1706 return;
1707
1708 /* Next two bytes are the ciphersuite length. */
1709 if (msg + 2 > end)
1710 return;
1711 rec_len = (msg[0] << 8) + msg[1];
1712 msg += 2;
1713 if (msg + rec_len > end || msg + rec_len < msg)
1714 return;
1715
Willy Tarreaubafbe012017-11-24 17:34:44 +01001716 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001717 if (!capture)
1718 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001719 /* Compute the xxh64 of the ciphersuite. */
1720 capture->xxh64 = XXH64(msg, rec_len, 0);
1721
1722 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001723 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1724 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001725 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001726
1727 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001728}
1729
Emeric Brun29f037d2014-04-25 19:05:36 +02001730/* Callback is called for ssl protocol analyse */
1731void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1732{
Emeric Brun29f037d2014-04-25 19:05:36 +02001733#ifdef TLS1_RT_HEARTBEAT
1734 /* test heartbeat received (write_p is set to 0
1735 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001736 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001737 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001738 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001739 const unsigned char *p = buf;
1740 unsigned int payload;
1741
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001742 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001743
1744 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1745 if (*p != TLS1_HB_REQUEST)
1746 return;
1747
Willy Tarreauaeed6722014-04-25 23:59:58 +02001748 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001749 goto kill_it;
1750
1751 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001752 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001753 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001754 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001755 /* We have a clear heartbleed attack (CVE-2014-0160), the
1756 * advertised payload is larger than the advertised packet
1757 * length, so we have garbage in the buffer between the
1758 * payload and the end of the buffer (p+len). We can't know
1759 * if the SSL stack is patched, and we don't know if we can
1760 * safely wipe out the area between p+3+len and payload.
1761 * So instead, we prevent the response from being sent by
1762 * setting the max_send_fragment to 0 and we report an SSL
1763 * error, which will kill this connection. It will be reported
1764 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001765 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1766 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001767 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001768 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1769 return;
1770 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001771#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001772 if (global_ssl.capture_cipherlist > 0)
1773 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001774}
1775
Bernard Spil13c53f82018-02-15 13:34:58 +01001776#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001777static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1778 const unsigned char *in, unsigned int inlen,
1779 void *arg)
1780{
1781 struct server *srv = arg;
1782
1783 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1784 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1785 return SSL_TLSEXT_ERR_OK;
1786 return SSL_TLSEXT_ERR_NOACK;
1787}
1788#endif
1789
1790#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001791/* This callback is used so that the server advertises the list of
1792 * negociable protocols for NPN.
1793 */
1794static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1795 unsigned int *len, void *arg)
1796{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001797 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001798
1799 *data = (const unsigned char *)conf->npn_str;
1800 *len = conf->npn_len;
1801 return SSL_TLSEXT_ERR_OK;
1802}
1803#endif
1804
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001805#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001806/* This callback is used so that the server advertises the list of
1807 * negociable protocols for ALPN.
1808 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001809static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1810 unsigned char *outlen,
1811 const unsigned char *server,
1812 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001813{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001814 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001815
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001816 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1817 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1818 return SSL_TLSEXT_ERR_NOACK;
1819 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001820 return SSL_TLSEXT_ERR_OK;
1821}
1822#endif
1823
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001824#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001825#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001826
Christopher Faulet30548802015-06-11 13:39:32 +02001827/* Create a X509 certificate with the specified servername and serial. This
1828 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001829static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001830ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001831{
Christopher Faulet7969a332015-10-09 11:15:03 +02001832 X509 *cacert = bind_conf->ca_sign_cert;
1833 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001834 SSL_CTX *ssl_ctx = NULL;
1835 X509 *newcrt = NULL;
1836 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001837 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001838 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001839 X509_NAME *name;
1840 const EVP_MD *digest;
1841 X509V3_CTX ctx;
1842 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001843 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001844
Christopher Faulet48a83322017-07-28 16:56:09 +02001845 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001846#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001847 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1848#else
1849 tmp_ssl = SSL_new(bind_conf->default_ctx);
1850 if (tmp_ssl)
1851 pkey = SSL_get_privatekey(tmp_ssl);
1852#endif
1853 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001854 goto mkcert_error;
1855
1856 /* Create the certificate */
1857 if (!(newcrt = X509_new()))
1858 goto mkcert_error;
1859
1860 /* Set version number for the certificate (X509v3) and the serial
1861 * number */
1862 if (X509_set_version(newcrt, 2L) != 1)
1863 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001864 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001865
1866 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001867 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1868 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001869 goto mkcert_error;
1870
1871 /* set public key in the certificate */
1872 if (X509_set_pubkey(newcrt, pkey) != 1)
1873 goto mkcert_error;
1874
1875 /* Set issuer name from the CA */
1876 if (!(name = X509_get_subject_name(cacert)))
1877 goto mkcert_error;
1878 if (X509_set_issuer_name(newcrt, name) != 1)
1879 goto mkcert_error;
1880
1881 /* Set the subject name using the same, but the CN */
1882 name = X509_NAME_dup(name);
1883 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
1884 (const unsigned char *)servername,
1885 -1, -1, 0) != 1) {
1886 X509_NAME_free(name);
1887 goto mkcert_error;
1888 }
1889 if (X509_set_subject_name(newcrt, name) != 1) {
1890 X509_NAME_free(name);
1891 goto mkcert_error;
1892 }
1893 X509_NAME_free(name);
1894
1895 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001896 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001897 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
1898 for (i = 0; i < X509V3_EXT_SIZE; i++) {
1899 X509_EXTENSION *ext;
1900
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001901 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001902 goto mkcert_error;
1903 if (!X509_add_ext(newcrt, ext, -1)) {
1904 X509_EXTENSION_free(ext);
1905 goto mkcert_error;
1906 }
1907 X509_EXTENSION_free(ext);
1908 }
1909
1910 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001911
1912 key_type = EVP_PKEY_base_id(capkey);
1913
1914 if (key_type == EVP_PKEY_DSA)
1915 digest = EVP_sha1();
1916 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001917 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001918 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02001919 digest = EVP_sha256();
1920 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02001921#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02001922 int nid;
1923
1924 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
1925 goto mkcert_error;
1926 if (!(digest = EVP_get_digestbynid(nid)))
1927 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02001928#else
1929 goto mkcert_error;
1930#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02001931 }
1932
Christopher Faulet31af49d2015-06-09 17:29:50 +02001933 if (!(X509_sign(newcrt, capkey, digest)))
1934 goto mkcert_error;
1935
1936 /* Create and set the new SSL_CTX */
1937 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
1938 goto mkcert_error;
1939 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1940 goto mkcert_error;
1941 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
1942 goto mkcert_error;
1943 if (!SSL_CTX_check_private_key(ssl_ctx))
1944 goto mkcert_error;
1945
1946 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02001947
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001948#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001949 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001950#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001951#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
1952 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001953 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001954 EC_KEY *ecc;
1955 int nid;
1956
1957 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
1958 goto end;
1959 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
1960 goto end;
1961 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
1962 EC_KEY_free(ecc);
1963 }
1964#endif
1965 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02001966 return ssl_ctx;
1967
1968 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001969 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001970 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001971 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
1972 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001973 return NULL;
1974}
1975
Christopher Faulet7969a332015-10-09 11:15:03 +02001976SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001977ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02001978{
Willy Tarreau07d94e42018-09-20 10:57:52 +02001979 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01001980 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001981
Olivier Houchard66ab4982019-02-26 18:37:15 +01001982 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02001983}
1984
Christopher Faulet30548802015-06-11 13:39:32 +02001985/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02001986 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02001987SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02001988ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02001989{
1990 struct lru64 *lru = NULL;
1991
1992 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001993 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001994 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02001995 if (lru && lru->domain) {
1996 if (ssl)
1997 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001998 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02001999 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002000 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002001 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002002 }
2003 return NULL;
2004}
2005
Emeric Brun821bb9b2017-06-15 16:37:39 +02002006/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2007 * function is not thread-safe, it should only be used to check if a certificate
2008 * exists in the lru cache (with no warranty it will not be removed by another
2009 * thread). It is kept for backward compatibility. */
2010SSL_CTX *
2011ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2012{
2013 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2014}
2015
Christopher Fauletd2cab922015-07-28 16:03:47 +02002016/* Set a certificate int the LRU cache used to store generated
2017 * certificate. Return 0 on success, otherwise -1 */
2018int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002019ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002020{
2021 struct lru64 *lru = NULL;
2022
2023 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002024 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002025 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002026 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002027 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002028 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002029 }
Christopher Faulet30548802015-06-11 13:39:32 +02002030 if (lru->domain && lru->data)
2031 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002032 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002033 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002034 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002035 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002036 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002037}
2038
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002039/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002040unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002041ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002042{
2043 return XXH32(data, len, ssl_ctx_lru_seed);
2044}
2045
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002046/* Generate a cert and immediately assign it to the SSL session so that the cert's
2047 * refcount is maintained regardless of the cert's presence in the LRU cache.
2048 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002049static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002050ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002051{
2052 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002053 SSL_CTX *ssl_ctx = NULL;
2054 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002055 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002056
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002057 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002058 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002059 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002060 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002061 if (lru && lru->domain)
2062 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002063 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002064 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002065 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002066 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002067 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002068 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002069 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002070 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002071 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002072 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002073 SSL_set_SSL_CTX(ssl, ssl_ctx);
2074 /* No LRU cache, this CTX will be released as soon as the session dies */
2075 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002076 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002077 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002078 return 0;
2079}
2080static int
2081ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2082{
2083 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002084 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002085
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002086 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002087 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002088 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002089 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002090 }
2091 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002092}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002093#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002094
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002095#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002096typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2097
2098static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002099{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002100#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002101 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002102 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2103#endif
2104}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002105static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2106 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002107 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2108}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002109static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002110#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002111 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002112 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2113#endif
2114}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002115static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002116#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002117 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002118 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2119#endif
2120}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002121/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002122static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2123/* Unusable in this context. */
2124static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2125static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2126static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2127static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2128static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002129#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002130typedef enum { SET_MIN, SET_MAX } set_context_func;
2131
2132static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2133 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002134 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2135}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002136static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2137 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2138 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2139}
2140static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2141 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002142 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2143}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002144static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2145 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2146 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2147}
2148static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2149 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002150 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2151}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002152static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2153 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2154 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2155}
2156static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2157 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002158 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2159}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002160static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2161 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2162 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2163}
2164static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002165#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002166 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002167 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2168#endif
2169}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002170static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2171#if SSL_OP_NO_TLSv1_3
2172 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2173 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002174#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002175}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002176#endif
2177static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2178static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002179
2180static struct {
2181 int option;
2182 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002183 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2184 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002185 const char *name;
2186} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002187 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2188 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2189 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2190 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2191 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2192 {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 +02002193};
2194
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002195static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2196{
2197 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2198 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2199 SSL_set_SSL_CTX(ssl, ctx);
2200}
2201
Willy Tarreau5db847a2019-05-09 14:13:35 +02002202#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002203
2204static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2205{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002206 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002207 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002208
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002209 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2210 return SSL_TLSEXT_ERR_OK;
2211 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002212}
2213
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002214#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002215static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2216{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002217 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002218#else
2219static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2220{
2221#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002222 struct connection *conn;
2223 struct bind_conf *s;
2224 const uint8_t *extension_data;
2225 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002226 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002227
2228 char *wildp = NULL;
2229 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002230 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002231 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002232 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002233 int i;
2234
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002235 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002236 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002237
Olivier Houchard9679ac92017-10-27 14:58:08 +02002238 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002239 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002240#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002241 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2242 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002243#else
2244 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2245#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002246 /*
2247 * The server_name extension was given too much extensibility when it
2248 * was written, so parsing the normal case is a bit complex.
2249 */
2250 size_t len;
2251 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002252 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002253 /* Extract the length of the supplied list of names. */
2254 len = (*extension_data++) << 8;
2255 len |= *extension_data++;
2256 if (len + 2 != extension_len)
2257 goto abort;
2258 /*
2259 * The list in practice only has a single element, so we only consider
2260 * the first one.
2261 */
2262 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2263 goto abort;
2264 extension_len = len - 1;
2265 /* Now we can finally pull out the byte array with the actual hostname. */
2266 if (extension_len <= 2)
2267 goto abort;
2268 len = (*extension_data++) << 8;
2269 len |= *extension_data++;
2270 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2271 || memchr(extension_data, 0, len) != NULL)
2272 goto abort;
2273 servername = extension_data;
2274 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002275 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002276#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2277 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002278 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002279 }
2280#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002281 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002282 if (!s->strict_sni) {
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002283 ssl_sock_switchctx_set(ssl, s->default_ctx);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002284 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002285 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002286 goto abort;
2287 }
2288
2289 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002290#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002291 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002292#else
2293 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2294#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002295 uint8_t sign;
2296 size_t len;
2297 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002298 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002299 len = (*extension_data++) << 8;
2300 len |= *extension_data++;
2301 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002302 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002303 if (len % 2 != 0)
2304 goto abort;
2305 for (; len > 0; len -= 2) {
2306 extension_data++; /* hash */
2307 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002308 switch (sign) {
2309 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002310 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002311 break;
2312 case TLSEXT_signature_ecdsa:
2313 has_ecdsa_sig = 1;
2314 break;
2315 default:
2316 continue;
2317 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002318 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002319 break;
2320 }
2321 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002322 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002323 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002324 }
2325 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002326 const SSL_CIPHER *cipher;
2327 size_t len;
2328 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002329 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002330#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002331 len = ctx->cipher_suites_len;
2332 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002333#else
2334 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2335#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002336 if (len % 2 != 0)
2337 goto abort;
2338 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002339#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002340 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002341 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002342#else
2343 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2344#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002345 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002346 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002347 break;
2348 }
2349 }
2350 }
2351
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002352 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002353 trash.area[i] = tolower(servername[i]);
2354 if (!wildp && (trash.area[i] == '.'))
2355 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002356 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002357 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002358
2359 /* lookup in full qualified names */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002360 node = ebst_lookup(&s->sni_ctx, trash.area);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002361
2362 /* lookup a not neg filter */
2363 for (n = node; n; n = ebmb_next_dup(n)) {
2364 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002365 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002366 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002367 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002368 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002369 break;
2370 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002371 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002372 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002373 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002374 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002375 if (!node_anonymous)
2376 node_anonymous = n;
2377 break;
2378 }
2379 }
2380 }
2381 if (wildp) {
2382 /* lookup in wildcards names */
2383 node = ebst_lookup(&s->sni_w_ctx, wildp);
2384 for (n = node; n; n = ebmb_next_dup(n)) {
2385 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002386 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002387 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002388 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002389 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002390 break;
2391 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002392 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002393 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002394 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002395 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002396 if (!node_anonymous)
2397 node_anonymous = n;
2398 break;
2399 }
2400 }
2401 }
2402 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002403 /* select by key_signature priority order */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002404 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2405 : ((has_rsa_sig && node_rsa) ? node_rsa
2406 : (node_anonymous ? node_anonymous
2407 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2408 : node_rsa /* no rsa signature case (far far away) */
2409 )));
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002410 if (node) {
2411 /* switch ctx */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02002412 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002413 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002414 if (conf) {
2415 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2416 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2417 if (conf->early_data)
2418 allow_early = 1;
2419 }
2420 goto allow_early;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002421 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002422#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002423 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002424 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002425 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002426 }
2427#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002428 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002429 /* no certificate match, is the default_ctx */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002430 ssl_sock_switchctx_set(ssl, s->default_ctx);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002431 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002432allow_early:
2433#ifdef OPENSSL_IS_BORINGSSL
2434 if (allow_early)
2435 SSL_set_early_data_enabled(ssl, 1);
2436#else
2437 if (!allow_early)
2438 SSL_set_max_early_data(ssl, 0);
2439#endif
2440 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002441 abort:
2442 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2443 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002444#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002445 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002446#else
2447 *al = SSL_AD_UNRECOGNIZED_NAME;
2448 return 0;
2449#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002450}
2451
2452#else /* OPENSSL_IS_BORINGSSL */
2453
Emeric Brunfc0421f2012-09-07 17:30:07 +02002454/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2455 * warning when no match is found, which implies the default (first) cert
2456 * will keep being used.
2457 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002458static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002459{
2460 const char *servername;
2461 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002462 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002463 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002464 int i;
2465 (void)al; /* shut gcc stupid warning */
2466
2467 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002468 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002469#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002470 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2471 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002472#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002473 if (s->strict_sni)
2474 return SSL_TLSEXT_ERR_ALERT_FATAL;
2475 ssl_sock_switchctx_set(ssl, s->default_ctx);
2476 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002477 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002478
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002479 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002480 if (!servername[i])
2481 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002482 trash.area[i] = tolower(servername[i]);
2483 if (!wildp && (trash.area[i] == '.'))
2484 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002485 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002486 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002487
2488 /* lookup in full qualified names */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002489 node = ebst_lookup(&s->sni_ctx, trash.area);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002490
2491 /* lookup a not neg filter */
2492 for (n = node; n; n = ebmb_next_dup(n)) {
2493 if (!container_of(n, struct sni_ctx, name)->neg) {
2494 node = n;
2495 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002496 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002497 }
2498 if (!node && wildp) {
2499 /* lookup in wildcards names */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002500 node = ebst_lookup(&s->sni_w_ctx, wildp);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002501 }
2502 if (!node || container_of(node, struct sni_ctx, name)->neg) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002503#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002504 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2505 /* switch ctx done in ssl_sock_generate_certificate */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002506 return SSL_TLSEXT_ERR_OK;
2507 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002508#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002509 if (s->strict_sni)
2510 return SSL_TLSEXT_ERR_ALERT_FATAL;
2511 ssl_sock_switchctx_set(ssl, s->default_ctx);
2512 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002513 }
2514
2515 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002516 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002517 return SSL_TLSEXT_ERR_OK;
2518}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002519#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002520#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2521
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002522#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002523
2524static DH * ssl_get_dh_1024(void)
2525{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002526 static unsigned char dh1024_p[]={
2527 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2528 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2529 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2530 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2531 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2532 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2533 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2534 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2535 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2536 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2537 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2538 };
2539 static unsigned char dh1024_g[]={
2540 0x02,
2541 };
2542
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002543 BIGNUM *p;
2544 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002545 DH *dh = DH_new();
2546 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002547 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2548 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002549
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002550 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002551 DH_free(dh);
2552 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002553 } else {
2554 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002555 }
2556 }
2557 return dh;
2558}
2559
2560static DH *ssl_get_dh_2048(void)
2561{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002562 static unsigned char dh2048_p[]={
2563 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2564 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2565 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2566 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2567 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2568 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2569 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2570 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2571 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2572 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2573 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2574 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2575 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2576 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2577 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2578 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2579 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2580 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2581 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2582 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2583 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2584 0xB7,0x1F,0x77,0xF3,
2585 };
2586 static unsigned char dh2048_g[]={
2587 0x02,
2588 };
2589
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002590 BIGNUM *p;
2591 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002592 DH *dh = DH_new();
2593 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002594 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2595 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002596
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002597 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002598 DH_free(dh);
2599 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002600 } else {
2601 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002602 }
2603 }
2604 return dh;
2605}
2606
2607static DH *ssl_get_dh_4096(void)
2608{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002609 static unsigned char dh4096_p[]={
2610 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2611 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2612 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2613 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2614 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2615 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2616 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2617 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2618 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2619 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2620 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2621 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2622 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2623 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2624 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2625 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2626 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2627 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2628 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2629 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2630 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2631 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2632 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2633 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2634 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2635 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2636 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2637 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2638 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2639 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2640 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2641 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2642 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2643 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2644 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2645 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2646 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2647 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2648 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2649 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2650 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2651 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2652 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002653 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002654 static unsigned char dh4096_g[]={
2655 0x02,
2656 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002657
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002658 BIGNUM *p;
2659 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002660 DH *dh = DH_new();
2661 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002662 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2663 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002664
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002665 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002666 DH_free(dh);
2667 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002668 } else {
2669 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002670 }
2671 }
2672 return dh;
2673}
2674
2675/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002676 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002677static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2678{
2679 DH *dh = NULL;
2680 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002681 int type;
2682
2683 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002684
2685 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2686 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2687 */
2688 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2689 keylen = EVP_PKEY_bits(pkey);
2690 }
2691
Willy Tarreauef934602016-12-22 23:12:01 +01002692 if (keylen > global_ssl.default_dh_param) {
2693 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002694 }
2695
Remi Gacogned3a341a2015-05-29 16:26:17 +02002696 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002697 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002698 }
2699 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002700 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002701 }
2702 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002703 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002704 }
2705
2706 return dh;
2707}
2708
Remi Gacogne47783ef2015-05-29 15:53:22 +02002709static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002710{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002711 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002712 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002713
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002714 if (in == NULL)
2715 goto end;
2716
Remi Gacogne47783ef2015-05-29 15:53:22 +02002717 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002718 goto end;
2719
Remi Gacogne47783ef2015-05-29 15:53:22 +02002720 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2721
2722end:
2723 if (in)
2724 BIO_free(in);
2725
Emeric Brune1b4ed42018-08-16 15:14:12 +02002726 ERR_clear_error();
2727
Remi Gacogne47783ef2015-05-29 15:53:22 +02002728 return dh;
2729}
2730
2731int ssl_sock_load_global_dh_param_from_file(const char *filename)
2732{
2733 global_dh = ssl_sock_get_dh_from_file(filename);
2734
2735 if (global_dh) {
2736 return 0;
2737 }
2738
2739 return -1;
2740}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002741#endif
2742
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002743static 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 +02002744 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002745{
2746 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002747 int wild = 0, neg = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002748 struct ebmb_node *node;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002749
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002750 if (*name == '!') {
2751 neg = 1;
2752 name++;
2753 }
2754 if (*name == '*') {
2755 wild = 1;
2756 name++;
2757 }
2758 /* !* filter is a nop */
2759 if (neg && wild)
2760 return order;
2761 if (*name) {
2762 int j, len;
2763 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002764 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002765 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002766 if (j >= trash.size)
2767 return order;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002768 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002769
2770 /* Check for duplicates. */
2771 if (wild)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002772 node = ebst_lookup(&s->sni_w_ctx, trash.area);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002773 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002774 node = ebst_lookup(&s->sni_ctx, trash.area);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002775 for (; node; node = ebmb_next_dup(node)) {
2776 sc = ebmb_entry(node, struct sni_ctx, name);
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002777 if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg)
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002778 return order;
2779 }
2780
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002781 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002782 if (!sc)
2783 return order;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002784 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002785 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002786 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002787 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002788 sc->order = order++;
2789 sc->neg = neg;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01002790 if (kinfo.sig != TLSEXT_signature_anonymous)
2791 SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002792 if (wild)
2793 ebst_insert(&s->sni_w_ctx, &sc->name);
2794 else
2795 ebst_insert(&s->sni_ctx, &sc->name);
2796 }
2797 return order;
2798}
2799
yanbzhu488a4d22015-12-01 15:16:07 -05002800
2801/* The following code is used for loading multiple crt files into
2802 * SSL_CTX's based on CN/SAN
2803 */
yanbzhu488a4d22015-12-01 15:16:07 -05002804/* This is used to preload the certifcate, private key
2805 * and Cert Chain of a file passed in via the crt
2806 * argument
2807 *
2808 * This way, we do not have to read the file multiple times
2809 */
2810struct cert_key_and_chain {
2811 X509 *cert;
2812 EVP_PKEY *key;
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01002813 STACK_OF(X509) *chain;
William Lallemandfa892222019-07-23 16:06:08 +02002814 DH *dh;
yanbzhu488a4d22015-12-01 15:16:07 -05002815};
2816
William Lallemand36b84632019-07-18 19:28:17 +02002817/*
2818 * this is used to store 1 to SSL_SOCK_NUM_KEYTYPES cert_key_and_chain and
2819 * metadata.
2820 */
2821struct ckch_node {
2822 struct cert_key_and_chain *ckch;
2823 int multi; /* is it a multi-cert bundle ? */
William Lallemand6af03992019-07-23 15:00:54 +02002824 struct ebmb_node node;
2825 char path[0];
William Lallemand36b84632019-07-18 19:28:17 +02002826};
2827
William Lallemand6af03992019-07-23 15:00:54 +02002828/*
2829 * tree used to store the ckchn ordered by filename/bundle name
2830 */
2831struct eb_root ckchn_tree = EB_ROOT_UNIQUE;
2832
William Lallemandc4ecddf2019-07-31 16:50:08 +02002833#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
2834
yanbzhu08ce6ab2015-12-02 13:01:29 -05002835#define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES))
2836
2837struct key_combo_ctx {
2838 SSL_CTX *ctx;
2839 int order;
2840};
2841
2842/* Map used for processing multiple keypairs for a single purpose
2843 *
2844 * This maps CN/SNI name to certificate type
2845 */
2846struct sni_keytype {
2847 int keytypes; /* BITMASK for keytypes */
2848 struct ebmb_node name; /* node holding the servername value */
2849};
2850
William Lallemandc4ecddf2019-07-31 16:50:08 +02002851#endif
William Lallemandfa892222019-07-23 16:06:08 +02002852
2853/* Loads Diffie-Hellman parameter from a ckchn. Returns 1 if loaded, else -1
2854 if an error occurred, and 0 if parameter not found. */
2855#ifndef OPENSSL_NO_DH
2856static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
2857{
2858 int ret = -1;
2859 DH *dh = NULL;
2860
William Lallemanda8c73742019-07-31 18:31:34 +02002861 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02002862 dh = ckch->dh;
William Lallemandfa892222019-07-23 16:06:08 +02002863 ret = 1;
2864 SSL_CTX_set_tmp_dh(ctx, dh);
2865
2866 if (ssl_dh_ptr_index >= 0) {
2867 /* store a pointer to the DH params to avoid complaining about
2868 ssl-default-dh-param not being set for this SSL_CTX */
2869 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
2870 }
2871 }
2872 else if (global_dh) {
2873 SSL_CTX_set_tmp_dh(ctx, global_dh);
2874 ret = 0; /* DH params not found */
2875 }
2876 else {
2877 /* Clear openssl global errors stack */
2878 ERR_clear_error();
2879
2880 if (global_ssl.default_dh_param <= 1024) {
2881 /* we are limited to DH parameter of 1024 bits anyway */
2882 if (local_dh_1024 == NULL)
2883 local_dh_1024 = ssl_get_dh_1024();
2884
2885 if (local_dh_1024 == NULL)
2886 goto end;
2887
2888 SSL_CTX_set_tmp_dh(ctx, local_dh_1024);
2889 }
2890 else {
2891 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
2892 }
2893
2894 ret = 0; /* DH params not found */
2895 }
2896
2897end:
William Lallemandfa892222019-07-23 16:06:08 +02002898 return ret;
2899}
2900#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05002901
yanbzhu488a4d22015-12-01 15:16:07 -05002902/* Frees the contents of a cert_key_and_chain
2903 */
2904static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
2905{
yanbzhu488a4d22015-12-01 15:16:07 -05002906 if (!ckch)
2907 return;
2908
2909 /* Free the certificate and set pointer to NULL */
2910 if (ckch->cert)
2911 X509_free(ckch->cert);
2912 ckch->cert = NULL;
2913
2914 /* Free the key and set pointer to NULL */
2915 if (ckch->key)
2916 EVP_PKEY_free(ckch->key);
2917 ckch->key = NULL;
2918
2919 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01002920 if (ckch->chain)
2921 sk_X509_pop_free(ckch->chain, X509_free);
2922 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05002923
2924}
2925
2926/* checks if a key and cert exists in the ckch
2927 */
William Lallemand1633e392019-09-30 12:58:13 +02002928#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05002929static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
2930{
2931 return (ckch->cert != NULL && ckch->key != NULL);
2932}
William Lallemand1633e392019-09-30 12:58:13 +02002933#endif
yanbzhu488a4d22015-12-01 15:16:07 -05002934
2935/* Loads the contents of a crt file (path) into a cert_key_and_chain
2936 * This allows us to carry the contents of the file without having to
2937 * read the file multiple times.
Emmanuel Hocdetefa4b952018-12-04 17:37:47 +01002938 * The caller must call ssl_sock_free_cert_key_and_chain_contents.
yanbzhu488a4d22015-12-01 15:16:07 -05002939 *
2940 * returns:
2941 * 0 on Success
2942 * 1 on SSL Failure
yanbzhu488a4d22015-12-01 15:16:07 -05002943 */
2944static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
2945{
2946
2947 BIO *in;
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01002948 X509 *ca;
yanbzhu488a4d22015-12-01 15:16:07 -05002949 int ret = 1;
2950
yanbzhu488a4d22015-12-01 15:16:07 -05002951 in = BIO_new(BIO_s_file());
2952 if (in == NULL)
2953 goto end;
2954
2955 if (BIO_read_filename(in, path) <= 0)
2956 goto end;
2957
yanbzhu488a4d22015-12-01 15:16:07 -05002958 /* Read Private Key */
2959 ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
2960 if (ckch->key == NULL) {
2961 memprintf(err, "%sunable to load private key from file '%s'.\n",
2962 err && *err ? *err : "", path);
2963 goto end;
2964 }
2965
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02002966#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02002967 /* Seek back to beginning of file */
2968 if (BIO_reset(in) == -1) {
2969 memprintf(err, "%san error occurred while reading the file '%s'.\n",
2970 err && *err ? *err : "", path);
2971 goto end;
2972 }
2973
2974 ckch->dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2975 /* no need to check for NULL there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02002976#endif
William Lallemandfa892222019-07-23 16:06:08 +02002977
Willy Tarreaubb137a82016-04-06 19:02:38 +02002978 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02002979 if (BIO_reset(in) == -1) {
2980 memprintf(err, "%san error occurred while reading the file '%s'.\n",
2981 err && *err ? *err : "", path);
2982 goto end;
2983 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02002984
2985 /* Read Certificate */
2986 ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
2987 if (ckch->cert == NULL) {
2988 memprintf(err, "%sunable to load certificate from file '%s'.\n",
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01002989 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02002990 goto end;
2991 }
2992
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02002993 if (!X509_check_private_key(ckch->cert, ckch->key)) {
2994 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
2995 err && *err ? *err : "", path);
2996 goto end;
2997 }
2998
yanbzhu488a4d22015-12-01 15:16:07 -05002999 /* Read Certificate Chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003000 ckch->chain = sk_X509_new_null();
3001 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL)))
3002 if (!sk_X509_push(ckch->chain, ca)) {
3003 X509_free(ca);
3004 goto end;
3005 }
yanbzhu488a4d22015-12-01 15:16:07 -05003006
yanbzhu488a4d22015-12-01 15:16:07 -05003007 ret = ERR_get_error();
3008 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3009 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
3010 err && *err ? *err : "", path);
3011 ret = 1;
3012 goto end;
3013 }
3014
3015 ret = 0;
3016
3017end:
3018
3019 ERR_clear_error();
3020 if (in)
3021 BIO_free(in);
3022
3023 /* Something went wrong in one of the reads */
3024 if (ret != 0)
3025 ssl_sock_free_cert_key_and_chain_contents(ckch);
3026
3027 return ret;
3028}
3029
3030/* Loads the info in ckch into ctx
3031 * Currently, this does not process any information about ocsp, dhparams or
3032 * sctl
3033 * Returns
3034 * 0 on success
3035 * 1 on failure
3036 */
3037static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3038{
yanbzhu488a4d22015-12-01 15:16:07 -05003039 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3040 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3041 err && *err ? *err : "", path);
3042 return 1;
3043 }
3044
3045 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3046 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3047 err && *err ? *err : "", path);
3048 return 1;
3049 }
3050
yanbzhu488a4d22015-12-01 15:16:07 -05003051 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003052#ifdef SSL_CTX_set1_chain
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003053 if (!SSL_CTX_set1_chain(ctx, ckch->chain)) {
3054 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3055 err && *err ? *err : "", path);
3056 return 1;
yanbzhu488a4d22015-12-01 15:16:07 -05003057 }
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003058#else
3059 { /* legacy compat (< openssl 1.0.2) */
3060 X509 *ca;
3061 while ((ca = sk_X509_shift(ckch->chain)))
3062 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3063 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3064 err && *err ? *err : "", path);
3065 X509_free(ca);
3066 return 1;
3067 }
3068 }
3069#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003070
William Lallemandfa892222019-07-23 16:06:08 +02003071#ifndef OPENSSL_NO_DH
3072 /* store a NULL pointer to indicate we have not yet loaded
3073 a custom DH param file */
3074 if (ssl_dh_ptr_index >= 0) {
3075 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3076 }
3077
3078 if (ssl_sock_load_dh_params(ctx, ckch) < 0) {
3079 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3080 err && *err ? *err : "", path);
3081 return 1;
3082 }
3083#endif
3084
yanbzhu488a4d22015-12-01 15:16:07 -05003085 return 0;
3086}
3087
William Lallemandc4ecddf2019-07-31 16:50:08 +02003088#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003089
3090static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
3091{
3092 struct sni_keytype *s_kt = NULL;
3093 struct ebmb_node *node;
3094 int i;
3095
3096 for (i = 0; i < trash.size; i++) {
3097 if (!str[i])
3098 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003099 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003100 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003101 trash.area[i] = 0;
3102 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003103 if (!node) {
3104 /* CN not found in tree */
3105 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3106 /* Using memcpy here instead of strncpy.
3107 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3108 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3109 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003110 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003111 s_kt->keytypes = 0;
3112 ebst_insert(sni_keytypes, &s_kt->name);
3113 } else {
3114 /* CN found in tree */
3115 s_kt = container_of(node, struct sni_keytype, name);
3116 }
3117
3118 /* Mark that this CN has the keytype of key_index via keytypes mask */
3119 s_kt->keytypes |= 1<<key_index;
3120
3121}
3122
William Lallemandc4ecddf2019-07-31 16:50:08 +02003123#endif
3124
William Lallemand36b84632019-07-18 19:28:17 +02003125/*
William Lallemand6af03992019-07-23 15:00:54 +02003126 * lookup a path into the ckchn tree.
3127 */
3128static inline struct ckch_node *ckchn_lookup(char *path)
3129{
3130 struct ebmb_node *eb;
3131
3132 eb = ebst_lookup(&ckchn_tree, path);
3133 if (!eb)
3134 return NULL;
3135
3136 return ebmb_entry(eb, struct ckch_node, node);
3137}
3138
3139/*
William Lallemand36b84632019-07-18 19:28:17 +02003140 * This function allocate a ckch_node and populate it with certificates from files.
3141 */
3142static struct ckch_node *ckchn_load_cert_file(char *path, int multi, char **err)
3143{
3144 struct ckch_node *ckchn;
William Lallemand36b84632019-07-18 19:28:17 +02003145
William Lallemand6af03992019-07-23 15:00:54 +02003146 ckchn = calloc(1, sizeof(*ckchn) + strlen(path) + 1);
William Lallemand36b84632019-07-18 19:28:17 +02003147 if (!ckchn) {
3148 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3149 goto end;
3150 }
3151 ckchn->ckch = calloc(1, sizeof(*ckchn->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
3152
3153 if (!ckchn->ckch) {
3154 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3155 goto end;
3156 }
3157
3158 if (!multi) {
3159
3160 if (ssl_sock_load_crt_file_into_ckch(path, ckchn->ckch, err) == 1)
3161 goto end;
3162
William Lallemand6af03992019-07-23 15:00:54 +02003163 /* insert into the ckchn tree */
3164 memcpy(ckchn->path, path, strlen(path) + 1);
3165 ebst_insert(&ckchn_tree, &ckchn->node);
William Lallemand36b84632019-07-18 19:28:17 +02003166 } else {
3167 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003168#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3169 char fp[MAXPATHLEN+1] = {0};
3170 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003171
3172 /* Load all possible certs and keys */
3173 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3174 struct stat buf;
3175 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3176 if (stat(fp, &buf) == 0) {
3177 if (ssl_sock_load_crt_file_into_ckch(fp, &ckchn->ckch[n], err) == 1)
3178 goto end;
3179 found = 1;
3180 ckchn->multi = 1;
3181 }
3182 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003183#endif
William Lallemand36b84632019-07-18 19:28:17 +02003184
3185 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003186 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003187 goto end;
3188 }
William Lallemand6af03992019-07-23 15:00:54 +02003189 /* insert into the ckchn tree */
3190 memcpy(ckchn->path, path, strlen(path) + 1);
3191 ebst_insert(&ckchn_tree, &ckchn->node);
William Lallemand36b84632019-07-18 19:28:17 +02003192 }
3193 return ckchn;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003194
William Lallemand36b84632019-07-18 19:28:17 +02003195end:
William Lallemand6af03992019-07-23 15:00:54 +02003196 if (ckchn) {
William Lallemand36b84632019-07-18 19:28:17 +02003197 free(ckchn->ckch);
William Lallemand6af03992019-07-23 15:00:54 +02003198 ebmb_delete(&ckchn->node);
3199 }
3200
William Lallemand36b84632019-07-18 19:28:17 +02003201 free(ckchn);
3202
3203 return NULL;
3204}
3205
William Lallemandc4ecddf2019-07-31 16:50:08 +02003206#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3207
William Lallemand36b84632019-07-18 19:28:17 +02003208/*
3209 * Take a ckch_node which contains a multi-certificate bundle.
3210 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003211 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3212 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003213 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003214 *
3215 * Returns
3216 * 0 on success
3217 * 1 on failure
William Lallemand36b84632019-07-18 19:28:17 +02003218 *
3219 * TODO: This function shouldn't access files anymore, sctl and ocsp file access
3220 * should be migrated to the ssl_sock_load_crt_file_into_ckch() function
yanbzhu08ce6ab2015-12-02 13:01:29 -05003221 */
William Lallemand36b84632019-07-18 19:28:17 +02003222static int ssl_sock_load_multi_ckchn(const char *path, struct ckch_node *ckch_n,
3223 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3224 char **sni_filter, int fcount, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003225{
William Lallemand36b84632019-07-18 19:28:17 +02003226 int i = 0, n = 0;
3227 struct cert_key_and_chain *certs_and_keys;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003228 struct eb_root sni_keytypes_map = { {0} };
3229 struct ebmb_node *node;
3230 struct ebmb_node *next;
3231 /* Array of SSL_CTX pointers corresponding to each possible combo
3232 * of keytypes
3233 */
3234 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
3235 int rv = 0;
3236 X509_NAME *xname = NULL;
3237 char *str = NULL;
3238#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3239 STACK_OF(GENERAL_NAME) *names = NULL;
3240#endif
3241
William Lallemand36b84632019-07-18 19:28:17 +02003242 if (!ckch_n || !ckch_n->ckch || !ckch_n->multi) {
3243 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3244 err && *err ? *err : "", path);
3245 return 1;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003246 }
3247
William Lallemand36b84632019-07-18 19:28:17 +02003248 certs_and_keys = ckch_n->ckch;
3249
yanbzhu08ce6ab2015-12-02 13:01:29 -05003250 /* Process each ckch and update keytypes for each CN/SAN
3251 * for example, if CN/SAN www.a.com is associated with
3252 * certs with keytype 0 and 2, then at the end of the loop,
3253 * www.a.com will have:
3254 * keyindex = 0 | 1 | 4 = 5
3255 */
3256 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3257
3258 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3259 continue;
3260
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003261 if (fcount) {
Willy Tarreau24b892f2016-06-20 23:01:57 +02003262 for (i = 0; i < fcount; i++)
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003263 ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3264 } else {
3265 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3266 * so the line that contains logic is marked via comments
3267 */
3268 xname = X509_get_subject_name(certs_and_keys[n].cert);
3269 i = -1;
3270 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3271 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003272 ASN1_STRING *value;
3273 value = X509_NAME_ENTRY_get_data(entry);
3274 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003275 /* Important line is here */
3276 ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003277
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003278 OPENSSL_free(str);
3279 str = NULL;
3280 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003281 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003282
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003283 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003284#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003285 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3286 if (names) {
3287 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3288 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003289
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003290 if (name->type == GEN_DNS) {
3291 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3292 /* Important line is here */
3293 ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003294
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003295 OPENSSL_free(str);
3296 str = NULL;
3297 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003298 }
3299 }
3300 }
3301 }
3302#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3303 }
3304
3305 /* If no files found, return error */
3306 if (eb_is_empty(&sni_keytypes_map)) {
3307 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3308 err && *err ? *err : "", path);
3309 rv = 1;
3310 goto end;
3311 }
3312
3313 /* We now have a map of CN/SAN to keytypes that are loaded in
3314 * Iterate through the map to create the SSL_CTX's (if needed)
3315 * and add each CTX to the SNI tree
3316 *
3317 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003318 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003319 * combination is denoted by the key in the map. Each key
3320 * has a value between 1 and 2^n - 1. Conveniently, the array
3321 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3322 * entry in the array to correspond to the unique combo (key)
3323 * associated with i. This unique key combo (i) will be associated
3324 * with combos[i-1]
3325 */
3326
3327 node = ebmb_first(&sni_keytypes_map);
3328 while (node) {
3329 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003330 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003331 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003332
3333 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3334 i = container_of(node, struct sni_keytype, name)->keytypes;
3335 cur_ctx = key_combos[i-1].ctx;
3336
3337 if (cur_ctx == NULL) {
3338 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003339 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003340 if (cur_ctx == NULL) {
3341 memprintf(err, "%sunable to allocate SSL context.\n",
3342 err && *err ? *err : "");
3343 rv = 1;
3344 goto end;
3345 }
3346
yanbzhube2774d2015-12-10 15:07:30 -05003347 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003348 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3349 if (i & (1<<n)) {
3350 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003351 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3352 if (ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err) != 0) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05003353 SSL_CTX_free(cur_ctx);
3354 rv = 1;
3355 goto end;
3356 }
yanbzhube2774d2015-12-10 15:07:30 -05003357
3358#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
3359 /* Load OCSP Info into context */
William Lallemand36b84632019-07-18 19:28:17 +02003360 /* TODO: store OCSP in ckch */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003361 if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) {
yanbzhube2774d2015-12-10 15:07:30 -05003362 if (err)
3363 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 +00003364 *err ? *err : "", cur_file);
yanbzhube2774d2015-12-10 15:07:30 -05003365 SSL_CTX_free(cur_ctx);
3366 rv = 1;
3367 goto end;
3368 }
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02003369#elif (defined OPENSSL_IS_BORINGSSL)
3370 ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file);
yanbzhube2774d2015-12-10 15:07:30 -05003371#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003372 }
3373 }
3374
yanbzhu08ce6ab2015-12-02 13:01:29 -05003375 /* Update key_combos */
3376 key_combos[i-1].ctx = cur_ctx;
3377 }
3378
3379 /* Update SNI Tree */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003380 key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf,
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003381 kinfo, str, key_combos[i-1].order);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003382 node = ebmb_next(node);
3383 }
3384
3385
3386 /* Mark a default context if none exists, using the ctx that has the most shared keys */
3387 if (!bind_conf->default_ctx) {
3388 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
3389 if (key_combos[i].ctx) {
3390 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003391 bind_conf->default_ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003392 break;
3393 }
3394 }
3395 }
3396
3397end:
3398
3399 if (names)
3400 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
3401
yanbzhu08ce6ab2015-12-02 13:01:29 -05003402 node = ebmb_first(&sni_keytypes_map);
3403 while (node) {
3404 next = ebmb_next(node);
3405 ebmb_delete(node);
3406 node = next;
3407 }
3408
3409 return rv;
3410}
3411#else
3412/* This is a dummy, that just logs an error and returns error */
William Lallemand36b84632019-07-18 19:28:17 +02003413static int ssl_sock_load_multi_ckchn(const char *path, struct ckch_node *ckch_n,
3414 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3415 char **sni_filter, int fcount, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003416{
3417 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3418 err && *err ? *err : "", path, strerror(errno));
3419 return 1;
3420}
3421
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003422#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05003423
William Lallemand36b84632019-07-18 19:28:17 +02003424static int ssl_sock_load_ckchn(const char *path, struct ckch_node *ckch_n, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemandc9402072019-05-15 15:33:54 +02003425 char **sni_filter, int fcount, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003426{
William Lallemandc9402072019-05-15 15:33:54 +02003427 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02003428 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003429 int order = 0;
3430 X509_NAME *xname;
3431 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003432 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003433 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02003434#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3435 STACK_OF(GENERAL_NAME) *names;
3436#endif
William Lallemand36b84632019-07-18 19:28:17 +02003437 struct cert_key_and_chain *ckch;
William Lallemanda59191b2019-05-15 16:08:56 +02003438
William Lallemand36b84632019-07-18 19:28:17 +02003439 if (!ckch_n || !ckch_n->ckch)
William Lallemanda59191b2019-05-15 16:08:56 +02003440 return 1;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003441
William Lallemand36b84632019-07-18 19:28:17 +02003442 ckch = ckch_n->ckch;
3443
William Lallemandc9402072019-05-15 15:33:54 +02003444 ctx = SSL_CTX_new(SSLv23_server_method());
3445 if (!ctx) {
3446 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3447 err && *err ? *err : "", path);
3448 return 1;
3449 }
3450
William Lallemand36b84632019-07-18 19:28:17 +02003451 if (ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err) != 0) {
William Lallemandc9402072019-05-15 15:33:54 +02003452 SSL_CTX_free(ctx);
3453 return 1;
3454 }
3455
William Lallemand36b84632019-07-18 19:28:17 +02003456 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003457 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003458 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003459 switch(EVP_PKEY_base_id(pkey)) {
3460 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003461 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003462 break;
3463 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003464 kinfo.sig = TLSEXT_signature_ecdsa;
3465 break;
3466 case EVP_PKEY_DSA:
3467 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003468 break;
3469 }
3470 EVP_PKEY_free(pkey);
3471 }
3472
Emeric Brun50bcecc2013-04-22 13:05:23 +02003473 if (fcount) {
3474 while (fcount--)
William Lallemandc9402072019-05-15 15:33:54 +02003475 order = ssl_sock_add_cert_sni(ctx, bind_conf, ssl_conf, kinfo, sni_filter[fcount], order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003476 }
3477 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003478#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02003479 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003480 if (names) {
3481 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3482 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
3483 if (name->type == GEN_DNS) {
3484 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemandc9402072019-05-15 15:33:54 +02003485 order = ssl_sock_add_cert_sni(ctx, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003486 OPENSSL_free(str);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003487 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003488 }
3489 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003490 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003491 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003492#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02003493 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003494 i = -1;
3495 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3496 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003497 ASN1_STRING *value;
3498
3499 value = X509_NAME_ENTRY_get_data(entry);
3500 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemandc9402072019-05-15 15:33:54 +02003501 order = ssl_sock_add_cert_sni(ctx, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003502 OPENSSL_free(str);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003503 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003504 }
3505 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003506 /* we must not free the SSL_CTX anymore below, since it's already in
3507 * the tree, so it will be discovered and cleaned in time.
3508 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003509
Lukas Tribuse4e30f72014-12-09 16:32:51 +01003510#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emmanuel Hocdeta7a0f992019-07-30 17:17:03 +02003511 if (ssl_sock_load_ocsp(ctx, path) < 0) {
Emeric Brun4147b2e2014-06-16 18:36:30 +02003512 if (err)
3513 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",
3514 *err ? *err : "", path);
3515 return 1;
3516 }
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02003517#elif (defined OPENSSL_IS_BORINGSSL)
3518 ssl_sock_set_ocsp_response_from_file(ctx, path);
Emeric Brun4147b2e2014-06-16 18:36:30 +02003519#endif
3520
Willy Tarreau5db847a2019-05-09 14:13:35 +02003521#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01003522 if (sctl_ex_index >= 0) {
Emmanuel Hocdeta7a0f992019-07-30 17:17:03 +02003523 if (ssl_sock_load_sctl(ctx, path) < 0) {
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01003524 if (err)
3525 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
3526 *err ? *err : "", path);
3527 return 1;
3528 }
3529 }
3530#endif
3531
Emeric Brunfc0421f2012-09-07 17:30:07 +02003532#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003533 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003534 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
3535 err && *err ? *err : "");
Emeric Brunfc0421f2012-09-07 17:30:07 +02003536 return 1;
3537 }
3538#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003539 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003540 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003541 bind_conf->default_ssl_conf = ssl_conf;
3542 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003543
3544 return 0;
3545}
3546
Willy Tarreau03209342016-12-22 17:08:28 +01003547int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003548{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003549 struct dirent **de_list;
3550 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003551 DIR *dir;
3552 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01003553 char *end;
3554 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02003555 int cfgerr = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003556 struct ckch_node *ckchn;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003557#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05003558 int is_bundle;
3559 int j;
3560#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02003561
William Lallemand6af03992019-07-23 15:00:54 +02003562 if ((ckchn = ckchn_lookup(path))) {
3563
3564 /* we found the ckchn in the tree, we can use it directly */
3565 if (ckchn->multi)
3566 return ssl_sock_load_multi_ckchn(path, ckchn, bind_conf, NULL, NULL, 0, err);
3567 else
3568 return ssl_sock_load_ckchn(path, ckchn, bind_conf, NULL, NULL, 0, err);
3569
3570 }
3571
yanbzhu08ce6ab2015-12-02 13:01:29 -05003572 if (stat(path, &buf) == 0) {
3573 dir = opendir(path);
William Lallemand36b84632019-07-18 19:28:17 +02003574 if (!dir) {
3575 ckchn = ckchn_load_cert_file(path, 0, err);
3576 if (!ckchn)
3577 return 1;
3578 return ssl_sock_load_ckchn(path, ckchn, bind_conf, NULL, NULL, 0, err);
3579 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003580
yanbzhu08ce6ab2015-12-02 13:01:29 -05003581 /* strip trailing slashes, including first one */
3582 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
3583 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003584
yanbzhu08ce6ab2015-12-02 13:01:29 -05003585 n = scandir(path, &de_list, 0, alphasort);
3586 if (n < 0) {
3587 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
3588 err && *err ? *err : "", path, strerror(errno));
3589 cfgerr++;
3590 }
3591 else {
3592 for (i = 0; i < n; i++) {
3593 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02003594
yanbzhu08ce6ab2015-12-02 13:01:29 -05003595 end = strrchr(de->d_name, '.');
3596 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
3597 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003598
yanbzhu08ce6ab2015-12-02 13:01:29 -05003599 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
3600 if (stat(fp, &buf) != 0) {
3601 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3602 err && *err ? *err : "", fp, strerror(errno));
3603 cfgerr++;
3604 goto ignore_entry;
3605 }
3606 if (!S_ISREG(buf.st_mode))
3607 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05003608
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003609#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05003610 is_bundle = 0;
3611 /* Check if current entry in directory is part of a multi-cert bundle */
3612
3613 if (end) {
3614 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
3615 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
3616 is_bundle = 1;
3617 break;
3618 }
3619 }
3620
3621 if (is_bundle) {
3622 char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */
3623 int dp_len;
3624
3625 dp_len = end - de->d_name;
3626 snprintf(dp, dp_len + 1, "%s", de->d_name);
3627
3628 /* increment i and free de until we get to a non-bundle cert
3629 * Note here that we look at de_list[i + 1] before freeing de
3630 * this is important since ignore_entry will free de
3631 */
3632 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) {
3633 free(de);
3634 i++;
3635 de = de_list[i];
3636 }
3637
3638 snprintf(fp, sizeof(fp), "%s/%s", path, dp);
William Lallemand6af03992019-07-23 15:00:54 +02003639 if ((ckchn = ckchn_lookup(fp)) == NULL)
3640 ckchn = ckchn_load_cert_file(fp, 1, err);
William Lallemand36b84632019-07-18 19:28:17 +02003641 if (!ckchn)
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02003642 cfgerr++;
3643 else
3644 cfgerr += ssl_sock_load_multi_ckchn(fp, ckchn, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05003645
3646 /* Successfully processed the bundle */
3647 goto ignore_entry;
3648 }
3649 }
3650
3651#endif
William Lallemand6af03992019-07-23 15:00:54 +02003652 if ((ckchn = ckchn_lookup(fp)) == NULL)
3653 ckchn = ckchn_load_cert_file(fp, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02003654 if (!ckchn)
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02003655 cfgerr++;
3656 else
3657 cfgerr += ssl_sock_load_ckchn(fp, ckchn, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02003658
yanbzhu08ce6ab2015-12-02 13:01:29 -05003659ignore_entry:
3660 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003661 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003662 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003663 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003664 closedir(dir);
3665 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003666 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003667
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003668 ckchn = ckchn_load_cert_file(path, 1, err);
William Lallemand36b84632019-07-18 19:28:17 +02003669 if (!ckchn)
3670 return 1;
3671 cfgerr = ssl_sock_load_multi_ckchn(path, ckchn, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003672
Emeric Brunfc0421f2012-09-07 17:30:07 +02003673 return cfgerr;
3674}
3675
Thierry Fournier383085f2013-01-24 14:15:43 +01003676/* Make sure openssl opens /dev/urandom before the chroot. The work is only
3677 * done once. Zero is returned if the operation fails. No error is returned
3678 * if the random is said as not implemented, because we expect that openssl
3679 * will use another method once needed.
3680 */
3681static int ssl_initialize_random()
3682{
3683 unsigned char random;
3684 static int random_initialized = 0;
3685
3686 if (!random_initialized && RAND_bytes(&random, 1) != 0)
3687 random_initialized = 1;
3688
3689 return random_initialized;
3690}
3691
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003692/* release ssl bind conf */
3693void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003694{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003695 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01003696#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003697 free(conf->npn_str);
3698 conf->npn_str = NULL;
3699#endif
3700#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
3701 free(conf->alpn_str);
3702 conf->alpn_str = NULL;
3703#endif
3704 free(conf->ca_file);
3705 conf->ca_file = NULL;
3706 free(conf->crl_file);
3707 conf->crl_file = NULL;
3708 free(conf->ciphers);
3709 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02003710#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02003711 free(conf->ciphersuites);
3712 conf->ciphersuites = NULL;
3713#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01003714 free(conf->curves);
3715 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003716 free(conf->ecdhe);
3717 conf->ecdhe = NULL;
3718 }
3719}
3720
3721int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
3722{
3723 char thisline[CRT_LINESIZE];
3724 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003725 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05003726 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003727 int linenum = 0;
3728 int cfgerr = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003729 struct ckch_node *ckchn;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003730
Willy Tarreauad1731d2013-04-02 17:35:58 +02003731 if ((f = fopen(file, "r")) == NULL) {
3732 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003733 return 1;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003734 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003735
3736 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003737 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003738 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003739 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003740 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003741 char *crt_path;
3742 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003743
3744 linenum++;
3745 end = line + strlen(line);
3746 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
3747 /* Check if we reached the limit and the last char is not \n.
3748 * Watch out for the last line without the terminating '\n'!
3749 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02003750 memprintf(err, "line %d too long in file '%s', limit is %d characters",
3751 linenum, file, (int)sizeof(thisline)-1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003752 cfgerr = 1;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003753 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003754 }
3755
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003756 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02003757 newarg = 1;
3758 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003759 if (*line == '#' || *line == '\n' || *line == '\r') {
3760 /* end of string, end of loop */
3761 *line = 0;
3762 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003763 } else if (isspace(*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02003764 newarg = 1;
3765 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003766 } else if (*line == '[') {
3767 if (ssl_b) {
3768 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
3769 cfgerr = 1;
3770 break;
3771 }
3772 if (!arg) {
3773 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
3774 cfgerr = 1;
3775 break;
3776 }
3777 ssl_b = arg;
3778 newarg = 1;
3779 *line = 0;
3780 } else if (*line == ']') {
3781 if (ssl_e) {
3782 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Emeric Brun50bcecc2013-04-22 13:05:23 +02003783 cfgerr = 1;
3784 break;
3785 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003786 if (!ssl_b) {
3787 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
3788 cfgerr = 1;
3789 break;
3790 }
3791 ssl_e = arg;
3792 newarg = 1;
3793 *line = 0;
3794 } else if (newarg) {
3795 if (arg == MAX_CRT_ARGS) {
3796 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
3797 cfgerr = 1;
3798 break;
3799 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02003800 newarg = 0;
3801 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003802 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02003803 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003804 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003805 if (cfgerr)
3806 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003807 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003808
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003809 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003810 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003811 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003812
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003813 crt_path = args[0];
3814 if (*crt_path != '/' && global_ssl.crt_base) {
3815 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
3816 memprintf(err, "'%s' : path too long on line %d in file '%s'",
3817 crt_path, linenum, file);
3818 cfgerr = 1;
3819 break;
3820 }
3821 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
3822 crt_path = path;
3823 }
3824
3825 ssl_conf = calloc(1, sizeof *ssl_conf);
3826 cur_arg = ssl_b ? ssl_b : 1;
3827 while (cur_arg < ssl_e) {
3828 newarg = 0;
3829 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
3830 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
3831 newarg = 1;
3832 cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
3833 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
3834 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
3835 args[cur_arg], linenum, file);
3836 cfgerr = 1;
3837 }
3838 cur_arg += 1 + ssl_bind_kws[i].skip;
3839 break;
3840 }
3841 }
3842 if (!cfgerr && !newarg) {
3843 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
3844 args[cur_arg], linenum, file);
3845 cfgerr = 1;
3846 break;
3847 }
3848 }
3849 if (cfgerr) {
3850 ssl_sock_free_ssl_conf(ssl_conf);
3851 free(ssl_conf);
3852 ssl_conf = NULL;
3853 break;
3854 }
3855
William Lallemandeed4bf22019-10-10 11:38:13 +02003856 if ((ckchn = ckchn_lookup(crt_path)) == NULL) {
3857 if (stat(crt_path, &buf) == 0)
3858 ckchn = ckchn_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02003859 else
William Lallemandeed4bf22019-10-10 11:38:13 +02003860 ckchn = ckchn_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05003861 }
3862
William Lallemandeed4bf22019-10-10 11:38:13 +02003863 if (!ckchn)
3864 cfgerr++;
3865 else
3866 cfgerr += ssl_sock_load_ckchn(crt_path, ckchn, bind_conf, ssl_conf,
3867 &args[cur_arg], arg - cur_arg - 1, err);
3868
Willy Tarreauad1731d2013-04-02 17:35:58 +02003869 if (cfgerr) {
3870 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003871 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003872 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003873 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003874 fclose(f);
3875 return cfgerr;
3876}
3877
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003878/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003879static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003880ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003881{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003882 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003883 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003884 SSL_OP_ALL | /* all known workarounds for bugs */
3885 SSL_OP_NO_SSLv2 |
3886 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003887 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02003888 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003889 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02003890 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003891 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003892 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003893 SSL_MODE_ENABLE_PARTIAL_WRITE |
3894 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01003895 SSL_MODE_RELEASE_BUFFERS |
3896 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02003897 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003898 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003899 int flags = MC_SSL_O_ALL;
3900 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003901
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003902 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003903 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003904
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003905 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01003906 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
3907 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3908 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003909 else
3910 flags = conf_ssl_methods->flags;
3911
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003912 min = conf_ssl_methods->min;
3913 max = conf_ssl_methods->max;
3914 /* start with TLSv10 to remove SSLv3 per default */
3915 if (!min && (!max || max >= CONF_TLSV10))
3916 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003917 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003918 if (min)
3919 flags |= (methodVersions[min].flag - 1);
3920 if (max)
3921 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003922 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003923 min = max = CONF_TLSV_NONE;
3924 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003925 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003926 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003927 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003928 if (min) {
3929 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003930 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
3931 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3932 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
3933 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003934 hole = 0;
3935 }
3936 max = i;
3937 }
3938 else {
3939 min = max = i;
3940 }
3941 }
3942 else {
3943 if (min)
3944 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003945 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003946 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003947 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
3948 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003949 cfgerr += 1;
3950 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02003951 /* save real min/max in bind_conf */
3952 conf_ssl_methods->min = min;
3953 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003954
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003955#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003956 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08003957 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003958 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003959 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003960 else
3961 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
3962 if (flags & methodVersions[i].flag)
3963 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003964#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003965 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003966 methodVersions[min].ctx_set_version(ctx, SET_MIN);
3967 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02003968#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003969
3970 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
3971 options |= SSL_OP_NO_TICKET;
3972 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
3973 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08003974
3975#ifdef SSL_OP_NO_RENEGOTIATION
3976 options |= SSL_OP_NO_RENEGOTIATION;
3977#endif
3978
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003979 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00003980
Willy Tarreau5db847a2019-05-09 14:13:35 +02003981#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00003982 if (global_ssl.async)
3983 mode |= SSL_MODE_ASYNC;
3984#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003985 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003986 if (global_ssl.life_time)
3987 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003988
3989#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3990#ifdef OPENSSL_IS_BORINGSSL
3991 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
3992 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Willy Tarreau5db847a2019-05-09 14:13:35 +02003993#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard51088ce2019-01-02 18:46:41 +01003994 if (bind_conf->ssl_conf.early_data) {
3995 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
3996 SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite);
3997 }
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02003998 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
3999 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004000#else
4001 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004002#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004003 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004004#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004005 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004006}
4007
William Lallemand4f45bb92017-10-30 20:08:51 +01004008
4009static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4010{
4011 if (first == block) {
4012 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4013 if (first->len > 0)
4014 sh_ssl_sess_tree_delete(sh_ssl_sess);
4015 }
4016}
4017
4018/* return first block from sh_ssl_sess */
4019static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4020{
4021 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4022
4023}
4024
4025/* store a session into the cache
4026 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4027 * data: asn1 encoded session
4028 * data_len: asn1 encoded session length
4029 * Returns 1 id session was stored (else 0)
4030 */
4031static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4032{
4033 struct shared_block *first;
4034 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4035
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004036 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004037 if (!first) {
4038 /* Could not retrieve enough free blocks to store that session */
4039 return 0;
4040 }
4041
4042 /* STORE the key in the first elem */
4043 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4044 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4045 first->len = sizeof(struct sh_ssl_sess_hdr);
4046
4047 /* it returns the already existing node
4048 or current node if none, never returns null */
4049 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4050 if (oldsh_ssl_sess != sh_ssl_sess) {
4051 /* NOTE: Row couldn't be in use because we lock read & write function */
4052 /* release the reserved row */
4053 shctx_row_dec_hot(ssl_shctx, first);
4054 /* replace the previous session already in the tree */
4055 sh_ssl_sess = oldsh_ssl_sess;
4056 /* ignore the previous session data, only use the header */
4057 first = sh_ssl_sess_first_block(sh_ssl_sess);
4058 shctx_row_inc_hot(ssl_shctx, first);
4059 first->len = sizeof(struct sh_ssl_sess_hdr);
4060 }
4061
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004062 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004063 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004064 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004065 }
4066
4067 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004068
4069 return 1;
4070}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004071
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004072/* SSL callback used when a new session is created while connecting to a server */
4073static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4074{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004075 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004076 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004077
Willy Tarreau07d94e42018-09-20 10:57:52 +02004078 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004079
Olivier Houcharde6060c52017-11-16 17:42:52 +01004080 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4081 int len;
4082 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004083
Olivier Houcharde6060c52017-11-16 17:42:52 +01004084 len = i2d_SSL_SESSION(sess, NULL);
4085 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4086 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4087 } else {
4088 free(s->ssl_ctx.reused_sess[tid].ptr);
4089 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4090 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4091 }
4092 if (s->ssl_ctx.reused_sess[tid].ptr) {
4093 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4094 &ptr);
4095 }
4096 } else {
4097 free(s->ssl_ctx.reused_sess[tid].ptr);
4098 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4099 }
4100
4101 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004102}
4103
Olivier Houcharde6060c52017-11-16 17:42:52 +01004104
William Lallemanded0b5ad2017-10-30 19:36:36 +01004105/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004106int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004107{
4108 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4109 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4110 unsigned char *p;
4111 int data_len;
4112 unsigned int sid_length, sid_ctx_length;
4113 const unsigned char *sid_data;
4114 const unsigned char *sid_ctx_data;
4115
4116 /* Session id is already stored in to key and session id is known
4117 * so we dont store it to keep size.
4118 */
4119
4120 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4121 sid_ctx_data = SSL_SESSION_get0_id_context(sess, &sid_ctx_length);
4122 SSL_SESSION_set1_id(sess, sid_data, 0);
4123 SSL_SESSION_set1_id_context(sess, sid_ctx_data, 0);
4124
4125 /* check if buffer is large enough for the ASN1 encoded session */
4126 data_len = i2d_SSL_SESSION(sess, NULL);
4127 if (data_len > SHSESS_MAX_DATA_LEN)
4128 goto err;
4129
4130 p = encsess;
4131
4132 /* process ASN1 session encoding before the lock */
4133 i2d_SSL_SESSION(sess, &p);
4134
4135 memcpy(encid, sid_data, sid_length);
4136 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4137 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4138
William Lallemanda3c77cf2017-10-30 23:44:40 +01004139 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004140 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004141 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004142 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004143err:
4144 /* reset original length values */
4145 SSL_SESSION_set1_id(sess, sid_data, sid_length);
4146 SSL_SESSION_set1_id_context(sess, sid_ctx_data, sid_ctx_length);
4147
4148 return 0; /* do not increment session reference count */
4149}
4150
4151/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004152SSL_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 +01004153{
William Lallemand4f45bb92017-10-30 20:08:51 +01004154 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004155 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4156 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004157 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004158 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004159
4160 global.shctx_lookups++;
4161
4162 /* allow the session to be freed automatically by openssl */
4163 *do_copy = 0;
4164
4165 /* tree key is zeros padded sessionid */
4166 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4167 memcpy(tmpkey, key, key_len);
4168 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4169 key = tmpkey;
4170 }
4171
4172 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004173 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004174
4175 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004176 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4177 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004178 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004179 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004180 global.shctx_misses++;
4181 return NULL;
4182 }
4183
William Lallemand4f45bb92017-10-30 20:08:51 +01004184 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4185 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004186
William Lallemand4f45bb92017-10-30 20:08:51 +01004187 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 +01004188
William Lallemanda3c77cf2017-10-30 23:44:40 +01004189 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004190
4191 /* decode ASN1 session */
4192 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004193 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004194 /* Reset session id and session id contenxt */
4195 if (sess) {
4196 SSL_SESSION_set1_id(sess, key, key_len);
4197 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4198 }
4199
4200 return sess;
4201}
4202
William Lallemand4f45bb92017-10-30 20:08:51 +01004203
William Lallemanded0b5ad2017-10-30 19:36:36 +01004204/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004205void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004206{
William Lallemand4f45bb92017-10-30 20:08:51 +01004207 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004208 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4209 unsigned int sid_length;
4210 const unsigned char *sid_data;
4211 (void)ctx;
4212
4213 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4214 /* tree key is zeros padded sessionid */
4215 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4216 memcpy(tmpkey, sid_data, sid_length);
4217 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4218 sid_data = tmpkey;
4219 }
4220
William Lallemanda3c77cf2017-10-30 23:44:40 +01004221 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004222
4223 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004224 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4225 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004226 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004227 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004228 }
4229
4230 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004231 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004232}
4233
4234/* Set session cache mode to server and disable openssl internal cache.
4235 * Set shared cache callbacks on an ssl context.
4236 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004237void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004238{
4239 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4240
4241 if (!ssl_shctx) {
4242 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4243 return;
4244 }
4245
4246 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4247 SSL_SESS_CACHE_NO_INTERNAL |
4248 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4249
4250 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004251 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4252 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4253 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004254}
4255
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004256int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx)
4257{
4258 struct proxy *curproxy = bind_conf->frontend;
4259 int cfgerr = 0;
4260 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004261 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004262 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004263#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004264 const char *conf_ciphersuites;
4265#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004266 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004267
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004268 if (ssl_conf) {
4269 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4270 int i, min, max;
4271 int flags = MC_SSL_O_ALL;
4272
4273 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004274 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4275 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004276 if (min)
4277 flags |= (methodVersions[min].flag - 1);
4278 if (max)
4279 flags |= ~((methodVersions[max].flag << 1) - 1);
4280 min = max = CONF_TLSV_NONE;
4281 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4282 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4283 if (min)
4284 max = i;
4285 else
4286 min = max = i;
4287 }
4288 /* save real min/max */
4289 conf_ssl_methods->min = min;
4290 conf_ssl_methods->max = max;
4291 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004292 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4293 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004294 cfgerr += 1;
4295 }
4296 }
4297
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004298 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01004299 case SSL_SOCK_VERIFY_NONE:
4300 verify = SSL_VERIFY_NONE;
4301 break;
4302 case SSL_SOCK_VERIFY_OPTIONAL:
4303 verify = SSL_VERIFY_PEER;
4304 break;
4305 case SSL_SOCK_VERIFY_REQUIRED:
4306 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
4307 break;
4308 }
4309 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
4310 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004311 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
4312 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
4313 if (ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004314 /* load CAfile to verify */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004315 if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004316 ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
4317 curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02004318 cfgerr++;
4319 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02004320 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
4321 /* set CA names for client cert request, function returns void */
4322 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file));
4323 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004324 }
Emeric Brun850efd52014-01-29 12:24:34 +01004325 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004326 ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
4327 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun850efd52014-01-29 12:24:34 +01004328 cfgerr++;
4329 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004330#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004331 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004332 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
4333
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004334 if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004335 ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
4336 curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02004337 cfgerr++;
4338 }
Emeric Brun561e5742012-10-02 15:20:55 +02004339 else {
4340 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4341 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004342 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004343#endif
Emeric Brun644cde02012-12-14 11:21:13 +01004344 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02004345 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004346#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02004347 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004348 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004349 ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
4350 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004351 cfgerr++;
4352 }
4353 }
4354#endif
4355
William Lallemand4f45bb92017-10-30 20:08:51 +01004356 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004357 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
4358 if (conf_ciphers &&
4359 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004360 ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
4361 curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004362 cfgerr++;
4363 }
4364
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004365#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004366 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
4367 if (conf_ciphersuites &&
4368 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
4369 ha_alert("Proxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
4370 curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
4371 cfgerr++;
4372 }
4373#endif
4374
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004375#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02004376 /* If tune.ssl.default-dh-param has not been set,
4377 neither has ssl-default-dh-file and no static DH
4378 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01004379 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02004380 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02004381 (ssl_dh_ptr_index == -1 ||
4382 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004383 STACK_OF(SSL_CIPHER) * ciphers = NULL;
4384 const SSL_CIPHER * cipher = NULL;
4385 char cipher_description[128];
4386 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
4387 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
4388 which is not ephemeral DH. */
4389 const char dhe_description[] = " Kx=DH ";
4390 const char dhe_export_description[] = " Kx=DH(";
4391 int idx = 0;
4392 int dhe_found = 0;
4393 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02004394
Remi Gacogne23d5d372014-10-10 17:04:26 +02004395 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004396
Remi Gacogne23d5d372014-10-10 17:04:26 +02004397 if (ssl) {
4398 ciphers = SSL_get_ciphers(ssl);
4399
4400 if (ciphers) {
4401 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
4402 cipher = sk_SSL_CIPHER_value(ciphers, idx);
4403 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
4404 if (strstr(cipher_description, dhe_description) != NULL ||
4405 strstr(cipher_description, dhe_export_description) != NULL) {
4406 dhe_found = 1;
4407 break;
4408 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02004409 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004410 }
4411 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02004412 SSL_free(ssl);
4413 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02004414 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004415
Lukas Tribus90132722014-08-18 00:56:33 +02004416 if (dhe_found) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004417 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 +02004418 }
4419
Willy Tarreauef934602016-12-22 23:12:01 +01004420 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004421 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004422
Willy Tarreauef934602016-12-22 23:12:01 +01004423 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004424 if (local_dh_1024 == NULL) {
4425 local_dh_1024 = ssl_get_dh_1024();
4426 }
Willy Tarreauef934602016-12-22 23:12:01 +01004427 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004428 if (local_dh_2048 == NULL) {
4429 local_dh_2048 = ssl_get_dh_2048();
4430 }
Willy Tarreauef934602016-12-22 23:12:01 +01004431 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004432 if (local_dh_4096 == NULL) {
4433 local_dh_4096 = ssl_get_dh_4096();
4434 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004435 }
4436 }
4437 }
4438#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004439
Emeric Brunfc0421f2012-09-07 17:30:07 +02004440 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004441#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02004442 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02004443#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02004444
Bernard Spil13c53f82018-02-15 13:34:58 +01004445#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004446 ssl_conf_cur = NULL;
4447 if (ssl_conf && ssl_conf->npn_str)
4448 ssl_conf_cur = ssl_conf;
4449 else if (bind_conf->ssl_conf.npn_str)
4450 ssl_conf_cur = &bind_conf->ssl_conf;
4451 if (ssl_conf_cur)
4452 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02004453#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01004454#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004455 ssl_conf_cur = NULL;
4456 if (ssl_conf && ssl_conf->alpn_str)
4457 ssl_conf_cur = ssl_conf;
4458 else if (bind_conf->ssl_conf.alpn_str)
4459 ssl_conf_cur = &bind_conf->ssl_conf;
4460 if (ssl_conf_cur)
4461 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02004462#endif
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004463#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004464 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
4465 if (conf_curves) {
4466 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004467 ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
4468 curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004469 cfgerr++;
4470 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01004471#if defined(SSL_CTX_set_ecdh_auto)
4472 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
4473#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004474 }
4475#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004476#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004477 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02004478 int i;
4479 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004480#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004481 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02004482 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4483 NULL);
4484
4485 if (ecdhe == NULL) {
4486 SSL_CTX_set_dh_auto(ctx, 1);
4487 return cfgerr;
4488 }
4489#else
4490 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
4491 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4492 ECDHE_DEFAULT_CURVE);
4493#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004494
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004495 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02004496 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004497 ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
4498 curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun2b58d042012-09-20 17:10:03 +02004499 cfgerr++;
4500 }
4501 else {
4502 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
4503 EC_KEY_free(ecdh);
4504 }
4505 }
4506#endif
4507
Emeric Brunfc0421f2012-09-07 17:30:07 +02004508 return cfgerr;
4509}
4510
Evan Broderbe554312013-06-27 00:05:25 -07004511static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
4512{
4513 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
4514 size_t prefixlen, suffixlen;
4515
4516 /* Trivial case */
4517 if (strcmp(pattern, hostname) == 0)
4518 return 1;
4519
Evan Broderbe554312013-06-27 00:05:25 -07004520 /* The rest of this logic is based on RFC 6125, section 6.4.3
4521 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
4522
Emeric Bruna848dae2013-10-08 11:27:28 +02004523 pattern_wildcard = NULL;
4524 pattern_left_label_end = pattern;
4525 while (*pattern_left_label_end != '.') {
4526 switch (*pattern_left_label_end) {
4527 case 0:
4528 /* End of label not found */
4529 return 0;
4530 case '*':
4531 /* If there is more than one wildcards */
4532 if (pattern_wildcard)
4533 return 0;
4534 pattern_wildcard = pattern_left_label_end;
4535 break;
4536 }
4537 pattern_left_label_end++;
4538 }
4539
4540 /* If it's not trivial and there is no wildcard, it can't
4541 * match */
4542 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07004543 return 0;
4544
4545 /* Make sure all labels match except the leftmost */
4546 hostname_left_label_end = strchr(hostname, '.');
4547 if (!hostname_left_label_end
4548 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
4549 return 0;
4550
4551 /* Make sure the leftmost label of the hostname is long enough
4552 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02004553 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07004554 return 0;
4555
4556 /* Finally compare the string on either side of the
4557 * wildcard */
4558 prefixlen = pattern_wildcard - pattern;
4559 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02004560 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
4561 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07004562 return 0;
4563
4564 return 1;
4565}
4566
4567static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
4568{
4569 SSL *ssl;
4570 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004571 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004572 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02004573 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07004574
4575 int depth;
4576 X509 *cert;
4577 STACK_OF(GENERAL_NAME) *alt_names;
4578 int i;
4579 X509_NAME *cert_subject;
4580 char *str;
4581
4582 if (ok == 0)
4583 return ok;
4584
4585 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004586 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01004587 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07004588
Willy Tarreauad92a9a2017-07-28 11:38:41 +02004589 /* We're checking if the provided hostnames match the desired one. The
4590 * desired hostname comes from the SNI we presented if any, or if not
4591 * provided then it may have been explicitly stated using a "verifyhost"
4592 * directive. If neither is set, we don't care about the name so the
4593 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02004594 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004595 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02004596 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004597 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02004598 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004599 if (!servername)
4600 return ok;
4601 }
Evan Broderbe554312013-06-27 00:05:25 -07004602
4603 /* We only need to verify the CN on the actual server cert,
4604 * not the indirect CAs */
4605 depth = X509_STORE_CTX_get_error_depth(ctx);
4606 if (depth != 0)
4607 return ok;
4608
4609 /* At this point, the cert is *not* OK unless we can find a
4610 * hostname match */
4611 ok = 0;
4612
4613 cert = X509_STORE_CTX_get_current_cert(ctx);
4614 /* It seems like this might happen if verify peer isn't set */
4615 if (!cert)
4616 return ok;
4617
4618 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
4619 if (alt_names) {
4620 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
4621 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
4622 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004623#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02004624 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
4625#else
Evan Broderbe554312013-06-27 00:05:25 -07004626 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02004627#endif
Evan Broderbe554312013-06-27 00:05:25 -07004628 ok = ssl_sock_srv_hostcheck(str, servername);
4629 OPENSSL_free(str);
4630 }
4631 }
4632 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02004633 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07004634 }
4635
4636 cert_subject = X509_get_subject_name(cert);
4637 i = -1;
4638 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
4639 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004640 ASN1_STRING *value;
4641 value = X509_NAME_ENTRY_get_data(entry);
4642 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07004643 ok = ssl_sock_srv_hostcheck(str, servername);
4644 OPENSSL_free(str);
4645 }
4646 }
4647
Willy Tarreau71d058c2017-07-26 20:09:56 +02004648 /* report the mismatch and indicate if SNI was used or not */
4649 if (!ok && !conn->err_code)
4650 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07004651 return ok;
4652}
4653
Emeric Brun94324a42012-10-11 14:00:19 +02004654/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01004655int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02004656{
Willy Tarreau03209342016-12-22 17:08:28 +01004657 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02004658 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004659 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02004660 SSL_OP_ALL | /* all known workarounds for bugs */
4661 SSL_OP_NO_SSLv2 |
4662 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004663 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02004664 SSL_MODE_ENABLE_PARTIAL_WRITE |
4665 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004666 SSL_MODE_RELEASE_BUFFERS |
4667 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01004668 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004669 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004670 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004671 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004672 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02004673
Thierry Fournier383085f2013-01-24 14:15:43 +01004674 /* Make sure openssl opens /dev/urandom before the chroot */
4675 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004676 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01004677 cfgerr++;
4678 }
4679
Willy Tarreaufce03112015-01-15 21:32:40 +01004680 /* Automatic memory computations need to know we use SSL there */
4681 global.ssl_used_backend = 1;
4682
4683 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02004684 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01004685 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004686 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
4687 curproxy->id, srv->id,
4688 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02004689 cfgerr++;
4690 return cfgerr;
4691 }
4692 }
Emeric Brun94324a42012-10-11 14:00:19 +02004693 if (srv->use_ssl)
4694 srv->xprt = &ssl_sock;
4695 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01004696 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02004697
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004698 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004699 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004700 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
4701 proxy_type_str(curproxy), curproxy->id,
4702 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02004703 cfgerr++;
4704 return cfgerr;
4705 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004706
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004707 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004708 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
4709 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4710 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004711 else
4712 flags = conf_ssl_methods->flags;
4713
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004714 /* Real min and max should be determinate with configuration and openssl's capabilities */
4715 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004716 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004717 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004718 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004719
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004720 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004721 min = max = CONF_TLSV_NONE;
4722 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004723 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004724 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004725 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004726 if (min) {
4727 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004728 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
4729 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4730 proxy_type_str(curproxy), curproxy->id, srv->id,
4731 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004732 hole = 0;
4733 }
4734 max = i;
4735 }
4736 else {
4737 min = max = i;
4738 }
4739 }
4740 else {
4741 if (min)
4742 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004743 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004744 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004745 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
4746 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004747 cfgerr += 1;
4748 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004749
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004750#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004751 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004752 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004753 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004754 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004755 else
4756 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4757 if (flags & methodVersions[i].flag)
4758 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004759#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004760 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004761 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4762 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004763#endif
4764
4765 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
4766 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004767 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004768
Willy Tarreau5db847a2019-05-09 14:13:35 +02004769#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004770 if (global_ssl.async)
4771 mode |= SSL_MODE_ASYNC;
4772#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004773 SSL_CTX_set_mode(ctx, mode);
4774 srv->ssl_ctx.ctx = ctx;
4775
Emeric Bruna7aa3092012-10-26 12:58:00 +02004776 if (srv->ssl_ctx.client_crt) {
4777 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 +01004778 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
4779 proxy_type_str(curproxy), curproxy->id,
4780 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004781 cfgerr++;
4782 }
4783 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 +01004784 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
4785 proxy_type_str(curproxy), curproxy->id,
4786 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004787 cfgerr++;
4788 }
4789 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004790 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
4791 proxy_type_str(curproxy), curproxy->id,
4792 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004793 cfgerr++;
4794 }
4795 }
Emeric Brun94324a42012-10-11 14:00:19 +02004796
Emeric Brun850efd52014-01-29 12:24:34 +01004797 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
4798 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01004799 switch (srv->ssl_ctx.verify) {
4800 case SSL_SOCK_VERIFY_NONE:
4801 verify = SSL_VERIFY_NONE;
4802 break;
4803 case SSL_SOCK_VERIFY_REQUIRED:
4804 verify = SSL_VERIFY_PEER;
4805 break;
4806 }
Evan Broderbe554312013-06-27 00:05:25 -07004807 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01004808 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02004809 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01004810 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02004811 if (srv->ssl_ctx.ca_file) {
4812 /* load CAfile to verify */
4813 if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004814 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n",
4815 curproxy->id, srv->id,
4816 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004817 cfgerr++;
4818 }
4819 }
Emeric Brun850efd52014-01-29 12:24:34 +01004820 else {
4821 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01004822 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",
4823 curproxy->id, srv->id,
4824 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004825 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01004826 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
4827 curproxy->id, srv->id,
4828 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004829 cfgerr++;
4830 }
Emeric Brunef42d922012-10-11 16:11:36 +02004831#ifdef X509_V_FLAG_CRL_CHECK
4832 if (srv->ssl_ctx.crl_file) {
4833 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
4834
4835 if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004836 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
4837 curproxy->id, srv->id,
4838 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004839 cfgerr++;
4840 }
4841 else {
4842 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4843 }
4844 }
4845#endif
4846 }
4847
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004848 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
4849 SSL_SESS_CACHE_NO_INTERNAL_STORE);
4850 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02004851 if (srv->ssl_ctx.ciphers &&
4852 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004853 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
4854 curproxy->id, srv->id,
4855 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02004856 cfgerr++;
4857 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004858
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004859#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004860 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00004861 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004862 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
4863 curproxy->id, srv->id,
4864 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
4865 cfgerr++;
4866 }
4867#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01004868#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
4869 if (srv->ssl_ctx.npn_str)
4870 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
4871#endif
4872#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4873 if (srv->ssl_ctx.alpn_str)
4874 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
4875#endif
4876
Emeric Brun94324a42012-10-11 14:00:19 +02004877
4878 return cfgerr;
4879}
4880
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004881/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004882 * be NULL, in which case nothing is done. Returns the number of errors
4883 * encountered.
4884 */
Willy Tarreau03209342016-12-22 17:08:28 +01004885int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004886{
4887 struct ebmb_node *node;
4888 struct sni_ctx *sni;
4889 int err = 0;
4890
Willy Tarreaufce03112015-01-15 21:32:40 +01004891 /* Automatic memory computations need to know we use SSL there */
4892 global.ssl_used_frontend = 1;
4893
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004894 /* Make sure openssl opens /dev/urandom before the chroot */
4895 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004896 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004897 err++;
4898 }
4899 /* Create initial_ctx used to start the ssl connection before do switchctx */
4900 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004901 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004902 /* It should not be necessary to call this function, but it's
4903 necessary first to check and move all initialisation related
4904 to initial_ctx in ssl_sock_initial_ctx. */
4905 err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx);
4906 }
Emeric Brun0bed9942014-10-30 19:25:24 +01004907 if (bind_conf->default_ctx)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004908 err += ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx);
Emeric Brun0bed9942014-10-30 19:25:24 +01004909
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004910 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004911 while (node) {
4912 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004913 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4914 /* only initialize the CTX on its first occurrence and
4915 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004916 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004917 node = ebmb_next(node);
4918 }
4919
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004920 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004921 while (node) {
4922 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004923 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4924 /* only initialize the CTX on its first occurrence and
4925 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004926 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004927 node = ebmb_next(node);
4928 }
4929 return err;
4930}
4931
Willy Tarreau55d37912016-12-21 23:38:39 +01004932/* Prepares all the contexts for a bind_conf and allocates the shared SSL
4933 * context if needed. Returns < 0 on error, 0 on success. The warnings and
4934 * alerts are directly emitted since the rest of the stack does it below.
4935 */
4936int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
4937{
4938 struct proxy *px = bind_conf->frontend;
4939 int alloc_ctx;
4940 int err;
4941
4942 if (!bind_conf->is_ssl) {
4943 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004944 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
4945 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01004946 }
4947 return 0;
4948 }
4949 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004950 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004951 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
4952 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004953 }
4954 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004955 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
4956 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004957 return -1;
4958 }
Willy Tarreau55d37912016-12-21 23:38:39 +01004959 }
William Lallemandc61c0b32017-12-04 18:46:39 +01004960 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01004961 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02004962 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01004963 sizeof(*sh_ssl_sess_tree),
4964 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02004965 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01004966 if (alloc_ctx == SHCTX_E_INIT_LOCK)
4967 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");
4968 else
4969 ha_alert("Unable to allocate SSL session cache.\n");
4970 return -1;
4971 }
4972 /* free block callback */
4973 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
4974 /* init the root tree within the extra space */
4975 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
4976 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01004977 }
Willy Tarreau55d37912016-12-21 23:38:39 +01004978 err = 0;
4979 /* initialize all certificate contexts */
4980 err += ssl_sock_prepare_all_ctx(bind_conf);
4981
4982 /* initialize CA variables if the certificates generation is enabled */
4983 err += ssl_sock_load_ca(bind_conf);
4984
4985 return -err;
4986}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02004987
4988/* release ssl context allocated for servers. */
4989void ssl_sock_free_srv_ctx(struct server *srv)
4990{
Olivier Houchardc7566002018-11-20 23:33:50 +01004991#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4992 if (srv->ssl_ctx.alpn_str)
4993 free(srv->ssl_ctx.alpn_str);
4994#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01004995#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01004996 if (srv->ssl_ctx.npn_str)
4997 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01004998#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02004999 if (srv->ssl_ctx.ctx)
5000 SSL_CTX_free(srv->ssl_ctx.ctx);
5001}
5002
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005003/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005004 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5005 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005006void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005007{
5008 struct ebmb_node *node, *back;
5009 struct sni_ctx *sni;
5010
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005011 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005012 while (node) {
5013 sni = ebmb_entry(node, struct sni_ctx, name);
5014 back = ebmb_next(node);
5015 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005016 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005017 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005018 ssl_sock_free_ssl_conf(sni->conf);
5019 free(sni->conf);
5020 sni->conf = NULL;
5021 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005022 free(sni);
5023 node = back;
5024 }
5025
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005026 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005027 while (node) {
5028 sni = ebmb_entry(node, struct sni_ctx, name);
5029 back = ebmb_next(node);
5030 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005031 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005032 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005033 ssl_sock_free_ssl_conf(sni->conf);
5034 free(sni->conf);
5035 sni->conf = NULL;
5036 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005037 free(sni);
5038 node = back;
5039 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005040 SSL_CTX_free(bind_conf->initial_ctx);
5041 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005042 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005043 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005044}
5045
Willy Tarreau795cdab2016-12-22 17:30:54 +01005046/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5047void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5048{
5049 ssl_sock_free_ca(bind_conf);
5050 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005051 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005052 free(bind_conf->ca_sign_file);
5053 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005054 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005055 free(bind_conf->keys_ref->filename);
5056 free(bind_conf->keys_ref->tlskeys);
5057 LIST_DEL(&bind_conf->keys_ref->list);
5058 free(bind_conf->keys_ref);
5059 }
5060 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005061 bind_conf->ca_sign_pass = NULL;
5062 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005063}
5064
Christopher Faulet31af49d2015-06-09 17:29:50 +02005065/* Load CA cert file and private key used to generate certificates */
5066int
Willy Tarreau03209342016-12-22 17:08:28 +01005067ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005068{
Willy Tarreau03209342016-12-22 17:08:28 +01005069 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005070 FILE *fp;
5071 X509 *cacert = NULL;
5072 EVP_PKEY *capkey = NULL;
5073 int err = 0;
5074
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005075 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005076 return err;
5077
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005078#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005079 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005080 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005081 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005082 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005083 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005084#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005085
Christopher Faulet31af49d2015-06-09 17:29:50 +02005086 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005087 ha_alert("Proxy '%s': cannot enable certificate generation, "
5088 "no CA certificate File configured at [%s:%d].\n",
5089 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005090 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005091 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005092
5093 /* read in the CA certificate */
5094 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005095 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5096 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005097 goto load_error;
5098 }
5099 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005100 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5101 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005102 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005103 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005104 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005105 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005106 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5107 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005108 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005109 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005110
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005111 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005112 bind_conf->ca_sign_cert = cacert;
5113 bind_conf->ca_sign_pkey = capkey;
5114 return err;
5115
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005116 read_error:
5117 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005118 if (capkey) EVP_PKEY_free(capkey);
5119 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005120 load_error:
5121 bind_conf->generate_certs = 0;
5122 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005123 return err;
5124}
5125
5126/* Release CA cert and private key used to generate certificated */
5127void
5128ssl_sock_free_ca(struct bind_conf *bind_conf)
5129{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005130 if (bind_conf->ca_sign_pkey)
5131 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5132 if (bind_conf->ca_sign_cert)
5133 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005134 bind_conf->ca_sign_pkey = NULL;
5135 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005136}
5137
Emeric Brun46591952012-05-18 15:47:34 +02005138/*
5139 * This function is called if SSL * context is not yet allocated. The function
5140 * is designed to be called before any other data-layer operation and sets the
5141 * handshake flag on the connection. It is safe to call it multiple times.
5142 * It returns 0 on success and -1 in error case.
5143 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005144static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005145{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005146 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005147 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005148 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005149 return 0;
5150
Willy Tarreau3c728722014-01-23 13:50:42 +01005151 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005152 return 0;
5153
Olivier Houchard66ab4982019-02-26 18:37:15 +01005154 ctx = pool_alloc(ssl_sock_ctx_pool);
5155 if (!ctx) {
5156 conn->err_code = CO_ER_SSL_NO_MEM;
5157 return -1;
5158 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005159 ctx->wait_event.tasklet = tasklet_new();
5160 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005161 conn->err_code = CO_ER_SSL_NO_MEM;
5162 pool_free(ssl_sock_ctx_pool, ctx);
5163 return -1;
5164 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005165 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5166 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005167 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005168 ctx->sent_early_data = 0;
5169 ctx->tmp_early_data = -1;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005170 ctx->conn = conn;
Olivier Houchard81284e62019-06-06 13:21:23 +02005171 ctx->send_wait = NULL;
5172 ctx->recv_wait = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02005173 ctx->xprt_st = 0;
5174 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005175
5176 /* Only work with sockets for now, this should be adapted when we'll
5177 * add QUIC support.
5178 */
5179 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005180 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005181 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5182 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005183 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005184
Willy Tarreau20879a02012-12-03 16:32:10 +01005185 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5186 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005187 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005188 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005189
Emeric Brun46591952012-05-18 15:47:34 +02005190 /* If it is in client mode initiate SSL session
5191 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005192 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005193 int may_retry = 1;
5194
5195 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005196 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005197 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5198 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005199 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005200 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005201 goto retry_connect;
5202 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005203 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005204 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005205 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005206 ctx->bio = BIO_new(ha_meth);
5207 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005208 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005209 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005210 goto retry_connect;
5211 }
Emeric Brun55476152014-11-12 17:35:37 +01005212 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005213 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005214 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005215 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005216 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005217
Evan Broderbe554312013-06-27 00:05:25 -07005218 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005219 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5220 SSL_free(ctx->ssl);
5221 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005222 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005223 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005224 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005225 goto retry_connect;
5226 }
Emeric Brun55476152014-11-12 17:35:37 +01005227 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005228 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005229 }
5230
Olivier Houchard66ab4982019-02-26 18:37:15 +01005231 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005232 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5233 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5234 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 +01005235 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005236 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005237 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5238 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005239 } else if (sess) {
5240 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005241 }
5242 }
Evan Broderbe554312013-06-27 00:05:25 -07005243
Emeric Brun46591952012-05-18 15:47:34 +02005244 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005245 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005246
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005247 _HA_ATOMIC_ADD(&sslconns, 1);
5248 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005249 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005250 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005251 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005252 if (conn->flags & CO_FL_ERROR)
5253 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005254 return 0;
5255 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005256 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005257 int may_retry = 1;
5258
5259 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005260 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005261 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5262 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005263 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005264 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005265 goto retry_accept;
5266 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005267 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005268 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005269 }
Emeric Brun46591952012-05-18 15:47:34 +02005270
Olivier Houcharda8955d52019-04-07 22:00:38 +02005271 ctx->bio = BIO_new(ha_meth);
5272 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005273 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005274 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005275 goto retry_accept;
5276 }
Emeric Brun55476152014-11-12 17:35:37 +01005277 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005278 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005279 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005280 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005281 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005282
Emeric Brune1f38db2012-09-03 20:36:47 +02005283 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005284 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5285 SSL_free(ctx->ssl);
5286 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005287 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005288 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005289 goto retry_accept;
5290 }
Emeric Brun55476152014-11-12 17:35:37 +01005291 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005292 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005293 }
5294
Olivier Houchard66ab4982019-02-26 18:37:15 +01005295 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02005296
Emeric Brun46591952012-05-18 15:47:34 +02005297 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005298 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02005299#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005300 conn->flags |= CO_FL_EARLY_SSL_HS;
5301#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02005302
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005303 _HA_ATOMIC_ADD(&sslconns, 1);
5304 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005305 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005306 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005307 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005308 if (conn->flags & CO_FL_ERROR)
5309 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005310 return 0;
5311 }
5312 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01005313 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005314err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005315 if (ctx && ctx->wait_event.tasklet)
5316 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005317 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02005318 return -1;
5319}
5320
5321
5322/* This is the callback which is used when an SSL handshake is pending. It
5323 * updates the FD status if it wants some polling before being called again.
5324 * It returns 0 if it fails in a fatal way or needs to poll to go further,
5325 * otherwise it returns non-zero and removes itself from the connection's
5326 * flags (the bit is provided in <flag> by the caller).
5327 */
Olivier Houchard000694c2019-05-23 14:45:12 +02005328static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02005329{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005330 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005331 int ret;
5332
Willy Tarreau3c728722014-01-23 13:50:42 +01005333 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005334 return 0;
5335
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005336 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005337 goto out_error;
5338
Willy Tarreau5db847a2019-05-09 14:13:35 +02005339#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02005340 /*
5341 * Check if we have early data. If we do, we have to read them
5342 * before SSL_do_handshake() is called, And there's no way to
5343 * detect early data, except to try to read them
5344 */
5345 if (conn->flags & CO_FL_EARLY_SSL_HS) {
5346 size_t read_data;
5347
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005348 ret = SSL_read_early_data(ctx->ssl, &ctx->tmp_early_data,
Olivier Houchardc2aae742017-09-22 18:26:28 +02005349 1, &read_data);
5350 if (ret == SSL_READ_EARLY_DATA_ERROR)
5351 goto check_error;
5352 if (ret == SSL_READ_EARLY_DATA_SUCCESS) {
5353 conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN);
5354 return 1;
5355 } else
5356 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5357 }
5358#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005359 /* If we use SSL_do_handshake to process a reneg initiated by
5360 * the remote peer, it sometimes returns SSL_ERROR_SSL.
5361 * Usually SSL_write and SSL_read are used and process implicitly
5362 * the reneg handshake.
5363 * Here we use SSL_peek as a workaround for reneg.
5364 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005365 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005366 char c;
5367
Olivier Houchard66ab4982019-02-26 18:37:15 +01005368 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01005369 if (ret <= 0) {
5370 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005371 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005372
Emeric Brun674b7432012-11-08 19:21:55 +01005373 if (ret == SSL_ERROR_WANT_WRITE) {
5374 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005375 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005376 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005377 return 0;
5378 }
5379 else if (ret == SSL_ERROR_WANT_READ) {
5380 /* handshake may have been completed but we have
5381 * no more data to read.
5382 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005383 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005384 ret = 1;
5385 goto reneg_ok;
5386 }
5387 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005388 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005389 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005390 return 0;
5391 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005392#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005393 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005394 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005395 return 0;
5396 }
5397#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005398 else if (ret == SSL_ERROR_SYSCALL) {
5399 /* if errno is null, then connection was successfully established */
5400 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5401 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01005402 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02005403#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5404 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005405 conn->err_code = CO_ER_SSL_HANDSHAKE;
5406#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005407 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005408#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02005409 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005410 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005411 empty_handshake = state == TLS_ST_BEFORE;
5412#else
Lukas Tribus49799162019-07-08 14:29:15 +02005413 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5414 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005415#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005416 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02005417 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005418 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005419 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5420 else
5421 conn->err_code = CO_ER_SSL_EMPTY;
5422 }
5423 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005424 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005425 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5426 else
5427 conn->err_code = CO_ER_SSL_ABORT;
5428 }
5429 }
5430 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005431 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005432 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01005433 else
Emeric Brun29f037d2014-04-25 19:05:36 +02005434 conn->err_code = CO_ER_SSL_HANDSHAKE;
5435 }
Lukas Tribus49799162019-07-08 14:29:15 +02005436#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01005437 }
Emeric Brun674b7432012-11-08 19:21:55 +01005438 goto out_error;
5439 }
5440 else {
5441 /* Fail on all other handshake errors */
5442 /* Note: OpenSSL may leave unread bytes in the socket's
5443 * buffer, causing an RST to be emitted upon close() on
5444 * TCP sockets. We first try to drain possibly pending
5445 * data to avoid this as much as possible.
5446 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005447 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005448 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005449 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005450 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01005451 goto out_error;
5452 }
5453 }
5454 /* read some data: consider handshake completed */
5455 goto reneg_ok;
5456 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005457 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005458check_error:
Emeric Brun46591952012-05-18 15:47:34 +02005459 if (ret != 1) {
5460 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005461 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005462
5463 if (ret == SSL_ERROR_WANT_WRITE) {
5464 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005465 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005466 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005467 return 0;
5468 }
5469 else if (ret == SSL_ERROR_WANT_READ) {
5470 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02005471 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005472 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5473 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005474 return 0;
5475 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005476#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005477 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005478 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005479 return 0;
5480 }
5481#endif
Willy Tarreau89230192012-09-28 20:22:13 +02005482 else if (ret == SSL_ERROR_SYSCALL) {
5483 /* if errno is null, then connection was successfully established */
5484 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5485 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005486 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02005487#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5488 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005489 conn->err_code = CO_ER_SSL_HANDSHAKE;
5490#else
5491 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005492#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02005493 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005494 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005495 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005496#else
Lukas Tribus49799162019-07-08 14:29:15 +02005497 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5498 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005499#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005500 if (empty_handshake) {
5501 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005502 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005503 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5504 else
5505 conn->err_code = CO_ER_SSL_EMPTY;
5506 }
5507 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005508 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005509 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5510 else
5511 conn->err_code = CO_ER_SSL_ABORT;
5512 }
Emeric Brun29f037d2014-04-25 19:05:36 +02005513 }
5514 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005515 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005516 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5517 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005518 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02005519 }
Lukas Tribus49799162019-07-08 14:29:15 +02005520#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02005521 }
Willy Tarreau89230192012-09-28 20:22:13 +02005522 goto out_error;
5523 }
Emeric Brun46591952012-05-18 15:47:34 +02005524 else {
5525 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02005526 /* Note: OpenSSL may leave unread bytes in the socket's
5527 * buffer, causing an RST to be emitted upon close() on
5528 * TCP sockets. We first try to drain possibly pending
5529 * data to avoid this as much as possible.
5530 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005531 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005532 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005533 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005534 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005535 goto out_error;
5536 }
5537 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005538#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01005539 else {
5540 /*
5541 * If the server refused the early data, we have to send a
5542 * 425 to the client, as we no longer have the data to sent
5543 * them again.
5544 */
5545 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005546 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01005547 conn->err_code = CO_ER_SSL_EARLY_FAILED;
5548 goto out_error;
5549 }
5550 }
5551 }
5552#endif
5553
Emeric Brun46591952012-05-18 15:47:34 +02005554
Emeric Brun674b7432012-11-08 19:21:55 +01005555reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00005556
Willy Tarreau5db847a2019-05-09 14:13:35 +02005557#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005558 /* ASYNC engine API doesn't support moving read/write
5559 * buffers. So we disable ASYNC mode right after
5560 * the handshake to avoid buffer oveflows.
5561 */
5562 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005563 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005564#endif
Emeric Brun46591952012-05-18 15:47:34 +02005565 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005566 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005567 if (objt_server(conn->target)) {
5568 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
5569 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
5570 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02005571 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005572 else {
5573 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
5574 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
5575 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
5576 }
Emeric Brun46591952012-05-18 15:47:34 +02005577 }
5578
5579 /* The connection is now established at both layers, it's time to leave */
5580 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
5581 return 1;
5582
5583 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01005584 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005585 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005586 ERR_clear_error();
5587
Emeric Brun9fa89732012-10-04 17:09:56 +02005588 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02005589 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5590 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5591 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02005592 }
5593
Emeric Brun46591952012-05-18 15:47:34 +02005594 /* Fail on all other handshake errors */
5595 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01005596 if (!conn->err_code)
5597 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005598 return 0;
5599}
5600
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005601static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01005602{
Olivier Houchardea8dd942019-05-20 14:02:16 +02005603 struct wait_event *sw;
5604 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005605
Olivier Houchard0ff28652019-06-24 18:57:39 +02005606 if (!ctx)
5607 return -1;
5608
Olivier Houchardea8dd942019-05-20 14:02:16 +02005609 if (event_type & SUB_RETRY_RECV) {
5610 sw = param;
5611 BUG_ON(ctx->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
5612 sw->events |= SUB_RETRY_RECV;
5613 ctx->recv_wait = sw;
5614 if (!(conn->flags & CO_FL_SSL_WAIT_HS) &&
5615 !(ctx->wait_event.events & SUB_RETRY_RECV))
5616 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
5617 event_type &= ~SUB_RETRY_RECV;
5618 }
5619 if (event_type & SUB_RETRY_SEND) {
5620sw = param;
5621 BUG_ON(ctx->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
5622 sw->events |= SUB_RETRY_SEND;
5623 ctx->send_wait = sw;
5624 if (!(conn->flags & CO_FL_SSL_WAIT_HS) &&
5625 !(ctx->wait_event.events & SUB_RETRY_SEND))
5626 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
5627 event_type &= ~SUB_RETRY_SEND;
5628
5629 }
5630 if (event_type != 0)
5631 return -1;
5632 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01005633}
5634
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005635static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01005636{
Olivier Houchardea8dd942019-05-20 14:02:16 +02005637 struct wait_event *sw;
5638 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005639
Olivier Houchardea8dd942019-05-20 14:02:16 +02005640 if (event_type & SUB_RETRY_RECV) {
5641 sw = param;
5642 BUG_ON(ctx->recv_wait != sw);
5643 ctx->recv_wait = NULL;
5644 sw->events &= ~SUB_RETRY_RECV;
5645 /* If we subscribed, and we're not doing the handshake,
5646 * then we subscribed because the upper layer asked for it,
5647 * as the upper layer is no longer interested, we can
5648 * unsubscribe too.
5649 */
5650 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) &&
5651 (ctx->wait_event.events & SUB_RETRY_RECV))
5652 conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV,
5653 &ctx->wait_event);
5654 }
5655 if (event_type & SUB_RETRY_SEND) {
5656 sw = param;
5657 BUG_ON(ctx->send_wait != sw);
5658 ctx->send_wait = NULL;
5659 sw->events &= ~SUB_RETRY_SEND;
5660 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) &&
5661 (ctx->wait_event.events & SUB_RETRY_SEND))
5662 conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND,
5663 &ctx->wait_event);
5664
5665 }
5666
5667 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01005668}
5669
Olivier Houchard2e055482019-05-27 19:50:12 +02005670/* Use the provided XPRT as an underlying XPRT, and provide the old one.
5671 * Returns 0 on success, and non-zero on failure.
5672 */
5673static 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)
5674{
5675 struct ssl_sock_ctx *ctx = xprt_ctx;
5676
5677 if (oldxprt_ops != NULL)
5678 *oldxprt_ops = ctx->xprt;
5679 if (oldxprt_ctx != NULL)
5680 *oldxprt_ctx = ctx->xprt_ctx;
5681 ctx->xprt = toadd_ops;
5682 ctx->xprt_ctx = toadd_ctx;
5683 return 0;
5684}
5685
Olivier Houchard5149b592019-05-23 17:47:36 +02005686/* Remove the specified xprt. If if it our underlying XPRT, remove it and
5687 * return 0, otherwise just call the remove_xprt method from the underlying
5688 * XPRT.
5689 */
5690static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
5691{
5692 struct ssl_sock_ctx *ctx = xprt_ctx;
5693
5694 if (ctx->xprt_ctx == toremove_ctx) {
5695 ctx->xprt_ctx = newctx;
5696 ctx->xprt = newops;
5697 return 0;
5698 }
5699 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
5700}
5701
Olivier Houchardea8dd942019-05-20 14:02:16 +02005702static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
5703{
5704 struct ssl_sock_ctx *ctx = context;
5705
5706 /* First if we're doing an handshake, try that */
5707 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
5708 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
5709 /* If we had an error, or the handshake is done and I/O is available,
5710 * let the upper layer know.
5711 * If no mux was set up yet, and nobody subscribed, then call
5712 * xprt_done_cb() ourself if it's set, or destroy the connection,
5713 * we can't be sure conn_fd_handler() will be called again.
5714 */
5715 if ((ctx->conn->flags & CO_FL_ERROR) ||
5716 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
5717 int ret = 0;
5718 int woke = 0;
5719
5720 /* On error, wake any waiter */
5721 if (ctx->recv_wait) {
5722 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005723 tasklet_wakeup(ctx->recv_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005724 ctx->recv_wait = NULL;
5725 woke = 1;
5726 }
5727 if (ctx->send_wait) {
5728 ctx->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005729 tasklet_wakeup(ctx->send_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005730 ctx->send_wait = NULL;
5731 woke = 1;
5732 }
5733 /* If we're the first xprt for the connection, let the
5734 * upper layers know. If xprt_done_cb() is set, call it,
5735 * otherwise, we should have a mux, so call its wake
5736 * method if we didn't woke a tasklet already.
5737 */
5738 if (ctx->conn->xprt_ctx == ctx) {
5739 if (ctx->conn->xprt_done_cb)
5740 ret = ctx->conn->xprt_done_cb(ctx->conn);
5741 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
5742 ctx->conn->mux->wake(ctx->conn);
5743 return NULL;
5744 }
5745 }
5746 return NULL;
5747}
5748
Emeric Brun46591952012-05-18 15:47:34 +02005749/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01005750 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02005751 * buffer wraps, in which case a second call may be performed. The connection's
5752 * flags are updated with whatever special event is detected (error, read0,
5753 * empty). The caller is responsible for taking care of those events and
5754 * avoiding the call if inappropriate. The function does not call the
5755 * connection's polling update function, so the caller is responsible for this.
5756 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005757static 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 +02005758{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005759 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02005760 ssize_t ret;
5761 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02005762
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005763 conn_refresh_polling_flags(conn);
5764
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005765 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005766 goto out_error;
5767
5768 if (conn->flags & CO_FL_HANDSHAKE)
5769 /* a handshake was requested */
5770 return 0;
5771
Emeric Brun46591952012-05-18 15:47:34 +02005772 /* read the largest possible block. For this, we perform only one call
5773 * to recv() unless the buffer wraps and we exactly fill the first hunk,
5774 * in which case we accept to do it once again. A new attempt is made on
5775 * EINTR too.
5776 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01005777 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02005778 int need_out = 0;
5779
Willy Tarreau591d4452018-06-15 17:21:00 +02005780 try = b_contig_space(buf);
5781 if (!try)
5782 break;
5783
Willy Tarreauabf08d92014-01-14 11:31:27 +01005784 if (try > count)
5785 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02005786
Olivier Houchardc2aae742017-09-22 18:26:28 +02005787 if (((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_EARLY_SSL_HS) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005788 ctx->tmp_early_data != -1) {
5789 *b_tail(buf) = ctx->tmp_early_data;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005790 done++;
5791 try--;
5792 count--;
Olivier Houchardacd14032018-06-28 18:17:23 +02005793 b_add(buf, 1);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005794 ctx->tmp_early_data = -1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005795 continue;
5796 }
Willy Tarreauabf08d92014-01-14 11:31:27 +01005797
Willy Tarreau5db847a2019-05-09 14:13:35 +02005798#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005799 if (conn->flags & CO_FL_EARLY_SSL_HS) {
5800 size_t read_length;
5801
Olivier Houchard66ab4982019-02-26 18:37:15 +01005802 ret = SSL_read_early_data(ctx->ssl,
Willy Tarreau8f9c72d2018-06-07 18:46:28 +02005803 b_tail(buf), try, &read_length);
Olivier Houchard522eea72017-11-03 16:27:47 +01005804 if (ret == SSL_READ_EARLY_DATA_SUCCESS &&
5805 read_length > 0)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005806 conn->flags |= CO_FL_EARLY_DATA;
5807 if (ret == SSL_READ_EARLY_DATA_SUCCESS ||
5808 ret == SSL_READ_EARLY_DATA_FINISH) {
5809 if (ret == SSL_READ_EARLY_DATA_FINISH) {
5810 /*
5811 * We're done reading the early data,
5812 * let's make the handshake
5813 */
5814 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5815 conn->flags |= CO_FL_SSL_WAIT_HS;
5816 need_out = 1;
Olivier Houchard965e84e2019-06-15 20:59:30 +02005817 /* Now initiate the handshake */
5818 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005819 if (read_length == 0)
5820 break;
5821 }
5822 ret = read_length;
5823 }
5824 } else
5825#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01005826 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02005827
Emeric Brune1f38db2012-09-03 20:36:47 +02005828 if (conn->flags & CO_FL_ERROR) {
5829 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01005830 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02005831 }
Emeric Brun46591952012-05-18 15:47:34 +02005832 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02005833 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005834 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02005835 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02005836 }
Emeric Brun46591952012-05-18 15:47:34 +02005837 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005838 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005839 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01005840 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02005841 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005842 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005843#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005844 /* Async mode can be re-enabled, because we're leaving data state.*/
5845 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005846 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005847#endif
Emeric Brun46591952012-05-18 15:47:34 +02005848 break;
5849 }
5850 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005851 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005852 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5853 SUB_RETRY_RECV,
5854 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01005855 /* handshake is running, and it may need to re-enable read */
5856 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005857#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005858 /* Async mode can be re-enabled, because we're leaving data state.*/
5859 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005860 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005861#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01005862 break;
5863 }
Emeric Brun46591952012-05-18 15:47:34 +02005864 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005865 } else if (ret == SSL_ERROR_ZERO_RETURN)
5866 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005867 /* For SSL_ERROR_SYSCALL, make sure to clear the error
5868 * stack before shutting down the connection for
5869 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005870 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
5871 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02005872 /* otherwise it's a real error */
5873 goto out_error;
5874 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005875 if (need_out)
5876 break;
Emeric Brun46591952012-05-18 15:47:34 +02005877 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005878 leave:
Emeric Brun46591952012-05-18 15:47:34 +02005879 return done;
5880
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005881 clear_ssl_error:
5882 /* Clear openssl global errors stack */
5883 ssl_sock_dump_errors(conn);
5884 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02005885 read0:
5886 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005887 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005888
Emeric Brun46591952012-05-18 15:47:34 +02005889 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005890 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01005891 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005892 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005893 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005894 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02005895}
5896
5897
Willy Tarreau787db9a2018-06-14 18:31:46 +02005898/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5899 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5900 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02005901 * Only one call to send() is performed, unless the buffer wraps, in which case
5902 * a second call may be performed. The connection's flags are updated with
5903 * whatever special event is detected (error, empty). The caller is responsible
5904 * for taking care of those events and avoiding the call if inappropriate. The
5905 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02005906 * is responsible for this. The buffer's output is not adjusted, it's up to the
5907 * caller to take care of this. It's up to the caller to update the buffer's
5908 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02005909 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005910static 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 +02005911{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005912 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02005913 ssize_t ret;
5914 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02005915
5916 done = 0;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005917 conn_refresh_polling_flags(conn);
Emeric Brun46591952012-05-18 15:47:34 +02005918
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005919 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005920 goto out_error;
5921
Olivier Houchard010941f2019-05-03 20:56:19 +02005922 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02005923 /* a handshake was requested */
5924 return 0;
5925
5926 /* send the largest possible block. For this we perform only one call
5927 * to send() unless the buffer wraps and we exactly fill the first hunk,
5928 * in which case we accept to do it once again.
5929 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02005930 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02005931#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005932 size_t written_data;
5933#endif
5934
Willy Tarreau787db9a2018-06-14 18:31:46 +02005935 try = b_contig_data(buf, done);
5936 if (try > count)
5937 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01005938
Willy Tarreau7bed9452014-02-02 02:00:24 +01005939 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005940 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01005941 global_ssl.max_record && try > global_ssl.max_record) {
5942 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01005943 }
5944 else {
5945 /* we need to keep the information about the fact that
5946 * we're not limiting the upcoming send(), because if it
5947 * fails, we'll have to retry with at least as many data.
5948 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005949 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01005950 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01005951
Willy Tarreau5db847a2019-05-09 14:13:35 +02005952#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02005953 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02005954 unsigned int max_early;
5955
Olivier Houchard522eea72017-11-03 16:27:47 +01005956 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01005957 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01005958 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005959 if (SSL_get0_session(ctx->ssl))
5960 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01005961 else
5962 max_early = 0;
5963 }
5964
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005965 if (try + ctx->sent_early_data > max_early) {
5966 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01005967 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02005968 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02005969 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005970 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01005971 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005972 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005973 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005974 if (ret == 1) {
5975 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005976 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02005977 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01005978 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02005979 /* Initiate the handshake, now */
5980 tasklet_wakeup(ctx->wait_event.tasklet);
5981 }
Olivier Houchard522eea72017-11-03 16:27:47 +01005982
Olivier Houchardc2aae742017-09-22 18:26:28 +02005983 }
5984
5985 } else
5986#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01005987 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01005988
Emeric Brune1f38db2012-09-03 20:36:47 +02005989 if (conn->flags & CO_FL_ERROR) {
5990 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01005991 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02005992 }
Emeric Brun46591952012-05-18 15:47:34 +02005993 if (ret > 0) {
Olivier Houchardf24502b2019-01-17 19:09:11 +01005994 /* A send succeeded, so we can consier ourself connected */
5995 conn->flags |= CO_FL_CONNECTED;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005996 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02005997 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02005998 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02005999 }
6000 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006001 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006002
Emeric Brun46591952012-05-18 15:47:34 +02006003 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006004 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006005 /* handshake is running, and it may need to re-enable write */
6006 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006007 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006008#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006009 /* Async mode can be re-enabled, because we're leaving data state.*/
6010 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006011 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006012#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006013 break;
6014 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006015
Emeric Brun46591952012-05-18 15:47:34 +02006016 break;
6017 }
6018 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006019 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006020 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006021 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6022 SUB_RETRY_RECV,
6023 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006024#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006025 /* Async mode can be re-enabled, because we're leaving data state.*/
6026 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006027 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006028#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006029 break;
6030 }
Emeric Brun46591952012-05-18 15:47:34 +02006031 goto out_error;
6032 }
6033 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006034 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006035 return done;
6036
6037 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006038 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006039 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006040 ERR_clear_error();
6041
Emeric Brun46591952012-05-18 15:47:34 +02006042 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006043 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006044}
6045
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006046static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006047
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006048 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006049
Olivier Houchardea8dd942019-05-20 14:02:16 +02006050
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006051 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006052 if (ctx->wait_event.events != 0)
6053 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6054 ctx->wait_event.events,
6055 &ctx->wait_event);
6056 if (ctx->send_wait) {
6057 ctx->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006058 tasklet_wakeup(ctx->send_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006059 }
6060 if (ctx->recv_wait) {
6061 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006062 tasklet_wakeup(ctx->recv_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006063 }
Olivier Houchard692c1d02019-05-23 18:41:47 +02006064 if (ctx->xprt->close)
6065 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006066#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006067 if (global_ssl.async) {
6068 OSSL_ASYNC_FD all_fd[32], afd;
6069 size_t num_all_fds = 0;
6070 int i;
6071
Olivier Houchard66ab4982019-02-26 18:37:15 +01006072 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006073 if (num_all_fds > 32) {
6074 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6075 return;
6076 }
6077
Olivier Houchard66ab4982019-02-26 18:37:15 +01006078 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006079
6080 /* If an async job is pending, we must try to
6081 to catch the end using polling before calling
6082 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006083 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006084 for (i=0 ; i < num_all_fds ; i++) {
6085 /* switch on an handler designed to
6086 * handle the SSL_free
6087 */
6088 afd = all_fd[i];
6089 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006090 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006091 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006092 /* To ensure that the fd cache won't be used
6093 * and we'll catch a real RD event.
6094 */
6095 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006096 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006097 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006098 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006099 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006100 return;
6101 }
Emeric Brun3854e012017-05-17 20:42:48 +02006102 /* Else we can remove the fds from the fdtab
6103 * and call SSL_free.
6104 * note: we do a fd_remove and not a delete
6105 * because the fd is owned by the engine.
6106 * the engine is responsible to close
6107 */
6108 for (i=0 ; i < num_all_fds ; i++)
6109 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006110 }
6111#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006112 SSL_free(ctx->ssl);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006113 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006114 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006115 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006116 }
Emeric Brun46591952012-05-18 15:47:34 +02006117}
6118
6119/* This function tries to perform a clean shutdown on an SSL connection, and in
6120 * any case, flags the connection as reusable if no handshake was in progress.
6121 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006122static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006123{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006124 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006125
Emeric Brun46591952012-05-18 15:47:34 +02006126 if (conn->flags & CO_FL_HANDSHAKE)
6127 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006128 if (!clean)
6129 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006130 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006131 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006132 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006133 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006134 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006135 ERR_clear_error();
6136 }
Emeric Brun46591952012-05-18 15:47:34 +02006137}
6138
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006139/* used for ppv2 pkey alog (can be used for logging) */
Willy Tarreau83061a82018-07-13 11:56:34 +02006140int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006141{
Christopher Faulet82004142019-09-10 10:12:03 +02006142 struct ssl_sock_ctx *ctx;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006143 struct pkey_info *pkinfo;
6144 int bits = 0;
6145 int sig = TLSEXT_signature_anonymous;
6146 int len = -1;
6147
6148 if (!ssl_sock_is_ssl(conn))
6149 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02006150 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006151 pkinfo = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ctx->ssl), ssl_pkey_info_index);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006152 if (pkinfo) {
6153 sig = pkinfo->sig;
6154 bits = pkinfo->bits;
6155 } else {
6156 /* multicert and generated cert have no pkey info */
6157 X509 *crt;
6158 EVP_PKEY *pkey;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006159 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006160 if (!crt)
6161 return 0;
6162 pkey = X509_get_pubkey(crt);
6163 if (pkey) {
6164 bits = EVP_PKEY_bits(pkey);
6165 switch(EVP_PKEY_base_id(pkey)) {
6166 case EVP_PKEY_RSA:
6167 sig = TLSEXT_signature_rsa;
6168 break;
6169 case EVP_PKEY_EC:
6170 sig = TLSEXT_signature_ecdsa;
6171 break;
6172 case EVP_PKEY_DSA:
6173 sig = TLSEXT_signature_dsa;
6174 break;
6175 }
6176 EVP_PKEY_free(pkey);
6177 }
6178 }
6179
6180 switch(sig) {
6181 case TLSEXT_signature_rsa:
6182 len = chunk_printf(out, "RSA%d", bits);
6183 break;
6184 case TLSEXT_signature_ecdsa:
6185 len = chunk_printf(out, "EC%d", bits);
6186 break;
6187 case TLSEXT_signature_dsa:
6188 len = chunk_printf(out, "DSA%d", bits);
6189 break;
6190 default:
6191 return 0;
6192 }
6193 if (len < 0)
6194 return 0;
6195 return 1;
6196}
6197
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006198/* used for ppv2 cert signature (can be used for logging) */
6199const char *ssl_sock_get_cert_sig(struct connection *conn)
6200{
Christopher Faulet82004142019-09-10 10:12:03 +02006201 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006202
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006203 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6204 X509 *crt;
6205
6206 if (!ssl_sock_is_ssl(conn))
6207 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006208 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006209 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006210 if (!crt)
6211 return NULL;
6212 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6213 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6214}
6215
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006216/* used for ppv2 authority */
6217const char *ssl_sock_get_sni(struct connection *conn)
6218{
6219#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02006220 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006221
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006222 if (!ssl_sock_is_ssl(conn))
6223 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006224 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006225 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006226#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006227 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006228#endif
6229}
6230
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006231/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006232const char *ssl_sock_get_cipher_name(struct connection *conn)
6233{
Christopher Faulet82004142019-09-10 10:12:03 +02006234 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006235
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006236 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006237 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006238 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006239 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006240}
6241
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006242/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006243const char *ssl_sock_get_proto_version(struct connection *conn)
6244{
Christopher Faulet82004142019-09-10 10:12:03 +02006245 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006246
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006247 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006248 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006249 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006250 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006251}
6252
Willy Tarreau8d598402012-10-22 17:58:39 +02006253/* Extract a serial from a cert, and copy it to a chunk.
6254 * Returns 1 if serial is found and copied, 0 if no serial found and
6255 * -1 if output is not large enough.
6256 */
6257static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006258ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006259{
6260 ASN1_INTEGER *serial;
6261
6262 serial = X509_get_serialNumber(crt);
6263 if (!serial)
6264 return 0;
6265
6266 if (out->size < serial->length)
6267 return -1;
6268
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006269 memcpy(out->area, serial->data, serial->length);
6270 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006271 return 1;
6272}
6273
Emeric Brun43e79582014-10-29 19:03:26 +01006274/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006275 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6276 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006277 */
6278static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006279ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006280{
6281 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006282 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01006283
6284 len =i2d_X509(crt, NULL);
6285 if (len <= 0)
6286 return 1;
6287
6288 if (out->size < len)
6289 return -1;
6290
6291 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006292 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01006293 return 1;
6294}
6295
Emeric Brunce5ad802012-10-22 14:11:22 +02006296
Willy Tarreau83061a82018-07-13 11:56:34 +02006297/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02006298 * Returns 1 if serial is found and copied, 0 if no valid time found
6299 * and -1 if output is not large enough.
6300 */
6301static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006302ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02006303{
6304 if (tm->type == V_ASN1_GENERALIZEDTIME) {
6305 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
6306
6307 if (gentm->length < 12)
6308 return 0;
6309 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
6310 return 0;
6311 if (out->size < gentm->length-2)
6312 return -1;
6313
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006314 memcpy(out->area, gentm->data+2, gentm->length-2);
6315 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02006316 return 1;
6317 }
6318 else if (tm->type == V_ASN1_UTCTIME) {
6319 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
6320
6321 if (utctm->length < 10)
6322 return 0;
6323 if (utctm->data[0] >= 0x35)
6324 return 0;
6325 if (out->size < utctm->length)
6326 return -1;
6327
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006328 memcpy(out->area, utctm->data, utctm->length);
6329 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02006330 return 1;
6331 }
6332
6333 return 0;
6334}
6335
Emeric Brun87855892012-10-17 17:39:35 +02006336/* Extract an entry from a X509_NAME and copy its value to an output chunk.
6337 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
6338 */
6339static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006340ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
6341 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006342{
6343 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006344 ASN1_OBJECT *obj;
6345 ASN1_STRING *data;
6346 const unsigned char *data_ptr;
6347 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006348 int i, j, n;
6349 int cur = 0;
6350 const char *s;
6351 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006352 int name_count;
6353
6354 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006355
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006356 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006357 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02006358 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006359 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02006360 else
6361 j = i;
6362
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006363 ne = X509_NAME_get_entry(a, j);
6364 obj = X509_NAME_ENTRY_get_object(ne);
6365 data = X509_NAME_ENTRY_get_data(ne);
6366 data_ptr = ASN1_STRING_get0_data(data);
6367 data_len = ASN1_STRING_length(data);
6368 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006369 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006370 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006371 s = tmp;
6372 }
6373
6374 if (chunk_strcasecmp(entry, s) != 0)
6375 continue;
6376
6377 if (pos < 0)
6378 cur--;
6379 else
6380 cur++;
6381
6382 if (cur != pos)
6383 continue;
6384
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006385 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02006386 return -1;
6387
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006388 memcpy(out->area, data_ptr, data_len);
6389 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006390 return 1;
6391 }
6392
6393 return 0;
6394
6395}
6396
6397/* Extract and format full DN from a X509_NAME and copy result into a chunk
6398 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
6399 */
6400static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006401ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006402{
6403 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006404 ASN1_OBJECT *obj;
6405 ASN1_STRING *data;
6406 const unsigned char *data_ptr;
6407 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006408 int i, n, ln;
6409 int l = 0;
6410 const char *s;
6411 char *p;
6412 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006413 int name_count;
6414
6415
6416 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006417
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006418 out->data = 0;
6419 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006420 for (i = 0; i < name_count; i++) {
6421 ne = X509_NAME_get_entry(a, i);
6422 obj = X509_NAME_ENTRY_get_object(ne);
6423 data = X509_NAME_ENTRY_get_data(ne);
6424 data_ptr = ASN1_STRING_get0_data(data);
6425 data_len = ASN1_STRING_length(data);
6426 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006427 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006428 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006429 s = tmp;
6430 }
6431 ln = strlen(s);
6432
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006433 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006434 if (l > out->size)
6435 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006436 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02006437
6438 *(p++)='/';
6439 memcpy(p, s, ln);
6440 p += ln;
6441 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006442 memcpy(p, data_ptr, data_len);
6443 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006444 }
6445
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006446 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02006447 return 0;
6448
6449 return 1;
6450}
6451
Olivier Houchardab28a322018-12-21 19:45:40 +01006452void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
6453{
6454#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02006455 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006456
Olivier Houcharde488ea82019-06-28 14:10:33 +02006457 if (!ssl_sock_is_ssl(conn))
6458 return;
Christopher Faulet82004142019-09-10 10:12:03 +02006459 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006460 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01006461#endif
6462}
6463
Willy Tarreau119a4082016-12-22 21:58:38 +01006464/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
6465 * to disable SNI.
6466 */
Willy Tarreau63076412015-07-10 11:33:32 +02006467void ssl_sock_set_servername(struct connection *conn, const char *hostname)
6468{
6469#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02006470 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006471
Willy Tarreau119a4082016-12-22 21:58:38 +01006472 char *prev_name;
6473
Willy Tarreau63076412015-07-10 11:33:32 +02006474 if (!ssl_sock_is_ssl(conn))
6475 return;
Christopher Faulet82004142019-09-10 10:12:03 +02006476 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02006477
Willy Tarreau119a4082016-12-22 21:58:38 +01006478 /* if the SNI changes, we must destroy the reusable context so that a
6479 * new connection will present a new SNI. As an optimization we could
6480 * later imagine having a small cache of ssl_ctx to hold a few SNI per
6481 * server.
6482 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006483 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01006484 if ((!prev_name && hostname) ||
6485 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006486 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01006487
Olivier Houchard66ab4982019-02-26 18:37:15 +01006488 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02006489#endif
6490}
6491
Emeric Brun0abf8362014-06-24 18:26:41 +02006492/* Extract peer certificate's common name into the chunk dest
6493 * Returns
6494 * the len of the extracted common name
6495 * or 0 if no CN found in DN
6496 * or -1 on error case (i.e. no peer certificate)
6497 */
Willy Tarreau83061a82018-07-13 11:56:34 +02006498int ssl_sock_get_remote_common_name(struct connection *conn,
6499 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04006500{
Christopher Faulet82004142019-09-10 10:12:03 +02006501 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04006502 X509 *crt = NULL;
6503 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04006504 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02006505 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006506 .area = (char *)&find_cn,
6507 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04006508 };
Emeric Brun0abf8362014-06-24 18:26:41 +02006509 int result = -1;
David Safb76832014-05-08 23:42:08 -04006510
6511 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02006512 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02006513 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04006514
6515 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006516 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006517 if (!crt)
6518 goto out;
6519
6520 name = X509_get_subject_name(crt);
6521 if (!name)
6522 goto out;
David Safb76832014-05-08 23:42:08 -04006523
Emeric Brun0abf8362014-06-24 18:26:41 +02006524 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
6525out:
David Safb76832014-05-08 23:42:08 -04006526 if (crt)
6527 X509_free(crt);
6528
6529 return result;
6530}
6531
Dave McCowan328fb582014-07-30 10:39:13 -04006532/* returns 1 if client passed a certificate for this session, 0 if not */
6533int ssl_sock_get_cert_used_sess(struct connection *conn)
6534{
Christopher Faulet82004142019-09-10 10:12:03 +02006535 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006536 X509 *crt = NULL;
6537
6538 if (!ssl_sock_is_ssl(conn))
6539 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02006540 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006541
6542 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006543 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04006544 if (!crt)
6545 return 0;
6546
6547 X509_free(crt);
6548 return 1;
6549}
6550
6551/* returns 1 if client passed a certificate for this connection, 0 if not */
6552int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04006553{
Christopher Faulet82004142019-09-10 10:12:03 +02006554 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006555
David Safb76832014-05-08 23:42:08 -04006556 if (!ssl_sock_is_ssl(conn))
6557 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02006558 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006559 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04006560}
6561
6562/* returns result from SSL verify */
6563unsigned int ssl_sock_get_verify_result(struct connection *conn)
6564{
Christopher Faulet82004142019-09-10 10:12:03 +02006565 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006566
David Safb76832014-05-08 23:42:08 -04006567 if (!ssl_sock_is_ssl(conn))
6568 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02006569 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006570 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006571}
6572
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006573/* Returns the application layer protocol name in <str> and <len> when known.
6574 * Zero is returned if the protocol name was not found, otherwise non-zero is
6575 * returned. The string is allocated in the SSL context and doesn't have to be
6576 * freed by the caller. NPN is also checked if available since older versions
6577 * of openssl (1.0.1) which are more common in field only support this one.
6578 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006579static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006580{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006581#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
6582 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006583 struct ssl_sock_ctx *ctx = xprt_ctx;
6584 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006585 return 0;
6586
6587 *str = NULL;
6588
6589#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01006590 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006591 if (*str)
6592 return 1;
6593#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01006594#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006595 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006596 if (*str)
6597 return 1;
6598#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006599#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006600 return 0;
6601}
6602
Willy Tarreau7875d092012-09-10 08:20:03 +02006603/***** Below are some sample fetching functions for ACL/patterns *****/
6604
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006605static int
6606smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
6607{
6608 struct connection *conn;
6609
6610 conn = objt_conn(smp->sess->origin);
6611 if (!conn || conn->xprt != &ssl_sock)
6612 return 0;
6613
6614 smp->flags = 0;
6615 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02006616#ifdef OPENSSL_IS_BORINGSSL
6617 {
6618 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
6619 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
6620 SSL_early_data_accepted(ctx->ssl));
6621 }
6622#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01006623 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
6624 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02006625#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006626 return 1;
6627}
6628
Emeric Brune64aef12012-09-21 13:15:06 +02006629/* boolean, returns true if client cert was present */
6630static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006631smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02006632{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006633 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006634 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006635
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006636 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006637 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02006638 return 0;
6639
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006640 ctx = conn->xprt_ctx;
6641
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006642 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02006643 smp->flags |= SMP_F_MAY_CHANGE;
6644 return 0;
6645 }
6646
6647 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006648 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006649 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02006650
6651 return 1;
6652}
6653
Emeric Brun43e79582014-10-29 19:03:26 +01006654/* binary, returns a certificate in a binary chunk (der/raw).
6655 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6656 * should be use.
6657 */
6658static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006659smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01006660{
6661 int cert_peer = (kw[4] == 'c') ? 1 : 0;
6662 X509 *crt = NULL;
6663 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006664 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01006665 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006666 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01006667
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006668 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01006669 if (!conn || conn->xprt != &ssl_sock)
6670 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006671 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01006672
6673 if (!(conn->flags & CO_FL_CONNECTED)) {
6674 smp->flags |= SMP_F_MAY_CHANGE;
6675 return 0;
6676 }
6677
6678 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006679 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01006680 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006681 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01006682
6683 if (!crt)
6684 goto out;
6685
6686 smp_trash = get_trash_chunk();
6687 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
6688 goto out;
6689
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006690 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006691 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01006692 ret = 1;
6693out:
6694 /* SSL_get_peer_certificate, it increase X509 * ref count */
6695 if (cert_peer && crt)
6696 X509_free(crt);
6697 return ret;
6698}
6699
Emeric Brunba841a12014-04-30 17:05:08 +02006700/* binary, returns serial of certificate in a binary chunk.
6701 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6702 * should be use.
6703 */
Willy Tarreau8d598402012-10-22 17:58:39 +02006704static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006705smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02006706{
Emeric Brunba841a12014-04-30 17:05:08 +02006707 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02006708 X509 *crt = NULL;
6709 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006710 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006711 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006712 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006713
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006714 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006715 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02006716 return 0;
6717
Olivier Houchard66ab4982019-02-26 18:37:15 +01006718 ctx = conn->xprt_ctx;
6719
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006720 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02006721 smp->flags |= SMP_F_MAY_CHANGE;
6722 return 0;
6723 }
6724
Emeric Brunba841a12014-04-30 17:05:08 +02006725 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006726 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006727 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006728 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006729
Willy Tarreau8d598402012-10-22 17:58:39 +02006730 if (!crt)
6731 goto out;
6732
Willy Tarreau47ca5452012-12-23 20:22:19 +01006733 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02006734 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
6735 goto out;
6736
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006737 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006738 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02006739 ret = 1;
6740out:
Emeric Brunba841a12014-04-30 17:05:08 +02006741 /* SSL_get_peer_certificate, it increase X509 * ref count */
6742 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02006743 X509_free(crt);
6744 return ret;
6745}
Emeric Brune64aef12012-09-21 13:15:06 +02006746
Emeric Brunba841a12014-04-30 17:05:08 +02006747/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
6748 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6749 * should be use.
6750 */
James Votha051b4a2013-05-14 20:37:59 +02006751static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006752smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02006753{
Emeric Brunba841a12014-04-30 17:05:08 +02006754 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02006755 X509 *crt = NULL;
6756 const EVP_MD *digest;
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;
James Votha051b4a2013-05-14 20:37:59 +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)
6764 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006765 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006766
6767 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02006768 smp->flags |= SMP_F_MAY_CHANGE;
6769 return 0;
6770 }
6771
Emeric Brunba841a12014-04-30 17:05:08 +02006772 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006773 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006774 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006775 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02006776 if (!crt)
6777 goto out;
6778
6779 smp_trash = get_trash_chunk();
6780 digest = EVP_sha1();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006781 X509_digest(crt, digest, (unsigned char *) smp_trash->area,
6782 (unsigned int *)&smp_trash->data);
James Votha051b4a2013-05-14 20:37:59 +02006783
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006784 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006785 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02006786 ret = 1;
6787out:
Emeric Brunba841a12014-04-30 17:05:08 +02006788 /* SSL_get_peer_certificate, it increase X509 * ref count */
6789 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02006790 X509_free(crt);
6791 return ret;
6792}
6793
Emeric Brunba841a12014-04-30 17:05:08 +02006794/* string, returns certificate's notafter date in ASN1_UTCTIME format.
6795 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6796 * should be use.
6797 */
Emeric Brunce5ad802012-10-22 14:11:22 +02006798static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006799smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02006800{
Emeric Brunba841a12014-04-30 17:05:08 +02006801 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02006802 X509 *crt = NULL;
6803 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006804 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006805 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006806 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02006807
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006808 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006809 if (!conn || conn->xprt != &ssl_sock)
6810 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006811 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006812
6813 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02006814 smp->flags |= SMP_F_MAY_CHANGE;
6815 return 0;
6816 }
6817
Emeric Brunba841a12014-04-30 17:05:08 +02006818 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006819 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006820 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006821 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02006822 if (!crt)
6823 goto out;
6824
Willy Tarreau47ca5452012-12-23 20:22:19 +01006825 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08006826 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02006827 goto out;
6828
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006829 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006830 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02006831 ret = 1;
6832out:
Emeric Brunba841a12014-04-30 17:05:08 +02006833 /* SSL_get_peer_certificate, it increase X509 * ref count */
6834 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02006835 X509_free(crt);
6836 return ret;
6837}
6838
Emeric Brunba841a12014-04-30 17:05:08 +02006839/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
6840 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6841 * should be use.
6842 */
Emeric Brun87855892012-10-17 17:39:35 +02006843static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006844smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02006845{
Emeric Brunba841a12014-04-30 17:05:08 +02006846 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02006847 X509 *crt = NULL;
6848 X509_NAME *name;
6849 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006850 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006851 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006852 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02006853
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006854 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006855 if (!conn || conn->xprt != &ssl_sock)
6856 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006857 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006858
6859 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02006860 smp->flags |= SMP_F_MAY_CHANGE;
6861 return 0;
6862 }
6863
Emeric Brunba841a12014-04-30 17:05:08 +02006864 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006865 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006866 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006867 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02006868 if (!crt)
6869 goto out;
6870
6871 name = X509_get_issuer_name(crt);
6872 if (!name)
6873 goto out;
6874
Willy Tarreau47ca5452012-12-23 20:22:19 +01006875 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02006876 if (args && args[0].type == ARGT_STR) {
6877 int pos = 1;
6878
6879 if (args[1].type == ARGT_SINT)
6880 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02006881
6882 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
6883 goto out;
6884 }
6885 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
6886 goto out;
6887
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006888 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006889 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02006890 ret = 1;
6891out:
Emeric Brunba841a12014-04-30 17:05:08 +02006892 /* SSL_get_peer_certificate, it increase X509 * ref count */
6893 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02006894 X509_free(crt);
6895 return ret;
6896}
6897
Emeric Brunba841a12014-04-30 17:05:08 +02006898/* string, returns notbefore date in ASN1_UTCTIME format.
6899 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6900 * should be use.
6901 */
Emeric Brunce5ad802012-10-22 14:11:22 +02006902static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006903smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02006904{
Emeric Brunba841a12014-04-30 17:05:08 +02006905 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02006906 X509 *crt = NULL;
6907 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006908 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006909 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006910 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006911
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006912 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006913 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02006914 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006915 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02006916
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006917 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02006918 smp->flags |= SMP_F_MAY_CHANGE;
6919 return 0;
6920 }
6921
Emeric Brunba841a12014-04-30 17:05:08 +02006922 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006923 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006924 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006925 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02006926 if (!crt)
6927 goto out;
6928
Willy Tarreau47ca5452012-12-23 20:22:19 +01006929 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08006930 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02006931 goto out;
6932
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006933 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006934 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02006935 ret = 1;
6936out:
Emeric Brunba841a12014-04-30 17:05:08 +02006937 /* SSL_get_peer_certificate, it increase X509 * ref count */
6938 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02006939 X509_free(crt);
6940 return ret;
6941}
6942
Emeric Brunba841a12014-04-30 17:05:08 +02006943/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
6944 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6945 * should be use.
6946 */
Emeric Brun87855892012-10-17 17:39:35 +02006947static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006948smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02006949{
Emeric Brunba841a12014-04-30 17:05:08 +02006950 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02006951 X509 *crt = NULL;
6952 X509_NAME *name;
6953 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006954 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006955 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006956 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02006957
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006958 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006959 if (!conn || conn->xprt != &ssl_sock)
6960 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006961 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006962
6963 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02006964 smp->flags |= SMP_F_MAY_CHANGE;
6965 return 0;
6966 }
6967
Emeric Brunba841a12014-04-30 17:05:08 +02006968 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006969 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006970 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006971 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02006972 if (!crt)
6973 goto out;
6974
6975 name = X509_get_subject_name(crt);
6976 if (!name)
6977 goto out;
6978
Willy Tarreau47ca5452012-12-23 20:22:19 +01006979 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02006980 if (args && args[0].type == ARGT_STR) {
6981 int pos = 1;
6982
6983 if (args[1].type == ARGT_SINT)
6984 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02006985
6986 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
6987 goto out;
6988 }
6989 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
6990 goto out;
6991
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006992 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006993 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02006994 ret = 1;
6995out:
Emeric Brunba841a12014-04-30 17:05:08 +02006996 /* SSL_get_peer_certificate, it increase X509 * ref count */
6997 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02006998 X509_free(crt);
6999 return ret;
7000}
Emeric Brun9143d372012-12-20 15:44:16 +01007001
7002/* integer, returns true if current session use a client certificate */
7003static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007004smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007005{
7006 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007007 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007008 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007009
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007010 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007011 if (!conn || conn->xprt != &ssl_sock)
7012 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007013 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007014
7015 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01007016 smp->flags |= SMP_F_MAY_CHANGE;
7017 return 0;
7018 }
7019
7020 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007021 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007022 if (crt) {
7023 X509_free(crt);
7024 }
7025
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007026 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007027 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007028 return 1;
7029}
7030
Emeric Brunba841a12014-04-30 17:05:08 +02007031/* integer, returns the certificate version
7032 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7033 * should be use.
7034 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007035static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007036smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007037{
Emeric Brunba841a12014-04-30 17:05:08 +02007038 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007039 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007040 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007041 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007042
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007043 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007044 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007045 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007046 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007047
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007048 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007049 smp->flags |= SMP_F_MAY_CHANGE;
7050 return 0;
7051 }
7052
Emeric Brunba841a12014-04-30 17:05:08 +02007053 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007054 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007055 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007056 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007057 if (!crt)
7058 return 0;
7059
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007060 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007061 /* SSL_get_peer_certificate increase X509 * ref count */
7062 if (cert_peer)
7063 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007064 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007065
7066 return 1;
7067}
7068
Emeric Brunba841a12014-04-30 17:05:08 +02007069/* string, returns the certificate's signature algorithm.
7070 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7071 * should be use.
7072 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007073static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007074smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007075{
Emeric Brunba841a12014-04-30 17:05:08 +02007076 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007077 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007078 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007079 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007080 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007081 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007082
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007083 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007084 if (!conn || conn->xprt != &ssl_sock)
7085 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007086 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007087
7088 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007089 smp->flags |= SMP_F_MAY_CHANGE;
7090 return 0;
7091 }
7092
Emeric Brunba841a12014-04-30 17:05:08 +02007093 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007094 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007095 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007096 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007097 if (!crt)
7098 return 0;
7099
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007100 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7101 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007102
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007103 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7104 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007105 /* SSL_get_peer_certificate increase X509 * ref count */
7106 if (cert_peer)
7107 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007108 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007109 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007110
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007111 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007112 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007113 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007114 /* SSL_get_peer_certificate increase X509 * ref count */
7115 if (cert_peer)
7116 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007117
7118 return 1;
7119}
7120
Emeric Brunba841a12014-04-30 17:05:08 +02007121/* string, returns the certificate's key algorithm.
7122 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7123 * should be use.
7124 */
Emeric Brun521a0112012-10-22 12:22:55 +02007125static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007126smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02007127{
Emeric Brunba841a12014-04-30 17:05:08 +02007128 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02007129 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007130 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02007131 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007132 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007133 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02007134
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007135 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007136 if (!conn || conn->xprt != &ssl_sock)
7137 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007138 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007139
7140 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02007141 smp->flags |= SMP_F_MAY_CHANGE;
7142 return 0;
7143 }
7144
Emeric Brunba841a12014-04-30 17:05:08 +02007145 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007146 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007147 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007148 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02007149 if (!crt)
7150 return 0;
7151
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007152 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
7153 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02007154
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007155 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7156 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007157 /* SSL_get_peer_certificate increase X509 * ref count */
7158 if (cert_peer)
7159 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007160 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007161 }
Emeric Brun521a0112012-10-22 12:22:55 +02007162
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007163 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007164 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007165 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007166 if (cert_peer)
7167 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007168
7169 return 1;
7170}
7171
Emeric Brun645ae792014-04-30 14:21:06 +02007172/* boolean, returns true if front conn. transport layer is SSL.
7173 * This function is also usable on backend conn if the fetch keyword 5th
7174 * char is 'b'.
7175 */
Willy Tarreau7875d092012-09-10 08:20:03 +02007176static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007177smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007178{
Emeric Bruneb8def92018-02-19 15:59:48 +01007179 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7180 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007181
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007182 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007183 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007184 return 1;
7185}
7186
Emeric Brun2525b6b2012-10-18 15:59:43 +02007187/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007188static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007189smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007190{
7191#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007192 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007193 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007194
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007195 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007196 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007197 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007198 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007199 return 1;
7200#else
7201 return 0;
7202#endif
7203}
7204
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007205/* boolean, returns true if client session has been resumed.
7206 * This function is also usable on backend conn if the fetch keyword 5th
7207 * char is 'b'.
7208 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007209static int
7210smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
7211{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007212 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7213 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007214 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007215
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007216
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007217 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007218 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007219 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007220 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007221 return 1;
7222}
7223
Emeric Brun645ae792014-04-30 14:21:06 +02007224/* string, returns the used cipher if front conn. transport layer is SSL.
7225 * This function is also usable on backend conn if the fetch keyword 5th
7226 * char is 'b'.
7227 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007228static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007229smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007230{
Emeric Bruneb8def92018-02-19 15:59:48 +01007231 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7232 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007233 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007234
Willy Tarreaube508f12016-03-10 11:47:01 +01007235 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007236 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007237 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007238 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007239
Olivier Houchard66ab4982019-02-26 18:37:15 +01007240 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007241 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007242 return 0;
7243
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007244 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007245 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007246 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007247
7248 return 1;
7249}
7250
Emeric Brun645ae792014-04-30 14:21:06 +02007251/* integer, returns the algoritm's keysize if front conn. transport layer
7252 * is SSL.
7253 * This function is also usable on backend conn if the fetch keyword 5th
7254 * char is 'b'.
7255 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007256static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007257smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007258{
Emeric Bruneb8def92018-02-19 15:59:48 +01007259 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7260 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007261 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007262 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01007263
Emeric Brun589fcad2012-10-16 14:13:26 +02007264 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007265 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007266 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007267 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007268
Olivier Houchard66ab4982019-02-26 18:37:15 +01007269 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007270 return 0;
7271
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007272 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007273 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007274
7275 return 1;
7276}
7277
Emeric Brun645ae792014-04-30 14:21:06 +02007278/* integer, returns the used keysize if front conn. transport layer is SSL.
7279 * This function is also usable on backend conn if the fetch keyword 5th
7280 * char is 'b'.
7281 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007282static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007283smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007284{
Emeric Bruneb8def92018-02-19 15:59:48 +01007285 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7286 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007287 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007288
Emeric Brun589fcad2012-10-16 14:13:26 +02007289 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007290 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7291 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007292 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007293
Olivier Houchard66ab4982019-02-26 18:37:15 +01007294 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007295 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02007296 return 0;
7297
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007298 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007299
7300 return 1;
7301}
7302
Bernard Spil13c53f82018-02-15 13:34:58 +01007303#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02007304static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007305smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007306{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007307 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007308 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007309
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007310 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007311 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007312
Olivier Houchard6b77f492018-11-22 18:18:29 +01007313 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
7314 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007315 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7316 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007317 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007318
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007319 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007320 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007321 (const unsigned char **)&smp->data.u.str.area,
7322 (unsigned *)&smp->data.u.str.data);
Willy Tarreaua33c6542012-10-15 13:19:06 +02007323
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007324 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007325 return 0;
7326
7327 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007328}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007329#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02007330
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01007331#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02007332static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007333smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02007334{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007335 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007336 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007337
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007338 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007339 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02007340
Olivier Houchard6b77f492018-11-22 18:18:29 +01007341 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
7342 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7343
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007344 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02007345 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007346 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02007347
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007348 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007349 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007350 (const unsigned char **)&smp->data.u.str.area,
7351 (unsigned *)&smp->data.u.str.data);
Willy Tarreauab861d32013-04-02 02:30:41 +02007352
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007353 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02007354 return 0;
7355
7356 return 1;
7357}
7358#endif
7359
Emeric Brun645ae792014-04-30 14:21:06 +02007360/* string, returns the used protocol if front conn. transport layer is SSL.
7361 * This function is also usable on backend conn if the fetch keyword 5th
7362 * char is 'b'.
7363 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02007364static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007365smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007366{
Emeric Bruneb8def92018-02-19 15:59:48 +01007367 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7368 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007369 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007370
Emeric Brun589fcad2012-10-16 14:13:26 +02007371 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007372 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7373 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007374 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007375
Olivier Houchard66ab4982019-02-26 18:37:15 +01007376 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007377 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007378 return 0;
7379
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007380 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007381 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007382 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007383
7384 return 1;
7385}
7386
Willy Tarreau87b09662015-04-03 00:22:06 +02007387/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02007388 * This function is also usable on backend conn if the fetch keyword 5th
7389 * char is 'b'.
7390 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007391#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02007392static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007393smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02007394{
Emeric Bruneb8def92018-02-19 15:59:48 +01007395 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7396 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007397 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007398 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01007399
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007400 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007401 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02007402
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007403 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7404 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007405 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007406
Olivier Houchard66ab4982019-02-26 18:37:15 +01007407 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02007408 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02007409 return 0;
7410
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007411 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess,
7412 (unsigned int *)&smp->data.u.str.data);
7413 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02007414 return 0;
7415
7416 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02007417}
Patrick Hemmer41966772018-04-28 19:15:48 -04007418#endif
7419
Emeric Brunfe68f682012-10-16 14:59:28 +02007420
Emmanuel Hocdet839af572019-05-14 16:27:35 +02007421#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04007422static int
Patrick Hemmer65674662019-06-04 08:13:03 -04007423smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
7424{
7425 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7426 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7427 struct buffer *data;
7428 struct ssl_sock_ctx *ctx;
7429
7430 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7431 return 0;
7432 ctx = conn->xprt_ctx;
7433
7434 data = get_trash_chunk();
7435 if (kw[7] == 'c')
7436 data->data = SSL_get_client_random(ctx->ssl,
7437 (unsigned char *) data->area,
7438 data->size);
7439 else
7440 data->data = SSL_get_server_random(ctx->ssl,
7441 (unsigned char *) data->area,
7442 data->size);
7443 if (!data->data)
7444 return 0;
7445
7446 smp->flags = 0;
7447 smp->data.type = SMP_T_BIN;
7448 smp->data.u.str = *data;
7449
7450 return 1;
7451}
7452
7453static int
Patrick Hemmere0275472018-04-28 19:15:51 -04007454smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
7455{
7456 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7457 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7458 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02007459 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007460 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04007461
7462 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7463 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007464 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04007465
Olivier Houchard66ab4982019-02-26 18:37:15 +01007466 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04007467 if (!ssl_sess)
7468 return 0;
7469
7470 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007471 data->data = SSL_SESSION_get_master_key(ssl_sess,
7472 (unsigned char *) data->area,
7473 data->size);
7474 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04007475 return 0;
7476
7477 smp->flags = 0;
7478 smp->data.type = SMP_T_BIN;
7479 smp->data.u.str = *data;
7480
7481 return 1;
7482}
7483#endif
7484
Patrick Hemmer41966772018-04-28 19:15:48 -04007485#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02007486static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007487smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007488{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007489 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007490 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007491
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007492 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007493 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02007494
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007495 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007496 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7497 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007498 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007499
Olivier Houchard66ab4982019-02-26 18:37:15 +01007500 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007501 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02007502 return 0;
7503
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007504 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02007505 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02007506}
Patrick Hemmer41966772018-04-28 19:15:48 -04007507#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02007508
David Sc1ad52e2014-04-08 18:48:47 -04007509static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007510smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
7511{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007512 struct connection *conn;
7513 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007514 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007515
7516 conn = objt_conn(smp->sess->origin);
7517 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7518 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007519 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007520
Olivier Houchard66ab4982019-02-26 18:37:15 +01007521 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007522 if (!capture)
7523 return 0;
7524
7525 smp->flags = SMP_F_CONST;
7526 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007527 smp->data.u.str.area = capture->ciphersuite;
7528 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007529 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007530}
7531
7532static int
7533smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
7534{
Willy Tarreau83061a82018-07-13 11:56:34 +02007535 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007536
7537 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
7538 return 0;
7539
7540 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007541 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007542 smp->data.type = SMP_T_BIN;
7543 smp->data.u.str = *data;
7544 return 1;
7545}
7546
7547static int
7548smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
7549{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007550 struct connection *conn;
7551 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007552 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007553
7554 conn = objt_conn(smp->sess->origin);
7555 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7556 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007557 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007558
Olivier Houchard66ab4982019-02-26 18:37:15 +01007559 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007560 if (!capture)
7561 return 0;
7562
7563 smp->data.type = SMP_T_SINT;
7564 smp->data.u.sint = capture->xxh64;
7565 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007566}
7567
7568static int
7569smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
7570{
Willy Tarreau5db847a2019-05-09 14:13:35 +02007571#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02007572 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007573 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007574
7575 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
7576 return 0;
7577
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007578 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007579 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007580 const char *str;
7581 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007582 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007583 uint16_t id = (bin[0] << 8) | bin[1];
7584#if defined(OPENSSL_IS_BORINGSSL)
7585 cipher = SSL_get_cipher_by_value(id);
7586#else
Willy Tarreaub7290772018-10-15 11:01:59 +02007587 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007588 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7589 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007590#endif
7591 str = SSL_CIPHER_get_name(cipher);
7592 if (!str || strcmp(str, "(NONE)") == 0)
7593 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007594 else
7595 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
7596 }
7597 smp->data.type = SMP_T_STR;
7598 smp->data.u.str = *data;
7599 return 1;
7600#else
7601 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
7602#endif
7603}
7604
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007605#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007606static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007607smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04007608{
Emeric Bruneb8def92018-02-19 15:59:48 +01007609 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7610 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04007611 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02007612 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007613 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04007614
7615 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04007616 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
7617 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007618 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04007619
7620 if (!(conn->flags & CO_FL_CONNECTED)) {
7621 smp->flags |= SMP_F_MAY_CHANGE;
7622 return 0;
7623 }
7624
7625 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01007626 if (!SSL_session_reused(ctx->ssl))
7627 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007628 finished_trash->area,
7629 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04007630 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007631 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007632 finished_trash->area,
7633 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04007634
7635 if (!finished_len)
7636 return 0;
7637
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007638 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007639 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007640 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04007641
7642 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04007643}
Patrick Hemmer41966772018-04-28 19:15:48 -04007644#endif
David Sc1ad52e2014-04-08 18:48:47 -04007645
Emeric Brun2525b6b2012-10-18 15:59:43 +02007646/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02007647static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007648smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02007649{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007650 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007651 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007652
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007653 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007654 if (!conn || conn->xprt != &ssl_sock)
7655 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007656 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007657
7658 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007659 smp->flags = SMP_F_MAY_CHANGE;
7660 return 0;
7661 }
7662
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007663 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007664 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007665 smp->flags = 0;
7666
7667 return 1;
7668}
7669
Emeric Brun2525b6b2012-10-18 15:59:43 +02007670/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02007671static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007672smp_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 +02007673{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007674 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007675 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007676
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007677 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007678 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02007679 return 0;
7680
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007681 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007682 smp->flags = SMP_F_MAY_CHANGE;
7683 return 0;
7684 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007685 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02007686
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007687 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007688 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007689 smp->flags = 0;
7690
7691 return 1;
7692}
7693
Emeric Brun2525b6b2012-10-18 15:59:43 +02007694/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02007695static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007696smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02007697{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007698 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007699 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007700
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007701 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007702 if (!conn || conn->xprt != &ssl_sock)
7703 return 0;
7704
7705 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02007706 smp->flags = SMP_F_MAY_CHANGE;
7707 return 0;
7708 }
7709
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007710 ctx = conn->xprt_ctx;
7711
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007712 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007713 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007714 smp->flags = 0;
7715
7716 return 1;
7717}
7718
Emeric Brun2525b6b2012-10-18 15:59:43 +02007719/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007720static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007721smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007722{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007723 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007724 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007725
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007726 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007727 if (!conn || conn->xprt != &ssl_sock)
7728 return 0;
7729
7730 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007731 smp->flags = SMP_F_MAY_CHANGE;
7732 return 0;
7733 }
7734
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007735 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007736 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007737 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007738
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007739 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007740 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007741 smp->flags = 0;
7742
7743 return 1;
7744}
7745
Emeric Brunfb510ea2012-10-05 12:00:26 +02007746/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007747static 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 +02007748{
7749 if (!*args[cur_arg + 1]) {
7750 if (err)
7751 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
7752 return ERR_ALERT | ERR_FATAL;
7753 }
7754
Willy Tarreauef934602016-12-22 23:12:01 +01007755 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7756 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007757 else
7758 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007759
Emeric Brund94b3fe2012-09-20 18:23:56 +02007760 return 0;
7761}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007762static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7763{
7764 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
7765}
Emeric Brund94b3fe2012-09-20 18:23:56 +02007766
Christopher Faulet31af49d2015-06-09 17:29:50 +02007767/* parse the "ca-sign-file" bind keyword */
7768static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7769{
7770 if (!*args[cur_arg + 1]) {
7771 if (err)
7772 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
7773 return ERR_ALERT | ERR_FATAL;
7774 }
7775
Willy Tarreauef934602016-12-22 23:12:01 +01007776 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7777 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02007778 else
7779 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
7780
7781 return 0;
7782}
7783
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007784/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02007785static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7786{
7787 if (!*args[cur_arg + 1]) {
7788 if (err)
7789 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
7790 return ERR_ALERT | ERR_FATAL;
7791 }
7792 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
7793 return 0;
7794}
7795
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007796/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007797static 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 +02007798{
7799 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007800 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007801 return ERR_ALERT | ERR_FATAL;
7802 }
7803
Emeric Brun76d88952012-10-05 15:47:31 +02007804 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02007805 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007806 return 0;
7807}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007808static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7809{
7810 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
7811}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007812
Emmanuel Hocdet839af572019-05-14 16:27:35 +02007813#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007814/* parse the "ciphersuites" bind keyword */
7815static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7816{
7817 if (!*args[cur_arg + 1]) {
7818 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
7819 return ERR_ALERT | ERR_FATAL;
7820 }
7821
7822 free(conf->ciphersuites);
7823 conf->ciphersuites = strdup(args[cur_arg + 1]);
7824 return 0;
7825}
7826static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7827{
7828 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
7829}
7830#endif
7831
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007832/* parse the "crt" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02007833static 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 +02007834{
Willy Tarreau38011032013-08-13 16:59:39 +02007835 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02007836
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007837 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007838 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007839 return ERR_ALERT | ERR_FATAL;
7840 }
7841
Willy Tarreauef934602016-12-22 23:12:01 +01007842 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
7843 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02007844 memprintf(err, "'%s' : path too long", args[cur_arg]);
7845 return ERR_ALERT | ERR_FATAL;
7846 }
Willy Tarreauef934602016-12-22 23:12:01 +01007847 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreau03209342016-12-22 17:08:28 +01007848 if (ssl_sock_load_cert(path, conf, err) > 0)
Emeric Brunc8e8d122012-10-02 18:42:10 +02007849 return ERR_ALERT | ERR_FATAL;
7850
7851 return 0;
7852 }
7853
Willy Tarreau03209342016-12-22 17:08:28 +01007854 if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007855 return ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02007856
7857 return 0;
7858}
7859
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007860/* parse the "crt-list" bind keyword */
7861static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7862{
7863 if (!*args[cur_arg + 1]) {
7864 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
7865 return ERR_ALERT | ERR_FATAL;
7866 }
7867
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007868 if (ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err) > 0) {
Willy Tarreauad1731d2013-04-02 17:35:58 +02007869 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007870 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02007871 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007872
7873 return 0;
7874}
7875
Emeric Brunfb510ea2012-10-05 12:00:26 +02007876/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007877static 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 +02007878{
Emeric Brun051cdab2012-10-02 19:25:50 +02007879#ifndef X509_V_FLAG_CRL_CHECK
7880 if (err)
7881 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
7882 return ERR_ALERT | ERR_FATAL;
7883#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02007884 if (!*args[cur_arg + 1]) {
7885 if (err)
7886 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
7887 return ERR_ALERT | ERR_FATAL;
7888 }
Emeric Brun2b58d042012-09-20 17:10:03 +02007889
Willy Tarreauef934602016-12-22 23:12:01 +01007890 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7891 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007892 else
7893 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007894
Emeric Brun2b58d042012-09-20 17:10:03 +02007895 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02007896#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02007897}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007898static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7899{
7900 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
7901}
Emeric Brun2b58d042012-09-20 17:10:03 +02007902
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007903/* parse the "curves" bind keyword keyword */
7904static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7905{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007906#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007907 if (!*args[cur_arg + 1]) {
7908 if (err)
7909 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
7910 return ERR_ALERT | ERR_FATAL;
7911 }
7912 conf->curves = strdup(args[cur_arg + 1]);
7913 return 0;
7914#else
7915 if (err)
7916 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
7917 return ERR_ALERT | ERR_FATAL;
7918#endif
7919}
7920static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7921{
7922 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
7923}
7924
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007925/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007926static 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 +02007927{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007928#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Emeric Brun2b58d042012-09-20 17:10:03 +02007929 if (err)
7930 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
7931 return ERR_ALERT | ERR_FATAL;
7932#elif defined(OPENSSL_NO_ECDH)
7933 if (err)
7934 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
7935 return ERR_ALERT | ERR_FATAL;
7936#else
7937 if (!*args[cur_arg + 1]) {
7938 if (err)
7939 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
7940 return ERR_ALERT | ERR_FATAL;
7941 }
7942
7943 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007944
7945 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02007946#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007947}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007948static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7949{
7950 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
7951}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007952
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007953/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02007954static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7955{
7956 int code;
7957 char *p = args[cur_arg + 1];
7958 unsigned long long *ignerr = &conf->crt_ignerr;
7959
7960 if (!*p) {
7961 if (err)
7962 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
7963 return ERR_ALERT | ERR_FATAL;
7964 }
7965
7966 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
7967 ignerr = &conf->ca_ignerr;
7968
7969 if (strcmp(p, "all") == 0) {
7970 *ignerr = ~0ULL;
7971 return 0;
7972 }
7973
7974 while (p) {
7975 code = atoi(p);
7976 if ((code <= 0) || (code > 63)) {
7977 if (err)
7978 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
7979 args[cur_arg], code, args[cur_arg + 1]);
7980 return ERR_ALERT | ERR_FATAL;
7981 }
7982 *ignerr |= 1ULL << code;
7983 p = strchr(p, ',');
7984 if (p)
7985 p++;
7986 }
7987
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007988 return 0;
7989}
7990
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007991/* parse tls_method_options "no-xxx" and "force-xxx" */
7992static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007993{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007994 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007995 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007996 p = strchr(arg, '-');
7997 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007998 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007999 p++;
8000 if (!strcmp(p, "sslv3"))
8001 v = CONF_SSLV3;
8002 else if (!strcmp(p, "tlsv10"))
8003 v = CONF_TLSV10;
8004 else if (!strcmp(p, "tlsv11"))
8005 v = CONF_TLSV11;
8006 else if (!strcmp(p, "tlsv12"))
8007 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008008 else if (!strcmp(p, "tlsv13"))
8009 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008010 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008011 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008012 if (!strncmp(arg, "no-", 3))
8013 methods->flags |= methodVersions[v].flag;
8014 else if (!strncmp(arg, "force-", 6))
8015 methods->min = methods->max = v;
8016 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008017 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008018 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008019 fail:
8020 if (err)
8021 memprintf(err, "'%s' : option not implemented", arg);
8022 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008023}
8024
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008025static 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 +02008026{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008027 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008028}
8029
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008030static 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 +02008031{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008032 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8033}
8034
8035/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8036static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8037{
8038 uint16_t i, v = 0;
8039 char *argv = args[cur_arg + 1];
8040 if (!*argv) {
8041 if (err)
8042 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
8043 return ERR_ALERT | ERR_FATAL;
8044 }
8045 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8046 if (!strcmp(argv, methodVersions[i].name))
8047 v = i;
8048 if (!v) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008049 if (err)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008050 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008051 return ERR_ALERT | ERR_FATAL;
8052 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008053 if (!strcmp("ssl-min-ver", args[cur_arg]))
8054 methods->min = v;
8055 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8056 methods->max = v;
8057 else {
8058 if (err)
8059 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
8060 return ERR_ALERT | ERR_FATAL;
8061 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008062 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008063}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008064
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008065static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8066{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008067#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008068 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 +02008069#endif
8070 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8071}
8072
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008073static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8074{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008075 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008076}
8077
8078static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8079{
8080 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8081}
8082
Emeric Brun2d0c4822012-10-02 13:45:20 +02008083/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008084static 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 +02008085{
Emeric Brun89675492012-10-05 13:48:26 +02008086 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008087 return 0;
8088}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008089
Olivier Houchardc2aae742017-09-22 18:26:28 +02008090/* parse the "allow-0rtt" bind keyword */
8091static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8092{
8093 conf->early_data = 1;
8094 return 0;
8095}
8096
8097static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8098{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008099 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008100 return 0;
8101}
8102
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008103/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008104static 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 +02008105{
Bernard Spil13c53f82018-02-15 13:34:58 +01008106#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008107 char *p1, *p2;
8108
8109 if (!*args[cur_arg + 1]) {
8110 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8111 return ERR_ALERT | ERR_FATAL;
8112 }
8113
8114 free(conf->npn_str);
8115
Willy Tarreau3724da12016-02-12 17:11:12 +01008116 /* the NPN string is built as a suite of (<len> <name>)*,
8117 * so we reuse each comma to store the next <len> and need
8118 * one more for the end of the string.
8119 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008120 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008121 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008122 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8123
8124 /* replace commas with the name length */
8125 p1 = conf->npn_str;
8126 p2 = p1 + 1;
8127 while (1) {
8128 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8129 if (!p2)
8130 p2 = p1 + 1 + strlen(p1 + 1);
8131
8132 if (p2 - (p1 + 1) > 255) {
8133 *p2 = '\0';
8134 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8135 return ERR_ALERT | ERR_FATAL;
8136 }
8137
8138 *p1 = p2 - (p1 + 1);
8139 p1 = p2;
8140
8141 if (!*p2)
8142 break;
8143
8144 *(p2++) = '\0';
8145 }
8146 return 0;
8147#else
8148 if (err)
8149 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
8150 return ERR_ALERT | ERR_FATAL;
8151#endif
8152}
8153
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008154static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8155{
8156 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8157}
8158
Willy Tarreauab861d32013-04-02 02:30:41 +02008159/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008160static 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 +02008161{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008162#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008163 char *p1, *p2;
8164
8165 if (!*args[cur_arg + 1]) {
8166 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
8167 return ERR_ALERT | ERR_FATAL;
8168 }
8169
8170 free(conf->alpn_str);
8171
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008172 /* the ALPN string is built as a suite of (<len> <name>)*,
8173 * so we reuse each comma to store the next <len> and need
8174 * one more for the end of the string.
8175 */
Willy Tarreauab861d32013-04-02 02:30:41 +02008176 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008177 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02008178 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
8179
8180 /* replace commas with the name length */
8181 p1 = conf->alpn_str;
8182 p2 = p1 + 1;
8183 while (1) {
8184 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
8185 if (!p2)
8186 p2 = p1 + 1 + strlen(p1 + 1);
8187
8188 if (p2 - (p1 + 1) > 255) {
8189 *p2 = '\0';
8190 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8191 return ERR_ALERT | ERR_FATAL;
8192 }
8193
8194 *p1 = p2 - (p1 + 1);
8195 p1 = p2;
8196
8197 if (!*p2)
8198 break;
8199
8200 *(p2++) = '\0';
8201 }
8202 return 0;
8203#else
8204 if (err)
8205 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
8206 return ERR_ALERT | ERR_FATAL;
8207#endif
8208}
8209
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008210static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8211{
8212 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8213}
8214
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008215/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008216static 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 +02008217{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01008218 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02008219 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02008220
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008221 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
8222 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008223#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008224 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
8225 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
8226#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008227 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008228 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
8229 if (!conf->ssl_conf.ssl_methods.min)
8230 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
8231 if (!conf->ssl_conf.ssl_methods.max)
8232 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02008233
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008234 return 0;
8235}
8236
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008237/* parse the "prefer-client-ciphers" bind keyword */
8238static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8239{
8240 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
8241 return 0;
8242}
8243
Christopher Faulet31af49d2015-06-09 17:29:50 +02008244/* parse the "generate-certificates" bind keyword */
8245static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8246{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01008247#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02008248 conf->generate_certs = 1;
8249#else
8250 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
8251 err && *err ? *err : "");
8252#endif
8253 return 0;
8254}
8255
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008256/* parse the "strict-sni" bind keyword */
8257static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8258{
8259 conf->strict_sni = 1;
8260 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008261}
8262
8263/* parse the "tls-ticket-keys" bind keyword */
8264static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8265{
8266#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
8267 FILE *f;
8268 int i = 0;
8269 char thisline[LINESIZE];
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008270 struct tls_keys_ref *keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008271
8272 if (!*args[cur_arg + 1]) {
8273 if (err)
8274 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
8275 return ERR_ALERT | ERR_FATAL;
8276 }
8277
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008278 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008279 if (keys_ref) {
8280 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008281 conf->keys_ref = keys_ref;
8282 return 0;
8283 }
8284
Vincent Bernat02779b62016-04-03 13:48:43 +02008285 keys_ref = malloc(sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01008286 if (!keys_ref) {
8287 if (err)
8288 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
8289 return ERR_ALERT | ERR_FATAL;
8290 }
8291
Emeric Brun9e754772019-01-10 17:51:55 +01008292 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01008293 if (!keys_ref->tlskeys) {
8294 free(keys_ref);
8295 if (err)
8296 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
8297 return ERR_ALERT | ERR_FATAL;
8298 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008299
8300 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Emeric Brun09852f72019-01-10 10:51:13 +01008301 free(keys_ref->tlskeys);
8302 free(keys_ref);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008303 if (err)
8304 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
8305 return ERR_ALERT | ERR_FATAL;
8306 }
8307
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008308 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01008309 if (!keys_ref->filename) {
8310 free(keys_ref->tlskeys);
8311 free(keys_ref);
8312 if (err)
8313 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
8314 return ERR_ALERT | ERR_FATAL;
8315 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008316
Emeric Brun9e754772019-01-10 17:51:55 +01008317 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008318 while (fgets(thisline, sizeof(thisline), f) != NULL) {
8319 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01008320 int dec_size;
8321
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008322 /* Strip newline characters from the end */
8323 if(thisline[len - 1] == '\n')
8324 thisline[--len] = 0;
8325
8326 if(thisline[len - 1] == '\r')
8327 thisline[--len] = 0;
8328
Emeric Brun9e754772019-01-10 17:51:55 +01008329 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
8330 if (dec_size < 0) {
Emeric Brun09852f72019-01-10 10:51:13 +01008331 free(keys_ref->filename);
8332 free(keys_ref->tlskeys);
8333 free(keys_ref);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008334 if (err)
8335 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
mildis16aa0152016-06-22 17:46:29 +02008336 fclose(f);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008337 return ERR_ALERT | ERR_FATAL;
8338 }
Emeric Brun9e754772019-01-10 17:51:55 +01008339 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
8340 keys_ref->key_size_bits = 128;
8341 }
8342 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
8343 keys_ref->key_size_bits = 256;
8344 }
8345 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
8346 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
8347 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
8348 free(keys_ref->filename);
8349 free(keys_ref->tlskeys);
8350 free(keys_ref);
8351 if (err)
8352 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
8353 fclose(f);
8354 return ERR_ALERT | ERR_FATAL;
8355 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008356 i++;
8357 }
8358
8359 if (i < TLS_TICKETS_NO) {
Emeric Brun09852f72019-01-10 10:51:13 +01008360 free(keys_ref->filename);
8361 free(keys_ref->tlskeys);
8362 free(keys_ref);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008363 if (err)
8364 memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO);
mildis16aa0152016-06-22 17:46:29 +02008365 fclose(f);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008366 return ERR_ALERT | ERR_FATAL;
8367 }
8368
8369 fclose(f);
8370
8371 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01008372 i -= 2;
8373 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008374 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008375 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01008376 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008377 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008378
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008379 LIST_ADD(&tlskeys_reference, &keys_ref->list);
8380
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008381 return 0;
8382#else
8383 if (err)
8384 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
8385 return ERR_ALERT | ERR_FATAL;
8386#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008387}
8388
Emeric Brund94b3fe2012-09-20 18:23:56 +02008389/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008390static 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 +02008391{
8392 if (!*args[cur_arg + 1]) {
8393 if (err)
8394 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
8395 return ERR_ALERT | ERR_FATAL;
8396 }
8397
8398 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008399 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008400 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008401 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008402 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008403 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008404 else {
8405 if (err)
8406 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
8407 args[cur_arg], args[cur_arg + 1]);
8408 return ERR_ALERT | ERR_FATAL;
8409 }
8410
8411 return 0;
8412}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008413static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8414{
8415 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
8416}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008417
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02008418/* parse the "no-ca-names" bind keyword */
8419static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8420{
8421 conf->no_ca_names = 1;
8422 return 0;
8423}
8424static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8425{
8426 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
8427}
8428
Willy Tarreau92faadf2012-10-10 23:04:25 +02008429/************** "server" keywords ****************/
8430
Olivier Houchardc7566002018-11-20 23:33:50 +01008431/* parse the "npn" bind keyword */
8432static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8433{
8434#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
8435 char *p1, *p2;
8436
8437 if (!*args[*cur_arg + 1]) {
8438 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
8439 return ERR_ALERT | ERR_FATAL;
8440 }
8441
8442 free(newsrv->ssl_ctx.npn_str);
8443
8444 /* the NPN string is built as a suite of (<len> <name>)*,
8445 * so we reuse each comma to store the next <len> and need
8446 * one more for the end of the string.
8447 */
8448 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
8449 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
8450 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
8451 newsrv->ssl_ctx.npn_len);
8452
8453 /* replace commas with the name length */
8454 p1 = newsrv->ssl_ctx.npn_str;
8455 p2 = p1 + 1;
8456 while (1) {
8457 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
8458 newsrv->ssl_ctx.npn_len - (p1 + 1));
8459 if (!p2)
8460 p2 = p1 + 1 + strlen(p1 + 1);
8461
8462 if (p2 - (p1 + 1) > 255) {
8463 *p2 = '\0';
8464 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
8465 return ERR_ALERT | ERR_FATAL;
8466 }
8467
8468 *p1 = p2 - (p1 + 1);
8469 p1 = p2;
8470
8471 if (!*p2)
8472 break;
8473
8474 *(p2++) = '\0';
8475 }
8476 return 0;
8477#else
8478 if (err)
8479 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
8480 return ERR_ALERT | ERR_FATAL;
8481#endif
8482}
8483
Olivier Houchard92150142018-12-21 19:47:01 +01008484/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01008485static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8486{
8487#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
8488 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01008489 char **alpn_str;
8490 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01008491
Olivier Houchard92150142018-12-21 19:47:01 +01008492 if (*args[*cur_arg] == 'c') {
8493 alpn_str = &newsrv->check.alpn_str;
8494 alpn_len = &newsrv->check.alpn_len;
8495 } else {
8496 alpn_str = &newsrv->ssl_ctx.alpn_str;
8497 alpn_len = &newsrv->ssl_ctx.alpn_len;
8498
8499 }
Olivier Houchardc7566002018-11-20 23:33:50 +01008500 if (!*args[*cur_arg + 1]) {
8501 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
8502 return ERR_ALERT | ERR_FATAL;
8503 }
8504
Olivier Houchard92150142018-12-21 19:47:01 +01008505 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01008506
8507 /* the ALPN string is built as a suite of (<len> <name>)*,
8508 * so we reuse each comma to store the next <len> and need
8509 * one more for the end of the string.
8510 */
Olivier Houchard92150142018-12-21 19:47:01 +01008511 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
8512 *alpn_str = calloc(1, *alpn_len + 1);
8513 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01008514
8515 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01008516 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01008517 p2 = p1 + 1;
8518 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01008519 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01008520 if (!p2)
8521 p2 = p1 + 1 + strlen(p1 + 1);
8522
8523 if (p2 - (p1 + 1) > 255) {
8524 *p2 = '\0';
8525 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
8526 return ERR_ALERT | ERR_FATAL;
8527 }
8528
8529 *p1 = p2 - (p1 + 1);
8530 p1 = p2;
8531
8532 if (!*p2)
8533 break;
8534
8535 *(p2++) = '\0';
8536 }
8537 return 0;
8538#else
8539 if (err)
8540 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
8541 return ERR_ALERT | ERR_FATAL;
8542#endif
8543}
8544
Emeric Brunef42d922012-10-11 16:11:36 +02008545/* parse the "ca-file" server keyword */
8546static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8547{
8548 if (!*args[*cur_arg + 1]) {
8549 if (err)
8550 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
8551 return ERR_ALERT | ERR_FATAL;
8552 }
8553
Willy Tarreauef934602016-12-22 23:12:01 +01008554 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
8555 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008556 else
8557 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
8558
8559 return 0;
8560}
8561
Olivier Houchard9130a962017-10-17 17:33:43 +02008562/* parse the "check-sni" server keyword */
8563static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8564{
8565 if (!*args[*cur_arg + 1]) {
8566 if (err)
8567 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
8568 return ERR_ALERT | ERR_FATAL;
8569 }
8570
8571 newsrv->check.sni = strdup(args[*cur_arg + 1]);
8572 if (!newsrv->check.sni) {
8573 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
8574 return ERR_ALERT | ERR_FATAL;
8575 }
8576 return 0;
8577
8578}
8579
Willy Tarreau92faadf2012-10-10 23:04:25 +02008580/* parse the "check-ssl" server keyword */
8581static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8582{
8583 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01008584 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
8585 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008586#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008587 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
8588 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
8589#endif
Willy Tarreauef934602016-12-22 23:12:01 +01008590 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008591 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
8592 if (!newsrv->ssl_ctx.methods.min)
8593 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
8594 if (!newsrv->ssl_ctx.methods.max)
8595 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
8596
Willy Tarreau92faadf2012-10-10 23:04:25 +02008597 return 0;
8598}
8599
8600/* parse the "ciphers" server keyword */
8601static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8602{
8603 if (!*args[*cur_arg + 1]) {
8604 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
8605 return ERR_ALERT | ERR_FATAL;
8606 }
8607
8608 free(newsrv->ssl_ctx.ciphers);
8609 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
8610 return 0;
8611}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008612
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008613#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008614/* parse the "ciphersuites" server keyword */
8615static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8616{
8617 if (!*args[*cur_arg + 1]) {
8618 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
8619 return ERR_ALERT | ERR_FATAL;
8620 }
8621
8622 free(newsrv->ssl_ctx.ciphersuites);
8623 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
8624 return 0;
8625}
8626#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02008627
Emeric Brunef42d922012-10-11 16:11:36 +02008628/* parse the "crl-file" server keyword */
8629static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8630{
8631#ifndef X509_V_FLAG_CRL_CHECK
8632 if (err)
8633 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
8634 return ERR_ALERT | ERR_FATAL;
8635#else
8636 if (!*args[*cur_arg + 1]) {
8637 if (err)
8638 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
8639 return ERR_ALERT | ERR_FATAL;
8640 }
8641
Willy Tarreauef934602016-12-22 23:12:01 +01008642 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
8643 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008644 else
8645 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
8646
8647 return 0;
8648#endif
8649}
8650
Emeric Bruna7aa3092012-10-26 12:58:00 +02008651/* parse the "crt" server keyword */
8652static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8653{
8654 if (!*args[*cur_arg + 1]) {
8655 if (err)
8656 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
8657 return ERR_ALERT | ERR_FATAL;
8658 }
8659
Willy Tarreauef934602016-12-22 23:12:01 +01008660 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01008661 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02008662 else
8663 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
8664
8665 return 0;
8666}
Emeric Brunef42d922012-10-11 16:11:36 +02008667
Frédéric Lécaille340ae602017-03-13 10:38:04 +01008668/* parse the "no-check-ssl" server keyword */
8669static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8670{
8671 newsrv->check.use_ssl = 0;
8672 free(newsrv->ssl_ctx.ciphers);
8673 newsrv->ssl_ctx.ciphers = NULL;
8674 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
8675 return 0;
8676}
8677
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01008678/* parse the "no-send-proxy-v2-ssl" server keyword */
8679static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8680{
8681 newsrv->pp_opts &= ~SRV_PP_V2;
8682 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
8683 return 0;
8684}
8685
8686/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
8687static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8688{
8689 newsrv->pp_opts &= ~SRV_PP_V2;
8690 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
8691 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
8692 return 0;
8693}
8694
Frédéric Lécaillee381d762017-03-13 11:54:17 +01008695/* parse the "no-ssl" server keyword */
8696static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8697{
8698 newsrv->use_ssl = 0;
8699 free(newsrv->ssl_ctx.ciphers);
8700 newsrv->ssl_ctx.ciphers = NULL;
8701 return 0;
8702}
8703
Olivier Houchard522eea72017-11-03 16:27:47 +01008704/* parse the "allow-0rtt" server keyword */
8705static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8706{
8707 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
8708 return 0;
8709}
8710
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01008711/* parse the "no-ssl-reuse" server keyword */
8712static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8713{
8714 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
8715 return 0;
8716}
8717
Emeric Brunf9c5c472012-10-11 15:28:34 +02008718/* parse the "no-tls-tickets" server keyword */
8719static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8720{
8721 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
8722 return 0;
8723}
David Safb76832014-05-08 23:42:08 -04008724/* parse the "send-proxy-v2-ssl" server keyword */
8725static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8726{
8727 newsrv->pp_opts |= SRV_PP_V2;
8728 newsrv->pp_opts |= SRV_PP_V2_SSL;
8729 return 0;
8730}
8731
8732/* parse the "send-proxy-v2-ssl-cn" server keyword */
8733static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8734{
8735 newsrv->pp_opts |= SRV_PP_V2;
8736 newsrv->pp_opts |= SRV_PP_V2_SSL;
8737 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
8738 return 0;
8739}
Emeric Brunf9c5c472012-10-11 15:28:34 +02008740
Willy Tarreau732eac42015-07-09 11:40:25 +02008741/* parse the "sni" server keyword */
8742static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8743{
8744#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
8745 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
8746 return ERR_ALERT | ERR_FATAL;
8747#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008748 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02008749
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008750 arg = args[*cur_arg + 1];
8751 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02008752 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
8753 return ERR_ALERT | ERR_FATAL;
8754 }
8755
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008756 free(newsrv->sni_expr);
8757 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02008758
Willy Tarreau732eac42015-07-09 11:40:25 +02008759 return 0;
8760#endif
8761}
8762
Willy Tarreau92faadf2012-10-10 23:04:25 +02008763/* parse the "ssl" server keyword */
8764static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8765{
8766 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01008767 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
8768 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008769#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008770 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
8771 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
8772#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02008773 return 0;
8774}
8775
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01008776/* parse the "ssl-reuse" server keyword */
8777static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8778{
8779 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
8780 return 0;
8781}
8782
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01008783/* parse the "tls-tickets" server keyword */
8784static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8785{
8786 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
8787 return 0;
8788}
8789
Emeric Brunef42d922012-10-11 16:11:36 +02008790/* parse the "verify" server keyword */
8791static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8792{
8793 if (!*args[*cur_arg + 1]) {
8794 if (err)
8795 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
8796 return ERR_ALERT | ERR_FATAL;
8797 }
8798
8799 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008800 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02008801 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008802 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02008803 else {
8804 if (err)
8805 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
8806 args[*cur_arg], args[*cur_arg + 1]);
8807 return ERR_ALERT | ERR_FATAL;
8808 }
8809
Evan Broderbe554312013-06-27 00:05:25 -07008810 return 0;
8811}
8812
8813/* parse the "verifyhost" server keyword */
8814static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8815{
8816 if (!*args[*cur_arg + 1]) {
8817 if (err)
8818 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
8819 return ERR_ALERT | ERR_FATAL;
8820 }
8821
Frédéric Lécaille273f3212017-03-13 15:52:01 +01008822 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07008823 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
8824
Emeric Brunef42d922012-10-11 16:11:36 +02008825 return 0;
8826}
8827
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008828/* parse the "ssl-default-bind-options" keyword in global section */
8829static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
8830 struct proxy *defpx, const char *file, int line,
8831 char **err) {
8832 int i = 1;
8833
8834 if (*(args[i]) == 0) {
8835 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
8836 return -1;
8837 }
8838 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008839 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01008840 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008841 else if (!strcmp(args[i], "prefer-client-ciphers"))
8842 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008843 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
8844 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
8845 i++;
8846 else {
8847 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
8848 return -1;
8849 }
8850 }
8851 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008852 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
8853 return -1;
8854 }
8855 i++;
8856 }
8857 return 0;
8858}
8859
8860/* parse the "ssl-default-server-options" keyword in global section */
8861static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
8862 struct proxy *defpx, const char *file, int line,
8863 char **err) {
8864 int i = 1;
8865
8866 if (*(args[i]) == 0) {
8867 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
8868 return -1;
8869 }
8870 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008871 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01008872 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008873 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
8874 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
8875 i++;
8876 else {
8877 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
8878 return -1;
8879 }
8880 }
8881 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008882 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
8883 return -1;
8884 }
8885 i++;
8886 }
8887 return 0;
8888}
8889
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008890/* parse the "ca-base" / "crt-base" keywords in global section.
8891 * Returns <0 on alert, >0 on warning, 0 on success.
8892 */
8893static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
8894 struct proxy *defpx, const char *file, int line,
8895 char **err)
8896{
8897 char **target;
8898
Willy Tarreauef934602016-12-22 23:12:01 +01008899 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008900
8901 if (too_many_args(1, args, err, NULL))
8902 return -1;
8903
8904 if (*target) {
8905 memprintf(err, "'%s' already specified.", args[0]);
8906 return -1;
8907 }
8908
8909 if (*(args[1]) == 0) {
8910 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
8911 return -1;
8912 }
8913 *target = strdup(args[1]);
8914 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008915}
8916
8917/* parse the "ssl-mode-async" keyword in global section.
8918 * Returns <0 on alert, >0 on warning, 0 on success.
8919 */
8920static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
8921 struct proxy *defpx, const char *file, int line,
8922 char **err)
8923{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008924#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008925 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01008926 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008927 return 0;
8928#else
8929 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
8930 return -1;
8931#endif
8932}
8933
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008934#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008935static int ssl_check_async_engine_count(void) {
8936 int err_code = 0;
8937
Emeric Brun3854e012017-05-17 20:42:48 +02008938 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008939 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008940 err_code = ERR_ABORT;
8941 }
8942 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008943}
8944
Grant Zhang872f9c22017-01-21 01:10:18 +00008945/* parse the "ssl-engine" keyword in global section.
8946 * Returns <0 on alert, >0 on warning, 0 on success.
8947 */
8948static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
8949 struct proxy *defpx, const char *file, int line,
8950 char **err)
8951{
8952 char *algo;
8953 int ret = -1;
8954
8955 if (*(args[1]) == 0) {
8956 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
8957 return ret;
8958 }
8959
8960 if (*(args[2]) == 0) {
8961 /* if no list of algorithms is given, it defaults to ALL */
8962 algo = strdup("ALL");
8963 goto add_engine;
8964 }
8965
8966 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
8967 if (strcmp(args[2], "algo") != 0) {
8968 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
8969 return ret;
8970 }
8971
8972 if (*(args[3]) == 0) {
8973 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
8974 return ret;
8975 }
8976 algo = strdup(args[3]);
8977
8978add_engine:
8979 if (ssl_init_single_engine(args[1], algo)==0) {
8980 openssl_engines_initialized++;
8981 ret = 0;
8982 }
8983 free(algo);
8984 return ret;
8985}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008986#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00008987
Willy Tarreauf22e9682016-12-21 23:23:19 +01008988/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
8989 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
8990 */
8991static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
8992 struct proxy *defpx, const char *file, int line,
8993 char **err)
8994{
8995 char **target;
8996
Willy Tarreauef934602016-12-22 23:12:01 +01008997 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01008998
8999 if (too_many_args(1, args, err, NULL))
9000 return -1;
9001
9002 if (*(args[1]) == 0) {
9003 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9004 return -1;
9005 }
9006
9007 free(*target);
9008 *target = strdup(args[1]);
9009 return 0;
9010}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009011
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009012#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009013/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9014 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9015 */
9016static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9017 struct proxy *defpx, const char *file, int line,
9018 char **err)
9019{
9020 char **target;
9021
9022 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9023
9024 if (too_many_args(1, args, err, NULL))
9025 return -1;
9026
9027 if (*(args[1]) == 0) {
9028 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9029 return -1;
9030 }
9031
9032 free(*target);
9033 *target = strdup(args[1]);
9034 return 0;
9035}
9036#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009037
Willy Tarreau9ceda382016-12-21 23:13:03 +01009038/* parse various global tune.ssl settings consisting in positive integers.
9039 * Returns <0 on alert, >0 on warning, 0 on success.
9040 */
9041static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9042 struct proxy *defpx, const char *file, int line,
9043 char **err)
9044{
9045 int *target;
9046
9047 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9048 target = &global.tune.sslcachesize;
9049 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009050 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009051 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009052 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009053 else if (strcmp(args[0], "maxsslconn") == 0)
9054 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009055 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9056 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009057 else {
9058 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9059 return -1;
9060 }
9061
9062 if (too_many_args(1, args, err, NULL))
9063 return -1;
9064
9065 if (*(args[1]) == 0) {
9066 memprintf(err, "'%s' expects an integer argument.", args[0]);
9067 return -1;
9068 }
9069
9070 *target = atoi(args[1]);
9071 if (*target < 0) {
9072 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9073 return -1;
9074 }
9075 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009076}
9077
9078static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9079 struct proxy *defpx, const char *file, int line,
9080 char **err)
9081{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009082 int ret;
9083
9084 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9085 if (ret != 0)
9086 return ret;
9087
Willy Tarreaubafbe012017-11-24 17:34:44 +01009088 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009089 memprintf(err, "'%s' is already configured.", args[0]);
9090 return -1;
9091 }
9092
Willy Tarreaubafbe012017-11-24 17:34:44 +01009093 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9094 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009095 memprintf(err, "Out of memory error.");
9096 return -1;
9097 }
9098 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009099}
9100
9101/* parse "ssl.force-private-cache".
9102 * Returns <0 on alert, >0 on warning, 0 on success.
9103 */
9104static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9105 struct proxy *defpx, const char *file, int line,
9106 char **err)
9107{
9108 if (too_many_args(0, args, err, NULL))
9109 return -1;
9110
Willy Tarreauef934602016-12-22 23:12:01 +01009111 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009112 return 0;
9113}
9114
9115/* parse "ssl.lifetime".
9116 * Returns <0 on alert, >0 on warning, 0 on success.
9117 */
9118static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9119 struct proxy *defpx, const char *file, int line,
9120 char **err)
9121{
9122 const char *res;
9123
9124 if (too_many_args(1, args, err, NULL))
9125 return -1;
9126
9127 if (*(args[1]) == 0) {
9128 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9129 return -1;
9130 }
9131
Willy Tarreauef934602016-12-22 23:12:01 +01009132 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009133 if (res == PARSE_TIME_OVER) {
9134 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9135 args[1], args[0]);
9136 return -1;
9137 }
9138 else if (res == PARSE_TIME_UNDER) {
9139 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9140 args[1], args[0]);
9141 return -1;
9142 }
9143 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009144 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9145 return -1;
9146 }
9147 return 0;
9148}
9149
9150#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009151/* parse "ssl-dh-param-file".
9152 * Returns <0 on alert, >0 on warning, 0 on success.
9153 */
9154static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9155 struct proxy *defpx, const char *file, int line,
9156 char **err)
9157{
9158 if (too_many_args(1, args, err, NULL))
9159 return -1;
9160
9161 if (*(args[1]) == 0) {
9162 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9163 return -1;
9164 }
9165
9166 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9167 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9168 return -1;
9169 }
9170 return 0;
9171}
9172
Willy Tarreau9ceda382016-12-21 23:13:03 +01009173/* parse "ssl.default-dh-param".
9174 * Returns <0 on alert, >0 on warning, 0 on success.
9175 */
9176static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9177 struct proxy *defpx, const char *file, int line,
9178 char **err)
9179{
9180 if (too_many_args(1, args, err, NULL))
9181 return -1;
9182
9183 if (*(args[1]) == 0) {
9184 memprintf(err, "'%s' expects an integer argument.", args[0]);
9185 return -1;
9186 }
9187
Willy Tarreauef934602016-12-22 23:12:01 +01009188 global_ssl.default_dh_param = atoi(args[1]);
9189 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009190 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9191 return -1;
9192 }
9193 return 0;
9194}
9195#endif
9196
9197
William Lallemand32af2032016-10-29 18:09:35 +02009198/* This function is used with TLS ticket keys management. It permits to browse
9199 * each reference. The variable <getnext> must contain the current node,
9200 * <end> point to the root node.
9201 */
9202#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9203static inline
9204struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
9205{
9206 struct tls_keys_ref *ref = getnext;
9207
9208 while (1) {
9209
9210 /* Get next list entry. */
9211 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
9212
9213 /* If the entry is the last of the list, return NULL. */
9214 if (&ref->list == end)
9215 return NULL;
9216
9217 return ref;
9218 }
9219}
9220
9221static inline
9222struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
9223{
9224 int id;
9225 char *error;
9226
9227 /* If the reference starts by a '#', this is numeric id. */
9228 if (reference[0] == '#') {
9229 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
9230 id = strtol(reference + 1, &error, 10);
9231 if (*error != '\0')
9232 return NULL;
9233
9234 /* Perform the unique id lookup. */
9235 return tlskeys_ref_lookupid(id);
9236 }
9237
9238 /* Perform the string lookup. */
9239 return tlskeys_ref_lookup(reference);
9240}
9241#endif
9242
9243
9244#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9245
9246static int cli_io_handler_tlskeys_files(struct appctx *appctx);
9247
9248static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
9249 return cli_io_handler_tlskeys_files(appctx);
9250}
9251
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009252/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
9253 * (next index to be dumped), and cli.p0 (next key reference).
9254 */
William Lallemand32af2032016-10-29 18:09:35 +02009255static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
9256
9257 struct stream_interface *si = appctx->owner;
9258
9259 switch (appctx->st2) {
9260 case STAT_ST_INIT:
9261 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08009262 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +02009263 * later and restart at the state "STAT_ST_INIT".
9264 */
9265 chunk_reset(&trash);
9266
9267 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
9268 chunk_appendf(&trash, "# id secret\n");
9269 else
9270 chunk_appendf(&trash, "# id (file)\n");
9271
Willy Tarreau06d80a92017-10-19 14:32:15 +02009272 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01009273 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009274 return 0;
9275 }
9276
William Lallemand32af2032016-10-29 18:09:35 +02009277 /* Now, we start the browsing of the references lists.
9278 * Note that the following call to LIST_ELEM return bad pointer. The only
9279 * available field of this pointer is <list>. It is used with the function
9280 * tlskeys_list_get_next() for retruning the first available entry
9281 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009282 if (appctx->ctx.cli.p0 == NULL) {
9283 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
9284 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009285 }
9286
9287 appctx->st2 = STAT_ST_LIST;
9288 /* fall through */
9289
9290 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009291 while (appctx->ctx.cli.p0) {
9292 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +02009293
9294 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009295 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +02009296 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009297
9298 if (appctx->ctx.cli.i1 == 0)
9299 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
9300
William Lallemand32af2032016-10-29 18:09:35 +02009301 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +01009302 int head;
9303
9304 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
9305 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009306 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +02009307 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +02009308
9309 chunk_reset(t2);
9310 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +01009311 if (ref->key_size_bits == 128) {
9312 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9313 sizeof(struct tls_sess_key_128),
9314 t2->area, t2->size);
9315 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9316 t2->area);
9317 }
9318 else if (ref->key_size_bits == 256) {
9319 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9320 sizeof(struct tls_sess_key_256),
9321 t2->area, t2->size);
9322 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9323 t2->area);
9324 }
9325 else {
9326 /* This case should never happen */
9327 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
9328 }
William Lallemand32af2032016-10-29 18:09:35 +02009329
Willy Tarreau06d80a92017-10-19 14:32:15 +02009330 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009331 /* let's try again later from this stream. We add ourselves into
9332 * this stream's users so that it can remove us upon termination.
9333 */
Christopher Faulet16f45c82018-02-16 11:23:49 +01009334 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +01009335 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009336 return 0;
9337 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009338 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +02009339 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01009340 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009341 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +02009342 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02009343 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009344 /* let's try again later from this stream. We add ourselves into
9345 * this stream's users so that it can remove us upon termination.
9346 */
Willy Tarreaudb398432018-11-15 11:08:52 +01009347 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009348 return 0;
9349 }
9350
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009351 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +02009352 break;
9353
9354 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009355 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009356 }
9357
9358 appctx->st2 = STAT_ST_FIN;
9359 /* fall through */
9360
9361 default:
9362 appctx->st2 = STAT_ST_FIN;
9363 return 1;
9364 }
9365 return 0;
9366}
9367
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009368/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009369static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009370{
William Lallemand32af2032016-10-29 18:09:35 +02009371 /* no parameter, shows only file list */
9372 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009373 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009374 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009375 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009376 }
9377
9378 if (args[2][0] == '*') {
9379 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009380 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009381 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009382 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +02009383 if (!appctx->ctx.cli.p0)
9384 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +02009385 }
William Lallemand32af2032016-10-29 18:09:35 +02009386 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009387 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009388}
9389
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009390static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009391{
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009392 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +02009393 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009394
William Lallemand32af2032016-10-29 18:09:35 +02009395 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +02009396 if (!*args[3] || !*args[4])
9397 return cli_err(appctx, "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n");
William Lallemand32af2032016-10-29 18:09:35 +02009398
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009399 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02009400 if (!ref)
9401 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +02009402
Willy Tarreau1c913e42018-08-22 05:26:57 +02009403 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +02009404 if (ret < 0)
9405 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +01009406
Willy Tarreau1c913e42018-08-22 05:26:57 +02009407 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +02009408 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
9409 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +02009410
Willy Tarreau9d008692019-08-09 11:21:01 +02009411 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +02009412}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009413#endif
William Lallemand32af2032016-10-29 18:09:35 +02009414
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009415static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009416{
9417#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
9418 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +02009419 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009420
9421 if (!payload)
9422 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +02009423
9424 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +02009425 if (!*payload)
9426 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02009427
9428 /* remove \r and \n from the payload */
9429 for (i = 0, j = 0; payload[i]; i++) {
9430 if (payload[i] == '\r' || payload[i] == '\n')
9431 continue;
9432 payload[j++] = payload[i];
9433 }
9434 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +02009435
Willy Tarreau1c913e42018-08-22 05:26:57 +02009436 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +02009437 if (ret < 0)
9438 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +02009439
Willy Tarreau1c913e42018-08-22 05:26:57 +02009440 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +02009441 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02009442 if (err)
9443 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
9444 else
9445 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +02009446 }
Willy Tarreau9d008692019-08-09 11:21:01 +02009447
9448 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +02009449#else
Willy Tarreau9d008692019-08-09 11:21:01 +02009450 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
William Lallemand32af2032016-10-29 18:09:35 +02009451#endif
9452
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009453}
9454
Willy Tarreau86a394e2019-05-09 14:15:32 +02009455#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009456static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
9457{
9458 switch (arg->type) {
9459 case ARGT_STR:
9460 smp->data.type = SMP_T_STR;
9461 smp->data.u.str = arg->data.str;
9462 return 1;
9463 case ARGT_VAR:
9464 if (!vars_get_by_desc(&arg->data.var, smp))
9465 return 0;
9466 if (!sample_casts[smp->data.type][SMP_T_STR])
9467 return 0;
9468 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
9469 return 0;
9470 return 1;
9471 default:
9472 return 0;
9473 }
9474}
9475
9476static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
9477 const char *file, int line, char **err)
9478{
9479 switch(args[0].data.sint) {
9480 case 128:
9481 case 192:
9482 case 256:
9483 break;
9484 default:
9485 memprintf(err, "key size must be 128, 192 or 256 (bits).");
9486 return 0;
9487 }
9488 /* Try to decode a variable. */
9489 vars_check_arg(&args[1], NULL);
9490 vars_check_arg(&args[2], NULL);
9491 vars_check_arg(&args[3], NULL);
9492 return 1;
9493}
9494
9495/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
9496static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
9497{
9498 struct sample nonce, key, aead_tag;
9499 struct buffer *smp_trash, *smp_trash_alloc;
9500 EVP_CIPHER_CTX *ctx;
9501 int dec_size, ret;
9502
9503 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
9504 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
9505 return 0;
9506
9507 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
9508 if (!sample_conv_var2smp_str(&arg_p[2], &key))
9509 return 0;
9510
9511 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
9512 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
9513 return 0;
9514
9515 smp_trash = get_trash_chunk();
9516 smp_trash_alloc = alloc_trash_chunk();
9517 if (!smp_trash_alloc)
9518 return 0;
9519
9520 ctx = EVP_CIPHER_CTX_new();
9521
9522 if (!ctx)
9523 goto err;
9524
9525 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
9526 if (dec_size < 0)
9527 goto err;
9528 smp_trash->data = dec_size;
9529
9530 /* Set cipher type and mode */
9531 switch(arg_p[0].data.sint) {
9532 case 128:
9533 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
9534 break;
9535 case 192:
9536 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
9537 break;
9538 case 256:
9539 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
9540 break;
9541 }
9542
9543 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
9544
9545 /* Initialise IV */
9546 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
9547 goto err;
9548
9549 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
9550 if (dec_size < 0)
9551 goto err;
9552 smp_trash->data = dec_size;
9553
9554 /* Initialise key */
9555 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
9556 goto err;
9557
9558 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
9559 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
9560 goto err;
9561
9562 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
9563 if (dec_size < 0)
9564 goto err;
9565 smp_trash_alloc->data = dec_size;
9566 dec_size = smp_trash->data;
9567
9568 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
9569 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
9570
9571 if (ret <= 0)
9572 goto err;
9573
9574 smp->data.u.str.data = dec_size + smp_trash->data;
9575 smp->data.u.str.area = smp_trash->area;
9576 smp->data.type = SMP_T_BIN;
9577 smp->flags &= ~SMP_F_CONST;
9578 free_trash_chunk(smp_trash_alloc);
9579 return 1;
9580
9581err:
9582 free_trash_chunk(smp_trash_alloc);
9583 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009584}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009585# endif
William Lallemand32af2032016-10-29 18:09:35 +02009586
9587/* register cli keywords */
9588static struct cli_kw_list cli_kws = {{ },{
9589#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9590 { { "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 +02009591 { { "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 +02009592#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009593 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemand32af2032016-10-29 18:09:35 +02009594 { { NULL }, NULL, NULL, NULL }
9595}};
9596
Willy Tarreau0108d902018-11-25 19:14:37 +01009597INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +02009598
Willy Tarreau7875d092012-09-10 08:20:03 +02009599/* Note: must not be declared <const> as its list will be overwritten.
9600 * Please take care of keeping this list alphabetically sorted.
9601 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009602static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +02009603 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009604 { "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 +01009605#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +01009606 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +01009607#endif
Emeric Brun645ae792014-04-30 14:21:06 +02009608 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +01009609#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9610 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
9611#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +01009612 { "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 +02009613 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +02009614 { "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 +02009615 { "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 +02009616#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +02009617 { "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 -04009618#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009619#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -04009620 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
9621 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -04009622 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
9623#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009624 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
9625 { "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 +01009626 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009627 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02009628 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9629 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9630 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9631 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9632 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9633 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9634 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9635 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009636 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009637 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
9638 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +01009639 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02009640 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9641 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9642 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9643 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9644 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9645 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
9646 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +02009647 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009648 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009649 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009650 { "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 +01009651 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01009652 { "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 +02009653 { "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 +01009654 { "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 +02009655 { "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 +01009656#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009657 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +02009658#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009659#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009660 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +02009661#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009662 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009663#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +02009664 { "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 -04009665#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02009666 { "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 +02009667#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009668 { "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 -04009669#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009670#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -04009671 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9672 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -04009673 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9674#endif
Patrick Hemmer41966772018-04-28 19:15:48 -04009675#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009676 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -04009677#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009678 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9679 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
9680 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
9681 { "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 +02009682 { NULL, NULL, 0, 0, 0 },
9683}};
9684
Willy Tarreau0108d902018-11-25 19:14:37 +01009685INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
9686
Willy Tarreau7875d092012-09-10 08:20:03 +02009687/* Note: must not be declared <const> as its list will be overwritten.
9688 * Please take care of keeping this list alphabetically sorted.
9689 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02009690static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +01009691 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
9692 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +01009693 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +02009694}};
9695
Willy Tarreau0108d902018-11-25 19:14:37 +01009696INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
9697
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009698/* Note: must not be declared <const> as its list will be overwritten.
9699 * Please take care of keeping this list alphabetically sorted, doing so helps
9700 * all code contributors.
9701 * Optional keywords are also declared with a NULL ->parse() function so that
9702 * the config parser can report an appropriate error when a known keyword was
9703 * not enabled.
9704 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009705static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +02009706 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009707 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
9708 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
9709 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009710#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009711 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
9712#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009713 { "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 +01009714 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009715 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009716 { "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 +01009717 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009718 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
9719 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009720 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
9721 { NULL, NULL, 0 },
9722};
9723
Willy Tarreau0108d902018-11-25 19:14:37 +01009724/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
9725
Willy Tarreau51fb7652012-09-18 18:24:39 +02009726static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +02009727 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009728 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
9729 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
9730 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
9731 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
9732 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
9733 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009734#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009735 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
9736#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009737 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
9738 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
9739 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
9740 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
9741 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
9742 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
9743 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
9744 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
9745 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
9746 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009747 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009748 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009749 { "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 +02009750 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
9751 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
9752 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
9753 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009754 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009755 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
9756 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009757 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
9758 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009759 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
9760 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
9761 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
9762 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
9763 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009764 { NULL, NULL, 0 },
9765}};
Emeric Brun46591952012-05-18 15:47:34 +02009766
Willy Tarreau0108d902018-11-25 19:14:37 +01009767INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
9768
Willy Tarreau92faadf2012-10-10 23:04:25 +02009769/* Note: must not be declared <const> as its list will be overwritten.
9770 * Please take care of keeping this list alphabetically sorted, doing so helps
9771 * all code contributors.
9772 * Optional keywords are also declared with a NULL ->parse() function so that
9773 * the config parser can report an appropriate error when a known keyword was
9774 * not enabled.
9775 */
9776static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +01009777 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +01009778 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009779 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +01009780 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +02009781 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009782 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
9783 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009784#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009785 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
9786#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009787 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
9788 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
9789 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
9790 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
9791 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
9792 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
9793 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
9794 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
9795 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
9796 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
9797 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
9798 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
9799 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
9800 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
9801 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
9802 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
9803 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
9804 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +01009805 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009806 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
9807 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
9808 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
9809 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
9810 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
9811 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
9812 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
9813 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
9814 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
9815 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +02009816 { NULL, NULL, 0, 0 },
9817}};
9818
Willy Tarreau0108d902018-11-25 19:14:37 +01009819INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
9820
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009821static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009822 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
9823 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009824 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009825 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
9826 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +01009827#ifndef OPENSSL_NO_DH
9828 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
9829#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009830 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009831#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00009832 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009833#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +01009834 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
9835#ifndef OPENSSL_NO_DH
9836 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
9837#endif
9838 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
9839 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
9840 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
9841 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009842 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +01009843 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
9844 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009845#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009846 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
9847 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
9848#endif
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009849 { 0, NULL, NULL },
9850}};
9851
Willy Tarreau0108d902018-11-25 19:14:37 +01009852INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
9853
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009854/* Note: must not be declared <const> as its list will be overwritten */
9855static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +02009856#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +01009857 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
9858#endif
9859 { NULL, NULL, 0, 0, 0 },
9860}};
9861
9862INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
9863
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02009864/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +01009865static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02009866 .snd_buf = ssl_sock_from_buf,
9867 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +01009868 .subscribe = ssl_subscribe,
9869 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +02009870 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +02009871 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +02009872 .rcv_pipe = NULL,
9873 .snd_pipe = NULL,
9874 .shutr = NULL,
9875 .shutw = ssl_sock_shutw,
9876 .close = ssl_sock_close,
9877 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +01009878 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +01009879 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +01009880 .prepare_srv = ssl_sock_prepare_srv_ctx,
9881 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +01009882 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01009883 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +02009884};
9885
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009886enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
9887 struct session *sess, struct stream *s, int flags)
9888{
9889 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009890 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009891
9892 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009893 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009894
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009895 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009896 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +01009897 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02009898 s->req.flags |= CF_READ_NULL;
9899 return ACT_RET_YIELD;
9900 }
9901 }
9902 return (ACT_RET_CONT);
9903}
9904
9905static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
9906{
9907 rule->action_ptr = ssl_action_wait_for_hs;
9908
9909 return ACT_RET_PRS_OK;
9910}
9911
9912static struct action_kw_list http_req_actions = {ILH, {
9913 { "wait-for-handshake", ssl_parse_wait_for_hs },
9914 { /* END */ }
9915}};
9916
Willy Tarreau0108d902018-11-25 19:14:37 +01009917INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
9918
Willy Tarreau5db847a2019-05-09 14:13:35 +02009919#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01009920
9921static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
9922{
9923 if (ptr) {
9924 chunk_destroy(ptr);
9925 free(ptr);
9926 }
9927}
9928
9929#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01009930static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
9931{
Willy Tarreaubafbe012017-11-24 17:34:44 +01009932 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01009933}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01009934
Emeric Brun46591952012-05-18 15:47:34 +02009935__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +02009936static void __ssl_sock_init(void)
9937{
Ilya Shipitsin0590f442019-05-25 19:30:50 +05009938#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +02009939 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +05009940 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +05009941#endif
Emeric Brun46591952012-05-18 15:47:34 +02009942
Willy Tarreauef934602016-12-22 23:12:01 +01009943 if (global_ssl.listen_default_ciphers)
9944 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
9945 if (global_ssl.connect_default_ciphers)
9946 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009947#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009948 if (global_ssl.listen_default_ciphersuites)
9949 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9950 if (global_ssl.connect_default_ciphersuites)
9951 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9952#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +01009953
Willy Tarreau13e14102016-12-22 20:25:26 +01009954 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009955#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +02009956 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -08009957#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +05009958#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +02009959 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +05009960 n = sk_SSL_COMP_num(cm);
9961 while (n--) {
9962 (void) sk_SSL_COMP_pop(cm);
9963 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +05009964#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +05009965
Willy Tarreau5db847a2019-05-09 14:13:35 +02009966#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +02009967 ssl_locking_init();
9968#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +02009969#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01009970 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
9971#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +02009972 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +02009973 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 +01009974 ssl_pkey_info_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009975#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00009976 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009977 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009978#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +01009979#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9980 hap_register_post_check(tlskeys_finalize_config);
9981#endif
Willy Tarreau80713382018-11-26 10:19:54 +01009982
9983 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
9984 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
9985
9986#ifndef OPENSSL_NO_DH
9987 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
9988 hap_register_post_deinit(ssl_free_dh);
9989#endif
9990#ifndef OPENSSL_NO_ENGINE
9991 hap_register_post_deinit(ssl_free_engines);
9992#endif
9993 /* Load SSL string for the verbose & debug mode. */
9994 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +02009995 ha_meth = BIO_meth_new(0x666, "ha methods");
9996 BIO_meth_set_write(ha_meth, ha_ssl_write);
9997 BIO_meth_set_read(ha_meth, ha_ssl_read);
9998 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
9999 BIO_meth_set_create(ha_meth, ha_ssl_new);
10000 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
10001 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
10002 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
Willy Tarreau80713382018-11-26 10:19:54 +010010003}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010010004
Willy Tarreau80713382018-11-26 10:19:54 +010010005/* Compute and register the version string */
10006static void ssl_register_build_options()
10007{
10008 char *ptr = NULL;
10009 int i;
10010
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010011 memprintf(&ptr, "Built with OpenSSL version : "
10012#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010010013 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010014#else /* OPENSSL_IS_BORINGSSL */
10015 OPENSSL_VERSION_TEXT
10016 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080010017 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020010018 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010019#endif
10020 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020010021#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010022 "no (library version too old)"
10023#elif defined(OPENSSL_NO_TLSEXT)
10024 "no (disabled via OPENSSL_NO_TLSEXT)"
10025#else
10026 "yes"
10027#endif
10028 "", ptr);
10029
10030 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
10031#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10032 "yes"
10033#else
10034#ifdef OPENSSL_NO_TLSEXT
10035 "no (because of OPENSSL_NO_TLSEXT)"
10036#else
10037 "no (version might be too old, 0.9.8f min needed)"
10038#endif
10039#endif
10040 "", ptr);
10041
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020010042 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
10043 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
10044 if (methodVersions[i].option)
10045 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010010046
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010047 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010010048}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010010049
Willy Tarreau80713382018-11-26 10:19:54 +010010050INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020010051
Emeric Brun46591952012-05-18 15:47:34 +020010052
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010053#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000010054void ssl_free_engines(void) {
10055 struct ssl_engine_list *wl, *wlb;
10056 /* free up engine list */
10057 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
10058 ENGINE_finish(wl->e);
10059 ENGINE_free(wl->e);
10060 LIST_DEL(&wl->list);
10061 free(wl);
10062 }
10063}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010064#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020010065
Remi Gacogned3a23c32015-05-28 16:39:47 +020010066#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000010067void ssl_free_dh(void) {
10068 if (local_dh_1024) {
10069 DH_free(local_dh_1024);
10070 local_dh_1024 = NULL;
10071 }
10072 if (local_dh_2048) {
10073 DH_free(local_dh_2048);
10074 local_dh_2048 = NULL;
10075 }
10076 if (local_dh_4096) {
10077 DH_free(local_dh_4096);
10078 local_dh_4096 = NULL;
10079 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020010080 if (global_dh) {
10081 DH_free(global_dh);
10082 global_dh = NULL;
10083 }
Grant Zhang872f9c22017-01-21 01:10:18 +000010084}
10085#endif
10086
10087__attribute__((destructor))
10088static void __ssl_sock_deinit(void)
10089{
10090#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020010091 if (ssl_ctx_lru_tree) {
10092 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010010093 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020010094 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020010095#endif
10096
Willy Tarreau5db847a2019-05-09 14:13:35 +020010097#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020010098 ERR_remove_state(0);
10099 ERR_free_strings();
10100
10101 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080010102#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020010103
Willy Tarreau5db847a2019-05-09 14:13:35 +020010104#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020010105 CRYPTO_cleanup_all_ex_data();
10106#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020010107 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020010108}
10109
10110
Emeric Brun46591952012-05-18 15:47:34 +020010111/*
10112 * Local variables:
10113 * c-indent-level: 8
10114 * c-basic-offset: 8
10115 * End:
10116 */