blob: 4ff051b9b48027c0ef4f803deb1ffba73e76faeb [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;
Willy Tarreau113d52b2020-01-10 09:20:26 +0100217 struct wait_event *subs;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100218 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100219 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100220 int sent_early_data; /* Amount of early data we sent so far */
221
Olivier Houchard66ab4982019-02-26 18:37:15 +0100222};
223
224DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
225
Olivier Houchardea8dd942019-05-20 14:02:16 +0200226static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200227static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200228
Olivier Houcharda8955d52019-04-07 22:00:38 +0200229/* Methods to implement OpenSSL BIO */
230static int ha_ssl_write(BIO *h, const char *buf, int num)
231{
232 struct buffer tmpbuf;
233 struct ssl_sock_ctx *ctx;
234 int ret;
235
236 ctx = BIO_get_data(h);
237 tmpbuf.size = num;
238 tmpbuf.area = (void *)(uintptr_t)buf;
239 tmpbuf.data = num;
240 tmpbuf.head = 0;
241 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200242 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200243 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200244 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200245 } else if (ret == 0)
246 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200247 return ret;
248}
249
250static int ha_ssl_gets(BIO *h, char *buf, int size)
251{
252
253 return 0;
254}
255
256static int ha_ssl_puts(BIO *h, const char *str)
257{
258
259 return ha_ssl_write(h, str, strlen(str));
260}
261
262static int ha_ssl_read(BIO *h, char *buf, int size)
263{
264 struct buffer tmpbuf;
265 struct ssl_sock_ctx *ctx;
266 int ret;
267
268 ctx = BIO_get_data(h);
269 tmpbuf.size = size;
270 tmpbuf.area = buf;
271 tmpbuf.data = 0;
272 tmpbuf.head = 0;
273 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200274 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200275 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200276 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200277 } else if (ret == 0)
278 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200279
280 return ret;
281}
282
283static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
284{
285 int ret = 0;
286 switch (cmd) {
287 case BIO_CTRL_DUP:
288 case BIO_CTRL_FLUSH:
289 ret = 1;
290 break;
291 }
292 return ret;
293}
294
295static int ha_ssl_new(BIO *h)
296{
297 BIO_set_init(h, 1);
298 BIO_set_data(h, NULL);
299 BIO_clear_flags(h, ~0);
300 return 1;
301}
302
303static int ha_ssl_free(BIO *data)
304{
305
306 return 1;
307}
308
309
Willy Tarreau5db847a2019-05-09 14:13:35 +0200310#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100311
Emeric Brun821bb9b2017-06-15 16:37:39 +0200312static HA_RWLOCK_T *ssl_rwlocks;
313
314
315unsigned long ssl_id_function(void)
316{
317 return (unsigned long)tid;
318}
319
320void ssl_locking_function(int mode, int n, const char * file, int line)
321{
322 if (mode & CRYPTO_LOCK) {
323 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100324 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200325 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100326 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200327 }
328 else {
329 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100330 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200331 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200333 }
334}
335
336static int ssl_locking_init(void)
337{
338 int i;
339
340 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
341 if (!ssl_rwlocks)
342 return -1;
343
344 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100345 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200346
347 CRYPTO_set_id_callback(ssl_id_function);
348 CRYPTO_set_locking_callback(ssl_locking_function);
349
350 return 0;
351}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100352
Emeric Brun821bb9b2017-06-15 16:37:39 +0200353#endif
354
William Lallemand150bfa82019-09-19 17:12:49 +0200355__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200356
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100357/* Uncommitted CKCH transaction */
358
359static struct {
360 struct ckch_store *new_ckchs;
361 struct ckch_store *old_ckchs;
362 char *path;
363} ckchs_transaction;
364
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200365/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100366 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200367 */
368struct cafile_entry {
369 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200370 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200371 struct ebmb_node node;
372 char path[0];
373};
374
375static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
376
377static X509_STORE* ssl_store_get0_locations_file(char *path)
378{
379 struct ebmb_node *eb;
380
381 eb = ebst_lookup(&cafile_tree, path);
382 if (eb) {
383 struct cafile_entry *ca_e;
384 ca_e = ebmb_entry(eb, struct cafile_entry, node);
385 return ca_e->ca_store;
386 }
387 return NULL;
388}
389
390static int ssl_store_load_locations_file(char *path)
391{
392 if (ssl_store_get0_locations_file(path) == NULL) {
393 struct cafile_entry *ca_e;
394 X509_STORE *store = X509_STORE_new();
395 if (X509_STORE_load_locations(store, path, NULL)) {
396 int pathlen;
397 pathlen = strlen(path);
398 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
399 if (ca_e) {
400 memcpy(ca_e->path, path, pathlen + 1);
401 ca_e->ca_store = store;
402 ebst_insert(&cafile_tree, &ca_e->node);
403 return 1;
404 }
405 }
406 X509_STORE_free(store);
407 return 0;
408 }
409 return 1;
410}
411
412/* mimic what X509_STORE_load_locations do with store_ctx */
413static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
414{
415 X509_STORE *store;
416 store = ssl_store_get0_locations_file(path);
417 if (store_ctx && store) {
418 int i;
419 X509_OBJECT *obj;
420 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
421 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
422 obj = sk_X509_OBJECT_value(objs, i);
423 switch (X509_OBJECT_get_type(obj)) {
424 case X509_LU_X509:
425 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
426 break;
427 case X509_LU_CRL:
428 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
429 break;
430 default:
431 break;
432 }
433 }
434 return 1;
435 }
436 return 0;
437}
438
439/* SSL_CTX_load_verify_locations substitute, internaly call X509_STORE_load_locations */
440static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
441{
442 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
443 return ssl_set_cert_crl_file(store_ctx, path);
444}
445
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200446/*
447 Extract CA_list from CA_file already in tree.
448 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
449 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
450*/
451static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
452{
453 struct ebmb_node *eb;
454 struct cafile_entry *ca_e;
455
456 eb = ebst_lookup(&cafile_tree, path);
457 if (!eb)
458 return NULL;
459 ca_e = ebmb_entry(eb, struct cafile_entry, node);
460
461 if (ca_e->ca_list == NULL) {
462 int i;
463 unsigned long key;
464 struct eb_root ca_name_tree = EB_ROOT;
465 struct eb64_node *node, *back;
466 struct {
467 struct eb64_node node;
468 X509_NAME *xname;
469 } *ca_name;
470 STACK_OF(X509_OBJECT) *objs;
471 STACK_OF(X509_NAME) *skn;
472 X509 *x;
473 X509_NAME *xn;
474
475 skn = sk_X509_NAME_new_null();
476 /* take x509 from cafile_tree */
477 objs = X509_STORE_get0_objects(ca_e->ca_store);
478 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
479 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
480 if (!x)
481 continue;
482 xn = X509_get_subject_name(x);
483 if (!xn)
484 continue;
485 /* Check for duplicates. */
486 key = X509_NAME_hash(xn);
487 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
488 node && ca_name == NULL;
489 node = eb64_next(node)) {
490 ca_name = container_of(node, typeof(*ca_name), node);
491 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
492 ca_name = NULL;
493 }
494 /* find a duplicate */
495 if (ca_name)
496 continue;
497 ca_name = calloc(1, sizeof *ca_name);
498 xn = X509_NAME_dup(xn);
499 if (!ca_name ||
500 !xn ||
501 !sk_X509_NAME_push(skn, xn)) {
502 free(ca_name);
503 X509_NAME_free(xn);
504 sk_X509_NAME_pop_free(skn, X509_NAME_free);
505 sk_X509_NAME_free(skn);
506 skn = NULL;
507 break;
508 }
509 ca_name->node.key = key;
510 ca_name->xname = xn;
511 eb64_insert(&ca_name_tree, &ca_name->node);
512 }
513 ca_e->ca_list = skn;
514 /* remove temporary ca_name tree */
515 node = eb64_first(&ca_name_tree);
516 while (node) {
517 ca_name = container_of(node, typeof(*ca_name), node);
518 back = eb64_next(node);
519 eb64_delete(node);
520 free(ca_name);
521 node = back;
522 }
523 }
524 return ca_e->ca_list;
525}
526
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100527/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100528struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100529 unsigned long long int xxh64;
530 unsigned char ciphersuite_len;
531 char ciphersuite[0];
532};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100533struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100534static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200535static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100536
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200537#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
538struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
539#endif
540
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200541#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000542static unsigned int openssl_engines_initialized;
543struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
544struct ssl_engine_list {
545 struct list list;
546 ENGINE *e;
547};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200548#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000549
Remi Gacogne8de54152014-07-15 11:36:40 +0200550#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200551static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200552static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200553static DH *local_dh_1024 = NULL;
554static DH *local_dh_2048 = NULL;
555static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100556static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200557#endif /* OPENSSL_NO_DH */
558
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100559#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200560/* X509V3 Extensions that will be added on generated certificates */
561#define X509V3_EXT_SIZE 5
562static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
563 "basicConstraints",
564 "nsComment",
565 "subjectKeyIdentifier",
566 "authorityKeyIdentifier",
567 "keyUsage",
568};
569static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
570 "CA:FALSE",
571 "\"OpenSSL Generated Certificate\"",
572 "hash",
573 "keyid,issuer:always",
574 "nonRepudiation,digitalSignature,keyEncipherment"
575};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200576/* LRU cache to store generated certificate */
577static struct lru64_head *ssl_ctx_lru_tree = NULL;
578static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200579static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100580__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200581
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200582#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
583
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100584static struct ssl_bind_kw ssl_bind_kws[];
585
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200586#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500587/* The order here matters for picking a default context,
588 * keep the most common keytype at the bottom of the list
589 */
590const char *SSL_SOCK_KEYTYPE_NAMES[] = {
591 "dsa",
592 "ecdsa",
593 "rsa"
594};
595#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100596#else
597#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500598#endif
599
William Lallemandc3cd35f2017-11-28 11:04:43 +0100600static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100601static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
602
603#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
604
605#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
606 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
607
608#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
609 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200610
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100611/*
612 * This function gives the detail of the SSL error. It is used only
613 * if the debug mode and the verbose mode are activated. It dump all
614 * the SSL error until the stack was empty.
615 */
616static forceinline void ssl_sock_dump_errors(struct connection *conn)
617{
618 unsigned long ret;
619
620 if (unlikely(global.mode & MODE_DEBUG)) {
621 while(1) {
622 ret = ERR_get_error();
623 if (ret == 0)
624 return;
625 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200626 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100627 ERR_func_error_string(ret), ERR_reason_error_string(ret));
628 }
629 }
630}
631
yanbzhube2774d2015-12-10 15:07:30 -0500632
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200633#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000634static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
635{
636 int err_code = ERR_ABORT;
637 ENGINE *engine;
638 struct ssl_engine_list *el;
639
640 /* grab the structural reference to the engine */
641 engine = ENGINE_by_id(engine_id);
642 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100643 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000644 goto fail_get;
645 }
646
647 if (!ENGINE_init(engine)) {
648 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100649 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000650 goto fail_init;
651 }
652
653 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100654 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000655 goto fail_set_method;
656 }
657
658 el = calloc(1, sizeof(*el));
659 el->e = engine;
660 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100661 nb_engines++;
662 if (global_ssl.async)
663 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000664 return 0;
665
666fail_set_method:
667 /* release the functional reference from ENGINE_init() */
668 ENGINE_finish(engine);
669
670fail_init:
671 /* release the structural reference from ENGINE_by_id() */
672 ENGINE_free(engine);
673
674fail_get:
675 return err_code;
676}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200677#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000678
Willy Tarreau5db847a2019-05-09 14:13:35 +0200679#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200680/*
681 * openssl async fd handler
682 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200683void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000684{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200685 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000686
Emeric Brun3854e012017-05-17 20:42:48 +0200687 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000688 * to poll this fd until it is requested
689 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000690 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000691 fd_cant_recv(fd);
692
693 /* crypto engine is available, let's notify the associated
694 * connection that it can pursue its processing.
695 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200696 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000697}
698
Emeric Brun3854e012017-05-17 20:42:48 +0200699/*
700 * openssl async delayed SSL_free handler
701 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200702void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000703{
704 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200705 OSSL_ASYNC_FD all_fd[32];
706 size_t num_all_fds = 0;
707 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000708
Emeric Brun3854e012017-05-17 20:42:48 +0200709 /* We suppose that the async job for a same SSL *
710 * are serialized. So if we are awake it is
711 * because the running job has just finished
712 * and we can remove all async fds safely
713 */
714 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
715 if (num_all_fds > 32) {
716 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
717 return;
718 }
719
720 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
721 for (i=0 ; i < num_all_fds ; i++)
722 fd_remove(all_fd[i]);
723
724 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000725 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100726 _HA_ATOMIC_SUB(&sslconns, 1);
727 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000728}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000729/*
Emeric Brun3854e012017-05-17 20:42:48 +0200730 * function used to manage a returned SSL_ERROR_WANT_ASYNC
731 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000732 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200733static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000734{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100735 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200736 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200737 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000738 size_t num_add_fds = 0;
739 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200740 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000741
742 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
743 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200744 if (num_add_fds > 32 || num_del_fds > 32) {
745 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 +0000746 return;
747 }
748
Emeric Brun3854e012017-05-17 20:42:48 +0200749 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000750
Emeric Brun3854e012017-05-17 20:42:48 +0200751 /* We remove unused fds from the fdtab */
752 for (i=0 ; i < num_del_fds ; i++)
753 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000754
Emeric Brun3854e012017-05-17 20:42:48 +0200755 /* We add new fds to the fdtab */
756 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200757 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000758 }
759
Emeric Brun3854e012017-05-17 20:42:48 +0200760 num_add_fds = 0;
761 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
762 if (num_add_fds > 32) {
763 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
764 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000765 }
Emeric Brun3854e012017-05-17 20:42:48 +0200766
767 /* We activate the polling for all known async fds */
768 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000769 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200770 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000771 /* To ensure that the fd cache won't be used
772 * We'll prefer to catch a real RD event
773 * because handling an EAGAIN on this fd will
774 * result in a context switch and also
775 * some engines uses a fd in blocking mode.
776 */
777 fd_cant_recv(add_fd[i]);
778 }
Emeric Brun3854e012017-05-17 20:42:48 +0200779
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000780}
781#endif
782
William Lallemand104a7a62019-10-14 14:14:59 +0200783#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200784/*
785 * This function returns the number of seconds elapsed
786 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
787 * date presented un ASN1_GENERALIZEDTIME.
788 *
789 * In parsing error case, it returns -1.
790 */
791static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
792{
793 long epoch;
794 char *p, *end;
795 const unsigned short month_offset[12] = {
796 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
797 };
798 int year, month;
799
800 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
801
802 p = (char *)d->data;
803 end = p + d->length;
804
805 if (end - p < 4) return -1;
806 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
807 p += 4;
808 if (end - p < 2) return -1;
809 month = 10 * (p[0] - '0') + p[1] - '0';
810 if (month < 1 || month > 12) return -1;
811 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
812 We consider leap years and the current month (<marsh or not) */
813 epoch = ( ((year - 1970) * 365)
814 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
815 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
816 + month_offset[month-1]
817 ) * 24 * 60 * 60;
818 p += 2;
819 if (end - p < 2) return -1;
820 /* Add the number of seconds of completed days of current month */
821 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
822 p += 2;
823 if (end - p < 2) return -1;
824 /* Add the completed hours of the current day */
825 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
826 p += 2;
827 if (end - p < 2) return -1;
828 /* Add the completed minutes of the current hour */
829 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
830 p += 2;
831 if (p == end) return -1;
832 /* Test if there is available seconds */
833 if (p[0] < '0' || p[0] > '9')
834 goto nosec;
835 if (end - p < 2) return -1;
836 /* Add the seconds of the current minute */
837 epoch += 10 * (p[0] - '0') + p[1] - '0';
838 p += 2;
839 if (p == end) return -1;
840 /* Ignore seconds float part if present */
841 if (p[0] == '.') {
842 do {
843 if (++p == end) return -1;
844 } while (p[0] >= '0' && p[0] <= '9');
845 }
846
847nosec:
848 if (p[0] == 'Z') {
849 if (end - p != 1) return -1;
850 return epoch;
851 }
852 else if (p[0] == '+') {
853 if (end - p != 5) return -1;
854 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700855 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 +0200856 }
857 else if (p[0] == '-') {
858 if (end - p != 5) return -1;
859 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700860 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 +0200861 }
862
863 return -1;
864}
865
William Lallemand104a7a62019-10-14 14:14:59 +0200866/*
867 * struct alignment works here such that the key.key is the same as key_data
868 * Do not change the placement of key_data
869 */
870struct certificate_ocsp {
871 struct ebmb_node key;
872 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
873 struct buffer response;
874 long expire;
875};
876
877struct ocsp_cbk_arg {
878 int is_single;
879 int single_kt;
880 union {
881 struct certificate_ocsp *s_ocsp;
882 /*
883 * m_ocsp will have multiple entries dependent on key type
884 * Entry 0 - DSA
885 * Entry 1 - ECDSA
886 * Entry 2 - RSA
887 */
888 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
889 };
890};
891
Emeric Brun1d3865b2014-06-20 15:37:32 +0200892static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200893
894/* This function starts to check if the OCSP response (in DER format) contained
895 * in chunk 'ocsp_response' is valid (else exits on error).
896 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
897 * contained in the OCSP Response and exits on error if no match.
898 * If it's a valid OCSP Response:
899 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
900 * pointed by 'ocsp'.
901 * If 'ocsp' is NULL, the function looks up into the OCSP response's
902 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
903 * from the response) and exits on error if not found. Finally, If an OCSP response is
904 * already present in the container, it will be overwritten.
905 *
906 * Note: OCSP response containing more than one OCSP Single response is not
907 * considered valid.
908 *
909 * Returns 0 on success, 1 in error case.
910 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200911static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
912 struct certificate_ocsp *ocsp,
913 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200914{
915 OCSP_RESPONSE *resp;
916 OCSP_BASICRESP *bs = NULL;
917 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200918 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200919 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200920 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200921 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200922 int reason;
923 int ret = 1;
924
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200925 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
926 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200927 if (!resp) {
928 memprintf(err, "Unable to parse OCSP response");
929 goto out;
930 }
931
932 rc = OCSP_response_status(resp);
933 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
934 memprintf(err, "OCSP response status not successful");
935 goto out;
936 }
937
938 bs = OCSP_response_get1_basic(resp);
939 if (!bs) {
940 memprintf(err, "Failed to get basic response from OCSP Response");
941 goto out;
942 }
943
944 count_sr = OCSP_resp_count(bs);
945 if (count_sr > 1) {
946 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
947 goto out;
948 }
949
950 sr = OCSP_resp_get0(bs, 0);
951 if (!sr) {
952 memprintf(err, "Failed to get OCSP single response");
953 goto out;
954 }
955
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200956 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
957
Emeric Brun4147b2e2014-06-16 18:36:30 +0200958 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200959 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200960 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200961 goto out;
962 }
963
Emeric Brun13a6b482014-06-20 15:44:34 +0200964 if (!nextupd) {
965 memprintf(err, "OCSP single response: missing nextupdate");
966 goto out;
967 }
968
Emeric Brunc8b27b62014-06-19 14:16:17 +0200969 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200970 if (!rc) {
971 memprintf(err, "OCSP single response: no longer valid.");
972 goto out;
973 }
974
975 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200976 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200977 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
978 goto out;
979 }
980 }
981
982 if (!ocsp) {
983 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
984 unsigned char *p;
985
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200986 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200987 if (!rc) {
988 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
989 goto out;
990 }
991
992 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
993 memprintf(err, "OCSP single response: Certificate ID too long");
994 goto out;
995 }
996
997 p = key;
998 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200999 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001000 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
1001 if (!ocsp) {
1002 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
1003 goto out;
1004 }
1005 }
1006
1007 /* According to comments on "chunk_dup", the
1008 previous chunk buffer will be freed */
1009 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1010 memprintf(err, "OCSP response: Memory allocation error");
1011 goto out;
1012 }
1013
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001014 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1015
Emeric Brun4147b2e2014-06-16 18:36:30 +02001016 ret = 0;
1017out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001018 ERR_clear_error();
1019
Emeric Brun4147b2e2014-06-16 18:36:30 +02001020 if (bs)
1021 OCSP_BASICRESP_free(bs);
1022
1023 if (resp)
1024 OCSP_RESPONSE_free(resp);
1025
1026 return ret;
1027}
1028/*
1029 * External function use to update the OCSP response in the OCSP response's
1030 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1031 * to update in DER format.
1032 *
1033 * Returns 0 on success, 1 in error case.
1034 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001035int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001036{
1037 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1038}
1039
William Lallemand4a660132019-10-14 14:51:41 +02001040#endif
1041
1042#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001043/*
1044 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +02001045 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +02001046 *
1047 * Returns 0 on success, 1 in error case.
1048 */
William Lallemand3b5f3602019-10-16 18:05:05 +02001049static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001050{
1051 int fd = -1;
1052 int r = 0;
1053 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +02001054 struct buffer *ocsp_response;
1055 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001056
William Lallemand3b5f3602019-10-16 18:05:05 +02001057 if (buf) {
1058 int i, j;
1059 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001060
William Lallemand3b5f3602019-10-16 18:05:05 +02001061 /* remove \r and \n from the payload */
1062 for (i = 0, j = 0; buf[i]; i++) {
1063 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +02001064 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +02001065 buf[j++] = buf[i];
1066 }
1067 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001068
William Lallemand3b5f3602019-10-16 18:05:05 +02001069 ret = base64dec(buf, j, trash.area, trash.size);
1070 if (ret < 0) {
1071 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +02001072 goto end;
1073 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001074 trash.data = ret;
1075 src = &trash;
1076 } else {
1077 fd = open(ocsp_path, O_RDONLY);
1078 if (fd == -1) {
1079 memprintf(err, "Error opening OCSP response file");
1080 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001081 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001082
1083 trash.data = 0;
1084 while (trash.data < trash.size) {
1085 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1086 if (r < 0) {
1087 if (errno == EINTR)
1088 continue;
1089
1090 memprintf(err, "Error reading OCSP response from file");
1091 goto end;
1092 }
1093 else if (r == 0) {
1094 break;
1095 }
1096 trash.data += r;
1097 }
1098 close(fd);
1099 fd = -1;
1100 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001101 }
1102
William Lallemand3b5f3602019-10-16 18:05:05 +02001103 ocsp_response = calloc(1, sizeof(*ocsp_response));
1104 if (!chunk_dup(ocsp_response, src)) {
1105 free(ocsp_response);
1106 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001107 goto end;
1108 }
Emmanuel Hocdet0667fae2020-01-16 14:41:36 +01001109 /* no error, fill ckch with new context, old context must be free */
1110 if (ckch->ocsp_response) {
1111 free(ckch->ocsp_response->area);
1112 ckch->ocsp_response->area = NULL;
1113 free(ckch->ocsp_response);
1114 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001115 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001116 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001117end:
1118 if (fd != -1)
1119 close(fd);
1120
1121 return ret;
1122}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001123#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001124
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001125#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1126static 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)
1127{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001128 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001129 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001130 struct connection *conn;
1131 int head;
1132 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001133 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001134
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001135 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001136 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001137 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1138
1139 keys = ref->tlskeys;
1140 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001141
1142 if (enc) {
1143 memcpy(key_name, keys[head].name, 16);
1144
1145 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001146 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001147
Emeric Brun9e754772019-01-10 17:51:55 +01001148 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001149
Emeric Brun9e754772019-01-10 17:51:55 +01001150 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1151 goto end;
1152
Willy Tarreau9356dac2019-05-10 09:22:53 +02001153 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001154 ret = 1;
1155 }
1156 else if (ref->key_size_bits == 256 ) {
1157
1158 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1159 goto end;
1160
Willy Tarreau9356dac2019-05-10 09:22:53 +02001161 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001162 ret = 1;
1163 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001164 } else {
1165 for (i = 0; i < TLS_TICKETS_NO; i++) {
1166 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1167 goto found;
1168 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001169 ret = 0;
1170 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001171
Christopher Faulet16f45c82018-02-16 11:23:49 +01001172 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001173 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001174 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 +01001175 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1176 goto end;
1177 /* 2 for key renewal, 1 if current key is still valid */
1178 ret = i ? 2 : 1;
1179 }
1180 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001181 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 +01001182 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1183 goto end;
1184 /* 2 for key renewal, 1 if current key is still valid */
1185 ret = i ? 2 : 1;
1186 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001187 }
Emeric Brun9e754772019-01-10 17:51:55 +01001188
Christopher Faulet16f45c82018-02-16 11:23:49 +01001189 end:
1190 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1191 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001192}
1193
1194struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1195{
1196 struct tls_keys_ref *ref;
1197
1198 list_for_each_entry(ref, &tlskeys_reference, list)
1199 if (ref->filename && strcmp(filename, ref->filename) == 0)
1200 return ref;
1201 return NULL;
1202}
1203
1204struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1205{
1206 struct tls_keys_ref *ref;
1207
1208 list_for_each_entry(ref, &tlskeys_reference, list)
1209 if (ref->unique_id == unique_id)
1210 return ref;
1211 return NULL;
1212}
1213
Emeric Brun9e754772019-01-10 17:51:55 +01001214/* Update the key into ref: if keysize doesnt
1215 * match existing ones, this function returns -1
1216 * else it returns 0 on success.
1217 */
1218int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001219 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001220{
Emeric Brun9e754772019-01-10 17:51:55 +01001221 if (ref->key_size_bits == 128) {
1222 if (tlskey->data != sizeof(struct tls_sess_key_128))
1223 return -1;
1224 }
1225 else if (ref->key_size_bits == 256) {
1226 if (tlskey->data != sizeof(struct tls_sess_key_256))
1227 return -1;
1228 }
1229 else
1230 return -1;
1231
Christopher Faulet16f45c82018-02-16 11:23:49 +01001232 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001233 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1234 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001235 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1236 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001237
1238 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001239}
1240
Willy Tarreau83061a82018-07-13 11:56:34 +02001241int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001242{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001243 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1244
1245 if(!ref) {
1246 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1247 return 1;
1248 }
Emeric Brun9e754772019-01-10 17:51:55 +01001249 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1250 memprintf(err, "Invalid key size");
1251 return 1;
1252 }
1253
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001254 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001255}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001256
1257/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001258 * automatic ids. It's called just after the basic checks. It returns
1259 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001260 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001261static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001262{
1263 int i = 0;
1264 struct tls_keys_ref *ref, *ref2, *ref3;
1265 struct list tkr = LIST_HEAD_INIT(tkr);
1266
1267 list_for_each_entry(ref, &tlskeys_reference, list) {
1268 if (ref->unique_id == -1) {
1269 /* Look for the first free id. */
1270 while (1) {
1271 list_for_each_entry(ref2, &tlskeys_reference, list) {
1272 if (ref2->unique_id == i) {
1273 i++;
1274 break;
1275 }
1276 }
1277 if (&ref2->list == &tlskeys_reference)
1278 break;
1279 }
1280
1281 /* Uses the unique id and increment it for the next entry. */
1282 ref->unique_id = i;
1283 i++;
1284 }
1285 }
1286
1287 /* This sort the reference list by id. */
1288 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1289 LIST_DEL(&ref->list);
1290 list_for_each_entry(ref3, &tkr, list) {
1291 if (ref->unique_id < ref3->unique_id) {
1292 LIST_ADDQ(&ref3->list, &ref->list);
1293 break;
1294 }
1295 }
1296 if (&ref3->list == &tkr)
1297 LIST_ADDQ(&tkr, &ref->list);
1298 }
1299
1300 /* swap root */
1301 LIST_ADD(&tkr, &tlskeys_reference);
1302 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001303 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001304}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001305#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1306
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001307#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001308int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1309{
1310 switch (evp_keytype) {
1311 case EVP_PKEY_RSA:
1312 return 2;
1313 case EVP_PKEY_DSA:
1314 return 0;
1315 case EVP_PKEY_EC:
1316 return 1;
1317 }
1318
1319 return -1;
1320}
1321
Emeric Brun4147b2e2014-06-16 18:36:30 +02001322/*
1323 * Callback used to set OCSP status extension content in server hello.
1324 */
1325int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1326{
yanbzhube2774d2015-12-10 15:07:30 -05001327 struct certificate_ocsp *ocsp;
1328 struct ocsp_cbk_arg *ocsp_arg;
1329 char *ssl_buf;
1330 EVP_PKEY *ssl_pkey;
1331 int key_type;
1332 int index;
1333
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001334 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001335
1336 ssl_pkey = SSL_get_privatekey(ssl);
1337 if (!ssl_pkey)
1338 return SSL_TLSEXT_ERR_NOACK;
1339
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001340 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001341
1342 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1343 ocsp = ocsp_arg->s_ocsp;
1344 else {
1345 /* For multiple certs per context, we have to find the correct OCSP response based on
1346 * the certificate type
1347 */
1348 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1349
1350 if (index < 0)
1351 return SSL_TLSEXT_ERR_NOACK;
1352
1353 ocsp = ocsp_arg->m_ocsp[index];
1354
1355 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001356
1357 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001358 !ocsp->response.area ||
1359 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001360 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001361 return SSL_TLSEXT_ERR_NOACK;
1362
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001363 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001364 if (!ssl_buf)
1365 return SSL_TLSEXT_ERR_NOACK;
1366
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001367 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1368 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001369
1370 return SSL_TLSEXT_ERR_OK;
1371}
1372
William Lallemand4a660132019-10-14 14:51:41 +02001373#endif
1374
1375#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001376/*
1377 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001378 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1379 * status extension, the issuer's certificate is mandatory. It should be
1380 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001381 *
William Lallemand246c0242019-10-11 08:59:13 +02001382 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1383 * OCSP response. If file is empty or content is not a valid OCSP response,
1384 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1385 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001386 *
1387 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001388 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001389 */
William Lallemand4a660132019-10-14 14:51:41 +02001390#ifndef OPENSSL_IS_BORINGSSL
William Lallemand246c0242019-10-11 08:59:13 +02001391static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001392{
William Lallemand246c0242019-10-11 08:59:13 +02001393 X509 *x = NULL, *issuer = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001394 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001395 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001396 struct certificate_ocsp *ocsp = NULL, *iocsp;
1397 char *warn = NULL;
1398 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001399 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001400
Emeric Brun4147b2e2014-06-16 18:36:30 +02001401
William Lallemand246c0242019-10-11 08:59:13 +02001402 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001403 if (!x)
1404 goto out;
1405
William Lallemand246c0242019-10-11 08:59:13 +02001406 issuer = ckch->ocsp_issuer;
1407 if (!issuer)
1408 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001409
1410 cid = OCSP_cert_to_id(0, x, issuer);
1411 if (!cid)
1412 goto out;
1413
1414 i = i2d_OCSP_CERTID(cid, NULL);
1415 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1416 goto out;
1417
Vincent Bernat02779b62016-04-03 13:48:43 +02001418 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001419 if (!ocsp)
1420 goto out;
1421
1422 p = ocsp->key_data;
1423 i2d_OCSP_CERTID(cid, &p);
1424
1425 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1426 if (iocsp == ocsp)
1427 ocsp = NULL;
1428
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001429#ifndef SSL_CTX_get_tlsext_status_cb
1430# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1431 *cb = (void (*) (void))ctx->tlsext_status_cb;
1432#endif
1433 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1434
1435 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001436 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001437 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001438
1439 cb_arg->is_single = 1;
1440 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001441
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001442 pkey = X509_get_pubkey(x);
1443 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1444 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001445
1446 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1447 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1448 } else {
1449 /*
1450 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1451 * Update that cb_arg with the new cert's staple
1452 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001453 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001454 struct certificate_ocsp *tmp_ocsp;
1455 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001456 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001457 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001458
1459#ifdef SSL_CTX_get_tlsext_status_arg
1460 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1461#else
1462 cb_arg = ctx->tlsext_status_arg;
1463#endif
yanbzhube2774d2015-12-10 15:07:30 -05001464
1465 /*
1466 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1467 * the order of operations below matter, take care when changing it
1468 */
1469 tmp_ocsp = cb_arg->s_ocsp;
1470 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1471 cb_arg->s_ocsp = NULL;
1472 cb_arg->m_ocsp[index] = tmp_ocsp;
1473 cb_arg->is_single = 0;
1474 cb_arg->single_kt = 0;
1475
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001476 pkey = X509_get_pubkey(x);
1477 key_type = EVP_PKEY_base_id(pkey);
1478 EVP_PKEY_free(pkey);
1479
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001480 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001481 if (index >= 0 && !cb_arg->m_ocsp[index])
1482 cb_arg->m_ocsp[index] = iocsp;
1483
1484 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001485
1486 ret = 0;
1487
1488 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001489 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001490 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001491 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001492 }
1493
1494out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001495 if (cid)
1496 OCSP_CERTID_free(cid);
1497
1498 if (ocsp)
1499 free(ocsp);
1500
1501 if (warn)
1502 free(warn);
1503
Emeric Brun4147b2e2014-06-16 18:36:30 +02001504 return ret;
1505}
William Lallemand4a660132019-10-14 14:51:41 +02001506#else /* OPENSSL_IS_BORINGSSL */
1507static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001508{
William Lallemand4a660132019-10-14 14:51:41 +02001509 return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)ckch->ocsp_response->area, ckch->ocsp_response->data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001510}
1511#endif
1512
William Lallemand4a660132019-10-14 14:51:41 +02001513#endif
1514
1515
Willy Tarreau5db847a2019-05-09 14:13:35 +02001516#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001517
1518#define CT_EXTENSION_TYPE 18
1519
1520static int sctl_ex_index = -1;
1521
1522/*
1523 * Try to parse Signed Certificate Timestamp List structure. This function
1524 * makes only basic test if the data seems like SCTL. No signature validation
1525 * is performed.
1526 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001527static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001528{
1529 int ret = 1;
1530 int len, pos, sct_len;
1531 unsigned char *data;
1532
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001533 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001534 goto out;
1535
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001536 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001537 len = (data[0] << 8) | data[1];
1538
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001539 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001540 goto out;
1541
1542 data = data + 2;
1543 pos = 0;
1544 while (pos < len) {
1545 if (len - pos < 2)
1546 goto out;
1547
1548 sct_len = (data[pos] << 8) | data[pos + 1];
1549 if (pos + sct_len + 2 > len)
1550 goto out;
1551
1552 pos += sct_len + 2;
1553 }
1554
1555 ret = 0;
1556
1557out:
1558 return ret;
1559}
1560
William Lallemand0dfae6c2019-10-16 18:06:58 +02001561/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1562 * It fills the ckch->sctl buffer
1563 * return 0 on success or != 0 on failure */
1564static int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001565{
1566 int fd = -1;
1567 int r = 0;
1568 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001569 struct buffer tmp;
1570 struct buffer *src;
1571 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001572
William Lallemand0dfae6c2019-10-16 18:06:58 +02001573 if (buf) {
1574 tmp.area = buf;
1575 tmp.data = strlen(buf);
1576 tmp.size = tmp.data + 1;
1577 src = &tmp;
1578 } else {
1579 fd = open(sctl_path, O_RDONLY);
1580 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001581 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001582
1583 trash.data = 0;
1584 while (trash.data < trash.size) {
1585 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1586 if (r < 0) {
1587 if (errno == EINTR)
1588 continue;
1589 goto end;
1590 }
1591 else if (r == 0) {
1592 break;
1593 }
1594 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001595 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001596 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001597 }
1598
William Lallemand0dfae6c2019-10-16 18:06:58 +02001599 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001600 if (ret)
1601 goto end;
1602
William Lallemand0dfae6c2019-10-16 18:06:58 +02001603 sctl = calloc(1, sizeof(*sctl));
1604 if (!chunk_dup(sctl, src)) {
1605 free(sctl);
1606 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001607 goto end;
1608 }
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001609 /* no error, fill ckch with new context, old context must be free */
1610 if (ckch->sctl) {
1611 free(ckch->sctl->area);
1612 ckch->sctl->area = NULL;
1613 free(ckch->sctl);
1614 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001615 ckch->sctl = sctl;
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001616 ret = 0;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001617end:
1618 if (fd != -1)
1619 close(fd);
1620
1621 return ret;
1622}
1623
1624int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1625{
Willy Tarreau83061a82018-07-13 11:56:34 +02001626 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001627
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001628 *out = (unsigned char *) sctl->area;
1629 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001630
1631 return 1;
1632}
1633
1634int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1635{
1636 return 1;
1637}
1638
William Lallemanda17f4112019-10-10 15:16:44 +02001639static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001640{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001641 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001642
William Lallemanda17f4112019-10-10 15:16:44 +02001643 if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL))
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001644 goto out;
1645
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001646 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1647
1648 ret = 0;
1649
1650out:
1651 return ret;
1652}
1653
1654#endif
1655
Emeric Brune1f38db2012-09-03 20:36:47 +02001656void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1657{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001658 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001659 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001660 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001661 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001662
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001663#ifndef SSL_OP_NO_RENEGOTIATION
1664 /* Please note that BoringSSL defines this macro to zero so don't
1665 * change this to #if and do not assign a default value to this macro!
1666 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001667 if (where & SSL_CB_HANDSHAKE_START) {
1668 /* Disable renegotiation (CVE-2009-3555) */
Olivier Houchard90084a12017-11-23 18:21:29 +01001669 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 +02001670 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001671 conn->err_code = CO_ER_SSL_RENEG;
1672 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001673 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001674#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001675
1676 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001677 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001678 /* Long certificate chains optimz
1679 If write and read bios are differents, we
1680 consider that the buffering was activated,
1681 so we rise the output buffer size from 4k
1682 to 16k */
1683 write_bio = SSL_get_wbio(ssl);
1684 if (write_bio != SSL_get_rbio(ssl)) {
1685 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001686 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001687 }
1688 }
1689 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001690}
1691
Emeric Brune64aef12012-09-21 13:15:06 +02001692/* Callback is called for each certificate of the chain during a verify
1693 ok is set to 1 if preverify detect no error on current certificate.
1694 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001695int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001696{
1697 SSL *ssl;
1698 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001699 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001700 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001701
1702 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001703 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001704
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001705 ctx = conn->xprt_ctx;
1706
1707 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001708
Emeric Brun81c00f02012-09-21 14:31:21 +02001709 if (ok) /* no errors */
1710 return ok;
1711
1712 depth = X509_STORE_CTX_get_error_depth(x_store);
1713 err = X509_STORE_CTX_get_error(x_store);
1714
1715 /* check if CA error needs to be ignored */
1716 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001717 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1718 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1719 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001720 }
1721
Willy Tarreau07d94e42018-09-20 10:57:52 +02001722 if (__objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001723 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001724 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001725 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001726 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001727
Willy Tarreau20879a02012-12-03 16:32:10 +01001728 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001729 return 0;
1730 }
1731
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001732 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1733 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001734
Emeric Brun81c00f02012-09-21 14:31:21 +02001735 /* check if certificate error needs to be ignored */
Willy Tarreau07d94e42018-09-20 10:57:52 +02001736 if (__objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001737 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001738 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001739 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001740 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001741
Willy Tarreau20879a02012-12-03 16:32:10 +01001742 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001743 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001744}
1745
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001746static inline
1747void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001748 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001749{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001750 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001751 unsigned char *msg;
1752 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001753 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001754
1755 /* This function is called for "from client" and "to server"
1756 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001757 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001758 */
1759
1760 /* "write_p" is set to 0 is the bytes are received messages,
1761 * otherwise it is set to 1.
1762 */
1763 if (write_p != 0)
1764 return;
1765
1766 /* content_type contains the type of message received or sent
1767 * according with the SSL/TLS protocol spec. This message is
1768 * encoded with one byte. The value 256 (two bytes) is used
1769 * for designing the SSL/TLS record layer. According with the
1770 * rfc6101, the expected message (other than 256) are:
1771 * - change_cipher_spec(20)
1772 * - alert(21)
1773 * - handshake(22)
1774 * - application_data(23)
1775 * - (255)
1776 * We are interessed by the handshake and specially the client
1777 * hello.
1778 */
1779 if (content_type != 22)
1780 return;
1781
1782 /* The message length is at least 4 bytes, containing the
1783 * message type and the message length.
1784 */
1785 if (len < 4)
1786 return;
1787
1788 /* First byte of the handshake message id the type of
1789 * message. The konwn types are:
1790 * - hello_request(0)
1791 * - client_hello(1)
1792 * - server_hello(2)
1793 * - certificate(11)
1794 * - server_key_exchange (12)
1795 * - certificate_request(13)
1796 * - server_hello_done(14)
1797 * We are interested by the client hello.
1798 */
1799 msg = (unsigned char *)buf;
1800 if (msg[0] != 1)
1801 return;
1802
1803 /* Next three bytes are the length of the message. The total length
1804 * must be this decoded length + 4. If the length given as argument
1805 * is not the same, we abort the protocol dissector.
1806 */
1807 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1808 if (len < rec_len + 4)
1809 return;
1810 msg += 4;
1811 end = msg + rec_len;
1812 if (end < msg)
1813 return;
1814
1815 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1816 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001817 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1818 */
1819 msg += 1 + 1 + 4 + 28;
1820 if (msg > end)
1821 return;
1822
1823 /* Next, is session id:
1824 * if present, we have to jump by length + 1 for the size information
1825 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001826 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001827 if (msg[0] > 0)
1828 msg += msg[0];
1829 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001830 if (msg > end)
1831 return;
1832
1833 /* Next two bytes are the ciphersuite length. */
1834 if (msg + 2 > end)
1835 return;
1836 rec_len = (msg[0] << 8) + msg[1];
1837 msg += 2;
1838 if (msg + rec_len > end || msg + rec_len < msg)
1839 return;
1840
Willy Tarreaubafbe012017-11-24 17:34:44 +01001841 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001842 if (!capture)
1843 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001844 /* Compute the xxh64 of the ciphersuite. */
1845 capture->xxh64 = XXH64(msg, rec_len, 0);
1846
1847 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001848 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1849 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001850 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001851
1852 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001853}
1854
Emeric Brun29f037d2014-04-25 19:05:36 +02001855/* Callback is called for ssl protocol analyse */
1856void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1857{
Emeric Brun29f037d2014-04-25 19:05:36 +02001858#ifdef TLS1_RT_HEARTBEAT
1859 /* test heartbeat received (write_p is set to 0
1860 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001861 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001862 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001863 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001864 const unsigned char *p = buf;
1865 unsigned int payload;
1866
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001867 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001868
1869 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1870 if (*p != TLS1_HB_REQUEST)
1871 return;
1872
Willy Tarreauaeed6722014-04-25 23:59:58 +02001873 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001874 goto kill_it;
1875
1876 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001877 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001878 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001879 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001880 /* We have a clear heartbleed attack (CVE-2014-0160), the
1881 * advertised payload is larger than the advertised packet
1882 * length, so we have garbage in the buffer between the
1883 * payload and the end of the buffer (p+len). We can't know
1884 * if the SSL stack is patched, and we don't know if we can
1885 * safely wipe out the area between p+3+len and payload.
1886 * So instead, we prevent the response from being sent by
1887 * setting the max_send_fragment to 0 and we report an SSL
1888 * error, which will kill this connection. It will be reported
1889 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001890 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1891 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001892 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001893 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1894 return;
1895 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001896#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001897 if (global_ssl.capture_cipherlist > 0)
1898 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001899}
1900
Bernard Spil13c53f82018-02-15 13:34:58 +01001901#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001902static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1903 const unsigned char *in, unsigned int inlen,
1904 void *arg)
1905{
1906 struct server *srv = arg;
1907
1908 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1909 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1910 return SSL_TLSEXT_ERR_OK;
1911 return SSL_TLSEXT_ERR_NOACK;
1912}
1913#endif
1914
1915#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001916/* This callback is used so that the server advertises the list of
1917 * negociable protocols for NPN.
1918 */
1919static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1920 unsigned int *len, void *arg)
1921{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001922 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001923
1924 *data = (const unsigned char *)conf->npn_str;
1925 *len = conf->npn_len;
1926 return SSL_TLSEXT_ERR_OK;
1927}
1928#endif
1929
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001930#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001931/* This callback is used so that the server advertises the list of
1932 * negociable protocols for ALPN.
1933 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001934static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1935 unsigned char *outlen,
1936 const unsigned char *server,
1937 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001938{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001939 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001940
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001941 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1942 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1943 return SSL_TLSEXT_ERR_NOACK;
1944 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001945 return SSL_TLSEXT_ERR_OK;
1946}
1947#endif
1948
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001949#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001950#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001951
Christopher Faulet30548802015-06-11 13:39:32 +02001952/* Create a X509 certificate with the specified servername and serial. This
1953 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001954static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001955ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001956{
Christopher Faulet7969a332015-10-09 11:15:03 +02001957 X509 *cacert = bind_conf->ca_sign_cert;
1958 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001959 SSL_CTX *ssl_ctx = NULL;
1960 X509 *newcrt = NULL;
1961 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001962 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001963 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001964 X509_NAME *name;
1965 const EVP_MD *digest;
1966 X509V3_CTX ctx;
1967 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001968 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001969
Christopher Faulet48a83322017-07-28 16:56:09 +02001970 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001971#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001972 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1973#else
1974 tmp_ssl = SSL_new(bind_conf->default_ctx);
1975 if (tmp_ssl)
1976 pkey = SSL_get_privatekey(tmp_ssl);
1977#endif
1978 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001979 goto mkcert_error;
1980
1981 /* Create the certificate */
1982 if (!(newcrt = X509_new()))
1983 goto mkcert_error;
1984
1985 /* Set version number for the certificate (X509v3) and the serial
1986 * number */
1987 if (X509_set_version(newcrt, 2L) != 1)
1988 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001989 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001990
1991 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001992 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1993 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001994 goto mkcert_error;
1995
1996 /* set public key in the certificate */
1997 if (X509_set_pubkey(newcrt, pkey) != 1)
1998 goto mkcert_error;
1999
2000 /* Set issuer name from the CA */
2001 if (!(name = X509_get_subject_name(cacert)))
2002 goto mkcert_error;
2003 if (X509_set_issuer_name(newcrt, name) != 1)
2004 goto mkcert_error;
2005
2006 /* Set the subject name using the same, but the CN */
2007 name = X509_NAME_dup(name);
2008 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2009 (const unsigned char *)servername,
2010 -1, -1, 0) != 1) {
2011 X509_NAME_free(name);
2012 goto mkcert_error;
2013 }
2014 if (X509_set_subject_name(newcrt, name) != 1) {
2015 X509_NAME_free(name);
2016 goto mkcert_error;
2017 }
2018 X509_NAME_free(name);
2019
2020 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002021 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002022 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2023 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2024 X509_EXTENSION *ext;
2025
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002026 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002027 goto mkcert_error;
2028 if (!X509_add_ext(newcrt, ext, -1)) {
2029 X509_EXTENSION_free(ext);
2030 goto mkcert_error;
2031 }
2032 X509_EXTENSION_free(ext);
2033 }
2034
2035 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002036
2037 key_type = EVP_PKEY_base_id(capkey);
2038
2039 if (key_type == EVP_PKEY_DSA)
2040 digest = EVP_sha1();
2041 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002042 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002043 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002044 digest = EVP_sha256();
2045 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002046#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002047 int nid;
2048
2049 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2050 goto mkcert_error;
2051 if (!(digest = EVP_get_digestbynid(nid)))
2052 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002053#else
2054 goto mkcert_error;
2055#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002056 }
2057
Christopher Faulet31af49d2015-06-09 17:29:50 +02002058 if (!(X509_sign(newcrt, capkey, digest)))
2059 goto mkcert_error;
2060
2061 /* Create and set the new SSL_CTX */
2062 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2063 goto mkcert_error;
2064 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2065 goto mkcert_error;
2066 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2067 goto mkcert_error;
2068 if (!SSL_CTX_check_private_key(ssl_ctx))
2069 goto mkcert_error;
2070
2071 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002072
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002073#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002074 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002075#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002076#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2077 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002078 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002079 EC_KEY *ecc;
2080 int nid;
2081
2082 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2083 goto end;
2084 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2085 goto end;
2086 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2087 EC_KEY_free(ecc);
2088 }
2089#endif
2090 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002091 return ssl_ctx;
2092
2093 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002094 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002095 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002096 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2097 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002098 return NULL;
2099}
2100
Christopher Faulet7969a332015-10-09 11:15:03 +02002101SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002102ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002103{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002104 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002105 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002106
Olivier Houchard66ab4982019-02-26 18:37:15 +01002107 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002108}
2109
Christopher Faulet30548802015-06-11 13:39:32 +02002110/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002111 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002112SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002113ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002114{
2115 struct lru64 *lru = NULL;
2116
2117 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002118 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002119 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002120 if (lru && lru->domain) {
2121 if (ssl)
2122 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002123 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002124 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002125 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002126 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002127 }
2128 return NULL;
2129}
2130
Emeric Brun821bb9b2017-06-15 16:37:39 +02002131/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2132 * function is not thread-safe, it should only be used to check if a certificate
2133 * exists in the lru cache (with no warranty it will not be removed by another
2134 * thread). It is kept for backward compatibility. */
2135SSL_CTX *
2136ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2137{
2138 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2139}
2140
Christopher Fauletd2cab922015-07-28 16:03:47 +02002141/* Set a certificate int the LRU cache used to store generated
2142 * certificate. Return 0 on success, otherwise -1 */
2143int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002144ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002145{
2146 struct lru64 *lru = NULL;
2147
2148 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002149 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002150 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002151 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002152 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002153 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002154 }
Christopher Faulet30548802015-06-11 13:39:32 +02002155 if (lru->domain && lru->data)
2156 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002157 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002158 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002159 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002160 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002161 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002162}
2163
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002164/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002165unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002166ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002167{
2168 return XXH32(data, len, ssl_ctx_lru_seed);
2169}
2170
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002171/* Generate a cert and immediately assign it to the SSL session so that the cert's
2172 * refcount is maintained regardless of the cert's presence in the LRU cache.
2173 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002174static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002175ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002176{
2177 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002178 SSL_CTX *ssl_ctx = NULL;
2179 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002180 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002181
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002182 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002183 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002184 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002185 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002186 if (lru && lru->domain)
2187 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002188 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002189 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002190 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002191 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002192 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002193 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002194 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002195 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002196 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002197 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002198 SSL_set_SSL_CTX(ssl, ssl_ctx);
2199 /* No LRU cache, this CTX will be released as soon as the session dies */
2200 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002201 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002202 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002203 return 0;
2204}
2205static int
2206ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2207{
2208 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002209 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002210
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002211 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002212 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002213 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002214 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002215 }
2216 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002217}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002218#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002219
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002220#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002221typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2222
2223static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002224{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002225#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002226 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002227 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2228#endif
2229}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002230static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2231 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002232 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2233}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002234static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002235#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002236 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002237 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2238#endif
2239}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002240static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002241#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002242 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002243 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2244#endif
2245}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002246/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002247static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2248/* Unusable in this context. */
2249static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2250static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2251static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2252static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2253static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002254#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002255typedef enum { SET_MIN, SET_MAX } set_context_func;
2256
2257static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2258 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002259 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2260}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002261static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2262 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2263 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2264}
2265static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2266 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002267 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2268}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002269static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2270 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2271 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2272}
2273static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2274 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002275 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2276}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002277static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2278 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2279 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2280}
2281static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2282 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002283 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2284}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002285static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2286 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2287 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2288}
2289static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002290#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002291 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002292 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2293#endif
2294}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002295static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2296#if SSL_OP_NO_TLSv1_3
2297 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2298 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002299#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002300}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002301#endif
2302static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2303static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002304
2305static struct {
2306 int option;
2307 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002308 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2309 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002310 const char *name;
2311} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002312 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2313 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2314 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2315 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2316 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2317 {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 +02002318};
2319
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002320static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2321{
2322 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2323 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2324 SSL_set_SSL_CTX(ssl, ctx);
2325}
2326
Willy Tarreau5db847a2019-05-09 14:13:35 +02002327#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002328
2329static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2330{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002331 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002332 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002333
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002334 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2335 return SSL_TLSEXT_ERR_OK;
2336 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002337}
2338
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002339#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002340static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2341{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002342 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002343#else
2344static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2345{
2346#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002347 struct connection *conn;
2348 struct bind_conf *s;
2349 const uint8_t *extension_data;
2350 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002351 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002352
2353 char *wildp = NULL;
2354 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002355 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002356 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002357 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002358 int i;
2359
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002360 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002361 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002362
Olivier Houchard9679ac92017-10-27 14:58:08 +02002363 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002364 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002365#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002366 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2367 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002368#else
2369 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2370#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002371 /*
2372 * The server_name extension was given too much extensibility when it
2373 * was written, so parsing the normal case is a bit complex.
2374 */
2375 size_t len;
2376 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002377 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002378 /* Extract the length of the supplied list of names. */
2379 len = (*extension_data++) << 8;
2380 len |= *extension_data++;
2381 if (len + 2 != extension_len)
2382 goto abort;
2383 /*
2384 * The list in practice only has a single element, so we only consider
2385 * the first one.
2386 */
2387 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2388 goto abort;
2389 extension_len = len - 1;
2390 /* Now we can finally pull out the byte array with the actual hostname. */
2391 if (extension_len <= 2)
2392 goto abort;
2393 len = (*extension_data++) << 8;
2394 len |= *extension_data++;
2395 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2396 || memchr(extension_data, 0, len) != NULL)
2397 goto abort;
2398 servername = extension_data;
2399 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002400 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002401#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2402 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002403 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002404 }
2405#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002406 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002407 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002408 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002409 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002410 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002411 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002412 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002413 goto abort;
2414 }
2415
2416 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002417#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002418 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002419#else
2420 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2421#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002422 uint8_t sign;
2423 size_t len;
2424 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002425 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002426 len = (*extension_data++) << 8;
2427 len |= *extension_data++;
2428 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002429 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002430 if (len % 2 != 0)
2431 goto abort;
2432 for (; len > 0; len -= 2) {
2433 extension_data++; /* hash */
2434 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002435 switch (sign) {
2436 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002437 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002438 break;
2439 case TLSEXT_signature_ecdsa:
2440 has_ecdsa_sig = 1;
2441 break;
2442 default:
2443 continue;
2444 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002445 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002446 break;
2447 }
2448 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002449 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002450 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002451 }
2452 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002453 const SSL_CIPHER *cipher;
2454 size_t len;
2455 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002456 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002457#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002458 len = ctx->cipher_suites_len;
2459 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002460#else
2461 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2462#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002463 if (len % 2 != 0)
2464 goto abort;
2465 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002466#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002467 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002468 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002469#else
2470 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2471#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002472 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002473 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002474 break;
2475 }
2476 }
2477 }
2478
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002479 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002480 trash.area[i] = tolower(servername[i]);
2481 if (!wildp && (trash.area[i] == '.'))
2482 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002483 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002484 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002485
William Lallemand150bfa82019-09-19 17:12:49 +02002486 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002487
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002488 for (i = 0; i < 2; i++) {
2489 if (i == 0) /* lookup in full qualified names */
2490 node = ebst_lookup(&s->sni_ctx, trash.area);
2491 else if (i == 1 && wildp) /* lookup in wildcards names */
2492 node = ebst_lookup(&s->sni_w_ctx, wildp);
2493 else
2494 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002495 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002496 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002497 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002498 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002499 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002500 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002501 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002502 break;
2503 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002504 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002505 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002506 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002507 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002508 if (!node_anonymous)
2509 node_anonymous = n;
2510 break;
2511 }
2512 }
2513 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002514 /* select by key_signature priority order */
2515 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2516 : ((has_rsa_sig && node_rsa) ? node_rsa
2517 : (node_anonymous ? node_anonymous
2518 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2519 : node_rsa /* no rsa signature case (far far away) */
2520 )));
2521 if (node) {
2522 /* switch ctx */
2523 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2524 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002525 if (conf) {
2526 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2527 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2528 if (conf->early_data)
2529 allow_early = 1;
2530 }
William Lallemand02010472019-10-18 11:02:19 +02002531 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002532 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002533 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002534 }
William Lallemand150bfa82019-09-19 17:12:49 +02002535
William Lallemand02010472019-10-18 11:02:19 +02002536 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002537#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002538 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002539 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002540 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002541 }
2542#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002543 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002544 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002545 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002546 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002547 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002548 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002549allow_early:
2550#ifdef OPENSSL_IS_BORINGSSL
2551 if (allow_early)
2552 SSL_set_early_data_enabled(ssl, 1);
2553#else
2554 if (!allow_early)
2555 SSL_set_max_early_data(ssl, 0);
2556#endif
2557 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002558 abort:
2559 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2560 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002561#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002562 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002563#else
2564 *al = SSL_AD_UNRECOGNIZED_NAME;
2565 return 0;
2566#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002567}
2568
2569#else /* OPENSSL_IS_BORINGSSL */
2570
Emeric Brunfc0421f2012-09-07 17:30:07 +02002571/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2572 * warning when no match is found, which implies the default (first) cert
2573 * will keep being used.
2574 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002575static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002576{
2577 const char *servername;
2578 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002579 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002580 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002581 int i;
2582 (void)al; /* shut gcc stupid warning */
2583
2584 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002585 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002586#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002587 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2588 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002589#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002590 if (s->strict_sni)
2591 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002592 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002593 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002594 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002595 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002596 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002597
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002598 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002599 if (!servername[i])
2600 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002601 trash.area[i] = tolower(servername[i]);
2602 if (!wildp && (trash.area[i] == '.'))
2603 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002604 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002605 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002606
William Lallemand150bfa82019-09-19 17:12:49 +02002607 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002608 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002609 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002610 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2611 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002612 if (!container_of(n, struct sni_ctx, name)->neg) {
2613 node = n;
2614 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002615 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002616 }
2617 if (!node && wildp) {
2618 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002619 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2620 /* lookup a not neg filter */
2621 if (!container_of(n, struct sni_ctx, name)->neg) {
2622 node = n;
2623 break;
2624 }
2625 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002626 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002627 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002628#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002629 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2630 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002631 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002632 return SSL_TLSEXT_ERR_OK;
2633 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002634#endif
William Lallemand21724f02019-11-04 17:56:13 +01002635 if (s->strict_sni) {
2636 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002637 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002638 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002639 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002640 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002641 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002642 }
2643
2644 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002645 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002646 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002647 return SSL_TLSEXT_ERR_OK;
2648}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002649#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002650#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2651
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002652#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002653
2654static DH * ssl_get_dh_1024(void)
2655{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002656 static unsigned char dh1024_p[]={
2657 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2658 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2659 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2660 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2661 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2662 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2663 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2664 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2665 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2666 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2667 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2668 };
2669 static unsigned char dh1024_g[]={
2670 0x02,
2671 };
2672
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002673 BIGNUM *p;
2674 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002675 DH *dh = DH_new();
2676 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002677 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2678 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002679
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002680 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002681 DH_free(dh);
2682 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002683 } else {
2684 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002685 }
2686 }
2687 return dh;
2688}
2689
2690static DH *ssl_get_dh_2048(void)
2691{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002692 static unsigned char dh2048_p[]={
2693 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2694 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2695 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2696 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2697 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2698 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2699 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2700 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2701 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2702 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2703 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2704 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2705 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2706 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2707 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2708 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2709 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2710 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2711 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2712 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2713 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2714 0xB7,0x1F,0x77,0xF3,
2715 };
2716 static unsigned char dh2048_g[]={
2717 0x02,
2718 };
2719
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002720 BIGNUM *p;
2721 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002722 DH *dh = DH_new();
2723 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002724 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2725 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002726
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002727 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002728 DH_free(dh);
2729 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002730 } else {
2731 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002732 }
2733 }
2734 return dh;
2735}
2736
2737static DH *ssl_get_dh_4096(void)
2738{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002739 static unsigned char dh4096_p[]={
2740 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2741 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2742 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2743 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2744 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2745 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2746 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2747 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2748 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2749 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2750 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2751 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2752 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2753 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2754 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2755 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2756 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2757 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2758 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2759 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2760 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2761 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2762 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2763 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2764 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2765 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2766 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2767 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2768 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2769 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2770 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2771 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2772 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2773 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2774 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2775 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2776 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2777 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2778 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2779 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2780 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2781 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2782 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002783 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002784 static unsigned char dh4096_g[]={
2785 0x02,
2786 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002787
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002788 BIGNUM *p;
2789 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002790 DH *dh = DH_new();
2791 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002792 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2793 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002794
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002795 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002796 DH_free(dh);
2797 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002798 } else {
2799 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002800 }
2801 }
2802 return dh;
2803}
2804
2805/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002806 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002807static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2808{
2809 DH *dh = NULL;
2810 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002811 int type;
2812
2813 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002814
2815 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2816 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2817 */
2818 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2819 keylen = EVP_PKEY_bits(pkey);
2820 }
2821
Willy Tarreauef934602016-12-22 23:12:01 +01002822 if (keylen > global_ssl.default_dh_param) {
2823 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002824 }
2825
Remi Gacogned3a341a2015-05-29 16:26:17 +02002826 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002827 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002828 }
2829 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002830 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002831 }
2832 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002833 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002834 }
2835
2836 return dh;
2837}
2838
Remi Gacogne47783ef2015-05-29 15:53:22 +02002839static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002840{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002841 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002842 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002843
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002844 if (in == NULL)
2845 goto end;
2846
Remi Gacogne47783ef2015-05-29 15:53:22 +02002847 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002848 goto end;
2849
Remi Gacogne47783ef2015-05-29 15:53:22 +02002850 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2851
2852end:
2853 if (in)
2854 BIO_free(in);
2855
Emeric Brune1b4ed42018-08-16 15:14:12 +02002856 ERR_clear_error();
2857
Remi Gacogne47783ef2015-05-29 15:53:22 +02002858 return dh;
2859}
2860
2861int ssl_sock_load_global_dh_param_from_file(const char *filename)
2862{
2863 global_dh = ssl_sock_get_dh_from_file(filename);
2864
2865 if (global_dh) {
2866 return 0;
2867 }
2868
2869 return -1;
2870}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002871#endif
2872
William Lallemand9117de92019-10-04 00:29:42 +02002873/* Alloc and init a ckch_inst */
2874static struct ckch_inst *ckch_inst_new()
2875{
2876 struct ckch_inst *ckch_inst;
2877
2878 ckch_inst = calloc(1, sizeof *ckch_inst);
2879 if (ckch_inst)
2880 LIST_INIT(&ckch_inst->sni_ctx);
2881
2882 return ckch_inst;
2883}
2884
2885
2886/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002887static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002888 struct bind_conf *s, struct ssl_bind_conf *conf,
2889 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002890{
2891 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002892 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002893
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002894 if (*name == '!') {
2895 neg = 1;
2896 name++;
2897 }
2898 if (*name == '*') {
2899 wild = 1;
2900 name++;
2901 }
2902 /* !* filter is a nop */
2903 if (neg && wild)
2904 return order;
2905 if (*name) {
2906 int j, len;
2907 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002908 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002909 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002910 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002911 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002912 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002913
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002914 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002915 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002916 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002917 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002918 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002919 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002920 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002921 sc->order = order++;
2922 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002923 sc->wild = wild;
2924 sc->name.node.leaf_p = NULL;
William Lallemand1d29c742019-10-04 00:53:29 +02002925 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002926 }
2927 return order;
2928}
2929
William Lallemand6af03992019-07-23 15:00:54 +02002930/*
William Lallemand1d29c742019-10-04 00:53:29 +02002931 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2932 * This function can't return an error.
2933 *
2934 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2935 */
2936static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2937{
2938
2939 struct sni_ctx *sc0, *sc0b, *sc1;
2940 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002941 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002942
2943 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2944
2945 /* ignore if sc0 was already inserted in a tree */
2946 if (sc0->name.node.leaf_p)
2947 continue;
2948
2949 /* Check for duplicates. */
2950 if (sc0->wild)
2951 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2952 else
2953 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2954
2955 for (; node; node = ebmb_next_dup(node)) {
2956 sc1 = ebmb_entry(node, struct sni_ctx, name);
2957 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2958 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2959 /* it's a duplicate, we should remove and free it */
2960 LIST_DEL(&sc0->by_ckch_inst);
2961 free(sc0);
2962 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002963 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002964 }
2965 }
2966
2967 /* if duplicate, ignore the insertion */
2968 if (!sc0)
2969 continue;
2970
2971 if (sc0->wild)
2972 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
2973 else
2974 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01002975
2976 /* replace the default_ctx if required with the first ctx */
2977 if (ckch_inst->is_default && !def) {
2978 /* we don't need to free the default_ctx because the refcount was not incremented */
2979 bind_conf->default_ctx = sc0->ctx;
2980 def = 1;
2981 }
William Lallemand1d29c742019-10-04 00:53:29 +02002982 }
2983}
2984
2985/*
William Lallemande3af8fb2019-10-08 11:36:53 +02002986 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02002987 */
William Lallemande3af8fb2019-10-08 11:36:53 +02002988struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02002989
William Lallemandfa892222019-07-23 16:06:08 +02002990
Emeric Brun7a883362019-10-17 13:27:40 +02002991/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
2992 * If there is no DH paramater availaible in the ckchs, the global
2993 * DH parameter is loaded into the SSL_CTX and if there is no
2994 * DH parameter available in ckchs nor in global, the default
2995 * DH parameters are applied on the SSL_CTX.
2996 * Returns a bitfield containing the flags:
2997 * ERR_FATAL in any fatal error case
2998 * ERR_ALERT if a reason of the error is availabine in err
2999 * ERR_WARN if a warning is available into err
3000 * The value 0 means there is no error nor warning and
3001 * the operation succeed.
3002 */
William Lallemandfa892222019-07-23 16:06:08 +02003003#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003004static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3005 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003006{
Emeric Brun7a883362019-10-17 13:27:40 +02003007 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003008 DH *dh = NULL;
3009
William Lallemanda8c73742019-07-31 18:31:34 +02003010 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003011 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003012 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3013 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3014 err && *err ? *err : "", path);
3015#if defined(SSL_CTX_set_dh_auto)
3016 SSL_CTX_set_dh_auto(ctx, 1);
3017 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3018 err && *err ? *err : "");
3019#else
3020 memprintf(err, "%s, DH ciphers won't be available.\n",
3021 err && *err ? *err : "");
3022#endif
3023 ret |= ERR_WARN;
3024 goto end;
3025 }
William Lallemandfa892222019-07-23 16:06:08 +02003026
3027 if (ssl_dh_ptr_index >= 0) {
3028 /* store a pointer to the DH params to avoid complaining about
3029 ssl-default-dh-param not being set for this SSL_CTX */
3030 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3031 }
3032 }
3033 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003034 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3035 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3036 err && *err ? *err : "", path);
3037#if defined(SSL_CTX_set_dh_auto)
3038 SSL_CTX_set_dh_auto(ctx, 1);
3039 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3040 err && *err ? *err : "");
3041#else
3042 memprintf(err, "%s, DH ciphers won't be available.\n",
3043 err && *err ? *err : "");
3044#endif
3045 ret |= ERR_WARN;
3046 goto end;
3047 }
William Lallemandfa892222019-07-23 16:06:08 +02003048 }
3049 else {
3050 /* Clear openssl global errors stack */
3051 ERR_clear_error();
3052
3053 if (global_ssl.default_dh_param <= 1024) {
3054 /* we are limited to DH parameter of 1024 bits anyway */
3055 if (local_dh_1024 == NULL)
3056 local_dh_1024 = ssl_get_dh_1024();
3057
Emeric Brun7a883362019-10-17 13:27:40 +02003058 if (local_dh_1024 == NULL) {
3059 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3060 err && *err ? *err : "", path);
3061 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003062 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003063 }
William Lallemandfa892222019-07-23 16:06:08 +02003064
Emeric Bruna9363eb2019-10-17 14:53:03 +02003065 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3066 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3067 err && *err ? *err : "", path);
3068#if defined(SSL_CTX_set_dh_auto)
3069 SSL_CTX_set_dh_auto(ctx, 1);
3070 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3071 err && *err ? *err : "");
3072#else
3073 memprintf(err, "%s, DH ciphers won't be available.\n",
3074 err && *err ? *err : "");
3075#endif
3076 ret |= ERR_WARN;
3077 goto end;
3078 }
William Lallemandfa892222019-07-23 16:06:08 +02003079 }
3080 else {
3081 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3082 }
William Lallemandfa892222019-07-23 16:06:08 +02003083 }
3084
3085end:
William Lallemandfa892222019-07-23 16:06:08 +02003086 return ret;
3087}
3088#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003089
yanbzhu488a4d22015-12-01 15:16:07 -05003090/* Frees the contents of a cert_key_and_chain
3091 */
3092static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3093{
yanbzhu488a4d22015-12-01 15:16:07 -05003094 if (!ckch)
3095 return;
3096
3097 /* Free the certificate and set pointer to NULL */
3098 if (ckch->cert)
3099 X509_free(ckch->cert);
3100 ckch->cert = NULL;
3101
3102 /* Free the key and set pointer to NULL */
3103 if (ckch->key)
3104 EVP_PKEY_free(ckch->key);
3105 ckch->key = NULL;
3106
3107 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003108 if (ckch->chain)
3109 sk_X509_pop_free(ckch->chain, X509_free);
3110 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003111
William Lallemand455af502019-10-17 18:04:45 +02003112 if (ckch->dh)
3113 DH_free(ckch->dh);
3114 ckch->dh = NULL;
3115
3116 if (ckch->sctl) {
3117 free(ckch->sctl->area);
3118 ckch->sctl->area = NULL;
3119 free(ckch->sctl);
3120 ckch->sctl = NULL;
3121 }
3122
3123 if (ckch->ocsp_response) {
3124 free(ckch->ocsp_response->area);
3125 ckch->ocsp_response->area = NULL;
3126 free(ckch->ocsp_response);
3127 ckch->ocsp_response = NULL;
3128 }
yanbzhu488a4d22015-12-01 15:16:07 -05003129}
3130
William Lallemand8d0f8932019-10-17 18:03:58 +02003131/*
3132 *
3133 * This function copy a cert_key_and_chain in memory
3134 *
3135 * It's used to try to apply changes on a ckch before committing them, because
3136 * most of the time it's not possible to revert those changes
3137 *
3138 * Return a the dst or NULL
3139 */
3140static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3141 struct cert_key_and_chain *dst)
3142{
3143 if (src->cert) {
3144 dst->cert = src->cert;
3145 X509_up_ref(src->cert);
3146 }
3147
3148 if (src->key) {
3149 dst->key = src->key;
3150 EVP_PKEY_up_ref(src->key);
3151 }
3152
3153 if (src->chain) {
3154 dst->chain = X509_chain_up_ref(src->chain);
3155 }
3156
3157 if (src->dh) {
3158 DH_up_ref(src->dh);
3159 dst->dh = src->dh;
3160 }
3161
3162 if (src->sctl) {
3163 struct buffer *sctl;
3164
3165 sctl = calloc(1, sizeof(*sctl));
3166 if (!chunk_dup(sctl, src->sctl)) {
3167 free(sctl);
3168 sctl = NULL;
3169 goto error;
3170 }
3171 dst->sctl = sctl;
3172 }
3173
3174 if (src->ocsp_response) {
3175 struct buffer *ocsp_response;
3176
3177 ocsp_response = calloc(1, sizeof(*ocsp_response));
3178 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3179 free(ocsp_response);
3180 ocsp_response = NULL;
3181 goto error;
3182 }
3183 dst->ocsp_response = ocsp_response;
3184 }
3185
3186 if (src->ocsp_issuer) {
3187 X509_up_ref(src->ocsp_issuer);
3188 dst->ocsp_issuer = src->ocsp_issuer;
3189 }
3190
3191 return dst;
3192
3193error:
3194
3195 /* free everything */
3196 ssl_sock_free_cert_key_and_chain_contents(dst);
3197
3198 return NULL;
3199}
3200
3201
yanbzhu488a4d22015-12-01 15:16:07 -05003202/* checks if a key and cert exists in the ckch
3203 */
William Lallemand1633e392019-09-30 12:58:13 +02003204#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003205static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3206{
3207 return (ckch->cert != NULL && ckch->key != NULL);
3208}
William Lallemand1633e392019-09-30 12:58:13 +02003209#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003210
William Lallemandf9568fc2019-10-16 18:27:58 +02003211/*
3212 * return 0 on success or != 0 on failure
3213 */
3214static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3215{
3216 int ret = 1;
3217 BIO *in = NULL;
3218 X509 *issuer;
3219
3220 if (buf) {
3221 /* reading from a buffer */
3222 in = BIO_new_mem_buf(buf, -1);
3223 if (in == NULL) {
3224 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3225 goto end;
3226 }
3227
3228 } else {
3229 /* reading from a file */
3230 in = BIO_new(BIO_s_file());
3231 if (in == NULL)
3232 goto end;
3233
3234 if (BIO_read_filename(in, path) <= 0)
3235 goto end;
3236 }
3237
3238 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3239 if (!issuer) {
3240 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003241 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003242 goto end;
3243 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003244 /* no error, fill ckch with new context, old context must be free */
3245 if (ckch->ocsp_issuer)
3246 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003247 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003248 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003249
3250end:
3251
3252 ERR_clear_error();
3253 if (in)
3254 BIO_free(in);
3255
3256 return ret;
3257}
3258
William Lallemand96a9c972019-10-17 11:56:17 +02003259
3260/*
3261 * Try to load a PEM file from a <path> or a buffer <buf>
3262 * The PEM must contain at least a Private Key and a Certificate,
3263 * It could contain a DH and a certificate chain.
yanbzhu488a4d22015-12-01 15:16:07 -05003264 *
William Lallemand96a9c972019-10-17 11:56:17 +02003265 * If it failed you should not attempt to use the ckch but free it.
3266 *
3267 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003268 */
William Lallemand96a9c972019-10-17 11:56:17 +02003269static int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
yanbzhu488a4d22015-12-01 15:16:07 -05003270{
William Lallemandf11365b2019-09-19 14:25:58 +02003271 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003272 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003273 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003274 X509 *cert = NULL;
3275 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003276 DH *dh = NULL;
3277 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003278
3279 if (buf) {
3280 /* reading from a buffer */
3281 in = BIO_new_mem_buf(buf, -1);
3282 if (in == NULL) {
3283 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3284 goto end;
3285 }
yanbzhu488a4d22015-12-01 15:16:07 -05003286
William Lallemand96a9c972019-10-17 11:56:17 +02003287 } else {
3288 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003289 in = BIO_new(BIO_s_file());
3290 if (in == NULL)
3291 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003292
William Lallemandf11365b2019-09-19 14:25:58 +02003293 if (BIO_read_filename(in, path) <= 0)
3294 goto end;
William Lallemandf11365b2019-09-19 14:25:58 +02003295 }
yanbzhu488a4d22015-12-01 15:16:07 -05003296
yanbzhu488a4d22015-12-01 15:16:07 -05003297 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003298 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3299 if (key == NULL) {
yanbzhu488a4d22015-12-01 15:16:07 -05003300 memprintf(err, "%sunable to load private key from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003301 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003302 goto end;
3303 }
3304
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003305#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003306 /* Seek back to beginning of file */
3307 if (BIO_reset(in) == -1) {
3308 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3309 err && *err ? *err : "", path);
3310 goto end;
3311 }
3312
William Lallemand96a9c972019-10-17 11:56:17 +02003313 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3314 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003315#endif
William Lallemandfa892222019-07-23 16:06:08 +02003316
Willy Tarreaubb137a82016-04-06 19:02:38 +02003317 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003318 if (BIO_reset(in) == -1) {
3319 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3320 err && *err ? *err : "", path);
3321 goto end;
3322 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003323
3324 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003325 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3326 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003327 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003328 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003329 goto end;
3330 }
3331
William Lallemand96a9c972019-10-17 11:56:17 +02003332 if (!X509_check_private_key(cert, key)) {
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003333 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003334 err && *err ? *err : "", path);
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003335 goto end;
3336 }
3337
William Lallemand96a9c972019-10-17 11:56:17 +02003338 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003339 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3340 if (chain == NULL)
3341 chain = sk_X509_new_null();
3342 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003343 X509_free(ca);
3344 goto end;
3345 }
3346 }
yanbzhu488a4d22015-12-01 15:16:07 -05003347
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003348 /* no chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003349 if (chain == NULL) {
3350 chain = sk_X509_new_null();
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003351 }
3352
yanbzhu488a4d22015-12-01 15:16:07 -05003353 ret = ERR_get_error();
3354 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3355 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003356 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003357 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003358 }
3359
William Lallemand75b15f72020-01-23 10:56:05 +01003360 /* once it loaded the PEM, it should remove everything else in the ckch */
3361 if (ckch->ocsp_response) {
3362 free(ckch->ocsp_response->area);
3363 ckch->ocsp_response->area = NULL;
3364 free(ckch->ocsp_response);
3365 ckch->ocsp_response = NULL;
3366 }
3367
3368 if (ckch->sctl) {
3369 free(ckch->sctl->area);
3370 ckch->sctl->area = NULL;
3371 free(ckch->sctl);
3372 ckch->sctl = NULL;
3373 }
3374
3375 if (ckch->ocsp_issuer) {
3376 X509_free(ckch->ocsp_issuer);
3377 ckch->ocsp_issuer = NULL;
3378 }
3379
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003380 /* no error, fill ckch with new context, old context will be free at end: */
3381 SWAP(ckch->key, key);
3382 SWAP(ckch->dh, dh);
3383 SWAP(ckch->cert, cert);
3384 SWAP(ckch->chain, chain);
3385
William Lallemand246c0242019-10-11 08:59:13 +02003386 ret = 0;
3387
William Lallemand96a9c972019-10-17 11:56:17 +02003388end:
William Lallemand246c0242019-10-11 08:59:13 +02003389
3390 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003391 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003392 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003393 if (key)
3394 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003395 if (dh)
3396 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003397 if (cert)
3398 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003399 if (chain)
3400 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003401
William Lallemand96a9c972019-10-17 11:56:17 +02003402 return ret;
3403}
3404
3405/*
3406 * Try to load in a ckch every files related to a ckch.
3407 * (PEM, sctl, ocsp, issuer etc.)
3408 *
3409 * This function is only used to load files during the configuration parsing,
3410 * it is not used with the CLI.
3411 *
3412 * This allows us to carry the contents of the file without having to read the
3413 * file multiple times. The caller must call
3414 * ssl_sock_free_cert_key_and_chain_contents.
3415 *
3416 * returns:
3417 * 0 on Success
3418 * 1 on SSL Failure
3419 */
3420static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3421{
3422 int ret = 1;
3423
3424 /* try to load the PEM */
3425 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3426 goto end;
3427 }
3428
William Lallemanda17f4112019-10-10 15:16:44 +02003429#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3430 /* try to load the sctl file */
3431 {
3432 char fp[MAXPATHLEN+1];
3433 struct stat st;
3434
3435 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3436 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003437 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003438 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003439 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003440 ret = 1;
3441 goto end;
3442 }
3443 }
3444 }
3445#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003446
William Lallemand246c0242019-10-11 08:59:13 +02003447 /* try to load an ocsp response file */
3448 {
3449 char fp[MAXPATHLEN+1];
3450 struct stat st;
3451
3452 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3453 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003454 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003455 ret = 1;
3456 goto end;
3457 }
3458 }
3459 }
3460
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003461#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand246c0242019-10-11 08:59:13 +02003462 if (ckch->ocsp_response) {
3463 X509 *issuer;
3464 int i;
3465
3466 /* check if one of the certificate of the chain is the issuer */
3467 for (i = 0; i < sk_X509_num(ckch->chain); i++) {
3468 issuer = sk_X509_value(ckch->chain, i);
3469 if (X509_check_issued(issuer, ckch->cert) == X509_V_OK) {
3470 ckch->ocsp_issuer = issuer;
3471 break;
3472 } else
3473 issuer = NULL;
3474 }
3475
3476 /* if no issuer was found, try to load an issuer from the .issuer */
3477 if (!issuer) {
3478 struct stat st;
3479 char fp[MAXPATHLEN+1];
3480
3481 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3482 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003483 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003484 ret = 1;
3485 goto end;
3486 }
3487
3488 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003489 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003490 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003491 ret = 1;
3492 goto end;
3493 }
3494 } else {
3495 memprintf(err, "%sNo issuer found, cannot use the OCSP response'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003496 err && *err ? *err : "");
William Lallemand246c0242019-10-11 08:59:13 +02003497 ret = 1;
3498 goto end;
3499 }
3500 }
3501 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003502#endif
William Lallemand246c0242019-10-11 08:59:13 +02003503
yanbzhu488a4d22015-12-01 15:16:07 -05003504 ret = 0;
3505
3506end:
3507
3508 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003509
3510 /* Something went wrong in one of the reads */
3511 if (ret != 0)
3512 ssl_sock_free_cert_key_and_chain_contents(ckch);
3513
3514 return ret;
3515}
3516
3517/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003518 * Returns a bitfield containing the flags:
3519 * ERR_FATAL in any fatal error case
3520 * ERR_ALERT if the reason of the error is available in err
3521 * ERR_WARN if a warning is available into err
3522 * The value 0 means there is no error nor warning and
3523 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003524 */
3525static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3526{
Emeric Bruna96b5822019-10-17 13:25:14 +02003527 int errcode = 0;
3528
yanbzhu488a4d22015-12-01 15:16:07 -05003529 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3530 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3531 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003532 errcode |= ERR_ALERT | ERR_FATAL;
3533 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003534 }
3535
3536 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3537 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3538 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003539 errcode |= ERR_ALERT | ERR_FATAL;
3540 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003541 }
3542
yanbzhu488a4d22015-12-01 15:16:07 -05003543 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003544#ifdef SSL_CTX_set1_chain
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003545 if (!SSL_CTX_set1_chain(ctx, ckch->chain)) {
3546 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3547 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003548 errcode |= ERR_ALERT | ERR_FATAL;
3549 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003550 }
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003551#else
3552 { /* legacy compat (< openssl 1.0.2) */
3553 X509 *ca;
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003554 STACK_OF(X509) *chain;
3555 chain = X509_chain_up_ref(ckch->chain);
3556 while ((ca = sk_X509_shift(chain)))
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003557 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3558 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3559 err && *err ? *err : "", path);
3560 X509_free(ca);
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003561 sk_X509_pop_free(chain, X509_free);
Emeric Bruna96b5822019-10-17 13:25:14 +02003562 errcode |= ERR_ALERT | ERR_FATAL;
3563 goto end;
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003564 }
3565 }
3566#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003567
William Lallemandfa892222019-07-23 16:06:08 +02003568#ifndef OPENSSL_NO_DH
3569 /* store a NULL pointer to indicate we have not yet loaded
3570 a custom DH param file */
3571 if (ssl_dh_ptr_index >= 0) {
3572 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3573 }
3574
Emeric Brun7a883362019-10-17 13:27:40 +02003575 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3576 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003577 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3578 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003579 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003580 }
3581#endif
3582
William Lallemanda17f4112019-10-10 15:16:44 +02003583#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3584 if (sctl_ex_index >= 0 && ckch->sctl) {
3585 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3586 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003587 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003588 errcode |= ERR_ALERT | ERR_FATAL;
3589 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003590 }
3591 }
3592#endif
3593
William Lallemand4a660132019-10-14 14:51:41 +02003594#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003595 /* Load OCSP Info into context */
3596 if (ckch->ocsp_response) {
3597 if (ssl_sock_load_ocsp(ctx, ckch) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003598 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",
3599 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003600 errcode |= ERR_ALERT | ERR_FATAL;
3601 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003602 }
3603 }
William Lallemand246c0242019-10-11 08:59:13 +02003604#endif
3605
Emeric Bruna96b5822019-10-17 13:25:14 +02003606 end:
3607 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003608}
3609
William Lallemandc4ecddf2019-07-31 16:50:08 +02003610#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003611
William Lallemand28a8fce2019-10-04 17:36:55 +02003612static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003613{
3614 struct sni_keytype *s_kt = NULL;
3615 struct ebmb_node *node;
3616 int i;
3617
3618 for (i = 0; i < trash.size; i++) {
3619 if (!str[i])
3620 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003621 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003622 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003623 trash.area[i] = 0;
3624 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003625 if (!node) {
3626 /* CN not found in tree */
3627 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3628 /* Using memcpy here instead of strncpy.
3629 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3630 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3631 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003632 if (!s_kt)
3633 return -1;
3634
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003635 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003636 s_kt->keytypes = 0;
3637 ebst_insert(sni_keytypes, &s_kt->name);
3638 } else {
3639 /* CN found in tree */
3640 s_kt = container_of(node, struct sni_keytype, name);
3641 }
3642
3643 /* Mark that this CN has the keytype of key_index via keytypes mask */
3644 s_kt->keytypes |= 1<<key_index;
3645
William Lallemand28a8fce2019-10-04 17:36:55 +02003646 return 0;
3647
yanbzhu08ce6ab2015-12-02 13:01:29 -05003648}
3649
William Lallemandc4ecddf2019-07-31 16:50:08 +02003650#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003651/*
3652 * Free a ckch_store and its ckch(s)
3653 * The linked ckch_inst are not free'd
3654 */
3655void ckchs_free(struct ckch_store *ckchs)
3656{
3657 if (!ckchs)
3658 return;
3659
3660#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3661 if (ckchs->multi) {
3662 int n;
3663
3664 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3665 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
3666 } else
3667#endif
3668 {
3669 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
3670 ckchs->ckch = NULL;
3671 }
3672
3673 free(ckchs);
3674}
3675
3676/* allocate and duplicate a ckch_store
3677 * Return a new ckch_store or NULL */
3678static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3679{
3680 struct ckch_store *dst;
3681 int pathlen;
3682
3683 pathlen = strlen(src->path);
3684 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3685 if (!dst)
3686 return NULL;
3687 /* copy previous key */
3688 memcpy(dst->path, src->path, pathlen + 1);
3689 dst->multi = src->multi;
3690 LIST_INIT(&dst->ckch_inst);
3691
3692 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3693 if (!dst->ckch)
3694 goto error;
3695
3696#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3697 if (src->multi) {
3698 int n;
3699
3700 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3701 if (&src->ckch[n]) {
3702 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3703 goto error;
3704 }
3705 }
3706 } else
3707#endif
3708 {
3709 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3710 goto error;
3711 }
3712
3713 return dst;
3714
3715error:
3716 ckchs_free(dst);
3717
3718 return NULL;
3719}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003720
William Lallemand36b84632019-07-18 19:28:17 +02003721/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003722 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003723 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003724static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003725{
3726 struct ebmb_node *eb;
3727
William Lallemande3af8fb2019-10-08 11:36:53 +02003728 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003729 if (!eb)
3730 return NULL;
3731
William Lallemande3af8fb2019-10-08 11:36:53 +02003732 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003733}
3734
3735/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003736 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003737 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003738static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003739{
William Lallemande3af8fb2019-10-08 11:36:53 +02003740 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003741
William Lallemande3af8fb2019-10-08 11:36:53 +02003742 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3743 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003744 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3745 goto end;
3746 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003747 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003748
William Lallemande3af8fb2019-10-08 11:36:53 +02003749 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003750 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3751 goto end;
3752 }
3753
William Lallemand9117de92019-10-04 00:29:42 +02003754 LIST_INIT(&ckchs->ckch_inst);
3755
William Lallemand36b84632019-07-18 19:28:17 +02003756 if (!multi) {
3757
William Lallemand96a9c972019-10-17 11:56:17 +02003758 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003759 goto end;
3760
William Lallemande3af8fb2019-10-08 11:36:53 +02003761 /* insert into the ckchs tree */
3762 memcpy(ckchs->path, path, strlen(path) + 1);
3763 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003764 } else {
3765 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003766#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3767 char fp[MAXPATHLEN+1] = {0};
3768 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003769
3770 /* Load all possible certs and keys */
3771 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3772 struct stat buf;
3773 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3774 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003775 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003776 goto end;
3777 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003778 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003779 }
3780 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003781#endif
William Lallemand36b84632019-07-18 19:28:17 +02003782
3783 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003784 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003785 goto end;
3786 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003787 /* insert into the ckchs tree */
3788 memcpy(ckchs->path, path, strlen(path) + 1);
3789 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003790 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003791 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003792
William Lallemand36b84632019-07-18 19:28:17 +02003793end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003794 if (ckchs) {
3795 free(ckchs->ckch);
3796 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003797 }
3798
William Lallemande3af8fb2019-10-08 11:36:53 +02003799 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003800
3801 return NULL;
3802}
3803
William Lallemandc4ecddf2019-07-31 16:50:08 +02003804#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3805
William Lallemand36b84632019-07-18 19:28:17 +02003806/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003807 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003808 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003809 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3810 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003811 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003812 *
Emeric Brun054563d2019-10-17 13:16:58 +02003813 * Returns a bitfield containing the flags:
3814 * ERR_FATAL in any fatal error case
3815 * ERR_ALERT if the reason of the error is available in err
3816 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003817 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003818 */
Emeric Brun054563d2019-10-17 13:16:58 +02003819static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3820 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3821 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003822{
William Lallemand36b84632019-07-18 19:28:17 +02003823 int i = 0, n = 0;
3824 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003825 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003826 struct ebmb_node *node;
3827 struct ebmb_node *next;
3828 /* Array of SSL_CTX pointers corresponding to each possible combo
3829 * of keytypes
3830 */
3831 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003832 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003833 X509_NAME *xname = NULL;
3834 char *str = NULL;
3835#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3836 STACK_OF(GENERAL_NAME) *names = NULL;
3837#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003838 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003839
Emeric Brun054563d2019-10-17 13:16:58 +02003840 *ckchi = NULL;
3841
William Lallemande3af8fb2019-10-08 11:36:53 +02003842 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003843 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3844 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003845 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003846 }
3847
3848 ckch_inst = ckch_inst_new();
3849 if (!ckch_inst) {
3850 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3851 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003852 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003853 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003854 }
3855
William Lallemande3af8fb2019-10-08 11:36:53 +02003856 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003857
William Lallemand150bfa82019-09-19 17:12:49 +02003858 /* at least one of the instances is using filters during the config
3859 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01003860 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02003861
yanbzhu08ce6ab2015-12-02 13:01:29 -05003862 /* Process each ckch and update keytypes for each CN/SAN
3863 * for example, if CN/SAN www.a.com is associated with
3864 * certs with keytype 0 and 2, then at the end of the loop,
3865 * www.a.com will have:
3866 * keyindex = 0 | 1 | 4 = 5
3867 */
3868 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003869 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003870
3871 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3872 continue;
3873
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003874 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003875 for (i = 0; i < fcount; i++) {
3876 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3877 if (ret < 0) {
3878 memprintf(err, "%sunable to allocate SSL context.\n",
3879 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003880 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003881 goto end;
3882 }
3883 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003884 } else {
3885 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3886 * so the line that contains logic is marked via comments
3887 */
3888 xname = X509_get_subject_name(certs_and_keys[n].cert);
3889 i = -1;
3890 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3891 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003892 ASN1_STRING *value;
3893 value = X509_NAME_ENTRY_get_data(entry);
3894 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003895 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003896 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003897
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003898 OPENSSL_free(str);
3899 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003900 if (ret < 0) {
3901 memprintf(err, "%sunable to allocate SSL context.\n",
3902 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003903 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003904 goto end;
3905 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003906 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003907 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003908
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003909 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003910#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003911 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3912 if (names) {
3913 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3914 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003915
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003916 if (name->type == GEN_DNS) {
3917 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3918 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003919 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003920
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003921 OPENSSL_free(str);
3922 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003923 if (ret < 0) {
3924 memprintf(err, "%sunable to allocate SSL context.\n",
3925 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003926 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003927 goto end;
3928 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003929 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003930 }
3931 }
3932 }
3933 }
3934#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3935 }
3936
3937 /* If no files found, return error */
3938 if (eb_is_empty(&sni_keytypes_map)) {
3939 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3940 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003941 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003942 goto end;
3943 }
3944
3945 /* We now have a map of CN/SAN to keytypes that are loaded in
3946 * Iterate through the map to create the SSL_CTX's (if needed)
3947 * and add each CTX to the SNI tree
3948 *
3949 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003950 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003951 * combination is denoted by the key in the map. Each key
3952 * has a value between 1 and 2^n - 1. Conveniently, the array
3953 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3954 * entry in the array to correspond to the unique combo (key)
3955 * associated with i. This unique key combo (i) will be associated
3956 * with combos[i-1]
3957 */
3958
3959 node = ebmb_first(&sni_keytypes_map);
3960 while (node) {
3961 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003962 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003963 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003964
3965 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3966 i = container_of(node, struct sni_keytype, name)->keytypes;
3967 cur_ctx = key_combos[i-1].ctx;
3968
3969 if (cur_ctx == NULL) {
3970 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003971 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003972 if (cur_ctx == NULL) {
3973 memprintf(err, "%sunable to allocate SSL context.\n",
3974 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003975 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003976 goto end;
3977 }
3978
yanbzhube2774d2015-12-10 15:07:30 -05003979 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003980 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3981 if (i & (1<<n)) {
3982 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003983 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02003984 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3985 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003986 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003987 }
3988 }
3989
yanbzhu08ce6ab2015-12-02 13:01:29 -05003990 /* Update key_combos */
3991 key_combos[i-1].ctx = cur_ctx;
3992 }
3993
3994 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02003995
William Lallemand1d29c742019-10-04 00:53:29 +02003996 key_combos[i-1].order = ckch_inst_add_cert_sni(cur_ctx, ckch_inst, bind_conf, ssl_conf,
William Lallemandfe49bb32019-10-03 23:46:33 +02003997 kinfo, str, key_combos[i-1].order);
3998 if (key_combos[i-1].order < 0) {
3999 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004000 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004001 goto end;
4002 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004003 node = ebmb_next(node);
4004 }
4005
4006
4007 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4008 if (!bind_conf->default_ctx) {
4009 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4010 if (key_combos[i].ctx) {
4011 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004012 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004013 ckch_inst->is_default = 1;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004014 break;
4015 }
4016 }
4017 }
4018
William Lallemand614ca0d2019-10-07 13:52:11 +02004019 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004020 ckch_inst->ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004021end:
4022
4023 if (names)
4024 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4025
yanbzhu08ce6ab2015-12-02 13:01:29 -05004026 node = ebmb_first(&sni_keytypes_map);
4027 while (node) {
4028 next = ebmb_next(node);
4029 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004030 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004031 node = next;
4032 }
4033
Emeric Brun054563d2019-10-17 13:16:58 +02004034 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02004035 struct sni_ctx *sc0, *sc0b;
4036
4037 /* free the SSL_CTX in case of error */
4038 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4039 if (key_combos[i].ctx)
4040 SSL_CTX_free(key_combos[i].ctx);
4041 }
4042
4043 /* free the sni_ctx in case of error */
4044 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4045
4046 ebmb_delete(&sc0->name);
4047 LIST_DEL(&sc0->by_ckch_inst);
4048 free(sc0);
4049 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004050 free(ckch_inst);
4051 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004052 }
4053
Emeric Brun054563d2019-10-17 13:16:58 +02004054 *ckchi = ckch_inst;
4055 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004056}
4057#else
4058/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004059static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4060 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4061 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004062{
4063 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4064 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004065 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004066}
4067
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004068#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004069
William Lallemand614ca0d2019-10-07 13:52:11 +02004070/*
4071 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004072 *
4073 * Returns a bitfield containing the flags:
4074 * ERR_FATAL in any fatal error case
4075 * ERR_ALERT if the reason of the error is available in err
4076 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004077 */
Emeric Brun054563d2019-10-17 13:16:58 +02004078static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4079 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004080{
William Lallemandc9402072019-05-15 15:33:54 +02004081 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004082 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004083 int order = 0;
4084 X509_NAME *xname;
4085 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004086 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004087 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004088#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4089 STACK_OF(GENERAL_NAME) *names;
4090#endif
William Lallemand36b84632019-07-18 19:28:17 +02004091 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004092 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004093 int errcode = 0;
4094
4095 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004096
William Lallemande3af8fb2019-10-08 11:36:53 +02004097 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004098 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004099
William Lallemande3af8fb2019-10-08 11:36:53 +02004100 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004101
William Lallemand150bfa82019-09-19 17:12:49 +02004102 /* at least one of the instances is using filters during the config
4103 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01004104 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02004105
William Lallemandc9402072019-05-15 15:33:54 +02004106 ctx = SSL_CTX_new(SSLv23_server_method());
4107 if (!ctx) {
4108 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4109 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004110 errcode |= ERR_ALERT | ERR_FATAL;
4111 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004112 }
4113
Emeric Bruna96b5822019-10-17 13:25:14 +02004114 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4115 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004116 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004117
4118 ckch_inst = ckch_inst_new();
4119 if (!ckch_inst) {
4120 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4121 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004122 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004123 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004124 }
4125
William Lallemand36b84632019-07-18 19:28:17 +02004126 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004127 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004128 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004129 switch(EVP_PKEY_base_id(pkey)) {
4130 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004131 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004132 break;
4133 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004134 kinfo.sig = TLSEXT_signature_ecdsa;
4135 break;
4136 case EVP_PKEY_DSA:
4137 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004138 break;
4139 }
4140 EVP_PKEY_free(pkey);
4141 }
4142
Emeric Brun50bcecc2013-04-22 13:05:23 +02004143 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004144 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004145 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, sni_filter[fcount], order);
William Lallemandfe49bb32019-10-03 23:46:33 +02004146 if (order < 0) {
4147 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004148 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004149 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004150 }
4151 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004152 }
4153 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004154#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004155 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004156 if (names) {
4157 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4158 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4159 if (name->type == GEN_DNS) {
4160 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004161 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004162 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004163 if (order < 0) {
4164 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004165 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004166 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004167 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004168 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004169 }
4170 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004171 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004172 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004173#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004174 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004175 i = -1;
4176 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4177 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004178 ASN1_STRING *value;
4179
4180 value = X509_NAME_ENTRY_get_data(entry);
4181 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004182 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004183 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004184 if (order < 0) {
4185 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004186 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004187 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004188 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004189 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004190 }
4191 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004192 /* we must not free the SSL_CTX anymore below, since it's already in
4193 * the tree, so it will be discovered and cleaned in time.
4194 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004195
Emeric Brunfc0421f2012-09-07 17:30:07 +02004196#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004197 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004198 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4199 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004200 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004201 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004202 }
4203#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004204 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004205 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004206 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004207 ckch_inst->is_default = 1;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004208 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004209
William Lallemand9117de92019-10-04 00:29:42 +02004210 /* everything succeed, the ckch instance can be used */
4211 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004212 ckch_inst->ssl_conf = ssl_conf;
William Lallemand9117de92019-10-04 00:29:42 +02004213
Emeric Brun054563d2019-10-17 13:16:58 +02004214 *ckchi = ckch_inst;
4215 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004216
4217error:
4218 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004219 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004220 struct sni_ctx *sc0, *sc0b;
4221
4222 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4223
4224 ebmb_delete(&sc0->name);
4225 LIST_DEL(&sc0->by_ckch_inst);
4226 free(sc0);
4227 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004228 free(ckch_inst);
4229 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004230 }
4231 /* We only created 1 SSL_CTX so we can free it there */
4232 SSL_CTX_free(ctx);
4233
Emeric Brun054563d2019-10-17 13:16:58 +02004234 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004235}
4236
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004237/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004238static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4239 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4240 char **sni_filter, int fcount, char **err)
4241{
4242 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004243 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004244
4245 /* we found the ckchs in the tree, we can use it directly */
4246 if (ckchs->multi)
Emeric Brun054563d2019-10-17 13:16:58 +02004247 errcode |= ckch_inst_new_load_multi_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, &ckch_inst, err);
William Lallemand614ca0d2019-10-07 13:52:11 +02004248 else
Emeric Brun054563d2019-10-17 13:16:58 +02004249 errcode |= ckch_inst_new_load_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, &ckch_inst, err);
William Lallemand614ca0d2019-10-07 13:52:11 +02004250
Emeric Brun054563d2019-10-17 13:16:58 +02004251 if (errcode & ERR_CODE)
4252 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004253
4254 ssl_sock_load_cert_sni(ckch_inst, bind_conf);
4255
4256 /* succeed, add the instance to the ckch_store's list of instance */
4257 LIST_ADDQ(&ckchs->ckch_inst, &ckch_inst->by_ckchs);
Emeric Brun054563d2019-10-17 13:16:58 +02004258 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004259}
4260
4261
Willy Tarreaubbc91962019-10-16 16:42:19 +02004262/* Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau03209342016-12-22 17:08:28 +01004263int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004264{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004265 struct dirent **de_list;
4266 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004267 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004268 char *end;
4269 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004270 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004271 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004272#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004273 int is_bundle;
4274 int j;
4275#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004276 if ((ckchs = ckchs_lookup(path))) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004277 /* we found the ckchs in the tree, we can use it directly */
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004278 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand6af03992019-07-23 15:00:54 +02004279 }
4280
yanbzhu08ce6ab2015-12-02 13:01:29 -05004281 if (stat(path, &buf) == 0) {
William Dauchy9a8ef7f2020-01-13 17:52:49 +01004282 if (S_ISDIR(buf.st_mode) == 0) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004283 ckchs = ckchs_load_cert_file(path, 0, err);
4284 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004285 return ERR_ALERT | ERR_FATAL;
4286
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004287 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004288 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004289
yanbzhu08ce6ab2015-12-02 13:01:29 -05004290 /* strip trailing slashes, including first one */
4291 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
4292 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004293
yanbzhu08ce6ab2015-12-02 13:01:29 -05004294 n = scandir(path, &de_list, 0, alphasort);
4295 if (n < 0) {
4296 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4297 err && *err ? *err : "", path, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004298 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004299 }
4300 else {
4301 for (i = 0; i < n; i++) {
4302 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004303
yanbzhu08ce6ab2015-12-02 13:01:29 -05004304 end = strrchr(de->d_name, '.');
4305 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
4306 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004307
yanbzhu08ce6ab2015-12-02 13:01:29 -05004308 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4309 if (stat(fp, &buf) != 0) {
4310 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4311 err && *err ? *err : "", fp, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004312 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004313 goto ignore_entry;
4314 }
4315 if (!S_ISREG(buf.st_mode))
4316 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004317
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004318#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004319 is_bundle = 0;
4320 /* Check if current entry in directory is part of a multi-cert bundle */
4321
4322 if (end) {
4323 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4324 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4325 is_bundle = 1;
4326 break;
4327 }
4328 }
4329
4330 if (is_bundle) {
yanbzhu63ea8462015-12-09 13:35:14 -05004331 int dp_len;
4332
4333 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004334
4335 /* increment i and free de until we get to a non-bundle cert
4336 * Note here that we look at de_list[i + 1] before freeing de
Willy Tarreau05800522019-10-29 10:48:50 +01004337 * this is important since ignore_entry will free de. This also
4338 * guarantees that de->d_name continues to hold the same prefix.
yanbzhu63ea8462015-12-09 13:35:14 -05004339 */
Willy Tarreau05800522019-10-29 10:48:50 +01004340 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
yanbzhu63ea8462015-12-09 13:35:14 -05004341 free(de);
4342 i++;
4343 de = de_list[i];
4344 }
4345
Willy Tarreau05800522019-10-29 10:48:50 +01004346 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemande3af8fb2019-10-08 11:36:53 +02004347 if ((ckchs = ckchs_lookup(fp)) == NULL)
4348 ckchs = ckchs_load_cert_file(fp, 1, err);
4349 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004350 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004351 else
4352 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05004353 /* Successfully processed the bundle */
4354 goto ignore_entry;
4355 }
4356 }
4357
4358#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004359 if ((ckchs = ckchs_lookup(fp)) == NULL)
4360 ckchs = ckchs_load_cert_file(fp, 0, err);
4361 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004362 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004363 else
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004364 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004365
yanbzhu08ce6ab2015-12-02 13:01:29 -05004366ignore_entry:
4367 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004368 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004369 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004370 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004371 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004372 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004373
William Lallemande3af8fb2019-10-08 11:36:53 +02004374 ckchs = ckchs_load_cert_file(path, 1, err);
4375 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004376 return ERR_ALERT | ERR_FATAL;
4377
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004378 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004379
Emeric Brunfc0421f2012-09-07 17:30:07 +02004380 return cfgerr;
4381}
4382
Thierry Fournier383085f2013-01-24 14:15:43 +01004383/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4384 * done once. Zero is returned if the operation fails. No error is returned
4385 * if the random is said as not implemented, because we expect that openssl
4386 * will use another method once needed.
4387 */
4388static int ssl_initialize_random()
4389{
4390 unsigned char random;
4391 static int random_initialized = 0;
4392
4393 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4394 random_initialized = 1;
4395
4396 return random_initialized;
4397}
4398
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004399/* release ssl bind conf */
4400void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004401{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004402 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01004403#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004404 free(conf->npn_str);
4405 conf->npn_str = NULL;
4406#endif
4407#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4408 free(conf->alpn_str);
4409 conf->alpn_str = NULL;
4410#endif
4411 free(conf->ca_file);
4412 conf->ca_file = NULL;
4413 free(conf->crl_file);
4414 conf->crl_file = NULL;
4415 free(conf->ciphers);
4416 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004417#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004418 free(conf->ciphersuites);
4419 conf->ciphersuites = NULL;
4420#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004421 free(conf->curves);
4422 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004423 free(conf->ecdhe);
4424 conf->ecdhe = NULL;
4425 }
4426}
4427
Willy Tarreaubbc91962019-10-16 16:42:19 +02004428/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004429int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
4430{
4431 char thisline[CRT_LINESIZE];
4432 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004433 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004434 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004435 int linenum = 0;
4436 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004437 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004438
Willy Tarreauad1731d2013-04-02 17:35:58 +02004439 if ((f = fopen(file, "r")) == NULL) {
4440 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004441 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004442 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004443
4444 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004445 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004446 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004447 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004448 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004449 char *crt_path;
4450 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004451
4452 linenum++;
4453 end = line + strlen(line);
4454 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4455 /* Check if we reached the limit and the last char is not \n.
4456 * Watch out for the last line without the terminating '\n'!
4457 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004458 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4459 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004460 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004461 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004462 }
4463
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004464 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004465 newarg = 1;
4466 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004467 if (*line == '#' || *line == '\n' || *line == '\r') {
4468 /* end of string, end of loop */
4469 *line = 0;
4470 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004471 } else if (isspace(*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02004472 newarg = 1;
4473 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004474 } else if (*line == '[') {
4475 if (ssl_b) {
4476 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004477 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004478 break;
4479 }
4480 if (!arg) {
4481 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004482 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004483 break;
4484 }
4485 ssl_b = arg;
4486 newarg = 1;
4487 *line = 0;
4488 } else if (*line == ']') {
4489 if (ssl_e) {
4490 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004491 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004492 break;
4493 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004494 if (!ssl_b) {
4495 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004496 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004497 break;
4498 }
4499 ssl_e = arg;
4500 newarg = 1;
4501 *line = 0;
4502 } else if (newarg) {
4503 if (arg == MAX_CRT_ARGS) {
4504 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004505 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004506 break;
4507 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004508 newarg = 0;
4509 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004510 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004511 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004512 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004513 if (cfgerr)
4514 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004515 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004516
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004517 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004518 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004519 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004520
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004521 crt_path = args[0];
4522 if (*crt_path != '/' && global_ssl.crt_base) {
4523 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4524 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4525 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004526 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004527 break;
4528 }
4529 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4530 crt_path = path;
4531 }
4532
4533 ssl_conf = calloc(1, sizeof *ssl_conf);
4534 cur_arg = ssl_b ? ssl_b : 1;
4535 while (cur_arg < ssl_e) {
4536 newarg = 0;
4537 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4538 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4539 newarg = 1;
Willy Tarreaubbc91962019-10-16 16:42:19 +02004540 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004541 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4542 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4543 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004544 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004545 }
4546 cur_arg += 1 + ssl_bind_kws[i].skip;
4547 break;
4548 }
4549 }
4550 if (!cfgerr && !newarg) {
4551 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4552 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004553 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004554 break;
4555 }
4556 }
Willy Tarreaubbc91962019-10-16 16:42:19 +02004557
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004558 if (cfgerr) {
4559 ssl_sock_free_ssl_conf(ssl_conf);
4560 free(ssl_conf);
4561 ssl_conf = NULL;
4562 break;
4563 }
4564
William Lallemande3af8fb2019-10-08 11:36:53 +02004565 if ((ckchs = ckchs_lookup(crt_path)) == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004566 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004567 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004568 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004569 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004570 }
4571
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004572 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004573 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004574 else
4575 cfgerr |= ssl_sock_load_ckchs(crt_path, ckchs, bind_conf, ssl_conf, &args[cur_arg], arg - cur_arg - 1, err);
William Lallemandeed4bf22019-10-10 11:38:13 +02004576
Willy Tarreauad1731d2013-04-02 17:35:58 +02004577 if (cfgerr) {
4578 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004579 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004580 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004581 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004582 fclose(f);
4583 return cfgerr;
4584}
4585
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004586/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004587static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004588ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004589{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004590 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004591 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004592 SSL_OP_ALL | /* all known workarounds for bugs */
4593 SSL_OP_NO_SSLv2 |
4594 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004595 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02004596 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004597 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02004598 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004599 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004600 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004601 SSL_MODE_ENABLE_PARTIAL_WRITE |
4602 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004603 SSL_MODE_RELEASE_BUFFERS |
4604 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004605 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004606 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004607 int flags = MC_SSL_O_ALL;
4608 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004609
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004610 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004611 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004612
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004613 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004614 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
4615 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4616 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004617 else
4618 flags = conf_ssl_methods->flags;
4619
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004620 min = conf_ssl_methods->min;
4621 max = conf_ssl_methods->max;
4622 /* start with TLSv10 to remove SSLv3 per default */
4623 if (!min && (!max || max >= CONF_TLSV10))
4624 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004625 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004626 if (min)
4627 flags |= (methodVersions[min].flag - 1);
4628 if (max)
4629 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004630 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004631 min = max = CONF_TLSV_NONE;
4632 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004633 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004634 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004635 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004636 if (min) {
4637 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004638 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
4639 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4640 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
4641 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004642 hole = 0;
4643 }
4644 max = i;
4645 }
4646 else {
4647 min = max = i;
4648 }
4649 }
4650 else {
4651 if (min)
4652 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004653 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004654 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004655 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4656 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004657 cfgerr += 1;
4658 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004659 /* save real min/max in bind_conf */
4660 conf_ssl_methods->min = min;
4661 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004662
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004663#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004664 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004665 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004666 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004667 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004668 else
4669 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4670 if (flags & methodVersions[i].flag)
4671 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004672#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004673 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004674 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4675 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004676#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004677
4678 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4679 options |= SSL_OP_NO_TICKET;
4680 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4681 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004682
4683#ifdef SSL_OP_NO_RENEGOTIATION
4684 options |= SSL_OP_NO_RENEGOTIATION;
4685#endif
4686
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004687 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004688
Willy Tarreau5db847a2019-05-09 14:13:35 +02004689#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004690 if (global_ssl.async)
4691 mode |= SSL_MODE_ASYNC;
4692#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004693 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004694 if (global_ssl.life_time)
4695 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004696
4697#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4698#ifdef OPENSSL_IS_BORINGSSL
4699 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4700 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05004701#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01004702 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004703 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004704 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4705 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004706#else
4707 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004708#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004709 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004710#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004711 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004712}
4713
William Lallemand4f45bb92017-10-30 20:08:51 +01004714
4715static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4716{
4717 if (first == block) {
4718 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4719 if (first->len > 0)
4720 sh_ssl_sess_tree_delete(sh_ssl_sess);
4721 }
4722}
4723
4724/* return first block from sh_ssl_sess */
4725static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4726{
4727 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4728
4729}
4730
4731/* store a session into the cache
4732 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4733 * data: asn1 encoded session
4734 * data_len: asn1 encoded session length
4735 * Returns 1 id session was stored (else 0)
4736 */
4737static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4738{
4739 struct shared_block *first;
4740 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4741
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004742 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004743 if (!first) {
4744 /* Could not retrieve enough free blocks to store that session */
4745 return 0;
4746 }
4747
4748 /* STORE the key in the first elem */
4749 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4750 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4751 first->len = sizeof(struct sh_ssl_sess_hdr);
4752
4753 /* it returns the already existing node
4754 or current node if none, never returns null */
4755 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4756 if (oldsh_ssl_sess != sh_ssl_sess) {
4757 /* NOTE: Row couldn't be in use because we lock read & write function */
4758 /* release the reserved row */
4759 shctx_row_dec_hot(ssl_shctx, first);
4760 /* replace the previous session already in the tree */
4761 sh_ssl_sess = oldsh_ssl_sess;
4762 /* ignore the previous session data, only use the header */
4763 first = sh_ssl_sess_first_block(sh_ssl_sess);
4764 shctx_row_inc_hot(ssl_shctx, first);
4765 first->len = sizeof(struct sh_ssl_sess_hdr);
4766 }
4767
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004768 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004769 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004770 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004771 }
4772
4773 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004774
4775 return 1;
4776}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004777
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004778/* SSL callback used when a new session is created while connecting to a server */
4779static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4780{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004781 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004782 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004783
Willy Tarreau07d94e42018-09-20 10:57:52 +02004784 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004785
Olivier Houcharde6060c52017-11-16 17:42:52 +01004786 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4787 int len;
4788 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004789
Olivier Houcharde6060c52017-11-16 17:42:52 +01004790 len = i2d_SSL_SESSION(sess, NULL);
4791 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4792 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4793 } else {
4794 free(s->ssl_ctx.reused_sess[tid].ptr);
4795 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4796 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4797 }
4798 if (s->ssl_ctx.reused_sess[tid].ptr) {
4799 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4800 &ptr);
4801 }
4802 } else {
4803 free(s->ssl_ctx.reused_sess[tid].ptr);
4804 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4805 }
4806
4807 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004808}
4809
Olivier Houcharde6060c52017-11-16 17:42:52 +01004810
William Lallemanded0b5ad2017-10-30 19:36:36 +01004811/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004812int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004813{
4814 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4815 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4816 unsigned char *p;
4817 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02004818 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004819 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004820
4821 /* Session id is already stored in to key and session id is known
4822 * so we dont store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02004823 * note: SSL_SESSION_set1_id is using
4824 * a memcpy so we need to use a different pointer
4825 * than sid_data or sid_ctx_data to avoid valgrind
4826 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004827 */
4828
4829 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02004830
4831 /* copy value in an other buffer */
4832 memcpy(encid, sid_data, sid_length);
4833
4834 /* pad with 0 */
4835 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4836 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4837
4838 /* force length to zero to avoid ASN1 encoding */
4839 SSL_SESSION_set1_id(sess, encid, 0);
4840
4841 /* force length to zero to avoid ASN1 encoding */
4842 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004843
4844 /* check if buffer is large enough for the ASN1 encoded session */
4845 data_len = i2d_SSL_SESSION(sess, NULL);
4846 if (data_len > SHSESS_MAX_DATA_LEN)
4847 goto err;
4848
4849 p = encsess;
4850
4851 /* process ASN1 session encoding before the lock */
4852 i2d_SSL_SESSION(sess, &p);
4853
William Lallemanded0b5ad2017-10-30 19:36:36 +01004854
William Lallemanda3c77cf2017-10-30 23:44:40 +01004855 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004856 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004857 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004858 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004859err:
4860 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02004861 SSL_SESSION_set1_id(sess, encid, sid_length);
4862 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004863
4864 return 0; /* do not increment session reference count */
4865}
4866
4867/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004868SSL_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 +01004869{
William Lallemand4f45bb92017-10-30 20:08:51 +01004870 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004871 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4872 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004873 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004874 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004875
4876 global.shctx_lookups++;
4877
4878 /* allow the session to be freed automatically by openssl */
4879 *do_copy = 0;
4880
4881 /* tree key is zeros padded sessionid */
4882 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4883 memcpy(tmpkey, key, key_len);
4884 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4885 key = tmpkey;
4886 }
4887
4888 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004889 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004890
4891 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004892 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4893 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004894 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004895 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004896 global.shctx_misses++;
4897 return NULL;
4898 }
4899
William Lallemand4f45bb92017-10-30 20:08:51 +01004900 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4901 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004902
William Lallemand4f45bb92017-10-30 20:08:51 +01004903 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 +01004904
William Lallemanda3c77cf2017-10-30 23:44:40 +01004905 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004906
4907 /* decode ASN1 session */
4908 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004909 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004910 /* Reset session id and session id contenxt */
4911 if (sess) {
4912 SSL_SESSION_set1_id(sess, key, key_len);
4913 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4914 }
4915
4916 return sess;
4917}
4918
William Lallemand4f45bb92017-10-30 20:08:51 +01004919
William Lallemanded0b5ad2017-10-30 19:36:36 +01004920/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004921void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004922{
William Lallemand4f45bb92017-10-30 20:08:51 +01004923 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004924 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4925 unsigned int sid_length;
4926 const unsigned char *sid_data;
4927 (void)ctx;
4928
4929 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4930 /* tree key is zeros padded sessionid */
4931 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4932 memcpy(tmpkey, sid_data, sid_length);
4933 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4934 sid_data = tmpkey;
4935 }
4936
William Lallemanda3c77cf2017-10-30 23:44:40 +01004937 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004938
4939 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004940 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4941 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004942 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004943 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004944 }
4945
4946 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004947 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004948}
4949
4950/* Set session cache mode to server and disable openssl internal cache.
4951 * Set shared cache callbacks on an ssl context.
4952 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004953void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004954{
4955 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4956
4957 if (!ssl_shctx) {
4958 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4959 return;
4960 }
4961
4962 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4963 SSL_SESS_CACHE_NO_INTERNAL |
4964 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4965
4966 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004967 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4968 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4969 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004970}
4971
William Lallemand8b453912019-11-21 15:48:10 +01004972/*
4973 * This function applies the SSL configuration on a SSL_CTX
4974 * It returns an error code and fills the <err> buffer
4975 */
4976int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx, char **err)
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004977{
4978 struct proxy *curproxy = bind_conf->frontend;
4979 int cfgerr = 0;
4980 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004981 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004982 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004983#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004984 const char *conf_ciphersuites;
4985#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004986 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004987
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004988 if (ssl_conf) {
4989 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4990 int i, min, max;
4991 int flags = MC_SSL_O_ALL;
4992
4993 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004994 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4995 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004996 if (min)
4997 flags |= (methodVersions[min].flag - 1);
4998 if (max)
4999 flags |= ~((methodVersions[max].flag << 1) - 1);
5000 min = max = CONF_TLSV_NONE;
5001 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5002 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5003 if (min)
5004 max = i;
5005 else
5006 min = max = i;
5007 }
5008 /* save real min/max */
5009 conf_ssl_methods->min = min;
5010 conf_ssl_methods->max = max;
5011 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005012 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5013 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005014 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005015 }
5016 }
5017
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005018 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005019 case SSL_SOCK_VERIFY_NONE:
5020 verify = SSL_VERIFY_NONE;
5021 break;
5022 case SSL_SOCK_VERIFY_OPTIONAL:
5023 verify = SSL_VERIFY_PEER;
5024 break;
5025 case SSL_SOCK_VERIFY_REQUIRED:
5026 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5027 break;
5028 }
5029 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5030 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005031 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
5032 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
5033 if (ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005034 /* set CAfile to verify */
5035 if (!ssl_set_verify_locations_file(ctx, ca_file)) {
5036 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005037 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005038 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005039 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005040 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
5041 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005042 SSL_CTX_set_client_CA_list(ctx, SSL_dup_CA_list(ssl_get_client_ca_file(ca_file)));
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005043 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005044 }
Emeric Brun850efd52014-01-29 12:24:34 +01005045 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005046 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5047 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005048 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005049 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005050#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005051 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005052 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5053
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005054 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005055 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5056 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005057 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005058 }
Emeric Brun561e5742012-10-02 15:20:55 +02005059 else {
5060 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5061 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005062 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005063#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005064 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005065 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005066#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005067 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005068 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005069 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5070 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005071 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005072 }
5073 }
5074#endif
5075
William Lallemand4f45bb92017-10-30 20:08:51 +01005076 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005077 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5078 if (conf_ciphers &&
5079 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005080 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5081 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005082 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005083 }
5084
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005085#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005086 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5087 if (conf_ciphersuites &&
5088 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005089 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5090 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005091 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005092 }
5093#endif
5094
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005095#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005096 /* If tune.ssl.default-dh-param has not been set,
5097 neither has ssl-default-dh-file and no static DH
5098 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005099 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005100 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005101 (ssl_dh_ptr_index == -1 ||
5102 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005103 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5104 const SSL_CIPHER * cipher = NULL;
5105 char cipher_description[128];
5106 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5107 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5108 which is not ephemeral DH. */
5109 const char dhe_description[] = " Kx=DH ";
5110 const char dhe_export_description[] = " Kx=DH(";
5111 int idx = 0;
5112 int dhe_found = 0;
5113 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005114
Remi Gacogne23d5d372014-10-10 17:04:26 +02005115 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005116
Remi Gacogne23d5d372014-10-10 17:04:26 +02005117 if (ssl) {
5118 ciphers = SSL_get_ciphers(ssl);
5119
5120 if (ciphers) {
5121 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5122 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5123 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5124 if (strstr(cipher_description, dhe_description) != NULL ||
5125 strstr(cipher_description, dhe_export_description) != NULL) {
5126 dhe_found = 1;
5127 break;
5128 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005129 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005130 }
5131 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005132 SSL_free(ssl);
5133 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005134 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005135
Lukas Tribus90132722014-08-18 00:56:33 +02005136 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005137 memprintf(err, "%sSetting 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",
5138 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005139 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005140 }
5141
Willy Tarreauef934602016-12-22 23:12:01 +01005142 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005143 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005144
Willy Tarreauef934602016-12-22 23:12:01 +01005145 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005146 if (local_dh_1024 == NULL) {
5147 local_dh_1024 = ssl_get_dh_1024();
5148 }
Willy Tarreauef934602016-12-22 23:12:01 +01005149 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005150 if (local_dh_2048 == NULL) {
5151 local_dh_2048 = ssl_get_dh_2048();
5152 }
Willy Tarreauef934602016-12-22 23:12:01 +01005153 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005154 if (local_dh_4096 == NULL) {
5155 local_dh_4096 = ssl_get_dh_4096();
5156 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005157 }
5158 }
5159 }
5160#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005161
Emeric Brunfc0421f2012-09-07 17:30:07 +02005162 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005163#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005164 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005165#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005166
Bernard Spil13c53f82018-02-15 13:34:58 +01005167#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005168 ssl_conf_cur = NULL;
5169 if (ssl_conf && ssl_conf->npn_str)
5170 ssl_conf_cur = ssl_conf;
5171 else if (bind_conf->ssl_conf.npn_str)
5172 ssl_conf_cur = &bind_conf->ssl_conf;
5173 if (ssl_conf_cur)
5174 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005175#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005176#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005177 ssl_conf_cur = NULL;
5178 if (ssl_conf && ssl_conf->alpn_str)
5179 ssl_conf_cur = ssl_conf;
5180 else if (bind_conf->ssl_conf.alpn_str)
5181 ssl_conf_cur = &bind_conf->ssl_conf;
5182 if (ssl_conf_cur)
5183 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005184#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005185#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005186 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5187 if (conf_curves) {
5188 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005189 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5190 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005191 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005192 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005193 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005194 }
5195#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005196#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005197 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005198 int i;
5199 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005200#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005201 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005202 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5203 NULL);
5204
5205 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005206 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005207 return cfgerr;
5208 }
5209#else
5210 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5211 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5212 ECDHE_DEFAULT_CURVE);
5213#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005214
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005215 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005216 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005217 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5218 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005219 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005220 }
5221 else {
5222 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5223 EC_KEY_free(ecdh);
5224 }
5225 }
5226#endif
5227
Emeric Brunfc0421f2012-09-07 17:30:07 +02005228 return cfgerr;
5229}
5230
Evan Broderbe554312013-06-27 00:05:25 -07005231static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5232{
5233 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5234 size_t prefixlen, suffixlen;
5235
5236 /* Trivial case */
5237 if (strcmp(pattern, hostname) == 0)
5238 return 1;
5239
Evan Broderbe554312013-06-27 00:05:25 -07005240 /* The rest of this logic is based on RFC 6125, section 6.4.3
5241 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5242
Emeric Bruna848dae2013-10-08 11:27:28 +02005243 pattern_wildcard = NULL;
5244 pattern_left_label_end = pattern;
5245 while (*pattern_left_label_end != '.') {
5246 switch (*pattern_left_label_end) {
5247 case 0:
5248 /* End of label not found */
5249 return 0;
5250 case '*':
5251 /* If there is more than one wildcards */
5252 if (pattern_wildcard)
5253 return 0;
5254 pattern_wildcard = pattern_left_label_end;
5255 break;
5256 }
5257 pattern_left_label_end++;
5258 }
5259
5260 /* If it's not trivial and there is no wildcard, it can't
5261 * match */
5262 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005263 return 0;
5264
5265 /* Make sure all labels match except the leftmost */
5266 hostname_left_label_end = strchr(hostname, '.');
5267 if (!hostname_left_label_end
5268 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5269 return 0;
5270
5271 /* Make sure the leftmost label of the hostname is long enough
5272 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005273 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005274 return 0;
5275
5276 /* Finally compare the string on either side of the
5277 * wildcard */
5278 prefixlen = pattern_wildcard - pattern;
5279 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005280 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5281 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005282 return 0;
5283
5284 return 1;
5285}
5286
5287static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5288{
5289 SSL *ssl;
5290 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005291 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005292 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005293 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005294
5295 int depth;
5296 X509 *cert;
5297 STACK_OF(GENERAL_NAME) *alt_names;
5298 int i;
5299 X509_NAME *cert_subject;
5300 char *str;
5301
5302 if (ok == 0)
5303 return ok;
5304
5305 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005306 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005307 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005308
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005309 /* We're checking if the provided hostnames match the desired one. The
5310 * desired hostname comes from the SNI we presented if any, or if not
5311 * provided then it may have been explicitly stated using a "verifyhost"
5312 * directive. If neither is set, we don't care about the name so the
5313 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005314 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005315 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005316 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005317 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005318 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005319 if (!servername)
5320 return ok;
5321 }
Evan Broderbe554312013-06-27 00:05:25 -07005322
5323 /* We only need to verify the CN on the actual server cert,
5324 * not the indirect CAs */
5325 depth = X509_STORE_CTX_get_error_depth(ctx);
5326 if (depth != 0)
5327 return ok;
5328
5329 /* At this point, the cert is *not* OK unless we can find a
5330 * hostname match */
5331 ok = 0;
5332
5333 cert = X509_STORE_CTX_get_current_cert(ctx);
5334 /* It seems like this might happen if verify peer isn't set */
5335 if (!cert)
5336 return ok;
5337
5338 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5339 if (alt_names) {
5340 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5341 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5342 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005343#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005344 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5345#else
Evan Broderbe554312013-06-27 00:05:25 -07005346 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005347#endif
Evan Broderbe554312013-06-27 00:05:25 -07005348 ok = ssl_sock_srv_hostcheck(str, servername);
5349 OPENSSL_free(str);
5350 }
5351 }
5352 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005353 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005354 }
5355
5356 cert_subject = X509_get_subject_name(cert);
5357 i = -1;
5358 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5359 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005360 ASN1_STRING *value;
5361 value = X509_NAME_ENTRY_get_data(entry);
5362 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005363 ok = ssl_sock_srv_hostcheck(str, servername);
5364 OPENSSL_free(str);
5365 }
5366 }
5367
Willy Tarreau71d058c2017-07-26 20:09:56 +02005368 /* report the mismatch and indicate if SNI was used or not */
5369 if (!ok && !conn->err_code)
5370 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005371 return ok;
5372}
5373
Emeric Brun94324a42012-10-11 14:00:19 +02005374/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005375int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005376{
Willy Tarreau03209342016-12-22 17:08:28 +01005377 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005378 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005379 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005380 SSL_OP_ALL | /* all known workarounds for bugs */
5381 SSL_OP_NO_SSLv2 |
5382 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005383 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005384 SSL_MODE_ENABLE_PARTIAL_WRITE |
5385 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005386 SSL_MODE_RELEASE_BUFFERS |
5387 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005388 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005389 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005390 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005391 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005392 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005393
Thierry Fournier383085f2013-01-24 14:15:43 +01005394 /* Make sure openssl opens /dev/urandom before the chroot */
5395 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005396 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005397 cfgerr++;
5398 }
5399
Willy Tarreaufce03112015-01-15 21:32:40 +01005400 /* Automatic memory computations need to know we use SSL there */
5401 global.ssl_used_backend = 1;
5402
5403 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005404 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005405 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005406 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5407 curproxy->id, srv->id,
5408 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005409 cfgerr++;
5410 return cfgerr;
5411 }
5412 }
Emeric Brun94324a42012-10-11 14:00:19 +02005413 if (srv->use_ssl)
5414 srv->xprt = &ssl_sock;
5415 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005416 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005417
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005418 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005419 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005420 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5421 proxy_type_str(curproxy), curproxy->id,
5422 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005423 cfgerr++;
5424 return cfgerr;
5425 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005426
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005427 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005428 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5429 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5430 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005431 else
5432 flags = conf_ssl_methods->flags;
5433
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005434 /* Real min and max should be determinate with configuration and openssl's capabilities */
5435 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005436 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005437 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005438 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005439
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005440 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005441 min = max = CONF_TLSV_NONE;
5442 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005443 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005444 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005445 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005446 if (min) {
5447 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005448 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5449 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5450 proxy_type_str(curproxy), curproxy->id, srv->id,
5451 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005452 hole = 0;
5453 }
5454 max = i;
5455 }
5456 else {
5457 min = max = i;
5458 }
5459 }
5460 else {
5461 if (min)
5462 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005463 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005464 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005465 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5466 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005467 cfgerr += 1;
5468 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005469
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005470#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005471 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005472 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005473 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005474 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005475 else
5476 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5477 if (flags & methodVersions[i].flag)
5478 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005479#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005480 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005481 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5482 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005483#endif
5484
5485 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5486 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005487 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005488
Willy Tarreau5db847a2019-05-09 14:13:35 +02005489#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005490 if (global_ssl.async)
5491 mode |= SSL_MODE_ASYNC;
5492#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005493 SSL_CTX_set_mode(ctx, mode);
5494 srv->ssl_ctx.ctx = ctx;
5495
Emeric Bruna7aa3092012-10-26 12:58:00 +02005496 if (srv->ssl_ctx.client_crt) {
5497 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 +01005498 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5499 proxy_type_str(curproxy), curproxy->id,
5500 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005501 cfgerr++;
5502 }
5503 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 +01005504 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5505 proxy_type_str(curproxy), curproxy->id,
5506 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005507 cfgerr++;
5508 }
5509 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005510 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5511 proxy_type_str(curproxy), curproxy->id,
5512 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005513 cfgerr++;
5514 }
5515 }
Emeric Brun94324a42012-10-11 14:00:19 +02005516
Emeric Brun850efd52014-01-29 12:24:34 +01005517 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5518 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005519 switch (srv->ssl_ctx.verify) {
5520 case SSL_SOCK_VERIFY_NONE:
5521 verify = SSL_VERIFY_NONE;
5522 break;
5523 case SSL_SOCK_VERIFY_REQUIRED:
5524 verify = SSL_VERIFY_PEER;
5525 break;
5526 }
Evan Broderbe554312013-06-27 00:05:25 -07005527 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005528 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005529 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005530 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005531 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005532 /* set CAfile to verify */
5533 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
5534 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01005535 curproxy->id, srv->id,
5536 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005537 cfgerr++;
5538 }
5539 }
Emeric Brun850efd52014-01-29 12:24:34 +01005540 else {
5541 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01005542 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",
5543 curproxy->id, srv->id,
5544 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005545 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01005546 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
5547 curproxy->id, srv->id,
5548 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005549 cfgerr++;
5550 }
Emeric Brunef42d922012-10-11 16:11:36 +02005551#ifdef X509_V_FLAG_CRL_CHECK
5552 if (srv->ssl_ctx.crl_file) {
5553 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
5554
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005555 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005556 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
5557 curproxy->id, srv->id,
5558 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005559 cfgerr++;
5560 }
5561 else {
5562 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5563 }
5564 }
5565#endif
5566 }
5567
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005568 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
5569 SSL_SESS_CACHE_NO_INTERNAL_STORE);
5570 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02005571 if (srv->ssl_ctx.ciphers &&
5572 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005573 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
5574 curproxy->id, srv->id,
5575 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02005576 cfgerr++;
5577 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005578
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005579#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005580 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00005581 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005582 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
5583 curproxy->id, srv->id,
5584 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
5585 cfgerr++;
5586 }
5587#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01005588#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
5589 if (srv->ssl_ctx.npn_str)
5590 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
5591#endif
5592#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5593 if (srv->ssl_ctx.alpn_str)
5594 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
5595#endif
5596
Emeric Brun94324a42012-10-11 14:00:19 +02005597
5598 return cfgerr;
5599}
5600
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005601/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005602 * be NULL, in which case nothing is done. Returns the number of errors
5603 * encountered.
5604 */
Willy Tarreau03209342016-12-22 17:08:28 +01005605int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005606{
5607 struct ebmb_node *node;
5608 struct sni_ctx *sni;
5609 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01005610 int errcode = 0;
5611 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005612
Willy Tarreaufce03112015-01-15 21:32:40 +01005613 /* Automatic memory computations need to know we use SSL there */
5614 global.ssl_used_frontend = 1;
5615
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005616 /* Make sure openssl opens /dev/urandom before the chroot */
5617 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005618 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005619 err++;
5620 }
5621 /* Create initial_ctx used to start the ssl connection before do switchctx */
5622 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005623 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005624 /* It should not be necessary to call this function, but it's
5625 necessary first to check and move all initialisation related
5626 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01005627 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005628 }
Emeric Brun0bed9942014-10-30 19:25:24 +01005629 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01005630 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01005631
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005632 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005633 while (node) {
5634 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01005635 if (!sni->order && sni->ctx != bind_conf->default_ctx)
5636 /* only initialize the CTX on its first occurrence and
5637 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005638 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005639 node = ebmb_next(node);
5640 }
5641
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005642 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005643 while (node) {
5644 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01005645 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01005646 /* only initialize the CTX on its first occurrence and
5647 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005648 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
5649 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005650 node = ebmb_next(node);
5651 }
William Lallemand8b453912019-11-21 15:48:10 +01005652
5653 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005654 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005655 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005656 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005657 err++;
5658 }
5659
5660 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005661 return err;
5662}
5663
Willy Tarreau55d37912016-12-21 23:38:39 +01005664/* Prepares all the contexts for a bind_conf and allocates the shared SSL
5665 * context if needed. Returns < 0 on error, 0 on success. The warnings and
5666 * alerts are directly emitted since the rest of the stack does it below.
5667 */
5668int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
5669{
5670 struct proxy *px = bind_conf->frontend;
5671 int alloc_ctx;
5672 int err;
5673
5674 if (!bind_conf->is_ssl) {
5675 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005676 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
5677 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01005678 }
5679 return 0;
5680 }
5681 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005682 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005683 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
5684 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005685 }
5686 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005687 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
5688 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005689 return -1;
5690 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005691 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005692 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005693 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005694 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01005695 sizeof(*sh_ssl_sess_tree),
5696 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005697 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005698 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5699 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");
5700 else
5701 ha_alert("Unable to allocate SSL session cache.\n");
5702 return -1;
5703 }
5704 /* free block callback */
5705 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5706 /* init the root tree within the extra space */
5707 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5708 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005709 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005710 err = 0;
5711 /* initialize all certificate contexts */
5712 err += ssl_sock_prepare_all_ctx(bind_conf);
5713
5714 /* initialize CA variables if the certificates generation is enabled */
5715 err += ssl_sock_load_ca(bind_conf);
5716
5717 return -err;
5718}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005719
5720/* release ssl context allocated for servers. */
5721void ssl_sock_free_srv_ctx(struct server *srv)
5722{
Olivier Houchardc7566002018-11-20 23:33:50 +01005723#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5724 if (srv->ssl_ctx.alpn_str)
5725 free(srv->ssl_ctx.alpn_str);
5726#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005727#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01005728 if (srv->ssl_ctx.npn_str)
5729 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005730#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005731 if (srv->ssl_ctx.ctx)
5732 SSL_CTX_free(srv->ssl_ctx.ctx);
5733}
5734
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005735/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005736 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5737 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005738void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005739{
5740 struct ebmb_node *node, *back;
5741 struct sni_ctx *sni;
5742
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005743 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005744 while (node) {
5745 sni = ebmb_entry(node, struct sni_ctx, name);
5746 back = ebmb_next(node);
5747 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005748 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005749 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005750 ssl_sock_free_ssl_conf(sni->conf);
5751 free(sni->conf);
5752 sni->conf = NULL;
5753 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005754 free(sni);
5755 node = back;
5756 }
5757
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005758 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005759 while (node) {
5760 sni = ebmb_entry(node, struct sni_ctx, name);
5761 back = ebmb_next(node);
5762 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005763 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005764 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005765 ssl_sock_free_ssl_conf(sni->conf);
5766 free(sni->conf);
5767 sni->conf = NULL;
5768 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005769 free(sni);
5770 node = back;
5771 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005772 SSL_CTX_free(bind_conf->initial_ctx);
5773 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005774 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005775 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005776}
5777
Willy Tarreau795cdab2016-12-22 17:30:54 +01005778/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5779void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5780{
5781 ssl_sock_free_ca(bind_conf);
5782 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005783 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005784 free(bind_conf->ca_sign_file);
5785 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005786 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005787 free(bind_conf->keys_ref->filename);
5788 free(bind_conf->keys_ref->tlskeys);
5789 LIST_DEL(&bind_conf->keys_ref->list);
5790 free(bind_conf->keys_ref);
5791 }
5792 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005793 bind_conf->ca_sign_pass = NULL;
5794 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005795}
5796
Christopher Faulet31af49d2015-06-09 17:29:50 +02005797/* Load CA cert file and private key used to generate certificates */
5798int
Willy Tarreau03209342016-12-22 17:08:28 +01005799ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005800{
Willy Tarreau03209342016-12-22 17:08:28 +01005801 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005802 FILE *fp;
5803 X509 *cacert = NULL;
5804 EVP_PKEY *capkey = NULL;
5805 int err = 0;
5806
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005807 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005808 return err;
5809
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005810#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005811 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005812 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005813 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005814 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005815 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005816#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005817
Christopher Faulet31af49d2015-06-09 17:29:50 +02005818 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005819 ha_alert("Proxy '%s': cannot enable certificate generation, "
5820 "no CA certificate File configured at [%s:%d].\n",
5821 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005822 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005823 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005824
5825 /* read in the CA certificate */
5826 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005827 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5828 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005829 goto load_error;
5830 }
5831 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005832 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5833 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005834 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005835 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005836 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005837 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005838 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5839 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005840 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005841 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005842
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005843 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005844 bind_conf->ca_sign_cert = cacert;
5845 bind_conf->ca_sign_pkey = capkey;
5846 return err;
5847
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005848 read_error:
5849 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005850 if (capkey) EVP_PKEY_free(capkey);
5851 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005852 load_error:
5853 bind_conf->generate_certs = 0;
5854 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005855 return err;
5856}
5857
5858/* Release CA cert and private key used to generate certificated */
5859void
5860ssl_sock_free_ca(struct bind_conf *bind_conf)
5861{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005862 if (bind_conf->ca_sign_pkey)
5863 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5864 if (bind_conf->ca_sign_cert)
5865 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005866 bind_conf->ca_sign_pkey = NULL;
5867 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005868}
5869
Emeric Brun46591952012-05-18 15:47:34 +02005870/*
5871 * This function is called if SSL * context is not yet allocated. The function
5872 * is designed to be called before any other data-layer operation and sets the
5873 * handshake flag on the connection. It is safe to call it multiple times.
5874 * It returns 0 on success and -1 in error case.
5875 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005876static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005877{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005878 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005879 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005880 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005881 return 0;
5882
Willy Tarreau3c728722014-01-23 13:50:42 +01005883 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005884 return 0;
5885
Olivier Houchard66ab4982019-02-26 18:37:15 +01005886 ctx = pool_alloc(ssl_sock_ctx_pool);
5887 if (!ctx) {
5888 conn->err_code = CO_ER_SSL_NO_MEM;
5889 return -1;
5890 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005891 ctx->wait_event.tasklet = tasklet_new();
5892 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005893 conn->err_code = CO_ER_SSL_NO_MEM;
5894 pool_free(ssl_sock_ctx_pool, ctx);
5895 return -1;
5896 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005897 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5898 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005899 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005900 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01005901 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005902 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01005903 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02005904 ctx->xprt_st = 0;
5905 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005906
5907 /* Only work with sockets for now, this should be adapted when we'll
5908 * add QUIC support.
5909 */
5910 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005911 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005912 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5913 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005914 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005915
Willy Tarreau20879a02012-12-03 16:32:10 +01005916 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5917 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005918 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005919 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005920
Emeric Brun46591952012-05-18 15:47:34 +02005921 /* If it is in client mode initiate SSL session
5922 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005923 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005924 int may_retry = 1;
5925
5926 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005927 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005928 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5929 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005930 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005931 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005932 goto retry_connect;
5933 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005934 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005935 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005936 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005937 ctx->bio = BIO_new(ha_meth);
5938 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005939 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005940 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005941 goto retry_connect;
5942 }
Emeric Brun55476152014-11-12 17:35:37 +01005943 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005944 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005945 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005946 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005947 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005948
Evan Broderbe554312013-06-27 00:05:25 -07005949 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005950 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5951 SSL_free(ctx->ssl);
5952 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005953 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005954 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005955 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005956 goto retry_connect;
5957 }
Emeric Brun55476152014-11-12 17:35:37 +01005958 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005959 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005960 }
5961
Olivier Houchard66ab4982019-02-26 18:37:15 +01005962 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005963 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5964 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5965 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 +01005966 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005967 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005968 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5969 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005970 } else if (sess) {
5971 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005972 }
5973 }
Evan Broderbe554312013-06-27 00:05:25 -07005974
Emeric Brun46591952012-05-18 15:47:34 +02005975 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005976 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005977
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005978 _HA_ATOMIC_ADD(&sslconns, 1);
5979 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005980 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005981 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005982 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005983 if (conn->flags & CO_FL_ERROR)
5984 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005985 return 0;
5986 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005987 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005988 int may_retry = 1;
5989
5990 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005991 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005992 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5993 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005994 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005995 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005996 goto retry_accept;
5997 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005998 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005999 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006000 }
Olivier Houchard545989f2019-12-17 15:39:54 +01006001#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard54907bb2019-12-19 15:02:39 +01006002 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6003 b_alloc(&ctx->early_buf);
6004 SSL_set_max_early_data(ctx->ssl,
6005 /* Only allow early data if we managed to allocate
6006 * a buffer.
6007 */
6008 (!b_is_null(&ctx->early_buf)) ?
6009 global.tune.bufsize - global.tune.maxrewrite : 0);
6010 }
Olivier Houchard545989f2019-12-17 15:39:54 +01006011#endif
Emeric Brun46591952012-05-18 15:47:34 +02006012
Olivier Houcharda8955d52019-04-07 22:00:38 +02006013 ctx->bio = BIO_new(ha_meth);
6014 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006015 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006016 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006017 goto retry_accept;
6018 }
Emeric Brun55476152014-11-12 17:35:37 +01006019 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006020 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006021 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006022 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006023 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006024
Emeric Brune1f38db2012-09-03 20:36:47 +02006025 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006026 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6027 SSL_free(ctx->ssl);
6028 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006029 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006030 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006031 goto retry_accept;
6032 }
Emeric Brun55476152014-11-12 17:35:37 +01006033 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006034 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006035 }
6036
Olivier Houchard66ab4982019-02-26 18:37:15 +01006037 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006038
Emeric Brun46591952012-05-18 15:47:34 +02006039 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006040 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006041#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006042 conn->flags |= CO_FL_EARLY_SSL_HS;
6043#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006044
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006045 _HA_ATOMIC_ADD(&sslconns, 1);
6046 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006047 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006048 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006049 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006050 if (conn->flags & CO_FL_ERROR)
6051 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02006052 return 0;
6053 }
6054 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006055 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006056err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006057 if (ctx && ctx->wait_event.tasklet)
6058 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006059 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006060 return -1;
6061}
6062
6063
6064/* This is the callback which is used when an SSL handshake is pending. It
6065 * updates the FD status if it wants some polling before being called again.
6066 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6067 * otherwise it returns non-zero and removes itself from the connection's
6068 * flags (the bit is provided in <flag> by the caller).
6069 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006070static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006071{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006072 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006073 int ret;
6074
Willy Tarreau3c728722014-01-23 13:50:42 +01006075 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006076 return 0;
6077
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006078 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006079 goto out_error;
6080
Willy Tarreau5db847a2019-05-09 14:13:35 +02006081#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006082 /*
6083 * Check if we have early data. If we do, we have to read them
6084 * before SSL_do_handshake() is called, And there's no way to
6085 * detect early data, except to try to read them
6086 */
6087 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006088 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006089
Olivier Houchard54907bb2019-12-19 15:02:39 +01006090 while (1) {
6091 ret = SSL_read_early_data(ctx->ssl,
6092 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6093 &read_data);
6094 if (ret == SSL_READ_EARLY_DATA_ERROR)
6095 goto check_error;
6096 if (read_data > 0) {
6097 conn->flags |= CO_FL_EARLY_DATA;
6098 b_add(&ctx->early_buf, read_data);
6099 }
6100 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6101 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6102 if (!b_data(&ctx->early_buf))
6103 b_free(&ctx->early_buf);
6104 break;
6105 }
6106 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006107 }
6108#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006109 /* If we use SSL_do_handshake to process a reneg initiated by
6110 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6111 * Usually SSL_write and SSL_read are used and process implicitly
6112 * the reneg handshake.
6113 * Here we use SSL_peek as a workaround for reneg.
6114 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006115 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006116 char c;
6117
Olivier Houchard66ab4982019-02-26 18:37:15 +01006118 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006119 if (ret <= 0) {
6120 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006121 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006122
Emeric Brun674b7432012-11-08 19:21:55 +01006123 if (ret == SSL_ERROR_WANT_WRITE) {
6124 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006125 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006126 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006127 return 0;
6128 }
6129 else if (ret == SSL_ERROR_WANT_READ) {
6130 /* handshake may have been completed but we have
6131 * no more data to read.
6132 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006133 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006134 ret = 1;
6135 goto reneg_ok;
6136 }
6137 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006138 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006139 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006140 return 0;
6141 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006142#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006143 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006144 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006145 return 0;
6146 }
6147#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006148 else if (ret == SSL_ERROR_SYSCALL) {
6149 /* if errno is null, then connection was successfully established */
6150 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6151 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006152 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006153#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6154 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006155 conn->err_code = CO_ER_SSL_HANDSHAKE;
6156#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006157 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006158#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006159 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006160 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006161 empty_handshake = state == TLS_ST_BEFORE;
6162#else
Lukas Tribus49799162019-07-08 14:29:15 +02006163 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6164 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006165#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006166 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006167 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006168 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006169 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6170 else
6171 conn->err_code = CO_ER_SSL_EMPTY;
6172 }
6173 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006174 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006175 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6176 else
6177 conn->err_code = CO_ER_SSL_ABORT;
6178 }
6179 }
6180 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006181 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006182 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006183 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006184 conn->err_code = CO_ER_SSL_HANDSHAKE;
6185 }
Lukas Tribus49799162019-07-08 14:29:15 +02006186#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006187 }
Emeric Brun674b7432012-11-08 19:21:55 +01006188 goto out_error;
6189 }
6190 else {
6191 /* Fail on all other handshake errors */
6192 /* Note: OpenSSL may leave unread bytes in the socket's
6193 * buffer, causing an RST to be emitted upon close() on
6194 * TCP sockets. We first try to drain possibly pending
6195 * data to avoid this as much as possible.
6196 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006197 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006198 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006199 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006200 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006201 goto out_error;
6202 }
6203 }
6204 /* read some data: consider handshake completed */
6205 goto reneg_ok;
6206 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006207 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006208check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006209 if (ret != 1) {
6210 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006211 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006212
6213 if (ret == SSL_ERROR_WANT_WRITE) {
6214 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006215 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006216 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006217 return 0;
6218 }
6219 else if (ret == SSL_ERROR_WANT_READ) {
6220 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006221 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006222 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6223 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006224 return 0;
6225 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006226#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006227 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006228 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006229 return 0;
6230 }
6231#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006232 else if (ret == SSL_ERROR_SYSCALL) {
6233 /* if errno is null, then connection was successfully established */
6234 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6235 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006236 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006237#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6238 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006239 conn->err_code = CO_ER_SSL_HANDSHAKE;
6240#else
6241 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006242#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006243 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006244 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006245 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006246#else
Lukas Tribus49799162019-07-08 14:29:15 +02006247 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6248 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006249#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006250 if (empty_handshake) {
6251 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006252 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006253 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6254 else
6255 conn->err_code = CO_ER_SSL_EMPTY;
6256 }
6257 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006258 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006259 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6260 else
6261 conn->err_code = CO_ER_SSL_ABORT;
6262 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006263 }
6264 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006265 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006266 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6267 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006268 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006269 }
Lukas Tribus49799162019-07-08 14:29:15 +02006270#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006271 }
Willy Tarreau89230192012-09-28 20:22:13 +02006272 goto out_error;
6273 }
Emeric Brun46591952012-05-18 15:47:34 +02006274 else {
6275 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006276 /* Note: OpenSSL may leave unread bytes in the socket's
6277 * buffer, causing an RST to be emitted upon close() on
6278 * TCP sockets. We first try to drain possibly pending
6279 * data to avoid this as much as possible.
6280 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006281 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006282 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006283 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006284 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006285 goto out_error;
6286 }
6287 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006288#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006289 else {
6290 /*
6291 * If the server refused the early data, we have to send a
6292 * 425 to the client, as we no longer have the data to sent
6293 * them again.
6294 */
6295 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006296 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006297 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6298 goto out_error;
6299 }
6300 }
6301 }
6302#endif
6303
Emeric Brun46591952012-05-18 15:47:34 +02006304
Emeric Brun674b7432012-11-08 19:21:55 +01006305reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006306
Willy Tarreau5db847a2019-05-09 14:13:35 +02006307#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006308 /* ASYNC engine API doesn't support moving read/write
6309 * buffers. So we disable ASYNC mode right after
6310 * the handshake to avoid buffer oveflows.
6311 */
6312 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006313 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006314#endif
Emeric Brun46591952012-05-18 15:47:34 +02006315 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006316 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006317 if (objt_server(conn->target)) {
6318 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6319 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6320 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006321 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006322 else {
6323 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6324 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6325 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6326 }
Emeric Brun46591952012-05-18 15:47:34 +02006327 }
6328
6329 /* The connection is now established at both layers, it's time to leave */
6330 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6331 return 1;
6332
6333 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006334 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006335 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006336 ERR_clear_error();
6337
Emeric Brun9fa89732012-10-04 17:09:56 +02006338 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006339 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6340 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6341 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006342 }
6343
Emeric Brun46591952012-05-18 15:47:34 +02006344 /* Fail on all other handshake errors */
6345 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006346 if (!conn->err_code)
6347 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006348 return 0;
6349}
6350
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006351/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6352 * event subscriber <es> is not allowed to change from a previous call as long
6353 * as at least one event is still subscribed. The <event_type> must only be a
6354 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6355 * unless the transport layer was already released.
6356 */
6357static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006358{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006359 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006360
Olivier Houchard0ff28652019-06-24 18:57:39 +02006361 if (!ctx)
6362 return -1;
6363
Willy Tarreau113d52b2020-01-10 09:20:26 +01006364 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6365 BUG_ON(ctx->subs && ctx->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006366 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006367
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006368 ctx->subs = es;
6369 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006370
6371 /* we may have to subscribe to lower layers for new events */
6372 event_type &= ~ctx->wait_event.events;
6373 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6374 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006375 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006376}
6377
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006378/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6379 * The <es> pointer is not allowed to differ from the one passed to the
6380 * subscribe() call. It always returns zero.
6381 */
6382static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006383{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006384 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006385
Willy Tarreau113d52b2020-01-10 09:20:26 +01006386 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006387 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006388
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006389 es->events &= ~event_type;
6390 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006391 ctx->subs = NULL;
6392
6393 /* If we subscribed, and we're not doing the handshake,
6394 * then we subscribed because the upper layer asked for it,
6395 * as the upper layer is no longer interested, we can
6396 * unsubscribe too.
6397 */
6398 event_type &= ctx->wait_event.events;
6399 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6400 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006401
6402 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006403}
6404
Olivier Houchard2e055482019-05-27 19:50:12 +02006405/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6406 * Returns 0 on success, and non-zero on failure.
6407 */
6408static 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)
6409{
6410 struct ssl_sock_ctx *ctx = xprt_ctx;
6411
6412 if (oldxprt_ops != NULL)
6413 *oldxprt_ops = ctx->xprt;
6414 if (oldxprt_ctx != NULL)
6415 *oldxprt_ctx = ctx->xprt_ctx;
6416 ctx->xprt = toadd_ops;
6417 ctx->xprt_ctx = toadd_ctx;
6418 return 0;
6419}
6420
Olivier Houchard5149b592019-05-23 17:47:36 +02006421/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6422 * return 0, otherwise just call the remove_xprt method from the underlying
6423 * XPRT.
6424 */
6425static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6426{
6427 struct ssl_sock_ctx *ctx = xprt_ctx;
6428
6429 if (ctx->xprt_ctx == toremove_ctx) {
6430 ctx->xprt_ctx = newctx;
6431 ctx->xprt = newops;
6432 return 0;
6433 }
6434 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6435}
6436
Olivier Houchardea8dd942019-05-20 14:02:16 +02006437static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6438{
6439 struct ssl_sock_ctx *ctx = context;
6440
6441 /* First if we're doing an handshake, try that */
6442 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6443 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6444 /* If we had an error, or the handshake is done and I/O is available,
6445 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006446 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006447 * we can't be sure conn_fd_handler() will be called again.
6448 */
6449 if ((ctx->conn->flags & CO_FL_ERROR) ||
6450 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6451 int ret = 0;
6452 int woke = 0;
6453
6454 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006455 if (ctx->subs) {
6456 tasklet_wakeup(ctx->subs->tasklet);
6457 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006458 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006459 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006460 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006461
Olivier Houchardea8dd942019-05-20 14:02:16 +02006462 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006463 * upper layers know. If we have no mux, create it,
6464 * and once we have a mux, call its wake method if we didn't
6465 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006466 */
6467 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006468 if (!ctx->conn->mux)
6469 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006470 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6471 ctx->conn->mux->wake(ctx->conn);
6472 return NULL;
6473 }
6474 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006475#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6476 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006477 else if (b_data(&ctx->early_buf) && ctx->subs &&
6478 ctx->subs->events & SUB_RETRY_RECV) {
6479 tasklet_wakeup(ctx->subs->tasklet);
6480 ctx->subs->events &= ~SUB_RETRY_RECV;
6481 if (!ctx->subs->events)
6482 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006483 }
6484#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006485 return NULL;
6486}
6487
Emeric Brun46591952012-05-18 15:47:34 +02006488/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006489 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006490 * buffer wraps, in which case a second call may be performed. The connection's
6491 * flags are updated with whatever special event is detected (error, read0,
6492 * empty). The caller is responsible for taking care of those events and
6493 * avoiding the call if inappropriate. The function does not call the
6494 * connection's polling update function, so the caller is responsible for this.
6495 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006496static 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 +02006497{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006498 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006499 ssize_t ret;
6500 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006501
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006502 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006503 goto out_error;
6504
Olivier Houchard54907bb2019-12-19 15:02:39 +01006505#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6506 if (b_data(&ctx->early_buf)) {
6507 try = b_contig_space(buf);
6508 if (try > b_data(&ctx->early_buf))
6509 try = b_data(&ctx->early_buf);
6510 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6511 b_add(buf, try);
6512 b_del(&ctx->early_buf, try);
6513 if (b_data(&ctx->early_buf) == 0)
6514 b_free(&ctx->early_buf);
6515 return try;
6516 }
6517#endif
6518
Emeric Brun46591952012-05-18 15:47:34 +02006519 if (conn->flags & CO_FL_HANDSHAKE)
6520 /* a handshake was requested */
6521 return 0;
6522
Emeric Brun46591952012-05-18 15:47:34 +02006523 /* read the largest possible block. For this, we perform only one call
6524 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6525 * in which case we accept to do it once again. A new attempt is made on
6526 * EINTR too.
6527 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006528 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006529
Willy Tarreau591d4452018-06-15 17:21:00 +02006530 try = b_contig_space(buf);
6531 if (!try)
6532 break;
6533
Willy Tarreauabf08d92014-01-14 11:31:27 +01006534 if (try > count)
6535 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006536
Olivier Houchard66ab4982019-02-26 18:37:15 +01006537 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006538
Emeric Brune1f38db2012-09-03 20:36:47 +02006539 if (conn->flags & CO_FL_ERROR) {
6540 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006541 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006542 }
Emeric Brun46591952012-05-18 15:47:34 +02006543 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006544 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006545 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006546 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006547 }
Emeric Brun46591952012-05-18 15:47:34 +02006548 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006549 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006550 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006551 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02006552 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006553 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006554#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006555 /* Async mode can be re-enabled, because we're leaving data state.*/
6556 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006557 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006558#endif
Emeric Brun46591952012-05-18 15:47:34 +02006559 break;
6560 }
6561 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006562 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006563 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6564 SUB_RETRY_RECV,
6565 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01006566 /* handshake is running, and it may need to re-enable read */
6567 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006568#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006569 /* Async mode can be re-enabled, because we're leaving data state.*/
6570 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006571 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006572#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006573 break;
6574 }
Emeric Brun46591952012-05-18 15:47:34 +02006575 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006576 } else if (ret == SSL_ERROR_ZERO_RETURN)
6577 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006578 /* For SSL_ERROR_SYSCALL, make sure to clear the error
6579 * stack before shutting down the connection for
6580 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006581 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
6582 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02006583 /* otherwise it's a real error */
6584 goto out_error;
6585 }
6586 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006587 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006588 return done;
6589
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006590 clear_ssl_error:
6591 /* Clear openssl global errors stack */
6592 ssl_sock_dump_errors(conn);
6593 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02006594 read0:
6595 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006596 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006597
Emeric Brun46591952012-05-18 15:47:34 +02006598 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006599 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01006600 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006601 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006602 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006603 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006604}
6605
6606
Willy Tarreau787db9a2018-06-14 18:31:46 +02006607/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
6608 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
6609 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02006610 * Only one call to send() is performed, unless the buffer wraps, in which case
6611 * a second call may be performed. The connection's flags are updated with
6612 * whatever special event is detected (error, empty). The caller is responsible
6613 * for taking care of those events and avoiding the call if inappropriate. The
6614 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02006615 * is responsible for this. The buffer's output is not adjusted, it's up to the
6616 * caller to take care of this. It's up to the caller to update the buffer's
6617 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02006618 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006619static 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 +02006620{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006621 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006622 ssize_t ret;
6623 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02006624
6625 done = 0;
6626
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006627 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006628 goto out_error;
6629
Olivier Houchard010941f2019-05-03 20:56:19 +02006630 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006631 /* a handshake was requested */
6632 return 0;
6633
6634 /* send the largest possible block. For this we perform only one call
6635 * to send() unless the buffer wraps and we exactly fill the first hunk,
6636 * in which case we accept to do it once again.
6637 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02006638 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02006639#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006640 size_t written_data;
6641#endif
6642
Willy Tarreau787db9a2018-06-14 18:31:46 +02006643 try = b_contig_data(buf, done);
6644 if (try > count)
6645 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01006646
Willy Tarreau7bed9452014-02-02 02:00:24 +01006647 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006648 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01006649 global_ssl.max_record && try > global_ssl.max_record) {
6650 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006651 }
6652 else {
6653 /* we need to keep the information about the fact that
6654 * we're not limiting the upcoming send(), because if it
6655 * fails, we'll have to retry with at least as many data.
6656 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006657 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006658 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01006659
Willy Tarreau5db847a2019-05-09 14:13:35 +02006660#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02006661 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006662 unsigned int max_early;
6663
Olivier Houchard522eea72017-11-03 16:27:47 +01006664 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006665 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006666 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006667 if (SSL_get0_session(ctx->ssl))
6668 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006669 else
6670 max_early = 0;
6671 }
6672
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006673 if (try + ctx->sent_early_data > max_early) {
6674 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006675 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006676 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006677 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006678 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006679 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006680 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006681 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006682 if (ret == 1) {
6683 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006684 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006685 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006686 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006687 /* Initiate the handshake, now */
6688 tasklet_wakeup(ctx->wait_event.tasklet);
6689 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006690
Olivier Houchardc2aae742017-09-22 18:26:28 +02006691 }
6692
6693 } else
6694#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006695 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006696
Emeric Brune1f38db2012-09-03 20:36:47 +02006697 if (conn->flags & CO_FL_ERROR) {
6698 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006699 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006700 }
Emeric Brun46591952012-05-18 15:47:34 +02006701 if (ret > 0) {
Olivier Houchardf24502b2019-01-17 19:09:11 +01006702 /* A send succeeded, so we can consier ourself connected */
6703 conn->flags |= CO_FL_CONNECTED;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006704 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006705 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006706 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006707 }
6708 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006709 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006710
Emeric Brun46591952012-05-18 15:47:34 +02006711 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006712 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006713 /* handshake is running, and it may need to re-enable write */
6714 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006715 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006716#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006717 /* Async mode can be re-enabled, because we're leaving data state.*/
6718 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006719 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006720#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006721 break;
6722 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006723
Emeric Brun46591952012-05-18 15:47:34 +02006724 break;
6725 }
6726 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006727 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006728 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006729 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6730 SUB_RETRY_RECV,
6731 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006732#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006733 /* Async mode can be re-enabled, because we're leaving data state.*/
6734 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006735 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006736#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006737 break;
6738 }
Emeric Brun46591952012-05-18 15:47:34 +02006739 goto out_error;
6740 }
6741 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006742 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006743 return done;
6744
6745 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006746 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006747 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006748 ERR_clear_error();
6749
Emeric Brun46591952012-05-18 15:47:34 +02006750 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006751 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006752}
6753
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006754static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006755
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006756 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006757
Olivier Houchardea8dd942019-05-20 14:02:16 +02006758
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006759 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006760 if (ctx->wait_event.events != 0)
6761 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6762 ctx->wait_event.events,
6763 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01006764 if (ctx->subs) {
6765 ctx->subs->events = 0;
6766 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006767 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006768
Olivier Houchard692c1d02019-05-23 18:41:47 +02006769 if (ctx->xprt->close)
6770 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006771#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006772 if (global_ssl.async) {
6773 OSSL_ASYNC_FD all_fd[32], afd;
6774 size_t num_all_fds = 0;
6775 int i;
6776
Olivier Houchard66ab4982019-02-26 18:37:15 +01006777 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006778 if (num_all_fds > 32) {
6779 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6780 return;
6781 }
6782
Olivier Houchard66ab4982019-02-26 18:37:15 +01006783 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006784
6785 /* If an async job is pending, we must try to
6786 to catch the end using polling before calling
6787 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006788 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006789 for (i=0 ; i < num_all_fds ; i++) {
6790 /* switch on an handler designed to
6791 * handle the SSL_free
6792 */
6793 afd = all_fd[i];
6794 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006795 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006796 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006797 /* To ensure that the fd cache won't be used
6798 * and we'll catch a real RD event.
6799 */
6800 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006801 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006802 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006803 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006804 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006805 return;
6806 }
Emeric Brun3854e012017-05-17 20:42:48 +02006807 /* Else we can remove the fds from the fdtab
6808 * and call SSL_free.
6809 * note: we do a fd_remove and not a delete
6810 * because the fd is owned by the engine.
6811 * the engine is responsible to close
6812 */
6813 for (i=0 ; i < num_all_fds ; i++)
6814 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006815 }
6816#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006817 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01006818 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006819 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006820 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006821 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006822 }
Emeric Brun46591952012-05-18 15:47:34 +02006823}
6824
6825/* This function tries to perform a clean shutdown on an SSL connection, and in
6826 * any case, flags the connection as reusable if no handshake was in progress.
6827 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006828static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006829{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006830 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006831
Emeric Brun46591952012-05-18 15:47:34 +02006832 if (conn->flags & CO_FL_HANDSHAKE)
6833 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006834 if (!clean)
6835 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006836 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006837 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006838 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006839 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006840 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006841 ERR_clear_error();
6842 }
Emeric Brun46591952012-05-18 15:47:34 +02006843}
6844
William Lallemandd4f946c2019-12-05 10:26:40 +01006845/* fill a buffer with the algorithm and size of a public key */
6846static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006847{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006848 int bits = 0;
6849 int sig = TLSEXT_signature_anonymous;
6850 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006851 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006852
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006853 pkey = X509_get_pubkey(crt);
6854 if (pkey) {
6855 bits = EVP_PKEY_bits(pkey);
6856 switch(EVP_PKEY_base_id(pkey)) {
6857 case EVP_PKEY_RSA:
6858 sig = TLSEXT_signature_rsa;
6859 break;
6860 case EVP_PKEY_EC:
6861 sig = TLSEXT_signature_ecdsa;
6862 break;
6863 case EVP_PKEY_DSA:
6864 sig = TLSEXT_signature_dsa;
6865 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006866 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006867 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006868 }
6869
6870 switch(sig) {
6871 case TLSEXT_signature_rsa:
6872 len = chunk_printf(out, "RSA%d", bits);
6873 break;
6874 case TLSEXT_signature_ecdsa:
6875 len = chunk_printf(out, "EC%d", bits);
6876 break;
6877 case TLSEXT_signature_dsa:
6878 len = chunk_printf(out, "DSA%d", bits);
6879 break;
6880 default:
6881 return 0;
6882 }
6883 if (len < 0)
6884 return 0;
6885 return 1;
6886}
6887
William Lallemandd4f946c2019-12-05 10:26:40 +01006888/* used for ppv2 pkey alog (can be used for logging) */
6889int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
6890{
6891 struct ssl_sock_ctx *ctx;
6892 X509 *crt;
6893
6894 if (!ssl_sock_is_ssl(conn))
6895 return 0;
6896
6897 ctx = conn->xprt_ctx;
6898
6899 crt = SSL_get_certificate(ctx->ssl);
6900 if (!crt)
6901 return 0;
6902
6903 return cert_get_pkey_algo(crt, out);
6904}
6905
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006906/* used for ppv2 cert signature (can be used for logging) */
6907const char *ssl_sock_get_cert_sig(struct connection *conn)
6908{
Christopher Faulet82004142019-09-10 10:12:03 +02006909 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006910
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006911 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6912 X509 *crt;
6913
6914 if (!ssl_sock_is_ssl(conn))
6915 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006916 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006917 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006918 if (!crt)
6919 return NULL;
6920 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6921 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6922}
6923
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006924/* used for ppv2 authority */
6925const char *ssl_sock_get_sni(struct connection *conn)
6926{
6927#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02006928 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006929
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006930 if (!ssl_sock_is_ssl(conn))
6931 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006932 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006933 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006934#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006935 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006936#endif
6937}
6938
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006939/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006940const char *ssl_sock_get_cipher_name(struct connection *conn)
6941{
Christopher Faulet82004142019-09-10 10:12:03 +02006942 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006943
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006944 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006945 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006946 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006947 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006948}
6949
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006950/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006951const char *ssl_sock_get_proto_version(struct connection *conn)
6952{
Christopher Faulet82004142019-09-10 10:12:03 +02006953 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006954
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006955 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006956 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006957 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006958 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006959}
6960
Willy Tarreau8d598402012-10-22 17:58:39 +02006961/* Extract a serial from a cert, and copy it to a chunk.
6962 * Returns 1 if serial is found and copied, 0 if no serial found and
6963 * -1 if output is not large enough.
6964 */
6965static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006966ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006967{
6968 ASN1_INTEGER *serial;
6969
6970 serial = X509_get_serialNumber(crt);
6971 if (!serial)
6972 return 0;
6973
6974 if (out->size < serial->length)
6975 return -1;
6976
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006977 memcpy(out->area, serial->data, serial->length);
6978 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006979 return 1;
6980}
6981
Emeric Brun43e79582014-10-29 19:03:26 +01006982/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006983 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6984 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006985 */
6986static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006987ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006988{
6989 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006990 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01006991
6992 len =i2d_X509(crt, NULL);
6993 if (len <= 0)
6994 return 1;
6995
6996 if (out->size < len)
6997 return -1;
6998
6999 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007000 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007001 return 1;
7002}
7003
Emeric Brunce5ad802012-10-22 14:11:22 +02007004
Willy Tarreau83061a82018-07-13 11:56:34 +02007005/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007006 * Returns 1 if serial is found and copied, 0 if no valid time found
7007 * and -1 if output is not large enough.
7008 */
7009static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007010ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007011{
7012 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7013 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7014
7015 if (gentm->length < 12)
7016 return 0;
7017 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7018 return 0;
7019 if (out->size < gentm->length-2)
7020 return -1;
7021
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007022 memcpy(out->area, gentm->data+2, gentm->length-2);
7023 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007024 return 1;
7025 }
7026 else if (tm->type == V_ASN1_UTCTIME) {
7027 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7028
7029 if (utctm->length < 10)
7030 return 0;
7031 if (utctm->data[0] >= 0x35)
7032 return 0;
7033 if (out->size < utctm->length)
7034 return -1;
7035
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007036 memcpy(out->area, utctm->data, utctm->length);
7037 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007038 return 1;
7039 }
7040
7041 return 0;
7042}
7043
Emeric Brun87855892012-10-17 17:39:35 +02007044/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7045 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7046 */
7047static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007048ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7049 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007050{
7051 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007052 ASN1_OBJECT *obj;
7053 ASN1_STRING *data;
7054 const unsigned char *data_ptr;
7055 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007056 int i, j, n;
7057 int cur = 0;
7058 const char *s;
7059 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007060 int name_count;
7061
7062 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007063
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007064 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007065 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007066 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007067 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007068 else
7069 j = i;
7070
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007071 ne = X509_NAME_get_entry(a, j);
7072 obj = X509_NAME_ENTRY_get_object(ne);
7073 data = X509_NAME_ENTRY_get_data(ne);
7074 data_ptr = ASN1_STRING_get0_data(data);
7075 data_len = ASN1_STRING_length(data);
7076 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007077 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007078 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007079 s = tmp;
7080 }
7081
7082 if (chunk_strcasecmp(entry, s) != 0)
7083 continue;
7084
7085 if (pos < 0)
7086 cur--;
7087 else
7088 cur++;
7089
7090 if (cur != pos)
7091 continue;
7092
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007093 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007094 return -1;
7095
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007096 memcpy(out->area, data_ptr, data_len);
7097 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007098 return 1;
7099 }
7100
7101 return 0;
7102
William Lallemandd4f946c2019-12-05 10:26:40 +01007103}
7104
7105/*
7106 * Extract and format the DNS SAN extensions and copy result into a chuink
7107 * Return 0;
7108 */
7109#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7110static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7111{
7112 int i;
7113 char *str;
7114 STACK_OF(GENERAL_NAME) *names = NULL;
7115
7116 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7117 if (names) {
7118 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7119 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7120 if (i > 0)
7121 chunk_appendf(out, ", ");
7122 if (name->type == GEN_DNS) {
7123 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7124 chunk_appendf(out, "DNS:%s", str);
7125 OPENSSL_free(str);
7126 }
7127 }
7128 }
7129 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7130 }
7131 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007132}
William Lallemandd4f946c2019-12-05 10:26:40 +01007133#endif
Emeric Brun87855892012-10-17 17:39:35 +02007134
Elliot Otchet71f82972020-01-15 08:12:14 -05007135/*
7136 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7137 * Currently supports rfc2253 for returning LDAP V3 DNs.
7138 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7139 */
7140static int
7141ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7142{
7143 BIO *bio = NULL;
7144 int ret = 0;
7145 int data_len = 0;
7146
7147 if (chunk_strcmp(format, "rfc2253") == 0) {
7148 bio = BIO_new(BIO_s_mem());
7149 if (bio == NULL)
7150 goto out;
7151
7152 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7153 goto out;
7154
7155 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7156 goto out;
7157
7158 out->data = data_len;
7159
7160 ret = 1;
7161 }
7162out:
7163 if (bio)
7164 BIO_free(bio);
7165 return ret;
7166}
7167
Emeric Brun87855892012-10-17 17:39:35 +02007168/* Extract and format full DN from a X509_NAME and copy result into a chunk
7169 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7170 */
7171static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007172ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007173{
7174 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007175 ASN1_OBJECT *obj;
7176 ASN1_STRING *data;
7177 const unsigned char *data_ptr;
7178 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007179 int i, n, ln;
7180 int l = 0;
7181 const char *s;
7182 char *p;
7183 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007184 int name_count;
7185
7186
7187 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007188
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007189 out->data = 0;
7190 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007191 for (i = 0; i < name_count; i++) {
7192 ne = X509_NAME_get_entry(a, i);
7193 obj = X509_NAME_ENTRY_get_object(ne);
7194 data = X509_NAME_ENTRY_get_data(ne);
7195 data_ptr = ASN1_STRING_get0_data(data);
7196 data_len = ASN1_STRING_length(data);
7197 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007198 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007199 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007200 s = tmp;
7201 }
7202 ln = strlen(s);
7203
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007204 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007205 if (l > out->size)
7206 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007207 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007208
7209 *(p++)='/';
7210 memcpy(p, s, ln);
7211 p += ln;
7212 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007213 memcpy(p, data_ptr, data_len);
7214 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007215 }
7216
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007217 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007218 return 0;
7219
7220 return 1;
7221}
7222
Olivier Houchardab28a322018-12-21 19:45:40 +01007223void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7224{
7225#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007226 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007227
Olivier Houcharde488ea82019-06-28 14:10:33 +02007228 if (!ssl_sock_is_ssl(conn))
7229 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007230 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007231 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007232#endif
7233}
7234
Willy Tarreau119a4082016-12-22 21:58:38 +01007235/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7236 * to disable SNI.
7237 */
Willy Tarreau63076412015-07-10 11:33:32 +02007238void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7239{
7240#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007241 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007242
Willy Tarreau119a4082016-12-22 21:58:38 +01007243 char *prev_name;
7244
Willy Tarreau63076412015-07-10 11:33:32 +02007245 if (!ssl_sock_is_ssl(conn))
7246 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007247 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007248
Willy Tarreau119a4082016-12-22 21:58:38 +01007249 /* if the SNI changes, we must destroy the reusable context so that a
7250 * new connection will present a new SNI. As an optimization we could
7251 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7252 * server.
7253 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007254 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007255 if ((!prev_name && hostname) ||
7256 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007257 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007258
Olivier Houchard66ab4982019-02-26 18:37:15 +01007259 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007260#endif
7261}
7262
Emeric Brun0abf8362014-06-24 18:26:41 +02007263/* Extract peer certificate's common name into the chunk dest
7264 * Returns
7265 * the len of the extracted common name
7266 * or 0 if no CN found in DN
7267 * or -1 on error case (i.e. no peer certificate)
7268 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007269int ssl_sock_get_remote_common_name(struct connection *conn,
7270 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007271{
Christopher Faulet82004142019-09-10 10:12:03 +02007272 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007273 X509 *crt = NULL;
7274 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007275 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007276 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007277 .area = (char *)&find_cn,
7278 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007279 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007280 int result = -1;
David Safb76832014-05-08 23:42:08 -04007281
7282 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007283 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007284 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007285
7286 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007287 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007288 if (!crt)
7289 goto out;
7290
7291 name = X509_get_subject_name(crt);
7292 if (!name)
7293 goto out;
David Safb76832014-05-08 23:42:08 -04007294
Emeric Brun0abf8362014-06-24 18:26:41 +02007295 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7296out:
David Safb76832014-05-08 23:42:08 -04007297 if (crt)
7298 X509_free(crt);
7299
7300 return result;
7301}
7302
Dave McCowan328fb582014-07-30 10:39:13 -04007303/* returns 1 if client passed a certificate for this session, 0 if not */
7304int ssl_sock_get_cert_used_sess(struct connection *conn)
7305{
Christopher Faulet82004142019-09-10 10:12:03 +02007306 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007307 X509 *crt = NULL;
7308
7309 if (!ssl_sock_is_ssl(conn))
7310 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007311 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007312
7313 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007314 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007315 if (!crt)
7316 return 0;
7317
7318 X509_free(crt);
7319 return 1;
7320}
7321
7322/* returns 1 if client passed a certificate for this connection, 0 if not */
7323int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007324{
Christopher Faulet82004142019-09-10 10:12:03 +02007325 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007326
David Safb76832014-05-08 23:42:08 -04007327 if (!ssl_sock_is_ssl(conn))
7328 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007329 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007330 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007331}
7332
7333/* returns result from SSL verify */
7334unsigned int ssl_sock_get_verify_result(struct connection *conn)
7335{
Christopher Faulet82004142019-09-10 10:12:03 +02007336 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007337
David Safb76832014-05-08 23:42:08 -04007338 if (!ssl_sock_is_ssl(conn))
7339 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007340 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007341 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007342}
7343
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007344/* Returns the application layer protocol name in <str> and <len> when known.
7345 * Zero is returned if the protocol name was not found, otherwise non-zero is
7346 * returned. The string is allocated in the SSL context and doesn't have to be
7347 * freed by the caller. NPN is also checked if available since older versions
7348 * of openssl (1.0.1) which are more common in field only support this one.
7349 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007350static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007351{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007352#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7353 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007354 struct ssl_sock_ctx *ctx = xprt_ctx;
7355 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007356 return 0;
7357
7358 *str = NULL;
7359
7360#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007361 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007362 if (*str)
7363 return 1;
7364#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007365#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007366 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007367 if (*str)
7368 return 1;
7369#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007370#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007371 return 0;
7372}
7373
Willy Tarreau7875d092012-09-10 08:20:03 +02007374/***** Below are some sample fetching functions for ACL/patterns *****/
7375
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007376static int
7377smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7378{
7379 struct connection *conn;
7380
7381 conn = objt_conn(smp->sess->origin);
7382 if (!conn || conn->xprt != &ssl_sock)
7383 return 0;
7384
7385 smp->flags = 0;
7386 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007387#ifdef OPENSSL_IS_BORINGSSL
7388 {
7389 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7390 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7391 SSL_early_data_accepted(ctx->ssl));
7392 }
7393#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007394 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
7395 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007396#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007397 return 1;
7398}
7399
Emeric Brune64aef12012-09-21 13:15:06 +02007400/* boolean, returns true if client cert was present */
7401static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007402smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007403{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007404 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007405 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007406
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007407 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007408 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007409 return 0;
7410
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007411 ctx = conn->xprt_ctx;
7412
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007413 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02007414 smp->flags |= SMP_F_MAY_CHANGE;
7415 return 0;
7416 }
7417
7418 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007419 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007420 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007421
7422 return 1;
7423}
7424
Emeric Brun43e79582014-10-29 19:03:26 +01007425/* binary, returns a certificate in a binary chunk (der/raw).
7426 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7427 * should be use.
7428 */
7429static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007430smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007431{
7432 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7433 X509 *crt = NULL;
7434 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007435 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007436 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007437 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007438
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007439 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007440 if (!conn || conn->xprt != &ssl_sock)
7441 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007442 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007443
7444 if (!(conn->flags & CO_FL_CONNECTED)) {
7445 smp->flags |= SMP_F_MAY_CHANGE;
7446 return 0;
7447 }
7448
7449 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007450 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007451 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007452 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007453
7454 if (!crt)
7455 goto out;
7456
7457 smp_trash = get_trash_chunk();
7458 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7459 goto out;
7460
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007461 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007462 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007463 ret = 1;
7464out:
7465 /* SSL_get_peer_certificate, it increase X509 * ref count */
7466 if (cert_peer && crt)
7467 X509_free(crt);
7468 return ret;
7469}
7470
Emeric Brunba841a12014-04-30 17:05:08 +02007471/* binary, returns serial of certificate in a binary chunk.
7472 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7473 * should be use.
7474 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007475static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007476smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007477{
Emeric Brunba841a12014-04-30 17:05:08 +02007478 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007479 X509 *crt = NULL;
7480 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007481 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007482 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007483 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007484
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007485 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007486 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007487 return 0;
7488
Olivier Houchard66ab4982019-02-26 18:37:15 +01007489 ctx = conn->xprt_ctx;
7490
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007491 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007492 smp->flags |= SMP_F_MAY_CHANGE;
7493 return 0;
7494 }
7495
Emeric Brunba841a12014-04-30 17:05:08 +02007496 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007497 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007498 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007499 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007500
Willy Tarreau8d598402012-10-22 17:58:39 +02007501 if (!crt)
7502 goto out;
7503
Willy Tarreau47ca5452012-12-23 20:22:19 +01007504 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007505 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7506 goto out;
7507
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007508 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007509 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007510 ret = 1;
7511out:
Emeric Brunba841a12014-04-30 17:05:08 +02007512 /* SSL_get_peer_certificate, it increase X509 * ref count */
7513 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007514 X509_free(crt);
7515 return ret;
7516}
Emeric Brune64aef12012-09-21 13:15:06 +02007517
Emeric Brunba841a12014-04-30 17:05:08 +02007518/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7519 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7520 * should be use.
7521 */
James Votha051b4a2013-05-14 20:37:59 +02007522static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007523smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007524{
Emeric Brunba841a12014-04-30 17:05:08 +02007525 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007526 X509 *crt = NULL;
7527 const EVP_MD *digest;
7528 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007529 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007530 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007531 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02007532
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007533 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007534 if (!conn || conn->xprt != &ssl_sock)
7535 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007536 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007537
7538 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02007539 smp->flags |= SMP_F_MAY_CHANGE;
7540 return 0;
7541 }
7542
Emeric Brunba841a12014-04-30 17:05:08 +02007543 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007544 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007545 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007546 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02007547 if (!crt)
7548 goto out;
7549
7550 smp_trash = get_trash_chunk();
7551 digest = EVP_sha1();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007552 X509_digest(crt, digest, (unsigned char *) smp_trash->area,
7553 (unsigned int *)&smp_trash->data);
James Votha051b4a2013-05-14 20:37:59 +02007554
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007555 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007556 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02007557 ret = 1;
7558out:
Emeric Brunba841a12014-04-30 17:05:08 +02007559 /* SSL_get_peer_certificate, it increase X509 * ref count */
7560 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02007561 X509_free(crt);
7562 return ret;
7563}
7564
Emeric Brunba841a12014-04-30 17:05:08 +02007565/* string, returns certificate's notafter date in ASN1_UTCTIME format.
7566 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7567 * should be use.
7568 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007569static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007570smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007571{
Emeric Brunba841a12014-04-30 17:05:08 +02007572 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007573 X509 *crt = NULL;
7574 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007575 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007576 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007577 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007578
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007579 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007580 if (!conn || conn->xprt != &ssl_sock)
7581 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007582 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007583
7584 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007585 smp->flags |= SMP_F_MAY_CHANGE;
7586 return 0;
7587 }
7588
Emeric Brunba841a12014-04-30 17:05:08 +02007589 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007590 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007591 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007592 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007593 if (!crt)
7594 goto out;
7595
Willy Tarreau47ca5452012-12-23 20:22:19 +01007596 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007597 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007598 goto out;
7599
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007600 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007601 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007602 ret = 1;
7603out:
Emeric Brunba841a12014-04-30 17:05:08 +02007604 /* SSL_get_peer_certificate, it increase X509 * ref count */
7605 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007606 X509_free(crt);
7607 return ret;
7608}
7609
Emeric Brunba841a12014-04-30 17:05:08 +02007610/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
7611 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7612 * should be use.
7613 */
Emeric Brun87855892012-10-17 17:39:35 +02007614static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007615smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007616{
Emeric Brunba841a12014-04-30 17:05:08 +02007617 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007618 X509 *crt = NULL;
7619 X509_NAME *name;
7620 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007621 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007622 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007623 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007624
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007625 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007626 if (!conn || conn->xprt != &ssl_sock)
7627 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007628 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007629
7630 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007631 smp->flags |= SMP_F_MAY_CHANGE;
7632 return 0;
7633 }
7634
Emeric Brunba841a12014-04-30 17:05:08 +02007635 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007636 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007637 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007638 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007639 if (!crt)
7640 goto out;
7641
7642 name = X509_get_issuer_name(crt);
7643 if (!name)
7644 goto out;
7645
Willy Tarreau47ca5452012-12-23 20:22:19 +01007646 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007647 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007648 int pos = 1;
7649
7650 if (args[1].type == ARGT_SINT)
7651 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007652
7653 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7654 goto out;
7655 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007656 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7657 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7658 goto out;
7659 }
Emeric Brun87855892012-10-17 17:39:35 +02007660 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7661 goto out;
7662
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007663 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007664 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007665 ret = 1;
7666out:
Emeric Brunba841a12014-04-30 17:05:08 +02007667 /* SSL_get_peer_certificate, it increase X509 * ref count */
7668 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007669 X509_free(crt);
7670 return ret;
7671}
7672
Emeric Brunba841a12014-04-30 17:05:08 +02007673/* string, returns notbefore date in ASN1_UTCTIME format.
7674 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7675 * should be use.
7676 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007677static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007678smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007679{
Emeric Brunba841a12014-04-30 17:05:08 +02007680 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007681 X509 *crt = NULL;
7682 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007683 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007684 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007685 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007686
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007687 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007688 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02007689 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007690 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007691
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007692 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007693 smp->flags |= SMP_F_MAY_CHANGE;
7694 return 0;
7695 }
7696
Emeric Brunba841a12014-04-30 17:05:08 +02007697 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007698 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007699 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007700 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007701 if (!crt)
7702 goto out;
7703
Willy Tarreau47ca5452012-12-23 20:22:19 +01007704 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007705 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007706 goto out;
7707
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007708 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007709 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007710 ret = 1;
7711out:
Emeric Brunba841a12014-04-30 17:05:08 +02007712 /* SSL_get_peer_certificate, it increase X509 * ref count */
7713 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007714 X509_free(crt);
7715 return ret;
7716}
7717
Emeric Brunba841a12014-04-30 17:05:08 +02007718/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
7719 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7720 * should be use.
7721 */
Emeric Brun87855892012-10-17 17:39:35 +02007722static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007723smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007724{
Emeric Brunba841a12014-04-30 17:05:08 +02007725 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007726 X509 *crt = NULL;
7727 X509_NAME *name;
7728 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007729 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007730 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007731 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007732
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007733 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007734 if (!conn || conn->xprt != &ssl_sock)
7735 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007736 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007737
7738 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007739 smp->flags |= SMP_F_MAY_CHANGE;
7740 return 0;
7741 }
7742
Emeric Brunba841a12014-04-30 17:05:08 +02007743 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007744 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007745 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007746 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007747 if (!crt)
7748 goto out;
7749
7750 name = X509_get_subject_name(crt);
7751 if (!name)
7752 goto out;
7753
Willy Tarreau47ca5452012-12-23 20:22:19 +01007754 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007755 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007756 int pos = 1;
7757
7758 if (args[1].type == ARGT_SINT)
7759 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007760
7761 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7762 goto out;
7763 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007764 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7765 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7766 goto out;
7767 }
Emeric Brun87855892012-10-17 17:39:35 +02007768 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7769 goto out;
7770
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007771 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007772 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007773 ret = 1;
7774out:
Emeric Brunba841a12014-04-30 17:05:08 +02007775 /* SSL_get_peer_certificate, it increase X509 * ref count */
7776 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007777 X509_free(crt);
7778 return ret;
7779}
Emeric Brun9143d372012-12-20 15:44:16 +01007780
7781/* integer, returns true if current session use a client certificate */
7782static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007783smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007784{
7785 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007786 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007787 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007788
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007789 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007790 if (!conn || conn->xprt != &ssl_sock)
7791 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007792 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007793
7794 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01007795 smp->flags |= SMP_F_MAY_CHANGE;
7796 return 0;
7797 }
7798
7799 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007800 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007801 if (crt) {
7802 X509_free(crt);
7803 }
7804
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007805 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007806 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007807 return 1;
7808}
7809
Emeric Brunba841a12014-04-30 17:05:08 +02007810/* integer, returns the certificate version
7811 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7812 * should be use.
7813 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007814static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007815smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007816{
Emeric Brunba841a12014-04-30 17:05:08 +02007817 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007818 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007819 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007820 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007821
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007822 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007823 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007824 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007825 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007826
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007827 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007828 smp->flags |= SMP_F_MAY_CHANGE;
7829 return 0;
7830 }
7831
Emeric Brunba841a12014-04-30 17:05:08 +02007832 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007833 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007834 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007835 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007836 if (!crt)
7837 return 0;
7838
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007839 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007840 /* SSL_get_peer_certificate increase X509 * ref count */
7841 if (cert_peer)
7842 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007843 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007844
7845 return 1;
7846}
7847
Emeric Brunba841a12014-04-30 17:05:08 +02007848/* string, returns the certificate's signature algorithm.
7849 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7850 * should be use.
7851 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007852static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007853smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007854{
Emeric Brunba841a12014-04-30 17:05:08 +02007855 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007856 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007857 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007858 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007859 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007860 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007861
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007862 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007863 if (!conn || conn->xprt != &ssl_sock)
7864 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007865 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007866
7867 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007868 smp->flags |= SMP_F_MAY_CHANGE;
7869 return 0;
7870 }
7871
Emeric Brunba841a12014-04-30 17:05:08 +02007872 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007873 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007874 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007875 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007876 if (!crt)
7877 return 0;
7878
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007879 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7880 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007881
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007882 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7883 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007884 /* SSL_get_peer_certificate increase X509 * ref count */
7885 if (cert_peer)
7886 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007887 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007888 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007889
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007890 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007891 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007892 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007893 /* SSL_get_peer_certificate increase X509 * ref count */
7894 if (cert_peer)
7895 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007896
7897 return 1;
7898}
7899
Emeric Brunba841a12014-04-30 17:05:08 +02007900/* string, returns the certificate's key algorithm.
7901 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7902 * should be use.
7903 */
Emeric Brun521a0112012-10-22 12:22:55 +02007904static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007905smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02007906{
Emeric Brunba841a12014-04-30 17:05:08 +02007907 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02007908 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007909 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02007910 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007911 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007912 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02007913
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007914 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007915 if (!conn || conn->xprt != &ssl_sock)
7916 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007917 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007918
7919 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02007920 smp->flags |= SMP_F_MAY_CHANGE;
7921 return 0;
7922 }
7923
Emeric Brunba841a12014-04-30 17:05:08 +02007924 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007925 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007926 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007927 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02007928 if (!crt)
7929 return 0;
7930
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007931 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
7932 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02007933
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007934 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7935 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007936 /* SSL_get_peer_certificate increase X509 * ref count */
7937 if (cert_peer)
7938 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007939 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007940 }
Emeric Brun521a0112012-10-22 12:22:55 +02007941
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007942 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007943 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007944 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007945 if (cert_peer)
7946 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007947
7948 return 1;
7949}
7950
Emeric Brun645ae792014-04-30 14:21:06 +02007951/* boolean, returns true if front conn. transport layer is SSL.
7952 * This function is also usable on backend conn if the fetch keyword 5th
7953 * char is 'b'.
7954 */
Willy Tarreau7875d092012-09-10 08:20:03 +02007955static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007956smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007957{
Emeric Bruneb8def92018-02-19 15:59:48 +01007958 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7959 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007960
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007961 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007962 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007963 return 1;
7964}
7965
Emeric Brun2525b6b2012-10-18 15:59:43 +02007966/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007967static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007968smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007969{
7970#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007971 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007972 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007973
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007974 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007975 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007976 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007977 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007978 return 1;
7979#else
7980 return 0;
7981#endif
7982}
7983
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007984/* boolean, returns true if client session has been resumed.
7985 * This function is also usable on backend conn if the fetch keyword 5th
7986 * char is 'b'.
7987 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007988static int
7989smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
7990{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007991 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7992 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007993 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007994
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007995
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007996 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007997 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007998 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007999 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008000 return 1;
8001}
8002
Emeric Brun645ae792014-04-30 14:21:06 +02008003/* string, returns the used cipher if front conn. transport layer is SSL.
8004 * This function is also usable on backend conn if the fetch keyword 5th
8005 * char is 'b'.
8006 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008007static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008008smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008009{
Emeric Bruneb8def92018-02-19 15:59:48 +01008010 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8011 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008012 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008013
Willy Tarreaube508f12016-03-10 11:47:01 +01008014 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008015 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008016 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008017 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008018
Olivier Houchard66ab4982019-02-26 18:37:15 +01008019 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008020 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008021 return 0;
8022
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008023 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008024 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008025 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008026
8027 return 1;
8028}
8029
Emeric Brun645ae792014-04-30 14:21:06 +02008030/* integer, returns the algoritm's keysize if front conn. transport layer
8031 * is SSL.
8032 * This function is also usable on backend conn if the fetch keyword 5th
8033 * char is 'b'.
8034 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008035static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008036smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008037{
Emeric Bruneb8def92018-02-19 15:59:48 +01008038 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8039 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008040 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008041 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008042
Emeric Brun589fcad2012-10-16 14:13:26 +02008043 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008044 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008045 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008046 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008047
Olivier Houchard66ab4982019-02-26 18:37:15 +01008048 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008049 return 0;
8050
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008051 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008052 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008053
8054 return 1;
8055}
8056
Emeric Brun645ae792014-04-30 14:21:06 +02008057/* integer, returns the used keysize if front conn. transport layer is SSL.
8058 * This function is also usable on backend conn if the fetch keyword 5th
8059 * char is 'b'.
8060 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008061static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008062smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008063{
Emeric Bruneb8def92018-02-19 15:59:48 +01008064 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8065 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008066 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008067
Emeric Brun589fcad2012-10-16 14:13:26 +02008068 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008069 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8070 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008071 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008072
Olivier Houchard66ab4982019-02-26 18:37:15 +01008073 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008074 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008075 return 0;
8076
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008077 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008078
8079 return 1;
8080}
8081
Bernard Spil13c53f82018-02-15 13:34:58 +01008082#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008083static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008084smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008085{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008086 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008087 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008088
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008089 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008090 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008091
Olivier Houchard6b77f492018-11-22 18:18:29 +01008092 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8093 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008094 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8095 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008096 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008097
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008098 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008099 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008100 (const unsigned char **)&smp->data.u.str.area,
8101 (unsigned *)&smp->data.u.str.data);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008102
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008103 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008104 return 0;
8105
8106 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008107}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008108#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008109
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008110#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008111static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008112smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008113{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008114 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008115 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008116
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008117 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008118 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008119
Olivier Houchard6b77f492018-11-22 18:18:29 +01008120 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8121 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8122
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008123 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008124 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008125 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008126
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008127 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008128 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008129 (const unsigned char **)&smp->data.u.str.area,
8130 (unsigned *)&smp->data.u.str.data);
Willy Tarreauab861d32013-04-02 02:30:41 +02008131
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008132 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008133 return 0;
8134
8135 return 1;
8136}
8137#endif
8138
Emeric Brun645ae792014-04-30 14:21:06 +02008139/* string, returns the used protocol if front conn. transport layer is SSL.
8140 * This function is also usable on backend conn if the fetch keyword 5th
8141 * char is 'b'.
8142 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008143static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008144smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008145{
Emeric Bruneb8def92018-02-19 15:59:48 +01008146 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8147 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008148 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008149
Emeric Brun589fcad2012-10-16 14:13:26 +02008150 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008151 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8152 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008153 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008154
Olivier Houchard66ab4982019-02-26 18:37:15 +01008155 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008156 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008157 return 0;
8158
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008159 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008160 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008161 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008162
8163 return 1;
8164}
8165
Willy Tarreau87b09662015-04-03 00:22:06 +02008166/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008167 * This function is also usable on backend conn if the fetch keyword 5th
8168 * char is 'b'.
8169 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008170#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008171static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008172smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008173{
Emeric Bruneb8def92018-02-19 15:59:48 +01008174 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8175 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008176 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008177 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008178
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008179 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008180 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008181
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008182 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8183 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008184 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008185
Olivier Houchard66ab4982019-02-26 18:37:15 +01008186 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008187 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008188 return 0;
8189
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008190 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess,
8191 (unsigned int *)&smp->data.u.str.data);
8192 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008193 return 0;
8194
8195 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008196}
Patrick Hemmer41966772018-04-28 19:15:48 -04008197#endif
8198
Emeric Brunfe68f682012-10-16 14:59:28 +02008199
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008200#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008201static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008202smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8203{
8204 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8205 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8206 struct buffer *data;
8207 struct ssl_sock_ctx *ctx;
8208
8209 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8210 return 0;
8211 ctx = conn->xprt_ctx;
8212
8213 data = get_trash_chunk();
8214 if (kw[7] == 'c')
8215 data->data = SSL_get_client_random(ctx->ssl,
8216 (unsigned char *) data->area,
8217 data->size);
8218 else
8219 data->data = SSL_get_server_random(ctx->ssl,
8220 (unsigned char *) data->area,
8221 data->size);
8222 if (!data->data)
8223 return 0;
8224
8225 smp->flags = 0;
8226 smp->data.type = SMP_T_BIN;
8227 smp->data.u.str = *data;
8228
8229 return 1;
8230}
8231
8232static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008233smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8234{
8235 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8236 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8237 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008238 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008239 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008240
8241 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8242 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008243 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008244
Olivier Houchard66ab4982019-02-26 18:37:15 +01008245 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008246 if (!ssl_sess)
8247 return 0;
8248
8249 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008250 data->data = SSL_SESSION_get_master_key(ssl_sess,
8251 (unsigned char *) data->area,
8252 data->size);
8253 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008254 return 0;
8255
8256 smp->flags = 0;
8257 smp->data.type = SMP_T_BIN;
8258 smp->data.u.str = *data;
8259
8260 return 1;
8261}
8262#endif
8263
Patrick Hemmer41966772018-04-28 19:15:48 -04008264#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008265static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008266smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008267{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008268 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008269 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008270
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008271 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008272 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008273
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008274 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008275 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8276 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008277 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008278
Olivier Houchard66ab4982019-02-26 18:37:15 +01008279 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008280 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008281 return 0;
8282
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008283 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008284 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008285}
Patrick Hemmer41966772018-04-28 19:15:48 -04008286#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008287
David Sc1ad52e2014-04-08 18:48:47 -04008288static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008289smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8290{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008291 struct connection *conn;
8292 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008293 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008294
8295 conn = objt_conn(smp->sess->origin);
8296 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8297 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008298 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008299
Olivier Houchard66ab4982019-02-26 18:37:15 +01008300 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008301 if (!capture)
8302 return 0;
8303
8304 smp->flags = SMP_F_CONST;
8305 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008306 smp->data.u.str.area = capture->ciphersuite;
8307 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008308 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008309}
8310
8311static int
8312smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8313{
Willy Tarreau83061a82018-07-13 11:56:34 +02008314 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008315
8316 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8317 return 0;
8318
8319 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008320 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008321 smp->data.type = SMP_T_BIN;
8322 smp->data.u.str = *data;
8323 return 1;
8324}
8325
8326static int
8327smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8328{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008329 struct connection *conn;
8330 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008331 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008332
8333 conn = objt_conn(smp->sess->origin);
8334 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8335 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008336 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008337
Olivier Houchard66ab4982019-02-26 18:37:15 +01008338 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008339 if (!capture)
8340 return 0;
8341
8342 smp->data.type = SMP_T_SINT;
8343 smp->data.u.sint = capture->xxh64;
8344 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008345}
8346
8347static int
8348smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8349{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008350#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008351 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008352 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008353
8354 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8355 return 0;
8356
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008357 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008358 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008359 const char *str;
8360 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008361 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008362 uint16_t id = (bin[0] << 8) | bin[1];
8363#if defined(OPENSSL_IS_BORINGSSL)
8364 cipher = SSL_get_cipher_by_value(id);
8365#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008366 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008367 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8368 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008369#endif
8370 str = SSL_CIPHER_get_name(cipher);
8371 if (!str || strcmp(str, "(NONE)") == 0)
8372 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008373 else
8374 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8375 }
8376 smp->data.type = SMP_T_STR;
8377 smp->data.u.str = *data;
8378 return 1;
8379#else
8380 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8381#endif
8382}
8383
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008384#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008385static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008386smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008387{
Emeric Bruneb8def92018-02-19 15:59:48 +01008388 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8389 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008390 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008391 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008392 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008393
8394 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008395 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8396 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008397 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008398
8399 if (!(conn->flags & CO_FL_CONNECTED)) {
8400 smp->flags |= SMP_F_MAY_CHANGE;
8401 return 0;
8402 }
8403
8404 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008405 if (!SSL_session_reused(ctx->ssl))
8406 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008407 finished_trash->area,
8408 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008409 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008410 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008411 finished_trash->area,
8412 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008413
8414 if (!finished_len)
8415 return 0;
8416
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008417 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008418 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008419 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008420
8421 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008422}
Patrick Hemmer41966772018-04-28 19:15:48 -04008423#endif
David Sc1ad52e2014-04-08 18:48:47 -04008424
Emeric Brun2525b6b2012-10-18 15:59:43 +02008425/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008426static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008427smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008428{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008429 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008430 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008431
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008432 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008433 if (!conn || conn->xprt != &ssl_sock)
8434 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008435 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008436
8437 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008438 smp->flags = SMP_F_MAY_CHANGE;
8439 return 0;
8440 }
8441
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008442 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008443 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008444 smp->flags = 0;
8445
8446 return 1;
8447}
8448
Emeric Brun2525b6b2012-10-18 15:59:43 +02008449/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008450static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008451smp_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 +02008452{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008453 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008454 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008455
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008456 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008457 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008458 return 0;
8459
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008460 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008461 smp->flags = SMP_F_MAY_CHANGE;
8462 return 0;
8463 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008464 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008465
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008466 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008467 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008468 smp->flags = 0;
8469
8470 return 1;
8471}
8472
Emeric Brun2525b6b2012-10-18 15:59:43 +02008473/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008474static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008475smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008476{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008477 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008478 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008479
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008480 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008481 if (!conn || conn->xprt != &ssl_sock)
8482 return 0;
8483
8484 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008485 smp->flags = SMP_F_MAY_CHANGE;
8486 return 0;
8487 }
8488
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008489 ctx = conn->xprt_ctx;
8490
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008491 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008492 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008493 smp->flags = 0;
8494
8495 return 1;
8496}
8497
Emeric Brun2525b6b2012-10-18 15:59:43 +02008498/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008499static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008500smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008501{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008502 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008503 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008504
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008505 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008506 if (!conn || conn->xprt != &ssl_sock)
8507 return 0;
8508
8509 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008510 smp->flags = SMP_F_MAY_CHANGE;
8511 return 0;
8512 }
8513
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008514 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008515 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008516 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008517
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008518 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008519 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008520 smp->flags = 0;
8521
8522 return 1;
8523}
8524
Emeric Brunfb510ea2012-10-05 12:00:26 +02008525/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008526static 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 +02008527{
8528 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008529 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008530 return ERR_ALERT | ERR_FATAL;
8531 }
8532
Willy Tarreauef934602016-12-22 23:12:01 +01008533 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8534 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008535 else
8536 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008537
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02008538 if (!ssl_store_load_locations_file(conf->ca_file)) {
8539 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->ca_file);
8540 return ERR_ALERT | ERR_FATAL;
8541 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02008542 return 0;
8543}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008544static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8545{
8546 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
8547}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008548
Christopher Faulet31af49d2015-06-09 17:29:50 +02008549/* parse the "ca-sign-file" bind keyword */
8550static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8551{
8552 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008553 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008554 return ERR_ALERT | ERR_FATAL;
8555 }
8556
Willy Tarreauef934602016-12-22 23:12:01 +01008557 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8558 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008559 else
8560 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
8561
8562 return 0;
8563}
8564
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008565/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02008566static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8567{
8568 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008569 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008570 return ERR_ALERT | ERR_FATAL;
8571 }
8572 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
8573 return 0;
8574}
8575
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008576/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008577static 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 +02008578{
8579 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008580 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008581 return ERR_ALERT | ERR_FATAL;
8582 }
8583
Emeric Brun76d88952012-10-05 15:47:31 +02008584 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02008585 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008586 return 0;
8587}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008588static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8589{
8590 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
8591}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008592
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008593#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008594/* parse the "ciphersuites" bind keyword */
8595static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8596{
8597 if (!*args[cur_arg + 1]) {
8598 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
8599 return ERR_ALERT | ERR_FATAL;
8600 }
8601
8602 free(conf->ciphersuites);
8603 conf->ciphersuites = strdup(args[cur_arg + 1]);
8604 return 0;
8605}
8606static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8607{
8608 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
8609}
8610#endif
8611
Willy Tarreaubbc91962019-10-16 16:42:19 +02008612/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008613static 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 +02008614{
Willy Tarreau38011032013-08-13 16:59:39 +02008615 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02008616
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008617 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008618 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008619 return ERR_ALERT | ERR_FATAL;
8620 }
8621
Willy Tarreauef934602016-12-22 23:12:01 +01008622 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
8623 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02008624 memprintf(err, "'%s' : path too long", args[cur_arg]);
8625 return ERR_ALERT | ERR_FATAL;
8626 }
Willy Tarreauef934602016-12-22 23:12:01 +01008627 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02008628 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008629 }
8630
Willy Tarreaubbc91962019-10-16 16:42:19 +02008631 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008632}
8633
Willy Tarreaubbc91962019-10-16 16:42:19 +02008634/* parse the "crt-list" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008635static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8636{
Willy Tarreaubbc91962019-10-16 16:42:19 +02008637 int err_code;
8638
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008639 if (!*args[cur_arg + 1]) {
8640 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
8641 return ERR_ALERT | ERR_FATAL;
8642 }
8643
Willy Tarreaubbc91962019-10-16 16:42:19 +02008644 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err);
8645 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02008646 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008647
Willy Tarreaubbc91962019-10-16 16:42:19 +02008648 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008649}
8650
Emeric Brunfb510ea2012-10-05 12:00:26 +02008651/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008652static 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 +02008653{
Emeric Brun051cdab2012-10-02 19:25:50 +02008654#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01008655 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02008656 return ERR_ALERT | ERR_FATAL;
8657#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02008658 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008659 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008660 return ERR_ALERT | ERR_FATAL;
8661 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008662
Willy Tarreauef934602016-12-22 23:12:01 +01008663 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8664 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008665 else
8666 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008667
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01008668 if (!ssl_store_load_locations_file(conf->crl_file)) {
8669 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
8670 return ERR_ALERT | ERR_FATAL;
8671 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008672 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02008673#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02008674}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008675static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8676{
8677 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
8678}
Emeric Brun2b58d042012-09-20 17:10:03 +02008679
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008680/* parse the "curves" bind keyword keyword */
8681static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8682{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01008683#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008684 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008685 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008686 return ERR_ALERT | ERR_FATAL;
8687 }
8688 conf->curves = strdup(args[cur_arg + 1]);
8689 return 0;
8690#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008691 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008692 return ERR_ALERT | ERR_FATAL;
8693#endif
8694}
8695static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8696{
8697 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
8698}
8699
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008700/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008701static 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 +02008702{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008703#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01008704 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008705 return ERR_ALERT | ERR_FATAL;
8706#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01008707 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008708 return ERR_ALERT | ERR_FATAL;
8709#else
8710 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008711 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008712 return ERR_ALERT | ERR_FATAL;
8713 }
8714
8715 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008716
8717 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02008718#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008719}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008720static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8721{
8722 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
8723}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008724
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008725/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02008726static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8727{
8728 int code;
8729 char *p = args[cur_arg + 1];
8730 unsigned long long *ignerr = &conf->crt_ignerr;
8731
8732 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008733 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008734 return ERR_ALERT | ERR_FATAL;
8735 }
8736
8737 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
8738 ignerr = &conf->ca_ignerr;
8739
8740 if (strcmp(p, "all") == 0) {
8741 *ignerr = ~0ULL;
8742 return 0;
8743 }
8744
8745 while (p) {
8746 code = atoi(p);
8747 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008748 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
8749 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008750 return ERR_ALERT | ERR_FATAL;
8751 }
8752 *ignerr |= 1ULL << code;
8753 p = strchr(p, ',');
8754 if (p)
8755 p++;
8756 }
8757
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008758 return 0;
8759}
8760
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008761/* parse tls_method_options "no-xxx" and "force-xxx" */
8762static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008763{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008764 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008765 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008766 p = strchr(arg, '-');
8767 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008768 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008769 p++;
8770 if (!strcmp(p, "sslv3"))
8771 v = CONF_SSLV3;
8772 else if (!strcmp(p, "tlsv10"))
8773 v = CONF_TLSV10;
8774 else if (!strcmp(p, "tlsv11"))
8775 v = CONF_TLSV11;
8776 else if (!strcmp(p, "tlsv12"))
8777 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008778 else if (!strcmp(p, "tlsv13"))
8779 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008780 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008781 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008782 if (!strncmp(arg, "no-", 3))
8783 methods->flags |= methodVersions[v].flag;
8784 else if (!strncmp(arg, "force-", 6))
8785 methods->min = methods->max = v;
8786 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008787 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008788 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008789 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01008790 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008791 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008792}
8793
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008794static 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 +02008795{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008796 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008797}
8798
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008799static 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 +02008800{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008801 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8802}
8803
8804/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8805static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8806{
8807 uint16_t i, v = 0;
8808 char *argv = args[cur_arg + 1];
8809 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008810 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008811 return ERR_ALERT | ERR_FATAL;
8812 }
8813 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8814 if (!strcmp(argv, methodVersions[i].name))
8815 v = i;
8816 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008817 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008818 return ERR_ALERT | ERR_FATAL;
8819 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008820 if (!strcmp("ssl-min-ver", args[cur_arg]))
8821 methods->min = v;
8822 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8823 methods->max = v;
8824 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01008825 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008826 return ERR_ALERT | ERR_FATAL;
8827 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008828 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008829}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008830
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008831static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8832{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008833#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008834 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 +02008835#endif
8836 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8837}
8838
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008839static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8840{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008841 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008842}
8843
8844static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8845{
8846 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8847}
8848
Emeric Brun2d0c4822012-10-02 13:45:20 +02008849/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008850static 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 +02008851{
Emeric Brun89675492012-10-05 13:48:26 +02008852 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008853 return 0;
8854}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008855
Olivier Houchardc2aae742017-09-22 18:26:28 +02008856/* parse the "allow-0rtt" bind keyword */
8857static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8858{
8859 conf->early_data = 1;
8860 return 0;
8861}
8862
8863static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8864{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008865 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008866 return 0;
8867}
8868
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008869/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008870static 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 +02008871{
Bernard Spil13c53f82018-02-15 13:34:58 +01008872#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008873 char *p1, *p2;
8874
8875 if (!*args[cur_arg + 1]) {
8876 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8877 return ERR_ALERT | ERR_FATAL;
8878 }
8879
8880 free(conf->npn_str);
8881
Willy Tarreau3724da12016-02-12 17:11:12 +01008882 /* the NPN string is built as a suite of (<len> <name>)*,
8883 * so we reuse each comma to store the next <len> and need
8884 * one more for the end of the string.
8885 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008886 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008887 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008888 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8889
8890 /* replace commas with the name length */
8891 p1 = conf->npn_str;
8892 p2 = p1 + 1;
8893 while (1) {
8894 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8895 if (!p2)
8896 p2 = p1 + 1 + strlen(p1 + 1);
8897
8898 if (p2 - (p1 + 1) > 255) {
8899 *p2 = '\0';
8900 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8901 return ERR_ALERT | ERR_FATAL;
8902 }
8903
8904 *p1 = p2 - (p1 + 1);
8905 p1 = p2;
8906
8907 if (!*p2)
8908 break;
8909
8910 *(p2++) = '\0';
8911 }
8912 return 0;
8913#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008914 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008915 return ERR_ALERT | ERR_FATAL;
8916#endif
8917}
8918
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008919static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8920{
8921 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8922}
8923
Willy Tarreauab861d32013-04-02 02:30:41 +02008924/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008925static 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 +02008926{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008927#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008928 char *p1, *p2;
8929
8930 if (!*args[cur_arg + 1]) {
8931 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
8932 return ERR_ALERT | ERR_FATAL;
8933 }
8934
8935 free(conf->alpn_str);
8936
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008937 /* the ALPN string is built as a suite of (<len> <name>)*,
8938 * so we reuse each comma to store the next <len> and need
8939 * one more for the end of the string.
8940 */
Willy Tarreauab861d32013-04-02 02:30:41 +02008941 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008942 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02008943 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
8944
8945 /* replace commas with the name length */
8946 p1 = conf->alpn_str;
8947 p2 = p1 + 1;
8948 while (1) {
8949 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
8950 if (!p2)
8951 p2 = p1 + 1 + strlen(p1 + 1);
8952
8953 if (p2 - (p1 + 1) > 255) {
8954 *p2 = '\0';
8955 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8956 return ERR_ALERT | ERR_FATAL;
8957 }
8958
8959 *p1 = p2 - (p1 + 1);
8960 p1 = p2;
8961
8962 if (!*p2)
8963 break;
8964
8965 *(p2++) = '\0';
8966 }
8967 return 0;
8968#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008969 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02008970 return ERR_ALERT | ERR_FATAL;
8971#endif
8972}
8973
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008974static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8975{
8976 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8977}
8978
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008979/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008980static 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 +02008981{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01008982 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02008983 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02008984
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008985 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
8986 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008987#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008988 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
8989 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
8990#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008991 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008992 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
8993 if (!conf->ssl_conf.ssl_methods.min)
8994 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
8995 if (!conf->ssl_conf.ssl_methods.max)
8996 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02008997
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008998 return 0;
8999}
9000
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009001/* parse the "prefer-client-ciphers" bind keyword */
9002static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9003{
9004 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9005 return 0;
9006}
9007
Christopher Faulet31af49d2015-06-09 17:29:50 +02009008/* parse the "generate-certificates" bind keyword */
9009static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9010{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009011#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009012 conf->generate_certs = 1;
9013#else
9014 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9015 err && *err ? *err : "");
9016#endif
9017 return 0;
9018}
9019
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009020/* parse the "strict-sni" bind keyword */
9021static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9022{
9023 conf->strict_sni = 1;
9024 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009025}
9026
9027/* parse the "tls-ticket-keys" bind keyword */
9028static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9029{
9030#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009031 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009032 int i = 0;
9033 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009034 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009035
9036 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009037 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009038 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009039 }
9040
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009041 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009042 if (keys_ref) {
9043 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009044 conf->keys_ref = keys_ref;
9045 return 0;
9046 }
9047
Christopher Faulete566f3d2019-10-21 09:55:49 +02009048 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009049 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009050 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009051 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009052 }
9053
Emeric Brun9e754772019-01-10 17:51:55 +01009054 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009055 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009056 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009057 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009058 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009059
9060 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009061 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009062 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009063 }
9064
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009065 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009066 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009067 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009068 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009069 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009070
Emeric Brun9e754772019-01-10 17:51:55 +01009071 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009072 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9073 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009074 int dec_size;
9075
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009076 /* Strip newline characters from the end */
9077 if(thisline[len - 1] == '\n')
9078 thisline[--len] = 0;
9079
9080 if(thisline[len - 1] == '\r')
9081 thisline[--len] = 0;
9082
Emeric Brun9e754772019-01-10 17:51:55 +01009083 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9084 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009085 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009086 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009087 }
Emeric Brun9e754772019-01-10 17:51:55 +01009088 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9089 keys_ref->key_size_bits = 128;
9090 }
9091 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9092 keys_ref->key_size_bits = 256;
9093 }
9094 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9095 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9096 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009097 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009098 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009099 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009100 i++;
9101 }
9102
9103 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009104 memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009105 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009106 }
9107
9108 fclose(f);
9109
9110 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009111 i -= 2;
9112 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009113 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009114 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009115 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009116 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009117
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009118 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9119
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009120 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009121
9122 fail:
9123 if (f)
9124 fclose(f);
9125 if (keys_ref) {
9126 free(keys_ref->filename);
9127 free(keys_ref->tlskeys);
9128 free(keys_ref);
9129 }
9130 return ERR_ALERT | ERR_FATAL;
9131
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009132#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009133 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009134 return ERR_ALERT | ERR_FATAL;
9135#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009136}
9137
Emeric Brund94b3fe2012-09-20 18:23:56 +02009138/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009139static 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 +02009140{
9141 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009142 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009143 return ERR_ALERT | ERR_FATAL;
9144 }
9145
9146 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009147 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009148 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009149 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009150 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009151 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009152 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009153 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9154 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009155 return ERR_ALERT | ERR_FATAL;
9156 }
9157
9158 return 0;
9159}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009160static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9161{
9162 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9163}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009164
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009165/* parse the "no-ca-names" bind keyword */
9166static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9167{
9168 conf->no_ca_names = 1;
9169 return 0;
9170}
9171static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9172{
9173 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9174}
9175
Willy Tarreau92faadf2012-10-10 23:04:25 +02009176/************** "server" keywords ****************/
9177
Olivier Houchardc7566002018-11-20 23:33:50 +01009178/* parse the "npn" bind keyword */
9179static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9180{
9181#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9182 char *p1, *p2;
9183
9184 if (!*args[*cur_arg + 1]) {
9185 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9186 return ERR_ALERT | ERR_FATAL;
9187 }
9188
9189 free(newsrv->ssl_ctx.npn_str);
9190
9191 /* the NPN string is built as a suite of (<len> <name>)*,
9192 * so we reuse each comma to store the next <len> and need
9193 * one more for the end of the string.
9194 */
9195 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9196 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9197 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9198 newsrv->ssl_ctx.npn_len);
9199
9200 /* replace commas with the name length */
9201 p1 = newsrv->ssl_ctx.npn_str;
9202 p2 = p1 + 1;
9203 while (1) {
9204 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9205 newsrv->ssl_ctx.npn_len - (p1 + 1));
9206 if (!p2)
9207 p2 = p1 + 1 + strlen(p1 + 1);
9208
9209 if (p2 - (p1 + 1) > 255) {
9210 *p2 = '\0';
9211 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9212 return ERR_ALERT | ERR_FATAL;
9213 }
9214
9215 *p1 = p2 - (p1 + 1);
9216 p1 = p2;
9217
9218 if (!*p2)
9219 break;
9220
9221 *(p2++) = '\0';
9222 }
9223 return 0;
9224#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009225 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009226 return ERR_ALERT | ERR_FATAL;
9227#endif
9228}
9229
Olivier Houchard92150142018-12-21 19:47:01 +01009230/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009231static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9232{
9233#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9234 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009235 char **alpn_str;
9236 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009237
Olivier Houchard92150142018-12-21 19:47:01 +01009238 if (*args[*cur_arg] == 'c') {
9239 alpn_str = &newsrv->check.alpn_str;
9240 alpn_len = &newsrv->check.alpn_len;
9241 } else {
9242 alpn_str = &newsrv->ssl_ctx.alpn_str;
9243 alpn_len = &newsrv->ssl_ctx.alpn_len;
9244
9245 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009246 if (!*args[*cur_arg + 1]) {
9247 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9248 return ERR_ALERT | ERR_FATAL;
9249 }
9250
Olivier Houchard92150142018-12-21 19:47:01 +01009251 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009252
9253 /* the ALPN string is built as a suite of (<len> <name>)*,
9254 * so we reuse each comma to store the next <len> and need
9255 * one more for the end of the string.
9256 */
Olivier Houchard92150142018-12-21 19:47:01 +01009257 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9258 *alpn_str = calloc(1, *alpn_len + 1);
9259 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009260
9261 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009262 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009263 p2 = p1 + 1;
9264 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009265 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009266 if (!p2)
9267 p2 = p1 + 1 + strlen(p1 + 1);
9268
9269 if (p2 - (p1 + 1) > 255) {
9270 *p2 = '\0';
9271 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9272 return ERR_ALERT | ERR_FATAL;
9273 }
9274
9275 *p1 = p2 - (p1 + 1);
9276 p1 = p2;
9277
9278 if (!*p2)
9279 break;
9280
9281 *(p2++) = '\0';
9282 }
9283 return 0;
9284#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009285 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009286 return ERR_ALERT | ERR_FATAL;
9287#endif
9288}
9289
Emeric Brunef42d922012-10-11 16:11:36 +02009290/* parse the "ca-file" server keyword */
9291static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9292{
9293 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009294 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009295 return ERR_ALERT | ERR_FATAL;
9296 }
9297
Willy Tarreauef934602016-12-22 23:12:01 +01009298 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9299 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009300 else
9301 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9302
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009303 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9304 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9305 return ERR_ALERT | ERR_FATAL;
9306 }
Emeric Brunef42d922012-10-11 16:11:36 +02009307 return 0;
9308}
9309
Olivier Houchard9130a962017-10-17 17:33:43 +02009310/* parse the "check-sni" server keyword */
9311static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9312{
9313 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009314 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009315 return ERR_ALERT | ERR_FATAL;
9316 }
9317
9318 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9319 if (!newsrv->check.sni) {
9320 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9321 return ERR_ALERT | ERR_FATAL;
9322 }
9323 return 0;
9324
9325}
9326
Willy Tarreau92faadf2012-10-10 23:04:25 +02009327/* parse the "check-ssl" server keyword */
9328static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9329{
9330 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009331 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9332 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009333#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009334 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9335 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9336#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009337 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009338 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9339 if (!newsrv->ssl_ctx.methods.min)
9340 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9341 if (!newsrv->ssl_ctx.methods.max)
9342 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9343
Willy Tarreau92faadf2012-10-10 23:04:25 +02009344 return 0;
9345}
9346
9347/* parse the "ciphers" server keyword */
9348static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9349{
9350 if (!*args[*cur_arg + 1]) {
9351 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9352 return ERR_ALERT | ERR_FATAL;
9353 }
9354
9355 free(newsrv->ssl_ctx.ciphers);
9356 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9357 return 0;
9358}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009359
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009360#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009361/* parse the "ciphersuites" server keyword */
9362static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9363{
9364 if (!*args[*cur_arg + 1]) {
9365 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9366 return ERR_ALERT | ERR_FATAL;
9367 }
9368
9369 free(newsrv->ssl_ctx.ciphersuites);
9370 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9371 return 0;
9372}
9373#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009374
Emeric Brunef42d922012-10-11 16:11:36 +02009375/* parse the "crl-file" server keyword */
9376static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9377{
9378#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009379 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009380 return ERR_ALERT | ERR_FATAL;
9381#else
9382 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009383 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009384 return ERR_ALERT | ERR_FATAL;
9385 }
9386
Willy Tarreauef934602016-12-22 23:12:01 +01009387 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9388 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009389 else
9390 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9391
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009392 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9393 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9394 return ERR_ALERT | ERR_FATAL;
9395 }
Emeric Brunef42d922012-10-11 16:11:36 +02009396 return 0;
9397#endif
9398}
9399
Emeric Bruna7aa3092012-10-26 12:58:00 +02009400/* parse the "crt" server keyword */
9401static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9402{
9403 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009404 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009405 return ERR_ALERT | ERR_FATAL;
9406 }
9407
Willy Tarreauef934602016-12-22 23:12:01 +01009408 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009409 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009410 else
9411 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9412
9413 return 0;
9414}
Emeric Brunef42d922012-10-11 16:11:36 +02009415
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009416/* parse the "no-check-ssl" server keyword */
9417static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9418{
9419 newsrv->check.use_ssl = 0;
9420 free(newsrv->ssl_ctx.ciphers);
9421 newsrv->ssl_ctx.ciphers = NULL;
9422 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9423 return 0;
9424}
9425
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009426/* parse the "no-send-proxy-v2-ssl" server keyword */
9427static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9428{
9429 newsrv->pp_opts &= ~SRV_PP_V2;
9430 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9431 return 0;
9432}
9433
9434/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9435static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9436{
9437 newsrv->pp_opts &= ~SRV_PP_V2;
9438 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9439 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9440 return 0;
9441}
9442
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009443/* parse the "no-ssl" server keyword */
9444static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9445{
9446 newsrv->use_ssl = 0;
9447 free(newsrv->ssl_ctx.ciphers);
9448 newsrv->ssl_ctx.ciphers = NULL;
9449 return 0;
9450}
9451
Olivier Houchard522eea72017-11-03 16:27:47 +01009452/* parse the "allow-0rtt" server keyword */
9453static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9454{
9455 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9456 return 0;
9457}
9458
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009459/* parse the "no-ssl-reuse" server keyword */
9460static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9461{
9462 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9463 return 0;
9464}
9465
Emeric Brunf9c5c472012-10-11 15:28:34 +02009466/* parse the "no-tls-tickets" server keyword */
9467static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9468{
9469 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9470 return 0;
9471}
David Safb76832014-05-08 23:42:08 -04009472/* parse the "send-proxy-v2-ssl" server keyword */
9473static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9474{
9475 newsrv->pp_opts |= SRV_PP_V2;
9476 newsrv->pp_opts |= SRV_PP_V2_SSL;
9477 return 0;
9478}
9479
9480/* parse the "send-proxy-v2-ssl-cn" server keyword */
9481static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9482{
9483 newsrv->pp_opts |= SRV_PP_V2;
9484 newsrv->pp_opts |= SRV_PP_V2_SSL;
9485 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9486 return 0;
9487}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009488
Willy Tarreau732eac42015-07-09 11:40:25 +02009489/* parse the "sni" server keyword */
9490static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9491{
9492#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9493 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9494 return ERR_ALERT | ERR_FATAL;
9495#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009496 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009497
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009498 arg = args[*cur_arg + 1];
9499 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009500 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9501 return ERR_ALERT | ERR_FATAL;
9502 }
9503
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009504 free(newsrv->sni_expr);
9505 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02009506
Willy Tarreau732eac42015-07-09 11:40:25 +02009507 return 0;
9508#endif
9509}
9510
Willy Tarreau92faadf2012-10-10 23:04:25 +02009511/* parse the "ssl" server keyword */
9512static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9513{
9514 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009515 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9516 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009517#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009518 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9519 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9520#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009521 return 0;
9522}
9523
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009524/* parse the "ssl-reuse" server keyword */
9525static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9526{
9527 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
9528 return 0;
9529}
9530
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009531/* parse the "tls-tickets" server keyword */
9532static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9533{
9534 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
9535 return 0;
9536}
9537
Emeric Brunef42d922012-10-11 16:11:36 +02009538/* parse the "verify" server keyword */
9539static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9540{
9541 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009542 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009543 return ERR_ALERT | ERR_FATAL;
9544 }
9545
9546 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009547 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02009548 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009549 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02009550 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009551 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
9552 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009553 return ERR_ALERT | ERR_FATAL;
9554 }
9555
Evan Broderbe554312013-06-27 00:05:25 -07009556 return 0;
9557}
9558
9559/* parse the "verifyhost" server keyword */
9560static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9561{
9562 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009563 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -07009564 return ERR_ALERT | ERR_FATAL;
9565 }
9566
Frédéric Lécaille273f3212017-03-13 15:52:01 +01009567 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07009568 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
9569
Emeric Brunef42d922012-10-11 16:11:36 +02009570 return 0;
9571}
9572
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009573/* parse the "ssl-default-bind-options" keyword in global section */
9574static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
9575 struct proxy *defpx, const char *file, int line,
9576 char **err) {
9577 int i = 1;
9578
9579 if (*(args[i]) == 0) {
9580 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9581 return -1;
9582 }
9583 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009584 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009585 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009586 else if (!strcmp(args[i], "prefer-client-ciphers"))
9587 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009588 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9589 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
9590 i++;
9591 else {
9592 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9593 return -1;
9594 }
9595 }
9596 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009597 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9598 return -1;
9599 }
9600 i++;
9601 }
9602 return 0;
9603}
9604
9605/* parse the "ssl-default-server-options" keyword in global section */
9606static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
9607 struct proxy *defpx, const char *file, int line,
9608 char **err) {
9609 int i = 1;
9610
9611 if (*(args[i]) == 0) {
9612 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9613 return -1;
9614 }
9615 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009616 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009617 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009618 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9619 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
9620 i++;
9621 else {
9622 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9623 return -1;
9624 }
9625 }
9626 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009627 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9628 return -1;
9629 }
9630 i++;
9631 }
9632 return 0;
9633}
9634
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009635/* parse the "ca-base" / "crt-base" keywords in global section.
9636 * Returns <0 on alert, >0 on warning, 0 on success.
9637 */
9638static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
9639 struct proxy *defpx, const char *file, int line,
9640 char **err)
9641{
9642 char **target;
9643
Willy Tarreauef934602016-12-22 23:12:01 +01009644 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009645
9646 if (too_many_args(1, args, err, NULL))
9647 return -1;
9648
9649 if (*target) {
9650 memprintf(err, "'%s' already specified.", args[0]);
9651 return -1;
9652 }
9653
9654 if (*(args[1]) == 0) {
9655 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
9656 return -1;
9657 }
9658 *target = strdup(args[1]);
9659 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009660}
9661
9662/* parse the "ssl-mode-async" keyword in global section.
9663 * Returns <0 on alert, >0 on warning, 0 on success.
9664 */
9665static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
9666 struct proxy *defpx, const char *file, int line,
9667 char **err)
9668{
Willy Tarreau5db847a2019-05-09 14:13:35 +02009669#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009670 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01009671 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009672 return 0;
9673#else
9674 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
9675 return -1;
9676#endif
9677}
9678
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009679#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009680static int ssl_check_async_engine_count(void) {
9681 int err_code = 0;
9682
Emeric Brun3854e012017-05-17 20:42:48 +02009683 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009684 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009685 err_code = ERR_ABORT;
9686 }
9687 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009688}
9689
Grant Zhang872f9c22017-01-21 01:10:18 +00009690/* parse the "ssl-engine" keyword in global section.
9691 * Returns <0 on alert, >0 on warning, 0 on success.
9692 */
9693static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9694 struct proxy *defpx, const char *file, int line,
9695 char **err)
9696{
9697 char *algo;
9698 int ret = -1;
9699
9700 if (*(args[1]) == 0) {
9701 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9702 return ret;
9703 }
9704
9705 if (*(args[2]) == 0) {
9706 /* if no list of algorithms is given, it defaults to ALL */
9707 algo = strdup("ALL");
9708 goto add_engine;
9709 }
9710
9711 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
9712 if (strcmp(args[2], "algo") != 0) {
9713 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
9714 return ret;
9715 }
9716
9717 if (*(args[3]) == 0) {
9718 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
9719 return ret;
9720 }
9721 algo = strdup(args[3]);
9722
9723add_engine:
9724 if (ssl_init_single_engine(args[1], algo)==0) {
9725 openssl_engines_initialized++;
9726 ret = 0;
9727 }
9728 free(algo);
9729 return ret;
9730}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009731#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00009732
Willy Tarreauf22e9682016-12-21 23:23:19 +01009733/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
9734 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9735 */
9736static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
9737 struct proxy *defpx, const char *file, int line,
9738 char **err)
9739{
9740 char **target;
9741
Willy Tarreauef934602016-12-22 23:12:01 +01009742 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01009743
9744 if (too_many_args(1, args, err, NULL))
9745 return -1;
9746
9747 if (*(args[1]) == 0) {
9748 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9749 return -1;
9750 }
9751
9752 free(*target);
9753 *target = strdup(args[1]);
9754 return 0;
9755}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009756
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009757#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009758/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9759 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9760 */
9761static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9762 struct proxy *defpx, const char *file, int line,
9763 char **err)
9764{
9765 char **target;
9766
9767 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9768
9769 if (too_many_args(1, args, err, NULL))
9770 return -1;
9771
9772 if (*(args[1]) == 0) {
9773 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9774 return -1;
9775 }
9776
9777 free(*target);
9778 *target = strdup(args[1]);
9779 return 0;
9780}
9781#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009782
Willy Tarreau9ceda382016-12-21 23:13:03 +01009783/* parse various global tune.ssl settings consisting in positive integers.
9784 * Returns <0 on alert, >0 on warning, 0 on success.
9785 */
9786static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9787 struct proxy *defpx, const char *file, int line,
9788 char **err)
9789{
9790 int *target;
9791
9792 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9793 target = &global.tune.sslcachesize;
9794 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009795 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009796 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009797 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009798 else if (strcmp(args[0], "maxsslconn") == 0)
9799 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009800 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9801 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009802 else {
9803 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9804 return -1;
9805 }
9806
9807 if (too_many_args(1, args, err, NULL))
9808 return -1;
9809
9810 if (*(args[1]) == 0) {
9811 memprintf(err, "'%s' expects an integer argument.", args[0]);
9812 return -1;
9813 }
9814
9815 *target = atoi(args[1]);
9816 if (*target < 0) {
9817 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9818 return -1;
9819 }
9820 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009821}
9822
9823static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9824 struct proxy *defpx, const char *file, int line,
9825 char **err)
9826{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009827 int ret;
9828
9829 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9830 if (ret != 0)
9831 return ret;
9832
Willy Tarreaubafbe012017-11-24 17:34:44 +01009833 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009834 memprintf(err, "'%s' is already configured.", args[0]);
9835 return -1;
9836 }
9837
Willy Tarreaubafbe012017-11-24 17:34:44 +01009838 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9839 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009840 memprintf(err, "Out of memory error.");
9841 return -1;
9842 }
9843 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009844}
9845
9846/* parse "ssl.force-private-cache".
9847 * Returns <0 on alert, >0 on warning, 0 on success.
9848 */
9849static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9850 struct proxy *defpx, const char *file, int line,
9851 char **err)
9852{
9853 if (too_many_args(0, args, err, NULL))
9854 return -1;
9855
Willy Tarreauef934602016-12-22 23:12:01 +01009856 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009857 return 0;
9858}
9859
9860/* parse "ssl.lifetime".
9861 * Returns <0 on alert, >0 on warning, 0 on success.
9862 */
9863static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9864 struct proxy *defpx, const char *file, int line,
9865 char **err)
9866{
9867 const char *res;
9868
9869 if (too_many_args(1, args, err, NULL))
9870 return -1;
9871
9872 if (*(args[1]) == 0) {
9873 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9874 return -1;
9875 }
9876
Willy Tarreauef934602016-12-22 23:12:01 +01009877 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009878 if (res == PARSE_TIME_OVER) {
9879 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9880 args[1], args[0]);
9881 return -1;
9882 }
9883 else if (res == PARSE_TIME_UNDER) {
9884 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9885 args[1], args[0]);
9886 return -1;
9887 }
9888 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009889 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9890 return -1;
9891 }
9892 return 0;
9893}
9894
9895#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009896/* parse "ssl-dh-param-file".
9897 * Returns <0 on alert, >0 on warning, 0 on success.
9898 */
9899static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9900 struct proxy *defpx, const char *file, int line,
9901 char **err)
9902{
9903 if (too_many_args(1, args, err, NULL))
9904 return -1;
9905
9906 if (*(args[1]) == 0) {
9907 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9908 return -1;
9909 }
9910
9911 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9912 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9913 return -1;
9914 }
9915 return 0;
9916}
9917
Willy Tarreau9ceda382016-12-21 23:13:03 +01009918/* parse "ssl.default-dh-param".
9919 * Returns <0 on alert, >0 on warning, 0 on success.
9920 */
9921static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9922 struct proxy *defpx, const char *file, int line,
9923 char **err)
9924{
9925 if (too_many_args(1, args, err, NULL))
9926 return -1;
9927
9928 if (*(args[1]) == 0) {
9929 memprintf(err, "'%s' expects an integer argument.", args[0]);
9930 return -1;
9931 }
9932
Willy Tarreauef934602016-12-22 23:12:01 +01009933 global_ssl.default_dh_param = atoi(args[1]);
9934 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009935 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9936 return -1;
9937 }
9938 return 0;
9939}
9940#endif
9941
9942
William Lallemand32af2032016-10-29 18:09:35 +02009943/* This function is used with TLS ticket keys management. It permits to browse
9944 * each reference. The variable <getnext> must contain the current node,
9945 * <end> point to the root node.
9946 */
9947#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9948static inline
9949struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
9950{
9951 struct tls_keys_ref *ref = getnext;
9952
9953 while (1) {
9954
9955 /* Get next list entry. */
9956 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
9957
9958 /* If the entry is the last of the list, return NULL. */
9959 if (&ref->list == end)
9960 return NULL;
9961
9962 return ref;
9963 }
9964}
9965
9966static inline
9967struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
9968{
9969 int id;
9970 char *error;
9971
9972 /* If the reference starts by a '#', this is numeric id. */
9973 if (reference[0] == '#') {
9974 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
9975 id = strtol(reference + 1, &error, 10);
9976 if (*error != '\0')
9977 return NULL;
9978
9979 /* Perform the unique id lookup. */
9980 return tlskeys_ref_lookupid(id);
9981 }
9982
9983 /* Perform the string lookup. */
9984 return tlskeys_ref_lookup(reference);
9985}
9986#endif
9987
9988
9989#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9990
9991static int cli_io_handler_tlskeys_files(struct appctx *appctx);
9992
9993static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
9994 return cli_io_handler_tlskeys_files(appctx);
9995}
9996
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009997/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
9998 * (next index to be dumped), and cli.p0 (next key reference).
9999 */
William Lallemand32af2032016-10-29 18:09:35 +020010000static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10001
10002 struct stream_interface *si = appctx->owner;
10003
10004 switch (appctx->st2) {
10005 case STAT_ST_INIT:
10006 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010007 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010008 * later and restart at the state "STAT_ST_INIT".
10009 */
10010 chunk_reset(&trash);
10011
10012 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10013 chunk_appendf(&trash, "# id secret\n");
10014 else
10015 chunk_appendf(&trash, "# id (file)\n");
10016
Willy Tarreau06d80a92017-10-19 14:32:15 +020010017 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010018 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010019 return 0;
10020 }
10021
William Lallemand32af2032016-10-29 18:09:35 +020010022 /* Now, we start the browsing of the references lists.
10023 * Note that the following call to LIST_ELEM return bad pointer. The only
10024 * available field of this pointer is <list>. It is used with the function
10025 * tlskeys_list_get_next() for retruning the first available entry
10026 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010027 if (appctx->ctx.cli.p0 == NULL) {
10028 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10029 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010030 }
10031
10032 appctx->st2 = STAT_ST_LIST;
10033 /* fall through */
10034
10035 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010036 while (appctx->ctx.cli.p0) {
10037 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010038
10039 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010040 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010041 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010042
10043 if (appctx->ctx.cli.i1 == 0)
10044 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10045
William Lallemand32af2032016-10-29 18:09:35 +020010046 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010047 int head;
10048
10049 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10050 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010051 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010052 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010053
10054 chunk_reset(t2);
10055 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010056 if (ref->key_size_bits == 128) {
10057 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10058 sizeof(struct tls_sess_key_128),
10059 t2->area, t2->size);
10060 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10061 t2->area);
10062 }
10063 else if (ref->key_size_bits == 256) {
10064 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10065 sizeof(struct tls_sess_key_256),
10066 t2->area, t2->size);
10067 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10068 t2->area);
10069 }
10070 else {
10071 /* This case should never happen */
10072 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10073 }
William Lallemand32af2032016-10-29 18:09:35 +020010074
Willy Tarreau06d80a92017-10-19 14:32:15 +020010075 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010076 /* let's try again later from this stream. We add ourselves into
10077 * this stream's users so that it can remove us upon termination.
10078 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010079 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010080 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010081 return 0;
10082 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010083 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010084 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010085 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010086 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010087 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010088 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010089 /* let's try again later from this stream. We add ourselves into
10090 * this stream's users so that it can remove us upon termination.
10091 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010092 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010093 return 0;
10094 }
10095
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010096 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010097 break;
10098
10099 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010100 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010101 }
10102
10103 appctx->st2 = STAT_ST_FIN;
10104 /* fall through */
10105
10106 default:
10107 appctx->st2 = STAT_ST_FIN;
10108 return 1;
10109 }
10110 return 0;
10111}
10112
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010113/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010114static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010115{
William Lallemand32af2032016-10-29 18:09:35 +020010116 /* no parameter, shows only file list */
10117 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010118 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010119 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010120 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010121 }
10122
10123 if (args[2][0] == '*') {
10124 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010125 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010126 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010127 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010128 if (!appctx->ctx.cli.p0)
10129 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010130 }
William Lallemand32af2032016-10-29 18:09:35 +020010131 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010132 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010133}
10134
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010135static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010136{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010137 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010138 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010139
William Lallemand32af2032016-10-29 18:09:35 +020010140 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010141 if (!*args[3] || !*args[4])
10142 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 +020010143
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010144 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010145 if (!ref)
10146 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010147
Willy Tarreau1c913e42018-08-22 05:26:57 +020010148 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010149 if (ret < 0)
10150 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010151
Willy Tarreau1c913e42018-08-22 05:26:57 +020010152 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010153 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10154 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010155
Willy Tarreau9d008692019-08-09 11:21:01 +020010156 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010157}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010158#endif
William Lallemand32af2032016-10-29 18:09:35 +020010159
William Lallemand44b35322019-10-17 16:28:40 +020010160
10161/* Type of SSL payloads that can be updated over the CLI */
10162
10163enum {
10164 CERT_TYPE_PEM = 0,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010165#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010166 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010167#endif
William Lallemand44b35322019-10-17 16:28:40 +020010168 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010169#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010170 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010171#endif
William Lallemand44b35322019-10-17 16:28:40 +020010172 CERT_TYPE_MAX,
10173};
10174
10175struct {
10176 const char *ext;
10177 int type;
10178 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
10179 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020010180} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020010181 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand541a5342019-10-23 14:11:54 +020010182#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010183 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010184#endif
10185#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010186 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010187#endif
William Lallemand44b35322019-10-17 16:28:40 +020010188 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020010189 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020010190};
10191
William Lallemand430413e2019-10-28 14:30:47 +010010192/* states of the CLI IO handler for 'set ssl cert' */
10193enum {
10194 SETCERT_ST_INIT = 0,
10195 SETCERT_ST_GEN,
10196 SETCERT_ST_INSERT,
10197 SETCERT_ST_FIN,
10198};
William Lallemand8f840d72019-10-23 10:53:05 +020010199
William Lallemandd4f946c2019-12-05 10:26:40 +010010200/* release function of the `show ssl cert' command */
10201static void cli_release_show_cert(struct appctx *appctx)
10202{
10203 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10204}
10205
10206/* IO handler of "show ssl cert <filename>" */
10207static int cli_io_handler_show_cert(struct appctx *appctx)
10208{
10209 struct buffer *trash = alloc_trash_chunk();
10210 struct ebmb_node *node;
10211 struct stream_interface *si = appctx->owner;
10212 struct ckch_store *ckchs;
10213 int n;
10214
10215 if (trash == NULL)
10216 return 1;
10217
10218 if (!appctx->ctx.ssl.old_ckchs) {
10219 if (ckchs_transaction.old_ckchs) {
10220 ckchs = ckchs_transaction.old_ckchs;
10221 chunk_appendf(trash, "# transaction\n");
10222 if (!ckchs->multi) {
10223 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010224#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010225 } else {
10226 chunk_appendf(trash, "*%s:", ckchs->path);
10227 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10228 if (ckchs->ckch[n].cert)
10229 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10230 }
10231 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010232#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010233 }
10234 }
10235 }
10236
10237 if (!appctx->ctx.cli.p0) {
10238 chunk_appendf(trash, "# filename\n");
10239 node = ebmb_first(&ckchs_tree);
10240 } else {
10241 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
10242 }
10243 while (node) {
10244 ckchs = ebmb_entry(node, struct ckch_store, node);
10245 if (!ckchs->multi) {
10246 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010247#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010248 } else {
10249 chunk_appendf(trash, "%s:", ckchs->path);
10250 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10251 if (ckchs->ckch[n].cert)
10252 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10253 }
10254 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010255#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010256 }
10257
10258 node = ebmb_next(node);
10259 if (ci_putchk(si_ic(si), trash) == -1) {
10260 si_rx_room_blk(si);
10261 goto yield;
10262 }
10263 }
10264
10265 appctx->ctx.cli.p0 = NULL;
10266 free_trash_chunk(trash);
10267 return 1;
10268yield:
10269
10270 free_trash_chunk(trash);
10271 appctx->ctx.cli.p0 = ckchs;
10272 return 0; /* should come back */
10273}
10274
10275/* IO handler of the details "show ssl cert <filename>" */
10276static int cli_io_handler_show_cert_detail(struct appctx *appctx)
10277{
10278 struct stream_interface *si = appctx->owner;
10279 struct ckch_store *ckchs = appctx->ctx.cli.p0;
10280 struct buffer *out = alloc_trash_chunk();
10281 struct buffer *tmp = alloc_trash_chunk();
10282 X509_NAME *name = NULL;
10283 int write = -1;
10284 BIO *bio = NULL;
10285
10286 if (!tmp || !out)
10287 goto end;
10288
10289 if (!ckchs->multi) {
10290 chunk_appendf(out, "Filename: ");
10291 if (ckchs == ckchs_transaction.new_ckchs)
10292 chunk_appendf(out, "*");
10293 chunk_appendf(out, "%s\n", ckchs->path);
10294 chunk_appendf(out, "Serial: ");
10295 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
10296 goto end;
10297 dump_binary(out, tmp->area, tmp->data);
10298 chunk_appendf(out, "\n");
10299
10300 chunk_appendf(out, "notBefore: ");
10301 chunk_reset(tmp);
10302 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10303 goto end;
10304 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
10305 goto end;
10306 write = BIO_read(bio, tmp->area, tmp->size-1);
10307 tmp->area[write] = '\0';
10308 BIO_free(bio);
10309 chunk_appendf(out, "%s\n", tmp->area);
10310
10311 chunk_appendf(out, "notAfter: ");
10312 chunk_reset(tmp);
10313 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10314 goto end;
10315 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
10316 goto end;
10317 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
10318 goto end;
10319 tmp->area[write] = '\0';
10320 BIO_free(bio);
10321 chunk_appendf(out, "%s\n", tmp->area);
10322
10323
10324 chunk_appendf(out, "Issuer: ");
10325 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
10326 goto end;
10327 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10328 goto end;
10329 *(tmp->area + tmp->data) = '\0';
10330 chunk_appendf(out, "%s\n", tmp->area);
10331
10332 chunk_appendf(out, "Subject: ");
10333 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
10334 goto end;
10335 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10336 goto end;
10337 *(tmp->area + tmp->data) = '\0';
10338 chunk_appendf(out, "%s\n", tmp->area);
10339
10340
10341#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10342 chunk_appendf(out, "Subject Alternative Name: ");
10343 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
10344 goto end;
10345 *(out->area + out->data) = '\0';
10346 chunk_appendf(out, "\n");
10347#endif
10348 chunk_reset(tmp);
10349 chunk_appendf(out, "Algorithm: ");
10350 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
10351 goto end;
10352 chunk_appendf(out, "%s\n", tmp->area);
10353
10354 chunk_reset(tmp);
10355 chunk_appendf(out, "SHA1 FingerPrint: ");
10356 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area,
10357 (unsigned int *)&tmp->data) == 0)
10358 goto end;
10359 dump_binary(out, tmp->area, tmp->data);
10360 chunk_appendf(out, "\n");
10361 }
10362
10363 if (ci_putchk(si_ic(si), out) == -1) {
10364 si_rx_room_blk(si);
10365 goto yield;
10366 }
10367
10368end:
10369 free_trash_chunk(tmp);
10370 free_trash_chunk(out);
10371 return 1;
10372yield:
10373 free_trash_chunk(tmp);
10374 free_trash_chunk(out);
10375 return 0; /* should come back */
10376}
10377
10378/* parsing function for 'show ssl cert [certfile]' */
10379static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
10380{
10381 struct ckch_store *ckchs;
10382
10383 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
10384 return cli_err(appctx, "Can't allocate memory!\n");
10385
10386 /* The operations on the CKCH architecture are locked so we can
10387 * manipulate ckch_store and ckch_inst */
10388 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10389 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
10390
10391 /* check if there is a certificate to lookup */
10392 if (*args[3]) {
10393 if (*args[3] == '*') {
10394 if (!ckchs_transaction.new_ckchs)
10395 goto error;
10396
10397 ckchs = ckchs_transaction.new_ckchs;
10398
10399 if (strcmp(args[3] + 1, ckchs->path))
10400 goto error;
10401
10402 } else {
10403 if ((ckchs = ckchs_lookup(args[3])) == NULL)
10404 goto error;
10405
10406 }
10407
10408 if (ckchs->multi)
10409 goto error;
10410
10411 appctx->ctx.cli.p0 = ckchs;
10412 /* use the IO handler that shows details */
10413 appctx->io_handler = cli_io_handler_show_cert_detail;
10414 }
10415
10416 return 0;
10417
10418error:
10419 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10420 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
10421}
10422
William Lallemand430413e2019-10-28 14:30:47 +010010423/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010424static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010425{
10426 struct ckch_store *new_ckchs;
10427 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010428
William Lallemand430413e2019-10-28 14:30:47 +010010429 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020010430
William Lallemand430413e2019-10-28 14:30:47 +010010431 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020010432 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
William Lallemandbeea2a42019-10-30 17:45:33 +010010433 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010434
William Lallemandbeea2a42019-10-30 17:45:33 +010010435 if (!new_ckchs)
10436 return;
William Lallemand8f840d72019-10-23 10:53:05 +020010437
William Lallemandbeea2a42019-10-30 17:45:33 +010010438 /* if the allocation failed, we need to free everything from the temporary list */
10439 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10440 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010441
William Lallemandbeea2a42019-10-30 17:45:33 +010010442 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10443 if (sc0->order == 0) /* we only free if it's the first inserted */
10444 SSL_CTX_free(sc0->ctx);
10445 LIST_DEL(&sc0->by_ckch_inst);
10446 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020010447 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010448 LIST_DEL(&ckchi->by_ckchs);
10449 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020010450 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010451 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010452 }
10453}
10454
10455
10456/*
10457 * This function tries to create the new ckch_inst and their SNIs
10458 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010459static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010460{
10461 struct stream_interface *si = appctx->owner;
10462 int y = 0;
10463 char *err = NULL;
10464 int errcode = 0;
10465 struct ckch_store *old_ckchs, *new_ckchs = NULL;
10466 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010467 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010468 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010469
William Lallemand33cc76f2019-10-31 11:43:45 +010010470 if (trash == NULL)
10471 goto error;
10472
William Lallemand8f840d72019-10-23 10:53:05 +020010473 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
10474 goto error;
10475
William Lallemand430413e2019-10-28 14:30:47 +010010476 while (1) {
10477 switch (appctx->st2) {
10478 case SETCERT_ST_INIT:
10479 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010480 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010010481 if (ci_putchk(si_ic(si), trash) == -1) {
10482 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020010483 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010010484 }
10485 appctx->st2 = SETCERT_ST_GEN;
10486 /* fallthrough */
10487 case SETCERT_ST_GEN:
10488 /*
10489 * This state generates the ckch instances with their
10490 * sni_ctxs and SSL_CTX.
10491 *
William Lallemand430413e2019-10-28 14:30:47 +010010492 * Since the SSL_CTX generation can be CPU consumer, we
10493 * yield every 10 instances.
10494 */
William Lallemand8f840d72019-10-23 10:53:05 +020010495
William Lallemandbeea2a42019-10-30 17:45:33 +010010496 old_ckchs = appctx->ctx.ssl.old_ckchs;
10497 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010498
William Lallemandbeea2a42019-10-30 17:45:33 +010010499 if (!new_ckchs)
10500 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020010501
William Lallemandbeea2a42019-10-30 17:45:33 +010010502 /* get the next ckchi to regenerate */
10503 ckchi = appctx->ctx.ssl.next_ckchi;
10504 /* we didn't start yet, set it to the first elem */
10505 if (ckchi == NULL)
10506 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010507
William Lallemandbeea2a42019-10-30 17:45:33 +010010508 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
10509 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
10510 struct ckch_inst *new_inst;
William Lallemand8f840d72019-10-23 10:53:05 +020010511
William Lallemandbeea2a42019-10-30 17:45:33 +010010512 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
10513 if (y >= 10) {
10514 /* save the next ckchi to compute */
10515 appctx->ctx.ssl.next_ckchi = ckchi;
10516 goto yield;
10517 }
William Lallemand8f840d72019-10-23 10:53:05 +020010518
William Lallemandbeea2a42019-10-30 17:45:33 +010010519 if (new_ckchs->multi)
10520 errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
10521 else
10522 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
William Lallemand8f840d72019-10-23 10:53:05 +020010523
William Lallemandbeea2a42019-10-30 17:45:33 +010010524 if (errcode & ERR_CODE)
10525 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020010526
William Lallemand21724f02019-11-04 17:56:13 +010010527 /* if the previous ckchi was used as the default */
10528 if (ckchi->is_default)
10529 new_inst->is_default = 1;
10530
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010531 /* we need to initialize the SSL_CTX generated */
10532 /* TODO: the prepare_ctx function need to be reworked to be safer there */
10533 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10534 if (!sc0->order) { /* we initiliazed only the first SSL_CTX because it's the same in the other sni_ctx's */
10535 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
10536 if (errcode & ERR_CODE)
10537 goto error;
10538 }
10539 }
10540
10541
William Lallemandbeea2a42019-10-30 17:45:33 +010010542 /* display one dot per new instance */
10543 chunk_appendf(trash, ".");
10544 /* link the new ckch_inst to the duplicate */
10545 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
10546 y++;
10547 }
William Lallemand430413e2019-10-28 14:30:47 +010010548 appctx->st2 = SETCERT_ST_INSERT;
10549 /* fallthrough */
10550 case SETCERT_ST_INSERT:
10551 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020010552
William Lallemandbeea2a42019-10-30 17:45:33 +010010553 old_ckchs = appctx->ctx.ssl.old_ckchs;
10554 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010555
William Lallemandbeea2a42019-10-30 17:45:33 +010010556 if (!new_ckchs)
10557 continue;
William Lallemand430413e2019-10-28 14:30:47 +010010558
William Lallemand21724f02019-11-04 17:56:13 +010010559 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010010560 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10561 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10562 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
10563 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10564 }
William Lallemand8f840d72019-10-23 10:53:05 +020010565
William Lallemandbeea2a42019-10-30 17:45:33 +010010566 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
10567 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemand430413e2019-10-28 14:30:47 +010010568
William Lallemandbeea2a42019-10-30 17:45:33 +010010569 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10570 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10571 ebmb_delete(&sc0->name);
10572 LIST_DEL(&sc0->by_ckch_inst);
10573 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010010574 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010575 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10576 LIST_DEL(&ckchi->by_ckchs);
10577 free(ckchi);
10578 }
William Lallemand8f840d72019-10-23 10:53:05 +020010579
William Lallemandbeea2a42019-10-30 17:45:33 +010010580 /* Replace the old ckchs by the new one */
10581 ebmb_delete(&old_ckchs->node);
10582 ckchs_free(old_ckchs);
10583 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010010584 appctx->st2 = SETCERT_ST_FIN;
10585 /* fallthrough */
10586 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010587 /* we achieved the transaction, we can set everything to NULL */
10588 free(ckchs_transaction.path);
10589 ckchs_transaction.path = NULL;
10590 ckchs_transaction.new_ckchs = NULL;
10591 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010010592 goto end;
10593 }
William Lallemand8f840d72019-10-23 10:53:05 +020010594 }
William Lallemand430413e2019-10-28 14:30:47 +010010595end:
William Lallemand8f840d72019-10-23 10:53:05 +020010596
William Lallemanded442432019-11-21 16:41:07 +010010597 chunk_appendf(trash, "\n");
10598 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010010599 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010010600 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010010601 if (ci_putchk(si_ic(si), trash) == -1)
10602 si_rx_room_blk(si);
10603 free_trash_chunk(trash);
10604 /* success: call the release function and don't come back */
10605 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010606yield:
10607 /* store the state */
10608 if (ci_putchk(si_ic(si), trash) == -1)
10609 si_rx_room_blk(si);
10610 free_trash_chunk(trash);
10611 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020010612 return 0; /* should come back */
10613
10614error:
10615 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010010616 if (trash) {
10617 chunk_appendf(trash, "\n%sFailed!\n", err);
10618 if (ci_putchk(si_ic(si), trash) == -1)
10619 si_rx_room_blk(si);
10620 free_trash_chunk(trash);
10621 }
William Lallemand430413e2019-10-28 14:30:47 +010010622 /* error: call the release function and don't come back */
10623 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010624}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010625
10626/*
10627 * Parsing function of 'commit ssl cert'
10628 */
10629static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
10630{
10631 char *err = NULL;
10632
William Lallemand230662a2019-12-03 13:32:54 +010010633 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10634 return 1;
10635
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010636 if (!*args[3])
10637 return cli_err(appctx, "'commit ssl cert expects a filename\n");
10638
10639 /* The operations on the CKCH architecture are locked so we can
10640 * manipulate ckch_store and ckch_inst */
10641 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10642 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
10643
10644 if (!ckchs_transaction.path) {
10645 memprintf(&err, "No ongoing transaction! !\n");
10646 goto error;
10647 }
10648
10649 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10650 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
10651 goto error;
10652 }
10653
10654 /* init the appctx structure */
10655 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010656 appctx->ctx.ssl.next_ckchi = NULL;
10657 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
10658 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
10659
10660 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
10661 return 0;
10662
10663error:
10664
10665 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10666 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
10667
10668 return cli_dynerr(appctx, err);
10669}
10670
10671
William Lallemand8f840d72019-10-23 10:53:05 +020010672/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010673 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020010674 */
William Lallemand150bfa82019-09-19 17:12:49 +020010675static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
10676{
William Lallemand0c3b7d92019-10-18 11:27:07 +020010677 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020010678 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020010679 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020010680 int i;
William Lallemand849eed62019-10-17 16:23:50 +020010681 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010682 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020010683 char *end;
10684 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010685 struct cert_key_and_chain *ckch;
10686 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020010687
William Lallemand230662a2019-12-03 13:32:54 +010010688 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10689 return 1;
10690
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010691 if ((buf = alloc_trash_chunk()) == NULL)
10692 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020010693
10694 if (!*args[3] || !payload)
10695 return cli_err(appctx, "'set ssl cert expects a filename and a certificat as a payload\n");
10696
10697 /* The operations on the CKCH architecture are locked so we can
10698 * manipulate ckch_store and ckch_inst */
10699 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10700 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
10701
William Lallemand8f840d72019-10-23 10:53:05 +020010702 if (!chunk_strcpy(buf, args[3])) {
10703 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10704 errcode |= ERR_ALERT | ERR_FATAL;
10705 goto end;
10706 }
10707
William Lallemand44b35322019-10-17 16:28:40 +020010708 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020010709 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020010710 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020010711 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
10712 *end = '\0';
10713 type = cert_exts[i].type;
10714 break;
10715 }
10716 }
10717
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010718 appctx->ctx.ssl.old_ckchs = NULL;
10719 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020010720
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010721 /* if there is an ongoing transaction */
10722 if (ckchs_transaction.path) {
10723 /* if the ongoing transaction is a bundle, we need to find which part of the bundle need to be updated */
William Lallemand963b2e72019-10-14 11:38:36 +020010724#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010725 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010726 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020010727 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020010728
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010729 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020010730 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020010731 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010732 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020010733 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10734 bundle = j; /* keep the type of certificate so we insert it at the right place */
10735 *end = '\0'; /* it's a bundle let's end the string*/
10736 break;
10737 }
William Lallemand150bfa82019-09-19 17:12:49 +020010738 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010739 if (bundle < 0) {
10740 memprintf(&err, "The ongoing transaction is the '%s' bundle. You need to specify which part of the bundle you want to update ('%s.{rsa,ecdsa,dsa}')\n", ckchs_transaction.path, buf->area);
10741 errcode |= ERR_ALERT | ERR_FATAL;
10742 goto end;
10743 }
10744 }
10745#endif
10746
10747 /* if there is an ongoing transaction, check if this is the same file */
10748 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
10749 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
10750 errcode |= ERR_ALERT | ERR_FATAL;
10751 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010752 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010753
10754 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
10755
10756 } else {
10757 struct ckch_store *find_ckchs[2] = { NULL, NULL };
10758
10759 /* lookup for the certificate in the tree:
10760 * check if this is used as a bundle AND as a unique certificate */
10761 for (i = 0; i < 2; i++) {
10762
10763 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
10764 /* only the bundle name is in the tree and you should
10765 * never update a bundle name, only a filename */
10766 if (bundle < 0 && find_ckchs[i]->multi) {
10767 /* we tried to look for a non-bundle and we found a bundle */
10768 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
10769 err ? err : "", args[3], args[3]);
10770 errcode |= ERR_ALERT | ERR_FATAL;
10771 goto end;
10772 }
William Lallemand3246d942019-11-04 14:02:11 +010010773 /* If we want a bundle but this is not a bundle
10774 * example: When you try to update <file>.rsa, but
10775 * <file> is a regular file */
10776 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
10777 find_ckchs[i] = NULL;
10778 break;
10779 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010780 }
10781#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
10782 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010783 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010784 int j;
10785
10786 /* check if it was used in a bundle by removing the
10787 * .dsa/.rsa/.ecdsa at the end of the filename */
10788 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010789 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010790 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10791 bundle = j; /* keep the type of certificate so we insert it at the right place */
10792 *end = '\0'; /* it's a bundle let's end the string*/
10793 break;
10794 }
10795 }
William Lallemand37031b82019-11-04 13:38:53 +010010796 if (bundle < 0) /* we didn't find a bundle extension */
10797 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010798 }
William Lallemand963b2e72019-10-14 11:38:36 +020010799#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010800 /* bundles are not supported here, so we don't need to lookup again */
10801 break;
William Lallemand963b2e72019-10-14 11:38:36 +020010802#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010803 }
10804
10805 if (find_ckchs[0] && find_ckchs[1]) {
10806 memprintf(&err, "%sUpdating a certificate which is used in the HAProxy configuration as a bundle and as a unique certificate is not supported. ('%s' and '%s')\n",
10807 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
10808 errcode |= ERR_ALERT | ERR_FATAL;
10809 goto end;
10810 }
10811
10812 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020010813 }
10814
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010815 if (!appctx->ctx.ssl.old_ckchs) {
10816 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020010817 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010818 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020010819 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010820 }
10821
William Lallemand8a7fdf02019-11-04 10:59:32 +010010822 if (!appctx->ctx.ssl.path) {
10823 /* this is a new transaction, set the path of the transaction */
10824 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
10825 if (!appctx->ctx.ssl.path) {
10826 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10827 errcode |= ERR_ALERT | ERR_FATAL;
10828 goto end;
10829 }
10830 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010831
10832 old_ckchs = appctx->ctx.ssl.old_ckchs;
10833
10834 /* TODO: handle filters */
10835 if (old_ckchs->filters) {
10836 memprintf(&err, "%sCertificates used in crt-list with filters are not supported!\n",
10837 err ? err : "");
10838 errcode |= ERR_ALERT | ERR_FATAL;
10839 goto end;
10840 }
10841
10842 /* duplicate the ckch store */
10843 new_ckchs = ckchs_dup(old_ckchs);
10844 if (!new_ckchs) {
10845 memprintf(&err, "%sCannot allocate memory!\n",
10846 err ? err : "");
10847 errcode |= ERR_ALERT | ERR_FATAL;
10848 goto end;
10849 }
10850
10851 if (!new_ckchs->multi)
10852 ckch = new_ckchs->ckch;
10853 else
10854 ckch = &new_ckchs->ckch[bundle];
10855
10856 /* appply the change on the duplicate */
10857 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
10858 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
10859 errcode |= ERR_ALERT | ERR_FATAL;
10860 goto end;
10861 }
10862
10863 appctx->ctx.ssl.new_ckchs = new_ckchs;
10864
10865 /* we succeed, we can save the ckchs in the transaction */
10866
10867 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010010868 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010869 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
10870 ckchs_transaction.path = appctx->ctx.ssl.path;
10871 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
10872 } else {
10873 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
10874
10875 }
10876
10877 /* free the previous ckchs if there was a transaction */
10878 ckchs_free(ckchs_transaction.new_ckchs);
10879
10880 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
10881
10882
William Lallemand8f840d72019-10-23 10:53:05 +020010883 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010884
William Lallemand8f840d72019-10-23 10:53:05 +020010885end:
10886 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020010887
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010888 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010889
10890 ckchs_free(appctx->ctx.ssl.new_ckchs);
10891 appctx->ctx.ssl.new_ckchs = NULL;
10892
10893 appctx->ctx.ssl.old_ckchs = NULL;
10894
10895 free(appctx->ctx.ssl.path);
10896 appctx->ctx.ssl.path = NULL;
10897
10898 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020010899 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010010900 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010901
10902 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10903 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010010904 }
William Lallemand8f840d72019-10-23 10:53:05 +020010905 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010906}
10907
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010908/* parsing function of 'abort ssl cert' */
10909static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
10910{
10911 char *err = NULL;
10912
William Lallemand230662a2019-12-03 13:32:54 +010010913 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10914 return 1;
10915
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010916 if (!*args[3])
10917 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
10918
10919 /* The operations on the CKCH architecture are locked so we can
10920 * manipulate ckch_store and ckch_inst */
10921 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10922 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
10923
10924 if (!ckchs_transaction.path) {
10925 memprintf(&err, "No ongoing transaction!\n");
10926 goto error;
10927 }
10928
10929 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10930 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
10931 goto error;
10932 }
10933
10934 /* Only free the ckchs there, because the SNI and instances were not generated yet */
10935 ckchs_free(ckchs_transaction.new_ckchs);
10936 ckchs_transaction.new_ckchs = NULL;
10937 ckchs_free(ckchs_transaction.old_ckchs);
10938 ckchs_transaction.old_ckchs = NULL;
10939 free(ckchs_transaction.path);
10940 ckchs_transaction.path = NULL;
10941
10942 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10943
10944 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
10945 return cli_dynmsg(appctx, LOG_NOTICE, err);
10946
10947error:
10948 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10949
10950 return cli_dynerr(appctx, err);
10951}
10952
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010953static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010954{
10955#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
10956 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010957 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010958
10959 if (!payload)
10960 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020010961
10962 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010963 if (!*payload)
10964 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010965
10966 /* remove \r and \n from the payload */
10967 for (i = 0, j = 0; payload[i]; i++) {
10968 if (payload[i] == '\r' || payload[i] == '\n')
10969 continue;
10970 payload[j++] = payload[i];
10971 }
10972 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010973
Willy Tarreau1c913e42018-08-22 05:26:57 +020010974 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010975 if (ret < 0)
10976 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010977
Willy Tarreau1c913e42018-08-22 05:26:57 +020010978 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020010979 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020010980 if (err)
10981 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
10982 else
10983 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010984 }
Willy Tarreau9d008692019-08-09 11:21:01 +020010985
10986 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010987#else
Willy Tarreau9d008692019-08-09 11:21:01 +020010988 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 +020010989#endif
10990
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010991}
10992
Willy Tarreau86a394e2019-05-09 14:15:32 +020010993#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010994static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
10995{
10996 switch (arg->type) {
10997 case ARGT_STR:
10998 smp->data.type = SMP_T_STR;
10999 smp->data.u.str = arg->data.str;
11000 return 1;
11001 case ARGT_VAR:
11002 if (!vars_get_by_desc(&arg->data.var, smp))
11003 return 0;
11004 if (!sample_casts[smp->data.type][SMP_T_STR])
11005 return 0;
11006 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
11007 return 0;
11008 return 1;
11009 default:
11010 return 0;
11011 }
11012}
11013
11014static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
11015 const char *file, int line, char **err)
11016{
11017 switch(args[0].data.sint) {
11018 case 128:
11019 case 192:
11020 case 256:
11021 break;
11022 default:
11023 memprintf(err, "key size must be 128, 192 or 256 (bits).");
11024 return 0;
11025 }
11026 /* Try to decode a variable. */
11027 vars_check_arg(&args[1], NULL);
11028 vars_check_arg(&args[2], NULL);
11029 vars_check_arg(&args[3], NULL);
11030 return 1;
11031}
11032
11033/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
11034static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
11035{
11036 struct sample nonce, key, aead_tag;
11037 struct buffer *smp_trash, *smp_trash_alloc;
11038 EVP_CIPHER_CTX *ctx;
11039 int dec_size, ret;
11040
11041 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
11042 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
11043 return 0;
11044
11045 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
11046 if (!sample_conv_var2smp_str(&arg_p[2], &key))
11047 return 0;
11048
11049 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
11050 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
11051 return 0;
11052
11053 smp_trash = get_trash_chunk();
11054 smp_trash_alloc = alloc_trash_chunk();
11055 if (!smp_trash_alloc)
11056 return 0;
11057
11058 ctx = EVP_CIPHER_CTX_new();
11059
11060 if (!ctx)
11061 goto err;
11062
11063 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
11064 if (dec_size < 0)
11065 goto err;
11066 smp_trash->data = dec_size;
11067
11068 /* Set cipher type and mode */
11069 switch(arg_p[0].data.sint) {
11070 case 128:
11071 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
11072 break;
11073 case 192:
11074 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
11075 break;
11076 case 256:
11077 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
11078 break;
11079 }
11080
11081 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
11082
11083 /* Initialise IV */
11084 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
11085 goto err;
11086
11087 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
11088 if (dec_size < 0)
11089 goto err;
11090 smp_trash->data = dec_size;
11091
11092 /* Initialise key */
11093 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
11094 goto err;
11095
11096 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
11097 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
11098 goto err;
11099
11100 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
11101 if (dec_size < 0)
11102 goto err;
11103 smp_trash_alloc->data = dec_size;
11104 dec_size = smp_trash->data;
11105
11106 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
11107 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
11108
11109 if (ret <= 0)
11110 goto err;
11111
11112 smp->data.u.str.data = dec_size + smp_trash->data;
11113 smp->data.u.str.area = smp_trash->area;
11114 smp->data.type = SMP_T_BIN;
11115 smp->flags &= ~SMP_F_CONST;
11116 free_trash_chunk(smp_trash_alloc);
11117 return 1;
11118
11119err:
11120 free_trash_chunk(smp_trash_alloc);
11121 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020011122}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011123# endif
William Lallemand32af2032016-10-29 18:09:35 +020011124
Elliot Otchet71f82972020-01-15 08:12:14 -050011125/* Argument validation functions */
11126
11127/* This function is used to validate the arguments passed to any "x_dn" ssl
11128 * keywords. These keywords support specifying a third parameter that must be
11129 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
11130 */
11131int val_dnfmt(struct arg *arg, char **err_msg)
11132{
11133 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
11134 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
11135 return 0;
11136 }
11137 return 1;
11138}
11139
William Lallemand32af2032016-10-29 18:09:35 +020011140/* register cli keywords */
11141static struct cli_kw_list cli_kws = {{ },{
11142#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11143 { { "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 +020011144 { { "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 +020011145#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010011146 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011147 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
11148 { { "commit", "ssl", "cert", NULL }, "commit ssl cert <certfile> : commit a certificate file", cli_parse_commit_cert, cli_io_handler_commit_cert, cli_release_commit_cert },
William Lallemand0bc9c8a2019-11-19 15:51:51 +010011149 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
William Lallemandd4f946c2019-12-05 10:26:40 +010011150 { { "show", "ssl", "cert", NULL }, "show ssl cert [<certfile>] : display the SSL certificates used in memory, or the details of a <certfile>", cli_parse_show_cert, cli_io_handler_show_cert, cli_release_show_cert },
William Lallemand32af2032016-10-29 18:09:35 +020011151 { { NULL }, NULL, NULL, NULL }
11152}};
11153
Willy Tarreau0108d902018-11-25 19:14:37 +010011154INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020011155
Willy Tarreau7875d092012-09-10 08:20:03 +020011156/* Note: must not be declared <const> as its list will be overwritten.
11157 * Please take care of keeping this list alphabetically sorted.
11158 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011159static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020011160 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011161 { "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 +010011162#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010011163 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011164#endif
Emeric Brun645ae792014-04-30 14:21:06 +020011165 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011166#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
11167 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
11168#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010011169 { "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 +020011170 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020011171 { "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 +020011172 { "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 +020011173#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020011174 { "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 -040011175#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011176#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011177 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11178 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040011179 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11180#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011181 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11182 { "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 +010011183 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011184 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011185 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020011186 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11187 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11188 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11189 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011190 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020011191 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11192 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011193 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011194 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11195 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010011196 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011197 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020011198 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11199 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11200 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11201 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011202 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020011203 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020011204 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011205 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011206 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011207 { "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 +010011208 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011209 { "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 +020011210 { "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 +010011211 { "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 +020011212 { "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 +010011213#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011214 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020011215#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010011216#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011217 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020011218#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011219 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011220#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020011221 { "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 -040011222#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011223 { "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 +020011224#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011225 { "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 -040011226#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011227#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011228 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11229 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040011230 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11231#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040011232#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011233 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040011234#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011235 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11236 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11237 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11238 { "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 +020011239 { NULL, NULL, 0, 0, 0 },
11240}};
11241
Willy Tarreau0108d902018-11-25 19:14:37 +010011242INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
11243
Willy Tarreau7875d092012-09-10 08:20:03 +020011244/* Note: must not be declared <const> as its list will be overwritten.
11245 * Please take care of keeping this list alphabetically sorted.
11246 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011247static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011248 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
11249 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011250 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020011251}};
11252
Willy Tarreau0108d902018-11-25 19:14:37 +010011253INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
11254
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011255/* Note: must not be declared <const> as its list will be overwritten.
11256 * Please take care of keeping this list alphabetically sorted, doing so helps
11257 * all code contributors.
11258 * Optional keywords are also declared with a NULL ->parse() function so that
11259 * the config parser can report an appropriate error when a known keyword was
11260 * not enabled.
11261 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011262static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011263 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011264 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11265 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11266 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011267#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011268 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11269#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011270 { "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 +010011271 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011272 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011273 { "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 +010011274 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020011275 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
11276 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011277 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
11278 { NULL, NULL, 0 },
11279};
11280
Willy Tarreau0108d902018-11-25 19:14:37 +010011281/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
11282
Willy Tarreau51fb7652012-09-18 18:24:39 +020011283static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011284 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011285 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11286 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11287 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
11288 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
11289 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
11290 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011291#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011292 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11293#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011294 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
11295 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
11296 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
11297 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
11298 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
11299 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
11300 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
11301 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
11302 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
11303 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011304 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011305 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011306 { "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 +020011307 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
11308 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
11309 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
11310 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011311 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011312 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
11313 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011314 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
11315 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011316 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
11317 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
11318 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
11319 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
11320 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011321 { NULL, NULL, 0 },
11322}};
Emeric Brun46591952012-05-18 15:47:34 +020011323
Willy Tarreau0108d902018-11-25 19:14:37 +010011324INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
11325
Willy Tarreau92faadf2012-10-10 23:04:25 +020011326/* Note: must not be declared <const> as its list will be overwritten.
11327 * Please take care of keeping this list alphabetically sorted, doing so helps
11328 * all code contributors.
11329 * Optional keywords are also declared with a NULL ->parse() function so that
11330 * the config parser can report an appropriate error when a known keyword was
11331 * not enabled.
11332 */
11333static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010011334 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010011335 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011336 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010011337 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020011338 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011339 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
11340 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011341#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011342 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
11343#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011344 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
11345 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
11346 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
11347 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
11348 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
11349 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
11350 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
11351 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
11352 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
11353 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
11354 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
11355 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
11356 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
11357 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
11358 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
11359 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
11360 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
11361 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010011362 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011363 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
11364 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
11365 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
11366 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
11367 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
11368 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
11369 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
11370 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
11371 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
11372 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020011373 { NULL, NULL, 0, 0 },
11374}};
11375
Willy Tarreau0108d902018-11-25 19:14:37 +010011376INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
11377
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011378static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010011379 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
11380 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010011381 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011382 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
11383 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010011384#ifndef OPENSSL_NO_DH
11385 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
11386#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011387 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011388#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011389 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011390#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010011391 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
11392#ifndef OPENSSL_NO_DH
11393 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
11394#endif
11395 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
11396 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
11397 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
11398 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011399 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010011400 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
11401 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011402#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011403 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
11404 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
11405#endif
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011406 { 0, NULL, NULL },
11407}};
11408
Willy Tarreau0108d902018-11-25 19:14:37 +010011409INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
11410
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011411/* Note: must not be declared <const> as its list will be overwritten */
11412static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020011413#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011414 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
11415#endif
11416 { NULL, NULL, 0, 0, 0 },
11417}};
11418
11419INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
11420
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020011421/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010011422static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020011423 .snd_buf = ssl_sock_from_buf,
11424 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010011425 .subscribe = ssl_subscribe,
11426 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020011427 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020011428 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020011429 .rcv_pipe = NULL,
11430 .snd_pipe = NULL,
11431 .shutr = NULL,
11432 .shutw = ssl_sock_shutw,
11433 .close = ssl_sock_close,
11434 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010011435 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010011436 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010011437 .prepare_srv = ssl_sock_prepare_srv_ctx,
11438 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010011439 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010011440 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020011441};
11442
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011443enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
11444 struct session *sess, struct stream *s, int flags)
11445{
11446 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011447 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011448
11449 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011450 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011451
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011452 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011453 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011454 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011455 s->req.flags |= CF_READ_NULL;
11456 return ACT_RET_YIELD;
11457 }
11458 }
11459 return (ACT_RET_CONT);
11460}
11461
11462static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
11463{
11464 rule->action_ptr = ssl_action_wait_for_hs;
11465
11466 return ACT_RET_PRS_OK;
11467}
11468
11469static struct action_kw_list http_req_actions = {ILH, {
11470 { "wait-for-handshake", ssl_parse_wait_for_hs },
11471 { /* END */ }
11472}};
11473
Willy Tarreau0108d902018-11-25 19:14:37 +010011474INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
11475
Willy Tarreau5db847a2019-05-09 14:13:35 +020011476#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011477
11478static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11479{
11480 if (ptr) {
11481 chunk_destroy(ptr);
11482 free(ptr);
11483 }
11484}
11485
11486#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011487static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11488{
Willy Tarreaubafbe012017-11-24 17:34:44 +010011489 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011490}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011491
Emeric Brun46591952012-05-18 15:47:34 +020011492__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020011493static void __ssl_sock_init(void)
11494{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011495#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011496 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011497 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011498#endif
Emeric Brun46591952012-05-18 15:47:34 +020011499
Willy Tarreauef934602016-12-22 23:12:01 +010011500 if (global_ssl.listen_default_ciphers)
11501 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
11502 if (global_ssl.connect_default_ciphers)
11503 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011504#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011505 if (global_ssl.listen_default_ciphersuites)
11506 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
11507 if (global_ssl.connect_default_ciphersuites)
11508 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
11509#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010011510
Willy Tarreau13e14102016-12-22 20:25:26 +010011511 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011512#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020011513 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080011514#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011515#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011516 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011517 n = sk_SSL_COMP_num(cm);
11518 while (n--) {
11519 (void) sk_SSL_COMP_pop(cm);
11520 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011521#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011522
Willy Tarreau5db847a2019-05-09 14:13:35 +020011523#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011524 ssl_locking_init();
11525#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020011526#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011527 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
11528#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020011529 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020011530 ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011531#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011532 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011533 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011534#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010011535#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11536 hap_register_post_check(tlskeys_finalize_config);
11537#endif
Willy Tarreau80713382018-11-26 10:19:54 +010011538
11539 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
11540 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
11541
11542#ifndef OPENSSL_NO_DH
11543 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
11544 hap_register_post_deinit(ssl_free_dh);
11545#endif
11546#ifndef OPENSSL_NO_ENGINE
11547 hap_register_post_deinit(ssl_free_engines);
11548#endif
11549 /* Load SSL string for the verbose & debug mode. */
11550 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020011551 ha_meth = BIO_meth_new(0x666, "ha methods");
11552 BIO_meth_set_write(ha_meth, ha_ssl_write);
11553 BIO_meth_set_read(ha_meth, ha_ssl_read);
11554 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
11555 BIO_meth_set_create(ha_meth, ha_ssl_new);
11556 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
11557 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
11558 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020011559
11560 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010011561}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010011562
Willy Tarreau80713382018-11-26 10:19:54 +010011563/* Compute and register the version string */
11564static void ssl_register_build_options()
11565{
11566 char *ptr = NULL;
11567 int i;
11568
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011569 memprintf(&ptr, "Built with OpenSSL version : "
11570#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011571 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011572#else /* OPENSSL_IS_BORINGSSL */
11573 OPENSSL_VERSION_TEXT
11574 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080011575 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020011576 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011577#endif
11578 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011579#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011580 "no (library version too old)"
11581#elif defined(OPENSSL_NO_TLSEXT)
11582 "no (disabled via OPENSSL_NO_TLSEXT)"
11583#else
11584 "yes"
11585#endif
11586 "", ptr);
11587
11588 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
11589#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11590 "yes"
11591#else
11592#ifdef OPENSSL_NO_TLSEXT
11593 "no (because of OPENSSL_NO_TLSEXT)"
11594#else
11595 "no (version might be too old, 0.9.8f min needed)"
11596#endif
11597#endif
11598 "", ptr);
11599
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020011600 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
11601 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
11602 if (methodVersions[i].option)
11603 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011604
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011605 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010011606}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011607
Willy Tarreau80713382018-11-26 10:19:54 +010011608INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020011609
Emeric Brun46591952012-05-18 15:47:34 +020011610
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011611#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011612void ssl_free_engines(void) {
11613 struct ssl_engine_list *wl, *wlb;
11614 /* free up engine list */
11615 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
11616 ENGINE_finish(wl->e);
11617 ENGINE_free(wl->e);
11618 LIST_DEL(&wl->list);
11619 free(wl);
11620 }
11621}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011622#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020011623
Remi Gacogned3a23c32015-05-28 16:39:47 +020011624#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000011625void ssl_free_dh(void) {
11626 if (local_dh_1024) {
11627 DH_free(local_dh_1024);
11628 local_dh_1024 = NULL;
11629 }
11630 if (local_dh_2048) {
11631 DH_free(local_dh_2048);
11632 local_dh_2048 = NULL;
11633 }
11634 if (local_dh_4096) {
11635 DH_free(local_dh_4096);
11636 local_dh_4096 = NULL;
11637 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020011638 if (global_dh) {
11639 DH_free(global_dh);
11640 global_dh = NULL;
11641 }
Grant Zhang872f9c22017-01-21 01:10:18 +000011642}
11643#endif
11644
11645__attribute__((destructor))
11646static void __ssl_sock_deinit(void)
11647{
11648#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011649 if (ssl_ctx_lru_tree) {
11650 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010011651 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020011652 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020011653#endif
11654
Willy Tarreau5db847a2019-05-09 14:13:35 +020011655#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011656 ERR_remove_state(0);
11657 ERR_free_strings();
11658
11659 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080011660#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020011661
Willy Tarreau5db847a2019-05-09 14:13:35 +020011662#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011663 CRYPTO_cleanup_all_ex_data();
11664#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020011665 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020011666}
11667
11668
Emeric Brun46591952012-05-18 15:47:34 +020011669/*
11670 * Local variables:
11671 * c-indent-level: 8
11672 * c-basic-offset: 8
11673 * End:
11674 */