blob: 95dbe4c891384d79aa5ed6f3bf50f54f755b069b [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 }
1109
William Lallemand3b5f3602019-10-16 18:05:05 +02001110 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001111 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001112end:
1113 if (fd != -1)
1114 close(fd);
1115
1116 return ret;
1117}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001118#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001119
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001120#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1121static 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)
1122{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001123 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001124 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001125 struct connection *conn;
1126 int head;
1127 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001128 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001129
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001130 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001131 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001132 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1133
1134 keys = ref->tlskeys;
1135 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001136
1137 if (enc) {
1138 memcpy(key_name, keys[head].name, 16);
1139
1140 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001141 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001142
Emeric Brun9e754772019-01-10 17:51:55 +01001143 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001144
Emeric Brun9e754772019-01-10 17:51:55 +01001145 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1146 goto end;
1147
Willy Tarreau9356dac2019-05-10 09:22:53 +02001148 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001149 ret = 1;
1150 }
1151 else if (ref->key_size_bits == 256 ) {
1152
1153 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1154 goto end;
1155
Willy Tarreau9356dac2019-05-10 09:22:53 +02001156 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001157 ret = 1;
1158 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001159 } else {
1160 for (i = 0; i < TLS_TICKETS_NO; i++) {
1161 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1162 goto found;
1163 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001164 ret = 0;
1165 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001166
Christopher Faulet16f45c82018-02-16 11:23:49 +01001167 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001168 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001169 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 +01001170 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1171 goto end;
1172 /* 2 for key renewal, 1 if current key is still valid */
1173 ret = i ? 2 : 1;
1174 }
1175 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001176 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 +01001177 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1178 goto end;
1179 /* 2 for key renewal, 1 if current key is still valid */
1180 ret = i ? 2 : 1;
1181 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001182 }
Emeric Brun9e754772019-01-10 17:51:55 +01001183
Christopher Faulet16f45c82018-02-16 11:23:49 +01001184 end:
1185 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1186 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001187}
1188
1189struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1190{
1191 struct tls_keys_ref *ref;
1192
1193 list_for_each_entry(ref, &tlskeys_reference, list)
1194 if (ref->filename && strcmp(filename, ref->filename) == 0)
1195 return ref;
1196 return NULL;
1197}
1198
1199struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1200{
1201 struct tls_keys_ref *ref;
1202
1203 list_for_each_entry(ref, &tlskeys_reference, list)
1204 if (ref->unique_id == unique_id)
1205 return ref;
1206 return NULL;
1207}
1208
Emeric Brun9e754772019-01-10 17:51:55 +01001209/* Update the key into ref: if keysize doesnt
1210 * match existing ones, this function returns -1
1211 * else it returns 0 on success.
1212 */
1213int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001214 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001215{
Emeric Brun9e754772019-01-10 17:51:55 +01001216 if (ref->key_size_bits == 128) {
1217 if (tlskey->data != sizeof(struct tls_sess_key_128))
1218 return -1;
1219 }
1220 else if (ref->key_size_bits == 256) {
1221 if (tlskey->data != sizeof(struct tls_sess_key_256))
1222 return -1;
1223 }
1224 else
1225 return -1;
1226
Christopher Faulet16f45c82018-02-16 11:23:49 +01001227 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001228 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1229 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001230 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1231 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001232
1233 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001234}
1235
Willy Tarreau83061a82018-07-13 11:56:34 +02001236int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001237{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001238 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1239
1240 if(!ref) {
1241 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1242 return 1;
1243 }
Emeric Brun9e754772019-01-10 17:51:55 +01001244 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1245 memprintf(err, "Invalid key size");
1246 return 1;
1247 }
1248
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001249 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001250}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001251
1252/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001253 * automatic ids. It's called just after the basic checks. It returns
1254 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001255 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001256static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001257{
1258 int i = 0;
1259 struct tls_keys_ref *ref, *ref2, *ref3;
1260 struct list tkr = LIST_HEAD_INIT(tkr);
1261
1262 list_for_each_entry(ref, &tlskeys_reference, list) {
1263 if (ref->unique_id == -1) {
1264 /* Look for the first free id. */
1265 while (1) {
1266 list_for_each_entry(ref2, &tlskeys_reference, list) {
1267 if (ref2->unique_id == i) {
1268 i++;
1269 break;
1270 }
1271 }
1272 if (&ref2->list == &tlskeys_reference)
1273 break;
1274 }
1275
1276 /* Uses the unique id and increment it for the next entry. */
1277 ref->unique_id = i;
1278 i++;
1279 }
1280 }
1281
1282 /* This sort the reference list by id. */
1283 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1284 LIST_DEL(&ref->list);
1285 list_for_each_entry(ref3, &tkr, list) {
1286 if (ref->unique_id < ref3->unique_id) {
1287 LIST_ADDQ(&ref3->list, &ref->list);
1288 break;
1289 }
1290 }
1291 if (&ref3->list == &tkr)
1292 LIST_ADDQ(&tkr, &ref->list);
1293 }
1294
1295 /* swap root */
1296 LIST_ADD(&tkr, &tlskeys_reference);
1297 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001298 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001299}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001300#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1301
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001302#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001303int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1304{
1305 switch (evp_keytype) {
1306 case EVP_PKEY_RSA:
1307 return 2;
1308 case EVP_PKEY_DSA:
1309 return 0;
1310 case EVP_PKEY_EC:
1311 return 1;
1312 }
1313
1314 return -1;
1315}
1316
Emeric Brun4147b2e2014-06-16 18:36:30 +02001317/*
1318 * Callback used to set OCSP status extension content in server hello.
1319 */
1320int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1321{
yanbzhube2774d2015-12-10 15:07:30 -05001322 struct certificate_ocsp *ocsp;
1323 struct ocsp_cbk_arg *ocsp_arg;
1324 char *ssl_buf;
1325 EVP_PKEY *ssl_pkey;
1326 int key_type;
1327 int index;
1328
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001329 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001330
1331 ssl_pkey = SSL_get_privatekey(ssl);
1332 if (!ssl_pkey)
1333 return SSL_TLSEXT_ERR_NOACK;
1334
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001335 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001336
1337 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1338 ocsp = ocsp_arg->s_ocsp;
1339 else {
1340 /* For multiple certs per context, we have to find the correct OCSP response based on
1341 * the certificate type
1342 */
1343 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1344
1345 if (index < 0)
1346 return SSL_TLSEXT_ERR_NOACK;
1347
1348 ocsp = ocsp_arg->m_ocsp[index];
1349
1350 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001351
1352 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001353 !ocsp->response.area ||
1354 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001355 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001356 return SSL_TLSEXT_ERR_NOACK;
1357
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001358 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001359 if (!ssl_buf)
1360 return SSL_TLSEXT_ERR_NOACK;
1361
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001362 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1363 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001364
1365 return SSL_TLSEXT_ERR_OK;
1366}
1367
William Lallemand4a660132019-10-14 14:51:41 +02001368#endif
1369
1370#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001371/*
1372 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001373 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1374 * status extension, the issuer's certificate is mandatory. It should be
1375 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001376 *
William Lallemand246c0242019-10-11 08:59:13 +02001377 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1378 * OCSP response. If file is empty or content is not a valid OCSP response,
1379 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1380 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001381 *
1382 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001383 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001384 */
William Lallemand4a660132019-10-14 14:51:41 +02001385#ifndef OPENSSL_IS_BORINGSSL
William Lallemand246c0242019-10-11 08:59:13 +02001386static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001387{
William Lallemand246c0242019-10-11 08:59:13 +02001388 X509 *x = NULL, *issuer = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001389 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001390 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001391 struct certificate_ocsp *ocsp = NULL, *iocsp;
1392 char *warn = NULL;
1393 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001394 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001395
Emeric Brun4147b2e2014-06-16 18:36:30 +02001396
William Lallemand246c0242019-10-11 08:59:13 +02001397 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001398 if (!x)
1399 goto out;
1400
William Lallemand246c0242019-10-11 08:59:13 +02001401 issuer = ckch->ocsp_issuer;
1402 if (!issuer)
1403 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001404
1405 cid = OCSP_cert_to_id(0, x, issuer);
1406 if (!cid)
1407 goto out;
1408
1409 i = i2d_OCSP_CERTID(cid, NULL);
1410 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1411 goto out;
1412
Vincent Bernat02779b62016-04-03 13:48:43 +02001413 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001414 if (!ocsp)
1415 goto out;
1416
1417 p = ocsp->key_data;
1418 i2d_OCSP_CERTID(cid, &p);
1419
1420 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1421 if (iocsp == ocsp)
1422 ocsp = NULL;
1423
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001424#ifndef SSL_CTX_get_tlsext_status_cb
1425# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1426 *cb = (void (*) (void))ctx->tlsext_status_cb;
1427#endif
1428 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1429
1430 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001431 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001432 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001433
1434 cb_arg->is_single = 1;
1435 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001436
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001437 pkey = X509_get_pubkey(x);
1438 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1439 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001440
1441 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1442 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1443 } else {
1444 /*
1445 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1446 * Update that cb_arg with the new cert's staple
1447 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001448 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001449 struct certificate_ocsp *tmp_ocsp;
1450 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001451 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001452 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001453
1454#ifdef SSL_CTX_get_tlsext_status_arg
1455 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1456#else
1457 cb_arg = ctx->tlsext_status_arg;
1458#endif
yanbzhube2774d2015-12-10 15:07:30 -05001459
1460 /*
1461 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1462 * the order of operations below matter, take care when changing it
1463 */
1464 tmp_ocsp = cb_arg->s_ocsp;
1465 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1466 cb_arg->s_ocsp = NULL;
1467 cb_arg->m_ocsp[index] = tmp_ocsp;
1468 cb_arg->is_single = 0;
1469 cb_arg->single_kt = 0;
1470
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001471 pkey = X509_get_pubkey(x);
1472 key_type = EVP_PKEY_base_id(pkey);
1473 EVP_PKEY_free(pkey);
1474
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001475 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001476 if (index >= 0 && !cb_arg->m_ocsp[index])
1477 cb_arg->m_ocsp[index] = iocsp;
1478
1479 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001480
1481 ret = 0;
1482
1483 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001484 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001485 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001486 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001487 }
1488
1489out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001490 if (cid)
1491 OCSP_CERTID_free(cid);
1492
1493 if (ocsp)
1494 free(ocsp);
1495
1496 if (warn)
1497 free(warn);
1498
Emeric Brun4147b2e2014-06-16 18:36:30 +02001499 return ret;
1500}
William Lallemand4a660132019-10-14 14:51:41 +02001501#else /* OPENSSL_IS_BORINGSSL */
1502static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001503{
William Lallemand4a660132019-10-14 14:51:41 +02001504 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 +02001505}
1506#endif
1507
William Lallemand4a660132019-10-14 14:51:41 +02001508#endif
1509
1510
Willy Tarreau5db847a2019-05-09 14:13:35 +02001511#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001512
1513#define CT_EXTENSION_TYPE 18
1514
1515static int sctl_ex_index = -1;
1516
1517/*
1518 * Try to parse Signed Certificate Timestamp List structure. This function
1519 * makes only basic test if the data seems like SCTL. No signature validation
1520 * is performed.
1521 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001522static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001523{
1524 int ret = 1;
1525 int len, pos, sct_len;
1526 unsigned char *data;
1527
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001528 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001529 goto out;
1530
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001531 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001532 len = (data[0] << 8) | data[1];
1533
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001534 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001535 goto out;
1536
1537 data = data + 2;
1538 pos = 0;
1539 while (pos < len) {
1540 if (len - pos < 2)
1541 goto out;
1542
1543 sct_len = (data[pos] << 8) | data[pos + 1];
1544 if (pos + sct_len + 2 > len)
1545 goto out;
1546
1547 pos += sct_len + 2;
1548 }
1549
1550 ret = 0;
1551
1552out:
1553 return ret;
1554}
1555
William Lallemand0dfae6c2019-10-16 18:06:58 +02001556/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1557 * It fills the ckch->sctl buffer
1558 * return 0 on success or != 0 on failure */
1559static 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 +01001560{
1561 int fd = -1;
1562 int r = 0;
1563 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001564 struct buffer tmp;
1565 struct buffer *src;
1566 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001567
William Lallemand0dfae6c2019-10-16 18:06:58 +02001568 if (buf) {
1569 tmp.area = buf;
1570 tmp.data = strlen(buf);
1571 tmp.size = tmp.data + 1;
1572 src = &tmp;
1573 } else {
1574 fd = open(sctl_path, O_RDONLY);
1575 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001576 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001577
1578 trash.data = 0;
1579 while (trash.data < trash.size) {
1580 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1581 if (r < 0) {
1582 if (errno == EINTR)
1583 continue;
1584 goto end;
1585 }
1586 else if (r == 0) {
1587 break;
1588 }
1589 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001590 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001591 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001592 }
1593
William Lallemand0dfae6c2019-10-16 18:06:58 +02001594 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001595 if (ret)
1596 goto end;
1597
William Lallemand0dfae6c2019-10-16 18:06:58 +02001598 sctl = calloc(1, sizeof(*sctl));
1599 if (!chunk_dup(sctl, src)) {
1600 free(sctl);
1601 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001602 goto end;
1603 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001604 ret = 0;
1605 /* TODO: free the previous SCTL in the ckch */
1606 ckch->sctl = sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001607
1608end:
1609 if (fd != -1)
1610 close(fd);
1611
1612 return ret;
1613}
1614
1615int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1616{
Willy Tarreau83061a82018-07-13 11:56:34 +02001617 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001618
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001619 *out = (unsigned char *) sctl->area;
1620 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001621
1622 return 1;
1623}
1624
1625int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1626{
1627 return 1;
1628}
1629
William Lallemanda17f4112019-10-10 15:16:44 +02001630static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001631{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001632 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001633
William Lallemanda17f4112019-10-10 15:16:44 +02001634 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 +01001635 goto out;
1636
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001637 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1638
1639 ret = 0;
1640
1641out:
1642 return ret;
1643}
1644
1645#endif
1646
Emeric Brune1f38db2012-09-03 20:36:47 +02001647void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1648{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001649 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001650 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001651 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001652 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001653
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001654#ifndef SSL_OP_NO_RENEGOTIATION
1655 /* Please note that BoringSSL defines this macro to zero so don't
1656 * change this to #if and do not assign a default value to this macro!
1657 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001658 if (where & SSL_CB_HANDSHAKE_START) {
1659 /* Disable renegotiation (CVE-2009-3555) */
Olivier Houchard90084a12017-11-23 18:21:29 +01001660 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 +02001661 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001662 conn->err_code = CO_ER_SSL_RENEG;
1663 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001664 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001665#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001666
1667 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001668 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001669 /* Long certificate chains optimz
1670 If write and read bios are differents, we
1671 consider that the buffering was activated,
1672 so we rise the output buffer size from 4k
1673 to 16k */
1674 write_bio = SSL_get_wbio(ssl);
1675 if (write_bio != SSL_get_rbio(ssl)) {
1676 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001677 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001678 }
1679 }
1680 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001681}
1682
Emeric Brune64aef12012-09-21 13:15:06 +02001683/* Callback is called for each certificate of the chain during a verify
1684 ok is set to 1 if preverify detect no error on current certificate.
1685 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001686int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001687{
1688 SSL *ssl;
1689 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001690 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001691 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001692
1693 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001694 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001695
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001696 ctx = conn->xprt_ctx;
1697
1698 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001699
Emeric Brun81c00f02012-09-21 14:31:21 +02001700 if (ok) /* no errors */
1701 return ok;
1702
1703 depth = X509_STORE_CTX_get_error_depth(x_store);
1704 err = X509_STORE_CTX_get_error(x_store);
1705
1706 /* check if CA error needs to be ignored */
1707 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001708 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1709 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1710 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001711 }
1712
Willy Tarreau07d94e42018-09-20 10:57:52 +02001713 if (__objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001714 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001715 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001716 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001717 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001718
Willy Tarreau20879a02012-12-03 16:32:10 +01001719 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001720 return 0;
1721 }
1722
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001723 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1724 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001725
Emeric Brun81c00f02012-09-21 14:31:21 +02001726 /* check if certificate error needs to be ignored */
Willy Tarreau07d94e42018-09-20 10:57:52 +02001727 if (__objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001728 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001729 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001730 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001731 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001732
Willy Tarreau20879a02012-12-03 16:32:10 +01001733 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001734 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001735}
1736
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001737static inline
1738void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001739 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001740{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001741 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001742 unsigned char *msg;
1743 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001744 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001745
1746 /* This function is called for "from client" and "to server"
1747 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001748 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001749 */
1750
1751 /* "write_p" is set to 0 is the bytes are received messages,
1752 * otherwise it is set to 1.
1753 */
1754 if (write_p != 0)
1755 return;
1756
1757 /* content_type contains the type of message received or sent
1758 * according with the SSL/TLS protocol spec. This message is
1759 * encoded with one byte. The value 256 (two bytes) is used
1760 * for designing the SSL/TLS record layer. According with the
1761 * rfc6101, the expected message (other than 256) are:
1762 * - change_cipher_spec(20)
1763 * - alert(21)
1764 * - handshake(22)
1765 * - application_data(23)
1766 * - (255)
1767 * We are interessed by the handshake and specially the client
1768 * hello.
1769 */
1770 if (content_type != 22)
1771 return;
1772
1773 /* The message length is at least 4 bytes, containing the
1774 * message type and the message length.
1775 */
1776 if (len < 4)
1777 return;
1778
1779 /* First byte of the handshake message id the type of
1780 * message. The konwn types are:
1781 * - hello_request(0)
1782 * - client_hello(1)
1783 * - server_hello(2)
1784 * - certificate(11)
1785 * - server_key_exchange (12)
1786 * - certificate_request(13)
1787 * - server_hello_done(14)
1788 * We are interested by the client hello.
1789 */
1790 msg = (unsigned char *)buf;
1791 if (msg[0] != 1)
1792 return;
1793
1794 /* Next three bytes are the length of the message. The total length
1795 * must be this decoded length + 4. If the length given as argument
1796 * is not the same, we abort the protocol dissector.
1797 */
1798 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1799 if (len < rec_len + 4)
1800 return;
1801 msg += 4;
1802 end = msg + rec_len;
1803 if (end < msg)
1804 return;
1805
1806 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1807 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001808 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1809 */
1810 msg += 1 + 1 + 4 + 28;
1811 if (msg > end)
1812 return;
1813
1814 /* Next, is session id:
1815 * if present, we have to jump by length + 1 for the size information
1816 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001817 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001818 if (msg[0] > 0)
1819 msg += msg[0];
1820 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001821 if (msg > end)
1822 return;
1823
1824 /* Next two bytes are the ciphersuite length. */
1825 if (msg + 2 > end)
1826 return;
1827 rec_len = (msg[0] << 8) + msg[1];
1828 msg += 2;
1829 if (msg + rec_len > end || msg + rec_len < msg)
1830 return;
1831
Willy Tarreaubafbe012017-11-24 17:34:44 +01001832 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001833 if (!capture)
1834 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001835 /* Compute the xxh64 of the ciphersuite. */
1836 capture->xxh64 = XXH64(msg, rec_len, 0);
1837
1838 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001839 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1840 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001841 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001842
1843 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001844}
1845
Emeric Brun29f037d2014-04-25 19:05:36 +02001846/* Callback is called for ssl protocol analyse */
1847void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1848{
Emeric Brun29f037d2014-04-25 19:05:36 +02001849#ifdef TLS1_RT_HEARTBEAT
1850 /* test heartbeat received (write_p is set to 0
1851 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001852 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001853 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001854 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001855 const unsigned char *p = buf;
1856 unsigned int payload;
1857
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001858 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001859
1860 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1861 if (*p != TLS1_HB_REQUEST)
1862 return;
1863
Willy Tarreauaeed6722014-04-25 23:59:58 +02001864 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001865 goto kill_it;
1866
1867 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001868 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001869 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001870 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001871 /* We have a clear heartbleed attack (CVE-2014-0160), the
1872 * advertised payload is larger than the advertised packet
1873 * length, so we have garbage in the buffer between the
1874 * payload and the end of the buffer (p+len). We can't know
1875 * if the SSL stack is patched, and we don't know if we can
1876 * safely wipe out the area between p+3+len and payload.
1877 * So instead, we prevent the response from being sent by
1878 * setting the max_send_fragment to 0 and we report an SSL
1879 * error, which will kill this connection. It will be reported
1880 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001881 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1882 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001883 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001884 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1885 return;
1886 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001887#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001888 if (global_ssl.capture_cipherlist > 0)
1889 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001890}
1891
Bernard Spil13c53f82018-02-15 13:34:58 +01001892#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001893static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1894 const unsigned char *in, unsigned int inlen,
1895 void *arg)
1896{
1897 struct server *srv = arg;
1898
1899 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1900 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1901 return SSL_TLSEXT_ERR_OK;
1902 return SSL_TLSEXT_ERR_NOACK;
1903}
1904#endif
1905
1906#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001907/* This callback is used so that the server advertises the list of
1908 * negociable protocols for NPN.
1909 */
1910static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1911 unsigned int *len, void *arg)
1912{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001913 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001914
1915 *data = (const unsigned char *)conf->npn_str;
1916 *len = conf->npn_len;
1917 return SSL_TLSEXT_ERR_OK;
1918}
1919#endif
1920
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001921#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001922/* This callback is used so that the server advertises the list of
1923 * negociable protocols for ALPN.
1924 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001925static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1926 unsigned char *outlen,
1927 const unsigned char *server,
1928 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001929{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001930 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001931
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001932 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1933 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1934 return SSL_TLSEXT_ERR_NOACK;
1935 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001936 return SSL_TLSEXT_ERR_OK;
1937}
1938#endif
1939
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001940#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001941#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001942
Christopher Faulet30548802015-06-11 13:39:32 +02001943/* Create a X509 certificate with the specified servername and serial. This
1944 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001945static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001946ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001947{
Christopher Faulet7969a332015-10-09 11:15:03 +02001948 X509 *cacert = bind_conf->ca_sign_cert;
1949 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001950 SSL_CTX *ssl_ctx = NULL;
1951 X509 *newcrt = NULL;
1952 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001953 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001954 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001955 X509_NAME *name;
1956 const EVP_MD *digest;
1957 X509V3_CTX ctx;
1958 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001959 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001960
Christopher Faulet48a83322017-07-28 16:56:09 +02001961 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001962#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001963 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1964#else
1965 tmp_ssl = SSL_new(bind_conf->default_ctx);
1966 if (tmp_ssl)
1967 pkey = SSL_get_privatekey(tmp_ssl);
1968#endif
1969 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001970 goto mkcert_error;
1971
1972 /* Create the certificate */
1973 if (!(newcrt = X509_new()))
1974 goto mkcert_error;
1975
1976 /* Set version number for the certificate (X509v3) and the serial
1977 * number */
1978 if (X509_set_version(newcrt, 2L) != 1)
1979 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001980 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001981
1982 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001983 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1984 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001985 goto mkcert_error;
1986
1987 /* set public key in the certificate */
1988 if (X509_set_pubkey(newcrt, pkey) != 1)
1989 goto mkcert_error;
1990
1991 /* Set issuer name from the CA */
1992 if (!(name = X509_get_subject_name(cacert)))
1993 goto mkcert_error;
1994 if (X509_set_issuer_name(newcrt, name) != 1)
1995 goto mkcert_error;
1996
1997 /* Set the subject name using the same, but the CN */
1998 name = X509_NAME_dup(name);
1999 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2000 (const unsigned char *)servername,
2001 -1, -1, 0) != 1) {
2002 X509_NAME_free(name);
2003 goto mkcert_error;
2004 }
2005 if (X509_set_subject_name(newcrt, name) != 1) {
2006 X509_NAME_free(name);
2007 goto mkcert_error;
2008 }
2009 X509_NAME_free(name);
2010
2011 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002012 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002013 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2014 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2015 X509_EXTENSION *ext;
2016
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002017 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002018 goto mkcert_error;
2019 if (!X509_add_ext(newcrt, ext, -1)) {
2020 X509_EXTENSION_free(ext);
2021 goto mkcert_error;
2022 }
2023 X509_EXTENSION_free(ext);
2024 }
2025
2026 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002027
2028 key_type = EVP_PKEY_base_id(capkey);
2029
2030 if (key_type == EVP_PKEY_DSA)
2031 digest = EVP_sha1();
2032 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002033 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002034 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002035 digest = EVP_sha256();
2036 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002037#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002038 int nid;
2039
2040 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2041 goto mkcert_error;
2042 if (!(digest = EVP_get_digestbynid(nid)))
2043 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002044#else
2045 goto mkcert_error;
2046#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002047 }
2048
Christopher Faulet31af49d2015-06-09 17:29:50 +02002049 if (!(X509_sign(newcrt, capkey, digest)))
2050 goto mkcert_error;
2051
2052 /* Create and set the new SSL_CTX */
2053 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2054 goto mkcert_error;
2055 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2056 goto mkcert_error;
2057 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2058 goto mkcert_error;
2059 if (!SSL_CTX_check_private_key(ssl_ctx))
2060 goto mkcert_error;
2061
2062 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002063
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002064#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002065 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002066#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002067#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2068 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002069 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002070 EC_KEY *ecc;
2071 int nid;
2072
2073 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2074 goto end;
2075 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2076 goto end;
2077 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2078 EC_KEY_free(ecc);
2079 }
2080#endif
2081 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002082 return ssl_ctx;
2083
2084 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002085 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002086 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002087 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2088 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002089 return NULL;
2090}
2091
Christopher Faulet7969a332015-10-09 11:15:03 +02002092SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002093ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002094{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002095 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002096 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002097
Olivier Houchard66ab4982019-02-26 18:37:15 +01002098 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002099}
2100
Christopher Faulet30548802015-06-11 13:39:32 +02002101/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002102 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002103SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002104ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002105{
2106 struct lru64 *lru = NULL;
2107
2108 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002109 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002110 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002111 if (lru && lru->domain) {
2112 if (ssl)
2113 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002114 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002115 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002116 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002117 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002118 }
2119 return NULL;
2120}
2121
Emeric Brun821bb9b2017-06-15 16:37:39 +02002122/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2123 * function is not thread-safe, it should only be used to check if a certificate
2124 * exists in the lru cache (with no warranty it will not be removed by another
2125 * thread). It is kept for backward compatibility. */
2126SSL_CTX *
2127ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2128{
2129 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2130}
2131
Christopher Fauletd2cab922015-07-28 16:03:47 +02002132/* Set a certificate int the LRU cache used to store generated
2133 * certificate. Return 0 on success, otherwise -1 */
2134int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002135ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002136{
2137 struct lru64 *lru = NULL;
2138
2139 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002140 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002141 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002142 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002143 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002144 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002145 }
Christopher Faulet30548802015-06-11 13:39:32 +02002146 if (lru->domain && lru->data)
2147 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002148 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002149 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002150 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002151 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002152 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002153}
2154
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002155/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002156unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002157ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002158{
2159 return XXH32(data, len, ssl_ctx_lru_seed);
2160}
2161
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002162/* Generate a cert and immediately assign it to the SSL session so that the cert's
2163 * refcount is maintained regardless of the cert's presence in the LRU cache.
2164 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002165static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002166ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002167{
2168 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002169 SSL_CTX *ssl_ctx = NULL;
2170 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002171 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002172
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002173 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002174 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002175 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002176 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002177 if (lru && lru->domain)
2178 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002179 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002180 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002181 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002182 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002183 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002184 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002185 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002186 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002187 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002188 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002189 SSL_set_SSL_CTX(ssl, ssl_ctx);
2190 /* No LRU cache, this CTX will be released as soon as the session dies */
2191 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002192 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002193 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002194 return 0;
2195}
2196static int
2197ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2198{
2199 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002200 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002201
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002202 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002203 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002204 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002205 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002206 }
2207 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002208}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002209#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002210
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002211#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002212typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2213
2214static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002215{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002216#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002217 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002218 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2219#endif
2220}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002221static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2222 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002223 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2224}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002225static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002226#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002227 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002228 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2229#endif
2230}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002231static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002232#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002233 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002234 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2235#endif
2236}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002237/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002238static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2239/* Unusable in this context. */
2240static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2241static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2242static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2243static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2244static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002245#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002246typedef enum { SET_MIN, SET_MAX } set_context_func;
2247
2248static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2249 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002250 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2251}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002252static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2253 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2254 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2255}
2256static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2257 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002258 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2259}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002260static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2261 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2262 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2263}
2264static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2265 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002266 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2267}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002268static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2269 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2270 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2271}
2272static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2273 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002274 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2275}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002276static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2277 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2278 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2279}
2280static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002281#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002282 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002283 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2284#endif
2285}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002286static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2287#if SSL_OP_NO_TLSv1_3
2288 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2289 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002290#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002291}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002292#endif
2293static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2294static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002295
2296static struct {
2297 int option;
2298 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002299 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2300 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002301 const char *name;
2302} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002303 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2304 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2305 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2306 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2307 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2308 {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 +02002309};
2310
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002311static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2312{
2313 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2314 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2315 SSL_set_SSL_CTX(ssl, ctx);
2316}
2317
Willy Tarreau5db847a2019-05-09 14:13:35 +02002318#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002319
2320static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2321{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002322 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002323 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002324
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002325 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2326 return SSL_TLSEXT_ERR_OK;
2327 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002328}
2329
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002330#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002331static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2332{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002333 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002334#else
2335static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2336{
2337#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002338 struct connection *conn;
2339 struct bind_conf *s;
2340 const uint8_t *extension_data;
2341 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002342 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002343
2344 char *wildp = NULL;
2345 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002346 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002347 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002348 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002349 int i;
2350
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002351 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002352 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002353
Olivier Houchard9679ac92017-10-27 14:58:08 +02002354 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002355 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002356#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002357 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2358 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002359#else
2360 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2361#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002362 /*
2363 * The server_name extension was given too much extensibility when it
2364 * was written, so parsing the normal case is a bit complex.
2365 */
2366 size_t len;
2367 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002368 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002369 /* Extract the length of the supplied list of names. */
2370 len = (*extension_data++) << 8;
2371 len |= *extension_data++;
2372 if (len + 2 != extension_len)
2373 goto abort;
2374 /*
2375 * The list in practice only has a single element, so we only consider
2376 * the first one.
2377 */
2378 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2379 goto abort;
2380 extension_len = len - 1;
2381 /* Now we can finally pull out the byte array with the actual hostname. */
2382 if (extension_len <= 2)
2383 goto abort;
2384 len = (*extension_data++) << 8;
2385 len |= *extension_data++;
2386 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2387 || memchr(extension_data, 0, len) != NULL)
2388 goto abort;
2389 servername = extension_data;
2390 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002391 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002392#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2393 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002394 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002395 }
2396#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002397 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002398 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002399 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002400 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002401 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002402 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002403 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002404 goto abort;
2405 }
2406
2407 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002408#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002409 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002410#else
2411 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2412#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002413 uint8_t sign;
2414 size_t len;
2415 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002416 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002417 len = (*extension_data++) << 8;
2418 len |= *extension_data++;
2419 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002420 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002421 if (len % 2 != 0)
2422 goto abort;
2423 for (; len > 0; len -= 2) {
2424 extension_data++; /* hash */
2425 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002426 switch (sign) {
2427 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002428 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002429 break;
2430 case TLSEXT_signature_ecdsa:
2431 has_ecdsa_sig = 1;
2432 break;
2433 default:
2434 continue;
2435 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002436 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002437 break;
2438 }
2439 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002440 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002441 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002442 }
2443 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002444 const SSL_CIPHER *cipher;
2445 size_t len;
2446 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002447 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002448#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002449 len = ctx->cipher_suites_len;
2450 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002451#else
2452 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2453#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002454 if (len % 2 != 0)
2455 goto abort;
2456 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002457#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002458 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002459 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002460#else
2461 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2462#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002463 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002464 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002465 break;
2466 }
2467 }
2468 }
2469
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002470 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002471 trash.area[i] = tolower(servername[i]);
2472 if (!wildp && (trash.area[i] == '.'))
2473 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002474 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002475 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002476
William Lallemand150bfa82019-09-19 17:12:49 +02002477 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002478
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002479 for (i = 0; i < 2; i++) {
2480 if (i == 0) /* lookup in full qualified names */
2481 node = ebst_lookup(&s->sni_ctx, trash.area);
2482 else if (i == 1 && wildp) /* lookup in wildcards names */
2483 node = ebst_lookup(&s->sni_w_ctx, wildp);
2484 else
2485 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002486 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002487 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002488 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002489 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002490 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002491 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002492 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002493 break;
2494 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002495 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002496 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002497 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002498 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002499 if (!node_anonymous)
2500 node_anonymous = n;
2501 break;
2502 }
2503 }
2504 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002505 /* select by key_signature priority order */
2506 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2507 : ((has_rsa_sig && node_rsa) ? node_rsa
2508 : (node_anonymous ? node_anonymous
2509 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2510 : node_rsa /* no rsa signature case (far far away) */
2511 )));
2512 if (node) {
2513 /* switch ctx */
2514 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2515 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002516 if (conf) {
2517 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2518 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2519 if (conf->early_data)
2520 allow_early = 1;
2521 }
William Lallemand02010472019-10-18 11:02:19 +02002522 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002523 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002524 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002525 }
William Lallemand150bfa82019-09-19 17:12:49 +02002526
William Lallemand02010472019-10-18 11:02:19 +02002527 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002528#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002529 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002530 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002531 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002532 }
2533#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002534 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002535 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002536 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002537 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002538 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002539 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002540allow_early:
2541#ifdef OPENSSL_IS_BORINGSSL
2542 if (allow_early)
2543 SSL_set_early_data_enabled(ssl, 1);
2544#else
2545 if (!allow_early)
2546 SSL_set_max_early_data(ssl, 0);
2547#endif
2548 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002549 abort:
2550 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2551 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002552#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002553 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002554#else
2555 *al = SSL_AD_UNRECOGNIZED_NAME;
2556 return 0;
2557#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002558}
2559
2560#else /* OPENSSL_IS_BORINGSSL */
2561
Emeric Brunfc0421f2012-09-07 17:30:07 +02002562/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2563 * warning when no match is found, which implies the default (first) cert
2564 * will keep being used.
2565 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002566static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002567{
2568 const char *servername;
2569 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002570 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002571 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002572 int i;
2573 (void)al; /* shut gcc stupid warning */
2574
2575 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002576 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002577#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002578 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2579 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002580#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002581 if (s->strict_sni)
2582 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002583 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002584 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002585 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002586 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002587 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002588
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002589 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002590 if (!servername[i])
2591 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002592 trash.area[i] = tolower(servername[i]);
2593 if (!wildp && (trash.area[i] == '.'))
2594 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002595 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002596 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002597
William Lallemand150bfa82019-09-19 17:12:49 +02002598 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002599 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002600 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002601 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2602 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002603 if (!container_of(n, struct sni_ctx, name)->neg) {
2604 node = n;
2605 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002606 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002607 }
2608 if (!node && wildp) {
2609 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002610 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2611 /* lookup a not neg filter */
2612 if (!container_of(n, struct sni_ctx, name)->neg) {
2613 node = n;
2614 break;
2615 }
2616 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002617 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002618 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002619#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002620 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2621 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002622 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002623 return SSL_TLSEXT_ERR_OK;
2624 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002625#endif
William Lallemand21724f02019-11-04 17:56:13 +01002626 if (s->strict_sni) {
2627 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002628 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002629 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002630 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002631 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002632 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002633 }
2634
2635 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002636 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002637 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002638 return SSL_TLSEXT_ERR_OK;
2639}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002640#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002641#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2642
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002643#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002644
2645static DH * ssl_get_dh_1024(void)
2646{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002647 static unsigned char dh1024_p[]={
2648 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2649 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2650 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2651 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2652 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2653 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2654 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2655 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2656 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2657 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2658 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2659 };
2660 static unsigned char dh1024_g[]={
2661 0x02,
2662 };
2663
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002664 BIGNUM *p;
2665 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002666 DH *dh = DH_new();
2667 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002668 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2669 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002670
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002671 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002672 DH_free(dh);
2673 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002674 } else {
2675 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002676 }
2677 }
2678 return dh;
2679}
2680
2681static DH *ssl_get_dh_2048(void)
2682{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002683 static unsigned char dh2048_p[]={
2684 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2685 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2686 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2687 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2688 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2689 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2690 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2691 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2692 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2693 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2694 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2695 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2696 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2697 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2698 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2699 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2700 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2701 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2702 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2703 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2704 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2705 0xB7,0x1F,0x77,0xF3,
2706 };
2707 static unsigned char dh2048_g[]={
2708 0x02,
2709 };
2710
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002711 BIGNUM *p;
2712 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002713 DH *dh = DH_new();
2714 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002715 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2716 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002717
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002718 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002719 DH_free(dh);
2720 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002721 } else {
2722 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002723 }
2724 }
2725 return dh;
2726}
2727
2728static DH *ssl_get_dh_4096(void)
2729{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002730 static unsigned char dh4096_p[]={
2731 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2732 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2733 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2734 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2735 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2736 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2737 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2738 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2739 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2740 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2741 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2742 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2743 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2744 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2745 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2746 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2747 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2748 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2749 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2750 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2751 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2752 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2753 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2754 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2755 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2756 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2757 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2758 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2759 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2760 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2761 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2762 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2763 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2764 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2765 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2766 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2767 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2768 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2769 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2770 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2771 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2772 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2773 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002774 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002775 static unsigned char dh4096_g[]={
2776 0x02,
2777 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002778
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002779 BIGNUM *p;
2780 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002781 DH *dh = DH_new();
2782 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002783 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2784 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002785
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002786 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002787 DH_free(dh);
2788 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002789 } else {
2790 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002791 }
2792 }
2793 return dh;
2794}
2795
2796/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002797 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002798static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2799{
2800 DH *dh = NULL;
2801 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002802 int type;
2803
2804 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002805
2806 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2807 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2808 */
2809 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2810 keylen = EVP_PKEY_bits(pkey);
2811 }
2812
Willy Tarreauef934602016-12-22 23:12:01 +01002813 if (keylen > global_ssl.default_dh_param) {
2814 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002815 }
2816
Remi Gacogned3a341a2015-05-29 16:26:17 +02002817 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002818 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002819 }
2820 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002821 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002822 }
2823 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002824 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002825 }
2826
2827 return dh;
2828}
2829
Remi Gacogne47783ef2015-05-29 15:53:22 +02002830static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002831{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002832 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002833 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002834
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002835 if (in == NULL)
2836 goto end;
2837
Remi Gacogne47783ef2015-05-29 15:53:22 +02002838 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002839 goto end;
2840
Remi Gacogne47783ef2015-05-29 15:53:22 +02002841 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2842
2843end:
2844 if (in)
2845 BIO_free(in);
2846
Emeric Brune1b4ed42018-08-16 15:14:12 +02002847 ERR_clear_error();
2848
Remi Gacogne47783ef2015-05-29 15:53:22 +02002849 return dh;
2850}
2851
2852int ssl_sock_load_global_dh_param_from_file(const char *filename)
2853{
2854 global_dh = ssl_sock_get_dh_from_file(filename);
2855
2856 if (global_dh) {
2857 return 0;
2858 }
2859
2860 return -1;
2861}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002862#endif
2863
William Lallemand9117de92019-10-04 00:29:42 +02002864/* Alloc and init a ckch_inst */
2865static struct ckch_inst *ckch_inst_new()
2866{
2867 struct ckch_inst *ckch_inst;
2868
2869 ckch_inst = calloc(1, sizeof *ckch_inst);
2870 if (ckch_inst)
2871 LIST_INIT(&ckch_inst->sni_ctx);
2872
2873 return ckch_inst;
2874}
2875
2876
2877/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002878static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002879 struct bind_conf *s, struct ssl_bind_conf *conf,
2880 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002881{
2882 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002883 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002884
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002885 if (*name == '!') {
2886 neg = 1;
2887 name++;
2888 }
2889 if (*name == '*') {
2890 wild = 1;
2891 name++;
2892 }
2893 /* !* filter is a nop */
2894 if (neg && wild)
2895 return order;
2896 if (*name) {
2897 int j, len;
2898 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002899 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002900 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002901 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002902 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002903 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002904
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002905 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002906 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002907 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002908 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002909 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002910 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002911 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002912 sc->order = order++;
2913 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002914 sc->wild = wild;
2915 sc->name.node.leaf_p = NULL;
William Lallemand1d29c742019-10-04 00:53:29 +02002916 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002917 }
2918 return order;
2919}
2920
William Lallemand6af03992019-07-23 15:00:54 +02002921/*
William Lallemand1d29c742019-10-04 00:53:29 +02002922 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2923 * This function can't return an error.
2924 *
2925 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2926 */
2927static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2928{
2929
2930 struct sni_ctx *sc0, *sc0b, *sc1;
2931 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002932 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002933
2934 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2935
2936 /* ignore if sc0 was already inserted in a tree */
2937 if (sc0->name.node.leaf_p)
2938 continue;
2939
2940 /* Check for duplicates. */
2941 if (sc0->wild)
2942 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2943 else
2944 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2945
2946 for (; node; node = ebmb_next_dup(node)) {
2947 sc1 = ebmb_entry(node, struct sni_ctx, name);
2948 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2949 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2950 /* it's a duplicate, we should remove and free it */
2951 LIST_DEL(&sc0->by_ckch_inst);
2952 free(sc0);
2953 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002954 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002955 }
2956 }
2957
2958 /* if duplicate, ignore the insertion */
2959 if (!sc0)
2960 continue;
2961
2962 if (sc0->wild)
2963 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
2964 else
2965 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01002966
2967 /* replace the default_ctx if required with the first ctx */
2968 if (ckch_inst->is_default && !def) {
2969 /* we don't need to free the default_ctx because the refcount was not incremented */
2970 bind_conf->default_ctx = sc0->ctx;
2971 def = 1;
2972 }
William Lallemand1d29c742019-10-04 00:53:29 +02002973 }
2974}
2975
2976/*
William Lallemande3af8fb2019-10-08 11:36:53 +02002977 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02002978 */
William Lallemande3af8fb2019-10-08 11:36:53 +02002979struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02002980
William Lallemandfa892222019-07-23 16:06:08 +02002981
Emeric Brun7a883362019-10-17 13:27:40 +02002982/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
2983 * If there is no DH paramater availaible in the ckchs, the global
2984 * DH parameter is loaded into the SSL_CTX and if there is no
2985 * DH parameter available in ckchs nor in global, the default
2986 * DH parameters are applied on the SSL_CTX.
2987 * Returns a bitfield containing the flags:
2988 * ERR_FATAL in any fatal error case
2989 * ERR_ALERT if a reason of the error is availabine in err
2990 * ERR_WARN if a warning is available into err
2991 * The value 0 means there is no error nor warning and
2992 * the operation succeed.
2993 */
William Lallemandfa892222019-07-23 16:06:08 +02002994#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02002995static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
2996 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02002997{
Emeric Brun7a883362019-10-17 13:27:40 +02002998 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02002999 DH *dh = NULL;
3000
William Lallemanda8c73742019-07-31 18:31:34 +02003001 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003002 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003003 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3004 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3005 err && *err ? *err : "", path);
3006#if defined(SSL_CTX_set_dh_auto)
3007 SSL_CTX_set_dh_auto(ctx, 1);
3008 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3009 err && *err ? *err : "");
3010#else
3011 memprintf(err, "%s, DH ciphers won't be available.\n",
3012 err && *err ? *err : "");
3013#endif
3014 ret |= ERR_WARN;
3015 goto end;
3016 }
William Lallemandfa892222019-07-23 16:06:08 +02003017
3018 if (ssl_dh_ptr_index >= 0) {
3019 /* store a pointer to the DH params to avoid complaining about
3020 ssl-default-dh-param not being set for this SSL_CTX */
3021 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3022 }
3023 }
3024 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003025 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3026 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3027 err && *err ? *err : "", path);
3028#if defined(SSL_CTX_set_dh_auto)
3029 SSL_CTX_set_dh_auto(ctx, 1);
3030 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3031 err && *err ? *err : "");
3032#else
3033 memprintf(err, "%s, DH ciphers won't be available.\n",
3034 err && *err ? *err : "");
3035#endif
3036 ret |= ERR_WARN;
3037 goto end;
3038 }
William Lallemandfa892222019-07-23 16:06:08 +02003039 }
3040 else {
3041 /* Clear openssl global errors stack */
3042 ERR_clear_error();
3043
3044 if (global_ssl.default_dh_param <= 1024) {
3045 /* we are limited to DH parameter of 1024 bits anyway */
3046 if (local_dh_1024 == NULL)
3047 local_dh_1024 = ssl_get_dh_1024();
3048
Emeric Brun7a883362019-10-17 13:27:40 +02003049 if (local_dh_1024 == NULL) {
3050 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3051 err && *err ? *err : "", path);
3052 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003053 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003054 }
William Lallemandfa892222019-07-23 16:06:08 +02003055
Emeric Bruna9363eb2019-10-17 14:53:03 +02003056 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3057 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3058 err && *err ? *err : "", path);
3059#if defined(SSL_CTX_set_dh_auto)
3060 SSL_CTX_set_dh_auto(ctx, 1);
3061 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3062 err && *err ? *err : "");
3063#else
3064 memprintf(err, "%s, DH ciphers won't be available.\n",
3065 err && *err ? *err : "");
3066#endif
3067 ret |= ERR_WARN;
3068 goto end;
3069 }
William Lallemandfa892222019-07-23 16:06:08 +02003070 }
3071 else {
3072 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3073 }
William Lallemandfa892222019-07-23 16:06:08 +02003074 }
3075
3076end:
William Lallemandfa892222019-07-23 16:06:08 +02003077 return ret;
3078}
3079#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003080
yanbzhu488a4d22015-12-01 15:16:07 -05003081/* Frees the contents of a cert_key_and_chain
3082 */
3083static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3084{
yanbzhu488a4d22015-12-01 15:16:07 -05003085 if (!ckch)
3086 return;
3087
3088 /* Free the certificate and set pointer to NULL */
3089 if (ckch->cert)
3090 X509_free(ckch->cert);
3091 ckch->cert = NULL;
3092
3093 /* Free the key and set pointer to NULL */
3094 if (ckch->key)
3095 EVP_PKEY_free(ckch->key);
3096 ckch->key = NULL;
3097
3098 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003099 if (ckch->chain)
3100 sk_X509_pop_free(ckch->chain, X509_free);
3101 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003102
William Lallemand455af502019-10-17 18:04:45 +02003103 if (ckch->dh)
3104 DH_free(ckch->dh);
3105 ckch->dh = NULL;
3106
3107 if (ckch->sctl) {
3108 free(ckch->sctl->area);
3109 ckch->sctl->area = NULL;
3110 free(ckch->sctl);
3111 ckch->sctl = NULL;
3112 }
3113
3114 if (ckch->ocsp_response) {
3115 free(ckch->ocsp_response->area);
3116 ckch->ocsp_response->area = NULL;
3117 free(ckch->ocsp_response);
3118 ckch->ocsp_response = NULL;
3119 }
yanbzhu488a4d22015-12-01 15:16:07 -05003120}
3121
William Lallemand8d0f8932019-10-17 18:03:58 +02003122/*
3123 *
3124 * This function copy a cert_key_and_chain in memory
3125 *
3126 * It's used to try to apply changes on a ckch before committing them, because
3127 * most of the time it's not possible to revert those changes
3128 *
3129 * Return a the dst or NULL
3130 */
3131static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3132 struct cert_key_and_chain *dst)
3133{
3134 if (src->cert) {
3135 dst->cert = src->cert;
3136 X509_up_ref(src->cert);
3137 }
3138
3139 if (src->key) {
3140 dst->key = src->key;
3141 EVP_PKEY_up_ref(src->key);
3142 }
3143
3144 if (src->chain) {
3145 dst->chain = X509_chain_up_ref(src->chain);
3146 }
3147
3148 if (src->dh) {
3149 DH_up_ref(src->dh);
3150 dst->dh = src->dh;
3151 }
3152
3153 if (src->sctl) {
3154 struct buffer *sctl;
3155
3156 sctl = calloc(1, sizeof(*sctl));
3157 if (!chunk_dup(sctl, src->sctl)) {
3158 free(sctl);
3159 sctl = NULL;
3160 goto error;
3161 }
3162 dst->sctl = sctl;
3163 }
3164
3165 if (src->ocsp_response) {
3166 struct buffer *ocsp_response;
3167
3168 ocsp_response = calloc(1, sizeof(*ocsp_response));
3169 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3170 free(ocsp_response);
3171 ocsp_response = NULL;
3172 goto error;
3173 }
3174 dst->ocsp_response = ocsp_response;
3175 }
3176
3177 if (src->ocsp_issuer) {
3178 X509_up_ref(src->ocsp_issuer);
3179 dst->ocsp_issuer = src->ocsp_issuer;
3180 }
3181
3182 return dst;
3183
3184error:
3185
3186 /* free everything */
3187 ssl_sock_free_cert_key_and_chain_contents(dst);
3188
3189 return NULL;
3190}
3191
3192
yanbzhu488a4d22015-12-01 15:16:07 -05003193/* checks if a key and cert exists in the ckch
3194 */
William Lallemand1633e392019-09-30 12:58:13 +02003195#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003196static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3197{
3198 return (ckch->cert != NULL && ckch->key != NULL);
3199}
William Lallemand1633e392019-09-30 12:58:13 +02003200#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003201
William Lallemandf9568fc2019-10-16 18:27:58 +02003202/*
3203 * return 0 on success or != 0 on failure
3204 */
3205static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3206{
3207 int ret = 1;
3208 BIO *in = NULL;
3209 X509 *issuer;
3210
3211 if (buf) {
3212 /* reading from a buffer */
3213 in = BIO_new_mem_buf(buf, -1);
3214 if (in == NULL) {
3215 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3216 goto end;
3217 }
3218
3219 } else {
3220 /* reading from a file */
3221 in = BIO_new(BIO_s_file());
3222 if (in == NULL)
3223 goto end;
3224
3225 if (BIO_read_filename(in, path) <= 0)
3226 goto end;
3227 }
3228
3229 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3230 if (!issuer) {
3231 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003232 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003233 goto end;
3234 }
3235 ret = 0;
3236 ckch->ocsp_issuer = issuer;
3237
3238end:
3239
3240 ERR_clear_error();
3241 if (in)
3242 BIO_free(in);
3243
3244 return ret;
3245}
3246
William Lallemand96a9c972019-10-17 11:56:17 +02003247
3248/*
3249 * Try to load a PEM file from a <path> or a buffer <buf>
3250 * The PEM must contain at least a Private Key and a Certificate,
3251 * It could contain a DH and a certificate chain.
yanbzhu488a4d22015-12-01 15:16:07 -05003252 *
William Lallemand96a9c972019-10-17 11:56:17 +02003253 * If it failed you should not attempt to use the ckch but free it.
3254 *
3255 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003256 */
William Lallemand96a9c972019-10-17 11:56:17 +02003257static 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 -05003258{
William Lallemandf11365b2019-09-19 14:25:58 +02003259 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003260 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003261 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003262 X509 *cert = NULL;
3263 EVP_PKEY *key = NULL;
3264 DH *dh;
3265
3266 if (buf) {
3267 /* reading from a buffer */
3268 in = BIO_new_mem_buf(buf, -1);
3269 if (in == NULL) {
3270 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3271 goto end;
3272 }
yanbzhu488a4d22015-12-01 15:16:07 -05003273
William Lallemand96a9c972019-10-17 11:56:17 +02003274 } else {
3275 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003276 in = BIO_new(BIO_s_file());
3277 if (in == NULL)
3278 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003279
William Lallemandf11365b2019-09-19 14:25:58 +02003280 if (BIO_read_filename(in, path) <= 0)
3281 goto end;
William Lallemandf11365b2019-09-19 14:25:58 +02003282 }
yanbzhu488a4d22015-12-01 15:16:07 -05003283
yanbzhu488a4d22015-12-01 15:16:07 -05003284 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003285 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3286 if (key == NULL) {
yanbzhu488a4d22015-12-01 15:16:07 -05003287 memprintf(err, "%sunable to load private key from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003288 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003289 goto end;
3290 }
3291
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003292#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003293 /* Seek back to beginning of file */
3294 if (BIO_reset(in) == -1) {
3295 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3296 err && *err ? *err : "", path);
3297 goto end;
3298 }
3299
William Lallemand96a9c972019-10-17 11:56:17 +02003300 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3301 /* no need to return an error there, dh is not mandatory */
3302
3303 if (dh) {
3304 if (ckch->dh)
3305 DH_free(ckch->dh);
3306 ckch->dh = dh;
3307 }
3308
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003309#endif
William Lallemandfa892222019-07-23 16:06:08 +02003310
Willy Tarreaubb137a82016-04-06 19:02:38 +02003311 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003312 if (BIO_reset(in) == -1) {
3313 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3314 err && *err ? *err : "", path);
3315 goto end;
3316 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003317
3318 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003319 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3320 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003321 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003322 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003323 goto end;
3324 }
3325
William Lallemand96a9c972019-10-17 11:56:17 +02003326 if (!X509_check_private_key(cert, key)) {
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003327 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003328 err && *err ? *err : "", path);
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003329 goto end;
3330 }
3331
William Lallemand96a9c972019-10-17 11:56:17 +02003332 /* Key and Cert are good, we can use them in the ckch */
3333 if (ckch->key) /* free the previous key */
3334 EVP_PKEY_free(ckch->key);
3335 ckch->key = key;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003336 key = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003337
3338 if (ckch->cert) /* free the previous cert */
3339 X509_free(ckch->cert);
3340 ckch->cert = cert;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003341 cert = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003342
3343 /* Look for a Certificate Chain */
3344 ca = PEM_read_bio_X509(in, NULL, NULL, NULL);
3345 if (ca) {
3346 /* there is a chain a in the PEM, clean the previous one in the CKCH */
3347 if (ckch->chain) /* free the previous chain */
3348 sk_X509_pop_free(ckch->chain, X509_free);
3349 ckch->chain = sk_X509_new_null();
3350 if (!sk_X509_push(ckch->chain, ca)) {
3351 X509_free(ca);
3352 goto end;
3353 }
3354 }
3355 /* look for other crt in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003356 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL)))
3357 if (!sk_X509_push(ckch->chain, ca)) {
3358 X509_free(ca);
3359 goto end;
3360 }
yanbzhu488a4d22015-12-01 15:16:07 -05003361
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003362 /* no chain */
3363 if (ckch->chain == NULL) {
3364 ckch->chain = sk_X509_new_null();
3365 }
3366
yanbzhu488a4d22015-12-01 15:16:07 -05003367 ret = ERR_get_error();
3368 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3369 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003370 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003371 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003372 }
3373
3374 ret = 0;
3375
William Lallemand96a9c972019-10-17 11:56:17 +02003376end:
William Lallemand246c0242019-10-11 08:59:13 +02003377
3378 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003379 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003380 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003381 if (key)
3382 EVP_PKEY_free(key);
3383 if (cert)
3384 X509_free(cert);
William Lallemanda17f4112019-10-10 15:16:44 +02003385
William Lallemand96a9c972019-10-17 11:56:17 +02003386 return ret;
3387}
3388
3389/*
3390 * Try to load in a ckch every files related to a ckch.
3391 * (PEM, sctl, ocsp, issuer etc.)
3392 *
3393 * This function is only used to load files during the configuration parsing,
3394 * it is not used with the CLI.
3395 *
3396 * This allows us to carry the contents of the file without having to read the
3397 * file multiple times. The caller must call
3398 * ssl_sock_free_cert_key_and_chain_contents.
3399 *
3400 * returns:
3401 * 0 on Success
3402 * 1 on SSL Failure
3403 */
3404static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3405{
3406 int ret = 1;
3407
3408 /* try to load the PEM */
3409 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3410 goto end;
3411 }
3412
William Lallemanda17f4112019-10-10 15:16:44 +02003413#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3414 /* try to load the sctl file */
3415 {
3416 char fp[MAXPATHLEN+1];
3417 struct stat st;
3418
3419 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3420 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003421 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003422 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003423 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003424 ret = 1;
3425 goto end;
3426 }
3427 }
3428 }
3429#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003430
William Lallemand246c0242019-10-11 08:59:13 +02003431 /* try to load an ocsp response file */
3432 {
3433 char fp[MAXPATHLEN+1];
3434 struct stat st;
3435
3436 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3437 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003438 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003439 ret = 1;
3440 goto end;
3441 }
3442 }
3443 }
3444
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003445#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand246c0242019-10-11 08:59:13 +02003446 if (ckch->ocsp_response) {
3447 X509 *issuer;
3448 int i;
3449
3450 /* check if one of the certificate of the chain is the issuer */
3451 for (i = 0; i < sk_X509_num(ckch->chain); i++) {
3452 issuer = sk_X509_value(ckch->chain, i);
3453 if (X509_check_issued(issuer, ckch->cert) == X509_V_OK) {
3454 ckch->ocsp_issuer = issuer;
3455 break;
3456 } else
3457 issuer = NULL;
3458 }
3459
3460 /* if no issuer was found, try to load an issuer from the .issuer */
3461 if (!issuer) {
3462 struct stat st;
3463 char fp[MAXPATHLEN+1];
3464
3465 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3466 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003467 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003468 ret = 1;
3469 goto end;
3470 }
3471
3472 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003473 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003474 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003475 ret = 1;
3476 goto end;
3477 }
3478 } else {
3479 memprintf(err, "%sNo issuer found, cannot use the OCSP response'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003480 err && *err ? *err : "");
William Lallemand246c0242019-10-11 08:59:13 +02003481 ret = 1;
3482 goto end;
3483 }
3484 }
3485 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003486#endif
William Lallemand246c0242019-10-11 08:59:13 +02003487
yanbzhu488a4d22015-12-01 15:16:07 -05003488 ret = 0;
3489
3490end:
3491
3492 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003493
3494 /* Something went wrong in one of the reads */
3495 if (ret != 0)
3496 ssl_sock_free_cert_key_and_chain_contents(ckch);
3497
3498 return ret;
3499}
3500
3501/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003502 * Returns a bitfield containing the flags:
3503 * ERR_FATAL in any fatal error case
3504 * ERR_ALERT if the reason of the error is available in err
3505 * ERR_WARN if a warning is available into err
3506 * The value 0 means there is no error nor warning and
3507 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003508 */
3509static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3510{
Emeric Bruna96b5822019-10-17 13:25:14 +02003511 int errcode = 0;
3512
yanbzhu488a4d22015-12-01 15:16:07 -05003513 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3514 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3515 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003516 errcode |= ERR_ALERT | ERR_FATAL;
3517 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003518 }
3519
3520 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3521 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3522 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003523 errcode |= ERR_ALERT | ERR_FATAL;
3524 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003525 }
3526
yanbzhu488a4d22015-12-01 15:16:07 -05003527 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003528#ifdef SSL_CTX_set1_chain
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003529 if (!SSL_CTX_set1_chain(ctx, ckch->chain)) {
3530 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3531 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003532 errcode |= ERR_ALERT | ERR_FATAL;
3533 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003534 }
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003535#else
3536 { /* legacy compat (< openssl 1.0.2) */
3537 X509 *ca;
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003538 STACK_OF(X509) *chain;
3539 chain = X509_chain_up_ref(ckch->chain);
3540 while ((ca = sk_X509_shift(chain)))
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003541 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3542 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3543 err && *err ? *err : "", path);
3544 X509_free(ca);
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003545 sk_X509_pop_free(chain, X509_free);
Emeric Bruna96b5822019-10-17 13:25:14 +02003546 errcode |= ERR_ALERT | ERR_FATAL;
3547 goto end;
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003548 }
3549 }
3550#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003551
William Lallemandfa892222019-07-23 16:06:08 +02003552#ifndef OPENSSL_NO_DH
3553 /* store a NULL pointer to indicate we have not yet loaded
3554 a custom DH param file */
3555 if (ssl_dh_ptr_index >= 0) {
3556 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3557 }
3558
Emeric Brun7a883362019-10-17 13:27:40 +02003559 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3560 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003561 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3562 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003563 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003564 }
3565#endif
3566
William Lallemanda17f4112019-10-10 15:16:44 +02003567#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3568 if (sctl_ex_index >= 0 && ckch->sctl) {
3569 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3570 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003571 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003572 errcode |= ERR_ALERT | ERR_FATAL;
3573 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003574 }
3575 }
3576#endif
3577
William Lallemand4a660132019-10-14 14:51:41 +02003578#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003579 /* Load OCSP Info into context */
3580 if (ckch->ocsp_response) {
3581 if (ssl_sock_load_ocsp(ctx, ckch) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003582 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",
3583 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003584 errcode |= ERR_ALERT | ERR_FATAL;
3585 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003586 }
3587 }
William Lallemand246c0242019-10-11 08:59:13 +02003588#endif
3589
Emeric Bruna96b5822019-10-17 13:25:14 +02003590 end:
3591 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003592}
3593
William Lallemandc4ecddf2019-07-31 16:50:08 +02003594#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003595
William Lallemand28a8fce2019-10-04 17:36:55 +02003596static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003597{
3598 struct sni_keytype *s_kt = NULL;
3599 struct ebmb_node *node;
3600 int i;
3601
3602 for (i = 0; i < trash.size; i++) {
3603 if (!str[i])
3604 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003605 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003606 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003607 trash.area[i] = 0;
3608 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003609 if (!node) {
3610 /* CN not found in tree */
3611 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3612 /* Using memcpy here instead of strncpy.
3613 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3614 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3615 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003616 if (!s_kt)
3617 return -1;
3618
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003619 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003620 s_kt->keytypes = 0;
3621 ebst_insert(sni_keytypes, &s_kt->name);
3622 } else {
3623 /* CN found in tree */
3624 s_kt = container_of(node, struct sni_keytype, name);
3625 }
3626
3627 /* Mark that this CN has the keytype of key_index via keytypes mask */
3628 s_kt->keytypes |= 1<<key_index;
3629
William Lallemand28a8fce2019-10-04 17:36:55 +02003630 return 0;
3631
yanbzhu08ce6ab2015-12-02 13:01:29 -05003632}
3633
William Lallemandc4ecddf2019-07-31 16:50:08 +02003634#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003635/*
3636 * Free a ckch_store and its ckch(s)
3637 * The linked ckch_inst are not free'd
3638 */
3639void ckchs_free(struct ckch_store *ckchs)
3640{
3641 if (!ckchs)
3642 return;
3643
3644#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3645 if (ckchs->multi) {
3646 int n;
3647
3648 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3649 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
3650 } else
3651#endif
3652 {
3653 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
3654 ckchs->ckch = NULL;
3655 }
3656
3657 free(ckchs);
3658}
3659
3660/* allocate and duplicate a ckch_store
3661 * Return a new ckch_store or NULL */
3662static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3663{
3664 struct ckch_store *dst;
3665 int pathlen;
3666
3667 pathlen = strlen(src->path);
3668 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3669 if (!dst)
3670 return NULL;
3671 /* copy previous key */
3672 memcpy(dst->path, src->path, pathlen + 1);
3673 dst->multi = src->multi;
3674 LIST_INIT(&dst->ckch_inst);
3675
3676 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3677 if (!dst->ckch)
3678 goto error;
3679
3680#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3681 if (src->multi) {
3682 int n;
3683
3684 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3685 if (&src->ckch[n]) {
3686 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3687 goto error;
3688 }
3689 }
3690 } else
3691#endif
3692 {
3693 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3694 goto error;
3695 }
3696
3697 return dst;
3698
3699error:
3700 ckchs_free(dst);
3701
3702 return NULL;
3703}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003704
William Lallemand36b84632019-07-18 19:28:17 +02003705/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003706 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003707 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003708static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003709{
3710 struct ebmb_node *eb;
3711
William Lallemande3af8fb2019-10-08 11:36:53 +02003712 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003713 if (!eb)
3714 return NULL;
3715
William Lallemande3af8fb2019-10-08 11:36:53 +02003716 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003717}
3718
3719/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003720 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003721 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003722static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003723{
William Lallemande3af8fb2019-10-08 11:36:53 +02003724 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003725
William Lallemande3af8fb2019-10-08 11:36:53 +02003726 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3727 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003728 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3729 goto end;
3730 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003731 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003732
William Lallemande3af8fb2019-10-08 11:36:53 +02003733 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003734 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3735 goto end;
3736 }
3737
William Lallemand9117de92019-10-04 00:29:42 +02003738 LIST_INIT(&ckchs->ckch_inst);
3739
William Lallemand36b84632019-07-18 19:28:17 +02003740 if (!multi) {
3741
William Lallemand96a9c972019-10-17 11:56:17 +02003742 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003743 goto end;
3744
William Lallemande3af8fb2019-10-08 11:36:53 +02003745 /* insert into the ckchs tree */
3746 memcpy(ckchs->path, path, strlen(path) + 1);
3747 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003748 } else {
3749 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003750#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3751 char fp[MAXPATHLEN+1] = {0};
3752 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003753
3754 /* Load all possible certs and keys */
3755 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3756 struct stat buf;
3757 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3758 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003759 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003760 goto end;
3761 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003762 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003763 }
3764 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003765#endif
William Lallemand36b84632019-07-18 19:28:17 +02003766
3767 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003768 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003769 goto end;
3770 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003771 /* insert into the ckchs tree */
3772 memcpy(ckchs->path, path, strlen(path) + 1);
3773 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003774 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003775 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003776
William Lallemand36b84632019-07-18 19:28:17 +02003777end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003778 if (ckchs) {
3779 free(ckchs->ckch);
3780 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003781 }
3782
William Lallemande3af8fb2019-10-08 11:36:53 +02003783 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003784
3785 return NULL;
3786}
3787
William Lallemandc4ecddf2019-07-31 16:50:08 +02003788#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3789
William Lallemand36b84632019-07-18 19:28:17 +02003790/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003791 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003792 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003793 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3794 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003795 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003796 *
Emeric Brun054563d2019-10-17 13:16:58 +02003797 * Returns a bitfield containing the flags:
3798 * ERR_FATAL in any fatal error case
3799 * ERR_ALERT if the reason of the error is available in err
3800 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003801 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003802 */
Emeric Brun054563d2019-10-17 13:16:58 +02003803static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3804 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3805 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003806{
William Lallemand36b84632019-07-18 19:28:17 +02003807 int i = 0, n = 0;
3808 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003809 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003810 struct ebmb_node *node;
3811 struct ebmb_node *next;
3812 /* Array of SSL_CTX pointers corresponding to each possible combo
3813 * of keytypes
3814 */
3815 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003816 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003817 X509_NAME *xname = NULL;
3818 char *str = NULL;
3819#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3820 STACK_OF(GENERAL_NAME) *names = NULL;
3821#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003822 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003823
Emeric Brun054563d2019-10-17 13:16:58 +02003824 *ckchi = NULL;
3825
William Lallemande3af8fb2019-10-08 11:36:53 +02003826 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003827 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3828 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003829 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003830 }
3831
3832 ckch_inst = ckch_inst_new();
3833 if (!ckch_inst) {
3834 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3835 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003836 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003837 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003838 }
3839
William Lallemande3af8fb2019-10-08 11:36:53 +02003840 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003841
William Lallemand150bfa82019-09-19 17:12:49 +02003842 /* at least one of the instances is using filters during the config
3843 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01003844 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02003845
yanbzhu08ce6ab2015-12-02 13:01:29 -05003846 /* Process each ckch and update keytypes for each CN/SAN
3847 * for example, if CN/SAN www.a.com is associated with
3848 * certs with keytype 0 and 2, then at the end of the loop,
3849 * www.a.com will have:
3850 * keyindex = 0 | 1 | 4 = 5
3851 */
3852 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003853 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003854
3855 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3856 continue;
3857
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003858 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003859 for (i = 0; i < fcount; i++) {
3860 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3861 if (ret < 0) {
3862 memprintf(err, "%sunable to allocate SSL context.\n",
3863 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003864 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003865 goto end;
3866 }
3867 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003868 } else {
3869 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3870 * so the line that contains logic is marked via comments
3871 */
3872 xname = X509_get_subject_name(certs_and_keys[n].cert);
3873 i = -1;
3874 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3875 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003876 ASN1_STRING *value;
3877 value = X509_NAME_ENTRY_get_data(entry);
3878 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003879 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003880 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003881
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003882 OPENSSL_free(str);
3883 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003884 if (ret < 0) {
3885 memprintf(err, "%sunable to allocate SSL context.\n",
3886 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003887 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003888 goto end;
3889 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003890 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003891 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003892
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003893 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003894#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003895 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3896 if (names) {
3897 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3898 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003899
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003900 if (name->type == GEN_DNS) {
3901 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3902 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003903 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003904
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003905 OPENSSL_free(str);
3906 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003907 if (ret < 0) {
3908 memprintf(err, "%sunable to allocate SSL context.\n",
3909 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003910 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003911 goto end;
3912 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003913 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003914 }
3915 }
3916 }
3917 }
3918#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3919 }
3920
3921 /* If no files found, return error */
3922 if (eb_is_empty(&sni_keytypes_map)) {
3923 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3924 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003925 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003926 goto end;
3927 }
3928
3929 /* We now have a map of CN/SAN to keytypes that are loaded in
3930 * Iterate through the map to create the SSL_CTX's (if needed)
3931 * and add each CTX to the SNI tree
3932 *
3933 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003934 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003935 * combination is denoted by the key in the map. Each key
3936 * has a value between 1 and 2^n - 1. Conveniently, the array
3937 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3938 * entry in the array to correspond to the unique combo (key)
3939 * associated with i. This unique key combo (i) will be associated
3940 * with combos[i-1]
3941 */
3942
3943 node = ebmb_first(&sni_keytypes_map);
3944 while (node) {
3945 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003946 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003947 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003948
3949 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3950 i = container_of(node, struct sni_keytype, name)->keytypes;
3951 cur_ctx = key_combos[i-1].ctx;
3952
3953 if (cur_ctx == NULL) {
3954 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003955 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003956 if (cur_ctx == NULL) {
3957 memprintf(err, "%sunable to allocate SSL context.\n",
3958 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003959 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003960 goto end;
3961 }
3962
yanbzhube2774d2015-12-10 15:07:30 -05003963 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003964 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3965 if (i & (1<<n)) {
3966 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003967 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02003968 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3969 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003970 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003971 }
3972 }
3973
yanbzhu08ce6ab2015-12-02 13:01:29 -05003974 /* Update key_combos */
3975 key_combos[i-1].ctx = cur_ctx;
3976 }
3977
3978 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02003979
William Lallemand1d29c742019-10-04 00:53:29 +02003980 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 +02003981 kinfo, str, key_combos[i-1].order);
3982 if (key_combos[i-1].order < 0) {
3983 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003984 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02003985 goto end;
3986 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003987 node = ebmb_next(node);
3988 }
3989
3990
3991 /* Mark a default context if none exists, using the ctx that has the most shared keys */
3992 if (!bind_conf->default_ctx) {
3993 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
3994 if (key_combos[i].ctx) {
3995 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003996 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01003997 ckch_inst->is_default = 1;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003998 break;
3999 }
4000 }
4001 }
4002
William Lallemand614ca0d2019-10-07 13:52:11 +02004003 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004004 ckch_inst->ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004005end:
4006
4007 if (names)
4008 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4009
yanbzhu08ce6ab2015-12-02 13:01:29 -05004010 node = ebmb_first(&sni_keytypes_map);
4011 while (node) {
4012 next = ebmb_next(node);
4013 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004014 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004015 node = next;
4016 }
4017
Emeric Brun054563d2019-10-17 13:16:58 +02004018 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02004019 struct sni_ctx *sc0, *sc0b;
4020
4021 /* free the SSL_CTX in case of error */
4022 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4023 if (key_combos[i].ctx)
4024 SSL_CTX_free(key_combos[i].ctx);
4025 }
4026
4027 /* free the sni_ctx in case of error */
4028 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4029
4030 ebmb_delete(&sc0->name);
4031 LIST_DEL(&sc0->by_ckch_inst);
4032 free(sc0);
4033 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004034 free(ckch_inst);
4035 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004036 }
4037
Emeric Brun054563d2019-10-17 13:16:58 +02004038 *ckchi = ckch_inst;
4039 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004040}
4041#else
4042/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004043static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4044 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4045 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004046{
4047 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4048 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004049 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004050}
4051
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004052#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004053
William Lallemand614ca0d2019-10-07 13:52:11 +02004054/*
4055 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004056 *
4057 * Returns a bitfield containing the flags:
4058 * ERR_FATAL in any fatal error case
4059 * ERR_ALERT if the reason of the error is available in err
4060 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004061 */
Emeric Brun054563d2019-10-17 13:16:58 +02004062static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4063 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004064{
William Lallemandc9402072019-05-15 15:33:54 +02004065 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004066 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004067 int order = 0;
4068 X509_NAME *xname;
4069 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004070 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004071 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004072#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4073 STACK_OF(GENERAL_NAME) *names;
4074#endif
William Lallemand36b84632019-07-18 19:28:17 +02004075 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004076 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004077 int errcode = 0;
4078
4079 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004080
William Lallemande3af8fb2019-10-08 11:36:53 +02004081 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004082 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004083
William Lallemande3af8fb2019-10-08 11:36:53 +02004084 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004085
William Lallemand150bfa82019-09-19 17:12:49 +02004086 /* at least one of the instances is using filters during the config
4087 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01004088 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02004089
William Lallemandc9402072019-05-15 15:33:54 +02004090 ctx = SSL_CTX_new(SSLv23_server_method());
4091 if (!ctx) {
4092 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4093 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004094 errcode |= ERR_ALERT | ERR_FATAL;
4095 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004096 }
4097
Emeric Bruna96b5822019-10-17 13:25:14 +02004098 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4099 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004100 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004101
4102 ckch_inst = ckch_inst_new();
4103 if (!ckch_inst) {
4104 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4105 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004106 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004107 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004108 }
4109
William Lallemand36b84632019-07-18 19:28:17 +02004110 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004111 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004112 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004113 switch(EVP_PKEY_base_id(pkey)) {
4114 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004115 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004116 break;
4117 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004118 kinfo.sig = TLSEXT_signature_ecdsa;
4119 break;
4120 case EVP_PKEY_DSA:
4121 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004122 break;
4123 }
4124 EVP_PKEY_free(pkey);
4125 }
4126
Emeric Brun50bcecc2013-04-22 13:05:23 +02004127 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004128 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004129 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 +02004130 if (order < 0) {
4131 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004132 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004133 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004134 }
4135 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004136 }
4137 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004138#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004139 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004140 if (names) {
4141 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4142 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4143 if (name->type == GEN_DNS) {
4144 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004145 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004146 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004147 if (order < 0) {
4148 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004149 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004150 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004151 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004152 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004153 }
4154 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004155 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004156 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004157#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004158 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004159 i = -1;
4160 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4161 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004162 ASN1_STRING *value;
4163
4164 value = X509_NAME_ENTRY_get_data(entry);
4165 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004166 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004167 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004168 if (order < 0) {
4169 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004170 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004171 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004172 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004173 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004174 }
4175 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004176 /* we must not free the SSL_CTX anymore below, since it's already in
4177 * the tree, so it will be discovered and cleaned in time.
4178 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004179
Emeric Brunfc0421f2012-09-07 17:30:07 +02004180#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004181 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004182 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4183 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004184 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004185 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004186 }
4187#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004188 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004189 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004190 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004191 ckch_inst->is_default = 1;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004192 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004193
William Lallemand9117de92019-10-04 00:29:42 +02004194 /* everything succeed, the ckch instance can be used */
4195 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004196 ckch_inst->ssl_conf = ssl_conf;
William Lallemand9117de92019-10-04 00:29:42 +02004197
Emeric Brun054563d2019-10-17 13:16:58 +02004198 *ckchi = ckch_inst;
4199 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004200
4201error:
4202 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004203 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004204 struct sni_ctx *sc0, *sc0b;
4205
4206 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4207
4208 ebmb_delete(&sc0->name);
4209 LIST_DEL(&sc0->by_ckch_inst);
4210 free(sc0);
4211 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004212 free(ckch_inst);
4213 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004214 }
4215 /* We only created 1 SSL_CTX so we can free it there */
4216 SSL_CTX_free(ctx);
4217
Emeric Brun054563d2019-10-17 13:16:58 +02004218 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004219}
4220
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004221/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004222static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4223 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4224 char **sni_filter, int fcount, char **err)
4225{
4226 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004227 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004228
4229 /* we found the ckchs in the tree, we can use it directly */
4230 if (ckchs->multi)
Emeric Brun054563d2019-10-17 13:16:58 +02004231 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 +02004232 else
Emeric Brun054563d2019-10-17 13:16:58 +02004233 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 +02004234
Emeric Brun054563d2019-10-17 13:16:58 +02004235 if (errcode & ERR_CODE)
4236 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004237
4238 ssl_sock_load_cert_sni(ckch_inst, bind_conf);
4239
4240 /* succeed, add the instance to the ckch_store's list of instance */
4241 LIST_ADDQ(&ckchs->ckch_inst, &ckch_inst->by_ckchs);
Emeric Brun054563d2019-10-17 13:16:58 +02004242 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004243}
4244
4245
Willy Tarreaubbc91962019-10-16 16:42:19 +02004246/* Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau03209342016-12-22 17:08:28 +01004247int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004248{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004249 struct dirent **de_list;
4250 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004251 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004252 char *end;
4253 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004254 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004255 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004256#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004257 int is_bundle;
4258 int j;
4259#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004260 if ((ckchs = ckchs_lookup(path))) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004261 /* we found the ckchs in the tree, we can use it directly */
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004262 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand6af03992019-07-23 15:00:54 +02004263 }
4264
yanbzhu08ce6ab2015-12-02 13:01:29 -05004265 if (stat(path, &buf) == 0) {
William Dauchy9a8ef7f2020-01-13 17:52:49 +01004266 if (S_ISDIR(buf.st_mode) == 0) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004267 ckchs = ckchs_load_cert_file(path, 0, err);
4268 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004269 return ERR_ALERT | ERR_FATAL;
4270
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004271 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004272 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004273
yanbzhu08ce6ab2015-12-02 13:01:29 -05004274 /* strip trailing slashes, including first one */
4275 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
4276 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004277
yanbzhu08ce6ab2015-12-02 13:01:29 -05004278 n = scandir(path, &de_list, 0, alphasort);
4279 if (n < 0) {
4280 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4281 err && *err ? *err : "", path, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004282 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004283 }
4284 else {
4285 for (i = 0; i < n; i++) {
4286 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004287
yanbzhu08ce6ab2015-12-02 13:01:29 -05004288 end = strrchr(de->d_name, '.');
4289 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
4290 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004291
yanbzhu08ce6ab2015-12-02 13:01:29 -05004292 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4293 if (stat(fp, &buf) != 0) {
4294 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4295 err && *err ? *err : "", fp, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004296 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004297 goto ignore_entry;
4298 }
4299 if (!S_ISREG(buf.st_mode))
4300 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004301
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004302#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004303 is_bundle = 0;
4304 /* Check if current entry in directory is part of a multi-cert bundle */
4305
4306 if (end) {
4307 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4308 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4309 is_bundle = 1;
4310 break;
4311 }
4312 }
4313
4314 if (is_bundle) {
yanbzhu63ea8462015-12-09 13:35:14 -05004315 int dp_len;
4316
4317 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004318
4319 /* increment i and free de until we get to a non-bundle cert
4320 * Note here that we look at de_list[i + 1] before freeing de
Willy Tarreau05800522019-10-29 10:48:50 +01004321 * this is important since ignore_entry will free de. This also
4322 * guarantees that de->d_name continues to hold the same prefix.
yanbzhu63ea8462015-12-09 13:35:14 -05004323 */
Willy Tarreau05800522019-10-29 10:48:50 +01004324 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
yanbzhu63ea8462015-12-09 13:35:14 -05004325 free(de);
4326 i++;
4327 de = de_list[i];
4328 }
4329
Willy Tarreau05800522019-10-29 10:48:50 +01004330 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemande3af8fb2019-10-08 11:36:53 +02004331 if ((ckchs = ckchs_lookup(fp)) == NULL)
4332 ckchs = ckchs_load_cert_file(fp, 1, err);
4333 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004334 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004335 else
4336 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05004337 /* Successfully processed the bundle */
4338 goto ignore_entry;
4339 }
4340 }
4341
4342#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004343 if ((ckchs = ckchs_lookup(fp)) == NULL)
4344 ckchs = ckchs_load_cert_file(fp, 0, err);
4345 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004346 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004347 else
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004348 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004349
yanbzhu08ce6ab2015-12-02 13:01:29 -05004350ignore_entry:
4351 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004352 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004353 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004354 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004355 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004356 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004357
William Lallemande3af8fb2019-10-08 11:36:53 +02004358 ckchs = ckchs_load_cert_file(path, 1, err);
4359 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004360 return ERR_ALERT | ERR_FATAL;
4361
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004362 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004363
Emeric Brunfc0421f2012-09-07 17:30:07 +02004364 return cfgerr;
4365}
4366
Thierry Fournier383085f2013-01-24 14:15:43 +01004367/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4368 * done once. Zero is returned if the operation fails. No error is returned
4369 * if the random is said as not implemented, because we expect that openssl
4370 * will use another method once needed.
4371 */
4372static int ssl_initialize_random()
4373{
4374 unsigned char random;
4375 static int random_initialized = 0;
4376
4377 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4378 random_initialized = 1;
4379
4380 return random_initialized;
4381}
4382
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004383/* release ssl bind conf */
4384void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004385{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004386 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01004387#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004388 free(conf->npn_str);
4389 conf->npn_str = NULL;
4390#endif
4391#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4392 free(conf->alpn_str);
4393 conf->alpn_str = NULL;
4394#endif
4395 free(conf->ca_file);
4396 conf->ca_file = NULL;
4397 free(conf->crl_file);
4398 conf->crl_file = NULL;
4399 free(conf->ciphers);
4400 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004401#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004402 free(conf->ciphersuites);
4403 conf->ciphersuites = NULL;
4404#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004405 free(conf->curves);
4406 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004407 free(conf->ecdhe);
4408 conf->ecdhe = NULL;
4409 }
4410}
4411
Willy Tarreaubbc91962019-10-16 16:42:19 +02004412/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004413int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
4414{
4415 char thisline[CRT_LINESIZE];
4416 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004417 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004418 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004419 int linenum = 0;
4420 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004421 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004422
Willy Tarreauad1731d2013-04-02 17:35:58 +02004423 if ((f = fopen(file, "r")) == NULL) {
4424 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004425 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004426 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004427
4428 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004429 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004430 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004431 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004432 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004433 char *crt_path;
4434 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004435
4436 linenum++;
4437 end = line + strlen(line);
4438 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4439 /* Check if we reached the limit and the last char is not \n.
4440 * Watch out for the last line without the terminating '\n'!
4441 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004442 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4443 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004444 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004445 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004446 }
4447
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004448 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004449 newarg = 1;
4450 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004451 if (*line == '#' || *line == '\n' || *line == '\r') {
4452 /* end of string, end of loop */
4453 *line = 0;
4454 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004455 } else if (isspace(*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02004456 newarg = 1;
4457 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004458 } else if (*line == '[') {
4459 if (ssl_b) {
4460 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004461 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004462 break;
4463 }
4464 if (!arg) {
4465 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004466 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004467 break;
4468 }
4469 ssl_b = arg;
4470 newarg = 1;
4471 *line = 0;
4472 } else if (*line == ']') {
4473 if (ssl_e) {
4474 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004475 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004476 break;
4477 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004478 if (!ssl_b) {
4479 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004480 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004481 break;
4482 }
4483 ssl_e = arg;
4484 newarg = 1;
4485 *line = 0;
4486 } else if (newarg) {
4487 if (arg == MAX_CRT_ARGS) {
4488 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004489 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004490 break;
4491 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004492 newarg = 0;
4493 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004494 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004495 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004496 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004497 if (cfgerr)
4498 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004499 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004500
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004501 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004502 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004503 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004504
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004505 crt_path = args[0];
4506 if (*crt_path != '/' && global_ssl.crt_base) {
4507 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4508 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4509 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004510 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004511 break;
4512 }
4513 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4514 crt_path = path;
4515 }
4516
4517 ssl_conf = calloc(1, sizeof *ssl_conf);
4518 cur_arg = ssl_b ? ssl_b : 1;
4519 while (cur_arg < ssl_e) {
4520 newarg = 0;
4521 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4522 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4523 newarg = 1;
Willy Tarreaubbc91962019-10-16 16:42:19 +02004524 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004525 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4526 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4527 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004528 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004529 }
4530 cur_arg += 1 + ssl_bind_kws[i].skip;
4531 break;
4532 }
4533 }
4534 if (!cfgerr && !newarg) {
4535 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4536 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004537 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004538 break;
4539 }
4540 }
Willy Tarreaubbc91962019-10-16 16:42:19 +02004541
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004542 if (cfgerr) {
4543 ssl_sock_free_ssl_conf(ssl_conf);
4544 free(ssl_conf);
4545 ssl_conf = NULL;
4546 break;
4547 }
4548
William Lallemande3af8fb2019-10-08 11:36:53 +02004549 if ((ckchs = ckchs_lookup(crt_path)) == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004550 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004551 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004552 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004553 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004554 }
4555
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004556 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004557 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004558 else
4559 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 +02004560
Willy Tarreauad1731d2013-04-02 17:35:58 +02004561 if (cfgerr) {
4562 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004563 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004564 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004565 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004566 fclose(f);
4567 return cfgerr;
4568}
4569
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004570/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004571static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004572ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004573{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004574 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004575 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004576 SSL_OP_ALL | /* all known workarounds for bugs */
4577 SSL_OP_NO_SSLv2 |
4578 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004579 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02004580 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004581 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02004582 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004583 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004584 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004585 SSL_MODE_ENABLE_PARTIAL_WRITE |
4586 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004587 SSL_MODE_RELEASE_BUFFERS |
4588 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004589 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004590 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004591 int flags = MC_SSL_O_ALL;
4592 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004593
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004594 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004595 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004596
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004597 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004598 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
4599 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4600 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004601 else
4602 flags = conf_ssl_methods->flags;
4603
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004604 min = conf_ssl_methods->min;
4605 max = conf_ssl_methods->max;
4606 /* start with TLSv10 to remove SSLv3 per default */
4607 if (!min && (!max || max >= CONF_TLSV10))
4608 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004609 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004610 if (min)
4611 flags |= (methodVersions[min].flag - 1);
4612 if (max)
4613 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004614 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004615 min = max = CONF_TLSV_NONE;
4616 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004617 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004618 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004619 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004620 if (min) {
4621 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004622 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
4623 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4624 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
4625 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004626 hole = 0;
4627 }
4628 max = i;
4629 }
4630 else {
4631 min = max = i;
4632 }
4633 }
4634 else {
4635 if (min)
4636 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004637 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004638 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004639 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4640 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004641 cfgerr += 1;
4642 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004643 /* save real min/max in bind_conf */
4644 conf_ssl_methods->min = min;
4645 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004646
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004647#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004648 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004649 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004650 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004651 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004652 else
4653 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4654 if (flags & methodVersions[i].flag)
4655 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004656#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004657 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004658 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4659 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004660#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004661
4662 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4663 options |= SSL_OP_NO_TICKET;
4664 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4665 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004666
4667#ifdef SSL_OP_NO_RENEGOTIATION
4668 options |= SSL_OP_NO_RENEGOTIATION;
4669#endif
4670
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004671 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004672
Willy Tarreau5db847a2019-05-09 14:13:35 +02004673#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004674 if (global_ssl.async)
4675 mode |= SSL_MODE_ASYNC;
4676#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004677 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004678 if (global_ssl.life_time)
4679 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004680
4681#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4682#ifdef OPENSSL_IS_BORINGSSL
4683 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4684 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Willy Tarreau5db847a2019-05-09 14:13:35 +02004685#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard545989f2019-12-17 15:39:54 +01004686 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004687 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004688 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4689 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004690#else
4691 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004692#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004693 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004694#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004695 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004696}
4697
William Lallemand4f45bb92017-10-30 20:08:51 +01004698
4699static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4700{
4701 if (first == block) {
4702 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4703 if (first->len > 0)
4704 sh_ssl_sess_tree_delete(sh_ssl_sess);
4705 }
4706}
4707
4708/* return first block from sh_ssl_sess */
4709static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4710{
4711 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4712
4713}
4714
4715/* store a session into the cache
4716 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4717 * data: asn1 encoded session
4718 * data_len: asn1 encoded session length
4719 * Returns 1 id session was stored (else 0)
4720 */
4721static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4722{
4723 struct shared_block *first;
4724 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4725
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004726 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004727 if (!first) {
4728 /* Could not retrieve enough free blocks to store that session */
4729 return 0;
4730 }
4731
4732 /* STORE the key in the first elem */
4733 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4734 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4735 first->len = sizeof(struct sh_ssl_sess_hdr);
4736
4737 /* it returns the already existing node
4738 or current node if none, never returns null */
4739 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4740 if (oldsh_ssl_sess != sh_ssl_sess) {
4741 /* NOTE: Row couldn't be in use because we lock read & write function */
4742 /* release the reserved row */
4743 shctx_row_dec_hot(ssl_shctx, first);
4744 /* replace the previous session already in the tree */
4745 sh_ssl_sess = oldsh_ssl_sess;
4746 /* ignore the previous session data, only use the header */
4747 first = sh_ssl_sess_first_block(sh_ssl_sess);
4748 shctx_row_inc_hot(ssl_shctx, first);
4749 first->len = sizeof(struct sh_ssl_sess_hdr);
4750 }
4751
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004752 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004753 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004754 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004755 }
4756
4757 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004758
4759 return 1;
4760}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004761
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004762/* SSL callback used when a new session is created while connecting to a server */
4763static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4764{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004765 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004766 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004767
Willy Tarreau07d94e42018-09-20 10:57:52 +02004768 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004769
Olivier Houcharde6060c52017-11-16 17:42:52 +01004770 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4771 int len;
4772 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004773
Olivier Houcharde6060c52017-11-16 17:42:52 +01004774 len = i2d_SSL_SESSION(sess, NULL);
4775 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4776 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4777 } else {
4778 free(s->ssl_ctx.reused_sess[tid].ptr);
4779 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4780 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4781 }
4782 if (s->ssl_ctx.reused_sess[tid].ptr) {
4783 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4784 &ptr);
4785 }
4786 } else {
4787 free(s->ssl_ctx.reused_sess[tid].ptr);
4788 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4789 }
4790
4791 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004792}
4793
Olivier Houcharde6060c52017-11-16 17:42:52 +01004794
William Lallemanded0b5ad2017-10-30 19:36:36 +01004795/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004796int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004797{
4798 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4799 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4800 unsigned char *p;
4801 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02004802 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004803 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004804
4805 /* Session id is already stored in to key and session id is known
4806 * so we dont store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02004807 * note: SSL_SESSION_set1_id is using
4808 * a memcpy so we need to use a different pointer
4809 * than sid_data or sid_ctx_data to avoid valgrind
4810 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004811 */
4812
4813 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02004814
4815 /* copy value in an other buffer */
4816 memcpy(encid, sid_data, sid_length);
4817
4818 /* pad with 0 */
4819 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4820 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4821
4822 /* force length to zero to avoid ASN1 encoding */
4823 SSL_SESSION_set1_id(sess, encid, 0);
4824
4825 /* force length to zero to avoid ASN1 encoding */
4826 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004827
4828 /* check if buffer is large enough for the ASN1 encoded session */
4829 data_len = i2d_SSL_SESSION(sess, NULL);
4830 if (data_len > SHSESS_MAX_DATA_LEN)
4831 goto err;
4832
4833 p = encsess;
4834
4835 /* process ASN1 session encoding before the lock */
4836 i2d_SSL_SESSION(sess, &p);
4837
William Lallemanded0b5ad2017-10-30 19:36:36 +01004838
William Lallemanda3c77cf2017-10-30 23:44:40 +01004839 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004840 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004841 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004842 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004843err:
4844 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02004845 SSL_SESSION_set1_id(sess, encid, sid_length);
4846 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004847
4848 return 0; /* do not increment session reference count */
4849}
4850
4851/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004852SSL_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 +01004853{
William Lallemand4f45bb92017-10-30 20:08:51 +01004854 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004855 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4856 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004857 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004858 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004859
4860 global.shctx_lookups++;
4861
4862 /* allow the session to be freed automatically by openssl */
4863 *do_copy = 0;
4864
4865 /* tree key is zeros padded sessionid */
4866 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4867 memcpy(tmpkey, key, key_len);
4868 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4869 key = tmpkey;
4870 }
4871
4872 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004873 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004874
4875 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004876 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4877 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004878 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004879 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004880 global.shctx_misses++;
4881 return NULL;
4882 }
4883
William Lallemand4f45bb92017-10-30 20:08:51 +01004884 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4885 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004886
William Lallemand4f45bb92017-10-30 20:08:51 +01004887 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 +01004888
William Lallemanda3c77cf2017-10-30 23:44:40 +01004889 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004890
4891 /* decode ASN1 session */
4892 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004893 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004894 /* Reset session id and session id contenxt */
4895 if (sess) {
4896 SSL_SESSION_set1_id(sess, key, key_len);
4897 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4898 }
4899
4900 return sess;
4901}
4902
William Lallemand4f45bb92017-10-30 20:08:51 +01004903
William Lallemanded0b5ad2017-10-30 19:36:36 +01004904/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004905void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004906{
William Lallemand4f45bb92017-10-30 20:08:51 +01004907 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004908 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4909 unsigned int sid_length;
4910 const unsigned char *sid_data;
4911 (void)ctx;
4912
4913 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4914 /* tree key is zeros padded sessionid */
4915 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4916 memcpy(tmpkey, sid_data, sid_length);
4917 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4918 sid_data = tmpkey;
4919 }
4920
William Lallemanda3c77cf2017-10-30 23:44:40 +01004921 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004922
4923 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004924 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4925 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004926 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004927 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004928 }
4929
4930 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004931 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004932}
4933
4934/* Set session cache mode to server and disable openssl internal cache.
4935 * Set shared cache callbacks on an ssl context.
4936 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004937void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004938{
4939 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4940
4941 if (!ssl_shctx) {
4942 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4943 return;
4944 }
4945
4946 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4947 SSL_SESS_CACHE_NO_INTERNAL |
4948 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4949
4950 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004951 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4952 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4953 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004954}
4955
William Lallemand8b453912019-11-21 15:48:10 +01004956/*
4957 * This function applies the SSL configuration on a SSL_CTX
4958 * It returns an error code and fills the <err> buffer
4959 */
4960int 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 +01004961{
4962 struct proxy *curproxy = bind_conf->frontend;
4963 int cfgerr = 0;
4964 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004965 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004966 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004967#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004968 const char *conf_ciphersuites;
4969#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004970 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004971
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004972 if (ssl_conf) {
4973 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4974 int i, min, max;
4975 int flags = MC_SSL_O_ALL;
4976
4977 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004978 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4979 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004980 if (min)
4981 flags |= (methodVersions[min].flag - 1);
4982 if (max)
4983 flags |= ~((methodVersions[max].flag << 1) - 1);
4984 min = max = CONF_TLSV_NONE;
4985 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4986 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4987 if (min)
4988 max = i;
4989 else
4990 min = max = i;
4991 }
4992 /* save real min/max */
4993 conf_ssl_methods->min = min;
4994 conf_ssl_methods->max = max;
4995 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004996 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4997 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004998 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004999 }
5000 }
5001
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005002 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005003 case SSL_SOCK_VERIFY_NONE:
5004 verify = SSL_VERIFY_NONE;
5005 break;
5006 case SSL_SOCK_VERIFY_OPTIONAL:
5007 verify = SSL_VERIFY_PEER;
5008 break;
5009 case SSL_SOCK_VERIFY_REQUIRED:
5010 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5011 break;
5012 }
5013 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5014 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005015 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
5016 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
5017 if (ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005018 /* set CAfile to verify */
5019 if (!ssl_set_verify_locations_file(ctx, ca_file)) {
5020 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005021 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005022 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005023 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005024 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
5025 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005026 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 +02005027 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005028 }
Emeric Brun850efd52014-01-29 12:24:34 +01005029 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005030 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5031 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005032 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005033 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005034#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005035 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005036 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5037
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005038 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005039 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5040 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005041 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005042 }
Emeric Brun561e5742012-10-02 15:20:55 +02005043 else {
5044 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5045 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005046 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005047#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005048 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005049 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005050#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005051 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005052 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005053 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5054 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005055 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005056 }
5057 }
5058#endif
5059
William Lallemand4f45bb92017-10-30 20:08:51 +01005060 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005061 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5062 if (conf_ciphers &&
5063 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005064 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5065 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005066 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005067 }
5068
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005069#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005070 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5071 if (conf_ciphersuites &&
5072 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005073 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5074 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005075 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005076 }
5077#endif
5078
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005079#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005080 /* If tune.ssl.default-dh-param has not been set,
5081 neither has ssl-default-dh-file and no static DH
5082 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005083 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005084 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005085 (ssl_dh_ptr_index == -1 ||
5086 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005087 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5088 const SSL_CIPHER * cipher = NULL;
5089 char cipher_description[128];
5090 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5091 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5092 which is not ephemeral DH. */
5093 const char dhe_description[] = " Kx=DH ";
5094 const char dhe_export_description[] = " Kx=DH(";
5095 int idx = 0;
5096 int dhe_found = 0;
5097 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005098
Remi Gacogne23d5d372014-10-10 17:04:26 +02005099 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005100
Remi Gacogne23d5d372014-10-10 17:04:26 +02005101 if (ssl) {
5102 ciphers = SSL_get_ciphers(ssl);
5103
5104 if (ciphers) {
5105 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5106 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5107 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5108 if (strstr(cipher_description, dhe_description) != NULL ||
5109 strstr(cipher_description, dhe_export_description) != NULL) {
5110 dhe_found = 1;
5111 break;
5112 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005113 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005114 }
5115 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005116 SSL_free(ssl);
5117 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005118 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005119
Lukas Tribus90132722014-08-18 00:56:33 +02005120 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005121 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",
5122 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005123 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005124 }
5125
Willy Tarreauef934602016-12-22 23:12:01 +01005126 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005127 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005128
Willy Tarreauef934602016-12-22 23:12:01 +01005129 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005130 if (local_dh_1024 == NULL) {
5131 local_dh_1024 = ssl_get_dh_1024();
5132 }
Willy Tarreauef934602016-12-22 23:12:01 +01005133 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005134 if (local_dh_2048 == NULL) {
5135 local_dh_2048 = ssl_get_dh_2048();
5136 }
Willy Tarreauef934602016-12-22 23:12:01 +01005137 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005138 if (local_dh_4096 == NULL) {
5139 local_dh_4096 = ssl_get_dh_4096();
5140 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005141 }
5142 }
5143 }
5144#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005145
Emeric Brunfc0421f2012-09-07 17:30:07 +02005146 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005147#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005148 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005149#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005150
Bernard Spil13c53f82018-02-15 13:34:58 +01005151#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005152 ssl_conf_cur = NULL;
5153 if (ssl_conf && ssl_conf->npn_str)
5154 ssl_conf_cur = ssl_conf;
5155 else if (bind_conf->ssl_conf.npn_str)
5156 ssl_conf_cur = &bind_conf->ssl_conf;
5157 if (ssl_conf_cur)
5158 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005159#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005160#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005161 ssl_conf_cur = NULL;
5162 if (ssl_conf && ssl_conf->alpn_str)
5163 ssl_conf_cur = ssl_conf;
5164 else if (bind_conf->ssl_conf.alpn_str)
5165 ssl_conf_cur = &bind_conf->ssl_conf;
5166 if (ssl_conf_cur)
5167 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005168#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005169#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005170 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5171 if (conf_curves) {
5172 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005173 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5174 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005175 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005176 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005177 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005178 }
5179#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005180#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005181 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005182 int i;
5183 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005184#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005185 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005186 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5187 NULL);
5188
5189 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005190 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005191 return cfgerr;
5192 }
5193#else
5194 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5195 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5196 ECDHE_DEFAULT_CURVE);
5197#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005198
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005199 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005200 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005201 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5202 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005203 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005204 }
5205 else {
5206 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5207 EC_KEY_free(ecdh);
5208 }
5209 }
5210#endif
5211
Emeric Brunfc0421f2012-09-07 17:30:07 +02005212 return cfgerr;
5213}
5214
Evan Broderbe554312013-06-27 00:05:25 -07005215static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5216{
5217 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5218 size_t prefixlen, suffixlen;
5219
5220 /* Trivial case */
5221 if (strcmp(pattern, hostname) == 0)
5222 return 1;
5223
Evan Broderbe554312013-06-27 00:05:25 -07005224 /* The rest of this logic is based on RFC 6125, section 6.4.3
5225 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5226
Emeric Bruna848dae2013-10-08 11:27:28 +02005227 pattern_wildcard = NULL;
5228 pattern_left_label_end = pattern;
5229 while (*pattern_left_label_end != '.') {
5230 switch (*pattern_left_label_end) {
5231 case 0:
5232 /* End of label not found */
5233 return 0;
5234 case '*':
5235 /* If there is more than one wildcards */
5236 if (pattern_wildcard)
5237 return 0;
5238 pattern_wildcard = pattern_left_label_end;
5239 break;
5240 }
5241 pattern_left_label_end++;
5242 }
5243
5244 /* If it's not trivial and there is no wildcard, it can't
5245 * match */
5246 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005247 return 0;
5248
5249 /* Make sure all labels match except the leftmost */
5250 hostname_left_label_end = strchr(hostname, '.');
5251 if (!hostname_left_label_end
5252 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5253 return 0;
5254
5255 /* Make sure the leftmost label of the hostname is long enough
5256 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005257 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005258 return 0;
5259
5260 /* Finally compare the string on either side of the
5261 * wildcard */
5262 prefixlen = pattern_wildcard - pattern;
5263 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005264 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5265 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005266 return 0;
5267
5268 return 1;
5269}
5270
5271static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5272{
5273 SSL *ssl;
5274 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005275 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005276 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005277 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005278
5279 int depth;
5280 X509 *cert;
5281 STACK_OF(GENERAL_NAME) *alt_names;
5282 int i;
5283 X509_NAME *cert_subject;
5284 char *str;
5285
5286 if (ok == 0)
5287 return ok;
5288
5289 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005290 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005291 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005292
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005293 /* We're checking if the provided hostnames match the desired one. The
5294 * desired hostname comes from the SNI we presented if any, or if not
5295 * provided then it may have been explicitly stated using a "verifyhost"
5296 * directive. If neither is set, we don't care about the name so the
5297 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005298 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005299 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005300 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005301 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005302 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005303 if (!servername)
5304 return ok;
5305 }
Evan Broderbe554312013-06-27 00:05:25 -07005306
5307 /* We only need to verify the CN on the actual server cert,
5308 * not the indirect CAs */
5309 depth = X509_STORE_CTX_get_error_depth(ctx);
5310 if (depth != 0)
5311 return ok;
5312
5313 /* At this point, the cert is *not* OK unless we can find a
5314 * hostname match */
5315 ok = 0;
5316
5317 cert = X509_STORE_CTX_get_current_cert(ctx);
5318 /* It seems like this might happen if verify peer isn't set */
5319 if (!cert)
5320 return ok;
5321
5322 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5323 if (alt_names) {
5324 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5325 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5326 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005327#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005328 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5329#else
Evan Broderbe554312013-06-27 00:05:25 -07005330 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005331#endif
Evan Broderbe554312013-06-27 00:05:25 -07005332 ok = ssl_sock_srv_hostcheck(str, servername);
5333 OPENSSL_free(str);
5334 }
5335 }
5336 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005337 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005338 }
5339
5340 cert_subject = X509_get_subject_name(cert);
5341 i = -1;
5342 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5343 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005344 ASN1_STRING *value;
5345 value = X509_NAME_ENTRY_get_data(entry);
5346 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005347 ok = ssl_sock_srv_hostcheck(str, servername);
5348 OPENSSL_free(str);
5349 }
5350 }
5351
Willy Tarreau71d058c2017-07-26 20:09:56 +02005352 /* report the mismatch and indicate if SNI was used or not */
5353 if (!ok && !conn->err_code)
5354 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005355 return ok;
5356}
5357
Emeric Brun94324a42012-10-11 14:00:19 +02005358/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005359int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005360{
Willy Tarreau03209342016-12-22 17:08:28 +01005361 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005362 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005363 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005364 SSL_OP_ALL | /* all known workarounds for bugs */
5365 SSL_OP_NO_SSLv2 |
5366 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005367 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005368 SSL_MODE_ENABLE_PARTIAL_WRITE |
5369 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005370 SSL_MODE_RELEASE_BUFFERS |
5371 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005372 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005373 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005374 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005375 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005376 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005377
Thierry Fournier383085f2013-01-24 14:15:43 +01005378 /* Make sure openssl opens /dev/urandom before the chroot */
5379 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005380 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005381 cfgerr++;
5382 }
5383
Willy Tarreaufce03112015-01-15 21:32:40 +01005384 /* Automatic memory computations need to know we use SSL there */
5385 global.ssl_used_backend = 1;
5386
5387 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005388 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005389 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005390 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5391 curproxy->id, srv->id,
5392 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005393 cfgerr++;
5394 return cfgerr;
5395 }
5396 }
Emeric Brun94324a42012-10-11 14:00:19 +02005397 if (srv->use_ssl)
5398 srv->xprt = &ssl_sock;
5399 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005400 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005401
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005402 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005403 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005404 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5405 proxy_type_str(curproxy), curproxy->id,
5406 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005407 cfgerr++;
5408 return cfgerr;
5409 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005410
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005411 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005412 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5413 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5414 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005415 else
5416 flags = conf_ssl_methods->flags;
5417
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005418 /* Real min and max should be determinate with configuration and openssl's capabilities */
5419 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005420 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005421 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005422 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005423
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005424 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005425 min = max = CONF_TLSV_NONE;
5426 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005427 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005428 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005429 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005430 if (min) {
5431 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005432 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5433 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5434 proxy_type_str(curproxy), curproxy->id, srv->id,
5435 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005436 hole = 0;
5437 }
5438 max = i;
5439 }
5440 else {
5441 min = max = i;
5442 }
5443 }
5444 else {
5445 if (min)
5446 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005447 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005448 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005449 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5450 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005451 cfgerr += 1;
5452 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005453
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005454#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005455 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005456 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005457 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005458 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005459 else
5460 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5461 if (flags & methodVersions[i].flag)
5462 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005463#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005464 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005465 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5466 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005467#endif
5468
5469 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5470 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005471 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005472
Willy Tarreau5db847a2019-05-09 14:13:35 +02005473#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005474 if (global_ssl.async)
5475 mode |= SSL_MODE_ASYNC;
5476#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005477 SSL_CTX_set_mode(ctx, mode);
5478 srv->ssl_ctx.ctx = ctx;
5479
Emeric Bruna7aa3092012-10-26 12:58:00 +02005480 if (srv->ssl_ctx.client_crt) {
5481 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 +01005482 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5483 proxy_type_str(curproxy), curproxy->id,
5484 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005485 cfgerr++;
5486 }
5487 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 +01005488 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5489 proxy_type_str(curproxy), curproxy->id,
5490 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005491 cfgerr++;
5492 }
5493 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005494 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5495 proxy_type_str(curproxy), curproxy->id,
5496 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005497 cfgerr++;
5498 }
5499 }
Emeric Brun94324a42012-10-11 14:00:19 +02005500
Emeric Brun850efd52014-01-29 12:24:34 +01005501 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5502 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005503 switch (srv->ssl_ctx.verify) {
5504 case SSL_SOCK_VERIFY_NONE:
5505 verify = SSL_VERIFY_NONE;
5506 break;
5507 case SSL_SOCK_VERIFY_REQUIRED:
5508 verify = SSL_VERIFY_PEER;
5509 break;
5510 }
Evan Broderbe554312013-06-27 00:05:25 -07005511 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005512 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005513 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005514 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005515 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005516 /* set CAfile to verify */
5517 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
5518 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01005519 curproxy->id, srv->id,
5520 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005521 cfgerr++;
5522 }
5523 }
Emeric Brun850efd52014-01-29 12:24:34 +01005524 else {
5525 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01005526 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",
5527 curproxy->id, srv->id,
5528 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005529 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01005530 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
5531 curproxy->id, srv->id,
5532 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005533 cfgerr++;
5534 }
Emeric Brunef42d922012-10-11 16:11:36 +02005535#ifdef X509_V_FLAG_CRL_CHECK
5536 if (srv->ssl_ctx.crl_file) {
5537 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
5538
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005539 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005540 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
5541 curproxy->id, srv->id,
5542 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005543 cfgerr++;
5544 }
5545 else {
5546 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5547 }
5548 }
5549#endif
5550 }
5551
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005552 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
5553 SSL_SESS_CACHE_NO_INTERNAL_STORE);
5554 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02005555 if (srv->ssl_ctx.ciphers &&
5556 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005557 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
5558 curproxy->id, srv->id,
5559 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02005560 cfgerr++;
5561 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005562
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005563#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005564 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00005565 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005566 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
5567 curproxy->id, srv->id,
5568 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
5569 cfgerr++;
5570 }
5571#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01005572#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
5573 if (srv->ssl_ctx.npn_str)
5574 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
5575#endif
5576#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5577 if (srv->ssl_ctx.alpn_str)
5578 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
5579#endif
5580
Emeric Brun94324a42012-10-11 14:00:19 +02005581
5582 return cfgerr;
5583}
5584
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005585/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005586 * be NULL, in which case nothing is done. Returns the number of errors
5587 * encountered.
5588 */
Willy Tarreau03209342016-12-22 17:08:28 +01005589int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005590{
5591 struct ebmb_node *node;
5592 struct sni_ctx *sni;
5593 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01005594 int errcode = 0;
5595 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005596
Willy Tarreaufce03112015-01-15 21:32:40 +01005597 /* Automatic memory computations need to know we use SSL there */
5598 global.ssl_used_frontend = 1;
5599
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005600 /* Make sure openssl opens /dev/urandom before the chroot */
5601 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005602 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005603 err++;
5604 }
5605 /* Create initial_ctx used to start the ssl connection before do switchctx */
5606 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005607 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005608 /* It should not be necessary to call this function, but it's
5609 necessary first to check and move all initialisation related
5610 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01005611 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005612 }
Emeric Brun0bed9942014-10-30 19:25:24 +01005613 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01005614 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01005615
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005616 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005617 while (node) {
5618 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01005619 if (!sni->order && sni->ctx != bind_conf->default_ctx)
5620 /* only initialize the CTX on its first occurrence and
5621 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005622 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005623 node = ebmb_next(node);
5624 }
5625
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005626 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005627 while (node) {
5628 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01005629 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01005630 /* only initialize the CTX on its first occurrence and
5631 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005632 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
5633 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005634 node = ebmb_next(node);
5635 }
William Lallemand8b453912019-11-21 15:48:10 +01005636
5637 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005638 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005639 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005640 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005641 err++;
5642 }
5643
5644 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005645 return err;
5646}
5647
Willy Tarreau55d37912016-12-21 23:38:39 +01005648/* Prepares all the contexts for a bind_conf and allocates the shared SSL
5649 * context if needed. Returns < 0 on error, 0 on success. The warnings and
5650 * alerts are directly emitted since the rest of the stack does it below.
5651 */
5652int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
5653{
5654 struct proxy *px = bind_conf->frontend;
5655 int alloc_ctx;
5656 int err;
5657
5658 if (!bind_conf->is_ssl) {
5659 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005660 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
5661 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01005662 }
5663 return 0;
5664 }
5665 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005666 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005667 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
5668 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005669 }
5670 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005671 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
5672 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005673 return -1;
5674 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005675 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005676 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005677 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005678 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01005679 sizeof(*sh_ssl_sess_tree),
5680 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005681 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005682 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5683 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");
5684 else
5685 ha_alert("Unable to allocate SSL session cache.\n");
5686 return -1;
5687 }
5688 /* free block callback */
5689 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5690 /* init the root tree within the extra space */
5691 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5692 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005693 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005694 err = 0;
5695 /* initialize all certificate contexts */
5696 err += ssl_sock_prepare_all_ctx(bind_conf);
5697
5698 /* initialize CA variables if the certificates generation is enabled */
5699 err += ssl_sock_load_ca(bind_conf);
5700
5701 return -err;
5702}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005703
5704/* release ssl context allocated for servers. */
5705void ssl_sock_free_srv_ctx(struct server *srv)
5706{
Olivier Houchardc7566002018-11-20 23:33:50 +01005707#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5708 if (srv->ssl_ctx.alpn_str)
5709 free(srv->ssl_ctx.alpn_str);
5710#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005711#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01005712 if (srv->ssl_ctx.npn_str)
5713 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005714#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005715 if (srv->ssl_ctx.ctx)
5716 SSL_CTX_free(srv->ssl_ctx.ctx);
5717}
5718
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005719/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005720 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5721 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005722void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005723{
5724 struct ebmb_node *node, *back;
5725 struct sni_ctx *sni;
5726
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005727 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005728 while (node) {
5729 sni = ebmb_entry(node, struct sni_ctx, name);
5730 back = ebmb_next(node);
5731 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005732 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005733 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005734 ssl_sock_free_ssl_conf(sni->conf);
5735 free(sni->conf);
5736 sni->conf = NULL;
5737 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005738 free(sni);
5739 node = back;
5740 }
5741
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005742 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005743 while (node) {
5744 sni = ebmb_entry(node, struct sni_ctx, name);
5745 back = ebmb_next(node);
5746 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005747 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005748 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005749 ssl_sock_free_ssl_conf(sni->conf);
5750 free(sni->conf);
5751 sni->conf = NULL;
5752 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005753 free(sni);
5754 node = back;
5755 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005756 SSL_CTX_free(bind_conf->initial_ctx);
5757 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005758 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005759 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005760}
5761
Willy Tarreau795cdab2016-12-22 17:30:54 +01005762/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5763void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5764{
5765 ssl_sock_free_ca(bind_conf);
5766 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005767 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005768 free(bind_conf->ca_sign_file);
5769 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005770 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005771 free(bind_conf->keys_ref->filename);
5772 free(bind_conf->keys_ref->tlskeys);
5773 LIST_DEL(&bind_conf->keys_ref->list);
5774 free(bind_conf->keys_ref);
5775 }
5776 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005777 bind_conf->ca_sign_pass = NULL;
5778 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005779}
5780
Christopher Faulet31af49d2015-06-09 17:29:50 +02005781/* Load CA cert file and private key used to generate certificates */
5782int
Willy Tarreau03209342016-12-22 17:08:28 +01005783ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005784{
Willy Tarreau03209342016-12-22 17:08:28 +01005785 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005786 FILE *fp;
5787 X509 *cacert = NULL;
5788 EVP_PKEY *capkey = NULL;
5789 int err = 0;
5790
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005791 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005792 return err;
5793
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005794#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005795 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005796 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005797 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005798 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005799 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005800#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005801
Christopher Faulet31af49d2015-06-09 17:29:50 +02005802 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005803 ha_alert("Proxy '%s': cannot enable certificate generation, "
5804 "no CA certificate File configured at [%s:%d].\n",
5805 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005806 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005807 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005808
5809 /* read in the CA certificate */
5810 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005811 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5812 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005813 goto load_error;
5814 }
5815 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005816 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5817 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005818 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005819 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005820 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005821 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005822 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5823 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005824 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005825 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005826
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005827 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005828 bind_conf->ca_sign_cert = cacert;
5829 bind_conf->ca_sign_pkey = capkey;
5830 return err;
5831
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005832 read_error:
5833 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005834 if (capkey) EVP_PKEY_free(capkey);
5835 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005836 load_error:
5837 bind_conf->generate_certs = 0;
5838 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005839 return err;
5840}
5841
5842/* Release CA cert and private key used to generate certificated */
5843void
5844ssl_sock_free_ca(struct bind_conf *bind_conf)
5845{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005846 if (bind_conf->ca_sign_pkey)
5847 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5848 if (bind_conf->ca_sign_cert)
5849 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005850 bind_conf->ca_sign_pkey = NULL;
5851 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005852}
5853
Emeric Brun46591952012-05-18 15:47:34 +02005854/*
5855 * This function is called if SSL * context is not yet allocated. The function
5856 * is designed to be called before any other data-layer operation and sets the
5857 * handshake flag on the connection. It is safe to call it multiple times.
5858 * It returns 0 on success and -1 in error case.
5859 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005860static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005861{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005862 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005863 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005864 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005865 return 0;
5866
Willy Tarreau3c728722014-01-23 13:50:42 +01005867 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005868 return 0;
5869
Olivier Houchard66ab4982019-02-26 18:37:15 +01005870 ctx = pool_alloc(ssl_sock_ctx_pool);
5871 if (!ctx) {
5872 conn->err_code = CO_ER_SSL_NO_MEM;
5873 return -1;
5874 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005875 ctx->wait_event.tasklet = tasklet_new();
5876 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005877 conn->err_code = CO_ER_SSL_NO_MEM;
5878 pool_free(ssl_sock_ctx_pool, ctx);
5879 return -1;
5880 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005881 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5882 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005883 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005884 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01005885 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005886 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01005887 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02005888 ctx->xprt_st = 0;
5889 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005890
5891 /* Only work with sockets for now, this should be adapted when we'll
5892 * add QUIC support.
5893 */
5894 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005895 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005896 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5897 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005898 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005899
Willy Tarreau20879a02012-12-03 16:32:10 +01005900 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5901 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005902 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005903 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005904
Emeric Brun46591952012-05-18 15:47:34 +02005905 /* If it is in client mode initiate SSL session
5906 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005907 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005908 int may_retry = 1;
5909
5910 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005911 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005912 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5913 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005914 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005915 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005916 goto retry_connect;
5917 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005918 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005919 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005920 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005921 ctx->bio = BIO_new(ha_meth);
5922 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005923 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005924 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005925 goto retry_connect;
5926 }
Emeric Brun55476152014-11-12 17:35:37 +01005927 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005928 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005929 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005930 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005931 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005932
Evan Broderbe554312013-06-27 00:05:25 -07005933 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005934 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5935 SSL_free(ctx->ssl);
5936 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005937 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005938 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005939 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005940 goto retry_connect;
5941 }
Emeric Brun55476152014-11-12 17:35:37 +01005942 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005943 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005944 }
5945
Olivier Houchard66ab4982019-02-26 18:37:15 +01005946 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005947 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5948 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5949 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 +01005950 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005951 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005952 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5953 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005954 } else if (sess) {
5955 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005956 }
5957 }
Evan Broderbe554312013-06-27 00:05:25 -07005958
Emeric Brun46591952012-05-18 15:47:34 +02005959 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005960 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005961
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005962 _HA_ATOMIC_ADD(&sslconns, 1);
5963 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005964 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005965 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005966 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005967 if (conn->flags & CO_FL_ERROR)
5968 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005969 return 0;
5970 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005971 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005972 int may_retry = 1;
5973
5974 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005975 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005976 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5977 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005978 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005979 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005980 goto retry_accept;
5981 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005982 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005983 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005984 }
Olivier Houchard545989f2019-12-17 15:39:54 +01005985#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard54907bb2019-12-19 15:02:39 +01005986 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
5987 b_alloc(&ctx->early_buf);
5988 SSL_set_max_early_data(ctx->ssl,
5989 /* Only allow early data if we managed to allocate
5990 * a buffer.
5991 */
5992 (!b_is_null(&ctx->early_buf)) ?
5993 global.tune.bufsize - global.tune.maxrewrite : 0);
5994 }
Olivier Houchard545989f2019-12-17 15:39:54 +01005995#endif
Emeric Brun46591952012-05-18 15:47:34 +02005996
Olivier Houcharda8955d52019-04-07 22:00:38 +02005997 ctx->bio = BIO_new(ha_meth);
5998 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005999 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006000 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006001 goto retry_accept;
6002 }
Emeric Brun55476152014-11-12 17:35:37 +01006003 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006004 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006005 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006006 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006007 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006008
Emeric Brune1f38db2012-09-03 20:36:47 +02006009 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006010 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6011 SSL_free(ctx->ssl);
6012 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006013 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006014 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006015 goto retry_accept;
6016 }
Emeric Brun55476152014-11-12 17:35:37 +01006017 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006018 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006019 }
6020
Olivier Houchard66ab4982019-02-26 18:37:15 +01006021 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006022
Emeric Brun46591952012-05-18 15:47:34 +02006023 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006024 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006025#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006026 conn->flags |= CO_FL_EARLY_SSL_HS;
6027#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006028
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006029 _HA_ATOMIC_ADD(&sslconns, 1);
6030 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006031 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006032 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006033 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006034 if (conn->flags & CO_FL_ERROR)
6035 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02006036 return 0;
6037 }
6038 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006039 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006040err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006041 if (ctx && ctx->wait_event.tasklet)
6042 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006043 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006044 return -1;
6045}
6046
6047
6048/* This is the callback which is used when an SSL handshake is pending. It
6049 * updates the FD status if it wants some polling before being called again.
6050 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6051 * otherwise it returns non-zero and removes itself from the connection's
6052 * flags (the bit is provided in <flag> by the caller).
6053 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006054static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006055{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006056 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006057 int ret;
6058
Willy Tarreau3c728722014-01-23 13:50:42 +01006059 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006060 return 0;
6061
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006062 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006063 goto out_error;
6064
Willy Tarreau5db847a2019-05-09 14:13:35 +02006065#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006066 /*
6067 * Check if we have early data. If we do, we have to read them
6068 * before SSL_do_handshake() is called, And there's no way to
6069 * detect early data, except to try to read them
6070 */
6071 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006072 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006073
Olivier Houchard54907bb2019-12-19 15:02:39 +01006074 while (1) {
6075 ret = SSL_read_early_data(ctx->ssl,
6076 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6077 &read_data);
6078 if (ret == SSL_READ_EARLY_DATA_ERROR)
6079 goto check_error;
6080 if (read_data > 0) {
6081 conn->flags |= CO_FL_EARLY_DATA;
6082 b_add(&ctx->early_buf, read_data);
6083 }
6084 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6085 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6086 if (!b_data(&ctx->early_buf))
6087 b_free(&ctx->early_buf);
6088 break;
6089 }
6090 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006091 }
6092#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006093 /* If we use SSL_do_handshake to process a reneg initiated by
6094 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6095 * Usually SSL_write and SSL_read are used and process implicitly
6096 * the reneg handshake.
6097 * Here we use SSL_peek as a workaround for reneg.
6098 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006099 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006100 char c;
6101
Olivier Houchard66ab4982019-02-26 18:37:15 +01006102 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006103 if (ret <= 0) {
6104 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006105 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006106
Emeric Brun674b7432012-11-08 19:21:55 +01006107 if (ret == SSL_ERROR_WANT_WRITE) {
6108 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006109 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006110 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006111 return 0;
6112 }
6113 else if (ret == SSL_ERROR_WANT_READ) {
6114 /* handshake may have been completed but we have
6115 * no more data to read.
6116 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006117 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006118 ret = 1;
6119 goto reneg_ok;
6120 }
6121 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006122 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006123 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006124 return 0;
6125 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006126#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006127 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006128 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006129 return 0;
6130 }
6131#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006132 else if (ret == SSL_ERROR_SYSCALL) {
6133 /* if errno is null, then connection was successfully established */
6134 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6135 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006136 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006137#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6138 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006139 conn->err_code = CO_ER_SSL_HANDSHAKE;
6140#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006141 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006142#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006143 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006144 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006145 empty_handshake = state == TLS_ST_BEFORE;
6146#else
Lukas Tribus49799162019-07-08 14:29:15 +02006147 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6148 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006149#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006150 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006151 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006152 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006153 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6154 else
6155 conn->err_code = CO_ER_SSL_EMPTY;
6156 }
6157 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006158 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006159 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6160 else
6161 conn->err_code = CO_ER_SSL_ABORT;
6162 }
6163 }
6164 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006165 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006166 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006167 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006168 conn->err_code = CO_ER_SSL_HANDSHAKE;
6169 }
Lukas Tribus49799162019-07-08 14:29:15 +02006170#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006171 }
Emeric Brun674b7432012-11-08 19:21:55 +01006172 goto out_error;
6173 }
6174 else {
6175 /* Fail on all other handshake errors */
6176 /* Note: OpenSSL may leave unread bytes in the socket's
6177 * buffer, causing an RST to be emitted upon close() on
6178 * TCP sockets. We first try to drain possibly pending
6179 * data to avoid this as much as possible.
6180 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006181 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006182 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006183 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006184 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006185 goto out_error;
6186 }
6187 }
6188 /* read some data: consider handshake completed */
6189 goto reneg_ok;
6190 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006191 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006192check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006193 if (ret != 1) {
6194 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006195 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006196
6197 if (ret == SSL_ERROR_WANT_WRITE) {
6198 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006199 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006200 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006201 return 0;
6202 }
6203 else if (ret == SSL_ERROR_WANT_READ) {
6204 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006205 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006206 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6207 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006208 return 0;
6209 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006210#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006211 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006212 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006213 return 0;
6214 }
6215#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006216 else if (ret == SSL_ERROR_SYSCALL) {
6217 /* if errno is null, then connection was successfully established */
6218 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6219 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006220 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006221#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6222 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006223 conn->err_code = CO_ER_SSL_HANDSHAKE;
6224#else
6225 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006226#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006227 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006228 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006229 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006230#else
Lukas Tribus49799162019-07-08 14:29:15 +02006231 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6232 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006233#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006234 if (empty_handshake) {
6235 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006236 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006237 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6238 else
6239 conn->err_code = CO_ER_SSL_EMPTY;
6240 }
6241 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006242 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006243 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6244 else
6245 conn->err_code = CO_ER_SSL_ABORT;
6246 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006247 }
6248 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006249 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006250 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6251 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006252 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006253 }
Lukas Tribus49799162019-07-08 14:29:15 +02006254#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006255 }
Willy Tarreau89230192012-09-28 20:22:13 +02006256 goto out_error;
6257 }
Emeric Brun46591952012-05-18 15:47:34 +02006258 else {
6259 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006260 /* Note: OpenSSL may leave unread bytes in the socket's
6261 * buffer, causing an RST to be emitted upon close() on
6262 * TCP sockets. We first try to drain possibly pending
6263 * data to avoid this as much as possible.
6264 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006265 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006266 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006267 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006268 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006269 goto out_error;
6270 }
6271 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006272#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006273 else {
6274 /*
6275 * If the server refused the early data, we have to send a
6276 * 425 to the client, as we no longer have the data to sent
6277 * them again.
6278 */
6279 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006280 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006281 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6282 goto out_error;
6283 }
6284 }
6285 }
6286#endif
6287
Emeric Brun46591952012-05-18 15:47:34 +02006288
Emeric Brun674b7432012-11-08 19:21:55 +01006289reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006290
Willy Tarreau5db847a2019-05-09 14:13:35 +02006291#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006292 /* ASYNC engine API doesn't support moving read/write
6293 * buffers. So we disable ASYNC mode right after
6294 * the handshake to avoid buffer oveflows.
6295 */
6296 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006297 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006298#endif
Emeric Brun46591952012-05-18 15:47:34 +02006299 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006300 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006301 if (objt_server(conn->target)) {
6302 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6303 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6304 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006305 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006306 else {
6307 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6308 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6309 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6310 }
Emeric Brun46591952012-05-18 15:47:34 +02006311 }
6312
6313 /* The connection is now established at both layers, it's time to leave */
6314 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6315 return 1;
6316
6317 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006318 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006319 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006320 ERR_clear_error();
6321
Emeric Brun9fa89732012-10-04 17:09:56 +02006322 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006323 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6324 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6325 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006326 }
6327
Emeric Brun46591952012-05-18 15:47:34 +02006328 /* Fail on all other handshake errors */
6329 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006330 if (!conn->err_code)
6331 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006332 return 0;
6333}
6334
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006335/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6336 * event subscriber <es> is not allowed to change from a previous call as long
6337 * as at least one event is still subscribed. The <event_type> must only be a
6338 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6339 * unless the transport layer was already released.
6340 */
6341static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006342{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006343 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006344
Olivier Houchard0ff28652019-06-24 18:57:39 +02006345 if (!ctx)
6346 return -1;
6347
Willy Tarreau113d52b2020-01-10 09:20:26 +01006348 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6349 BUG_ON(ctx->subs && ctx->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006350 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006351
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006352 ctx->subs = es;
6353 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006354
6355 /* we may have to subscribe to lower layers for new events */
6356 event_type &= ~ctx->wait_event.events;
6357 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6358 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006359 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006360}
6361
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006362/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6363 * The <es> pointer is not allowed to differ from the one passed to the
6364 * subscribe() call. It always returns zero.
6365 */
6366static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006367{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006368 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006369
Willy Tarreau113d52b2020-01-10 09:20:26 +01006370 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006371 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006372
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006373 es->events &= ~event_type;
6374 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006375 ctx->subs = NULL;
6376
6377 /* If we subscribed, and we're not doing the handshake,
6378 * then we subscribed because the upper layer asked for it,
6379 * as the upper layer is no longer interested, we can
6380 * unsubscribe too.
6381 */
6382 event_type &= ctx->wait_event.events;
6383 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6384 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006385
6386 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006387}
6388
Olivier Houchard2e055482019-05-27 19:50:12 +02006389/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6390 * Returns 0 on success, and non-zero on failure.
6391 */
6392static 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)
6393{
6394 struct ssl_sock_ctx *ctx = xprt_ctx;
6395
6396 if (oldxprt_ops != NULL)
6397 *oldxprt_ops = ctx->xprt;
6398 if (oldxprt_ctx != NULL)
6399 *oldxprt_ctx = ctx->xprt_ctx;
6400 ctx->xprt = toadd_ops;
6401 ctx->xprt_ctx = toadd_ctx;
6402 return 0;
6403}
6404
Olivier Houchard5149b592019-05-23 17:47:36 +02006405/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6406 * return 0, otherwise just call the remove_xprt method from the underlying
6407 * XPRT.
6408 */
6409static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6410{
6411 struct ssl_sock_ctx *ctx = xprt_ctx;
6412
6413 if (ctx->xprt_ctx == toremove_ctx) {
6414 ctx->xprt_ctx = newctx;
6415 ctx->xprt = newops;
6416 return 0;
6417 }
6418 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6419}
6420
Olivier Houchardea8dd942019-05-20 14:02:16 +02006421static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6422{
6423 struct ssl_sock_ctx *ctx = context;
6424
6425 /* First if we're doing an handshake, try that */
6426 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6427 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6428 /* If we had an error, or the handshake is done and I/O is available,
6429 * let the upper layer know.
6430 * If no mux was set up yet, and nobody subscribed, then call
6431 * xprt_done_cb() ourself if it's set, or destroy the connection,
6432 * we can't be sure conn_fd_handler() will be called again.
6433 */
6434 if ((ctx->conn->flags & CO_FL_ERROR) ||
6435 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6436 int ret = 0;
6437 int woke = 0;
6438
6439 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006440 if (ctx->subs) {
6441 tasklet_wakeup(ctx->subs->tasklet);
6442 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006443 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006444 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006445 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006446
Olivier Houchardea8dd942019-05-20 14:02:16 +02006447 /* If we're the first xprt for the connection, let the
6448 * upper layers know. If xprt_done_cb() is set, call it,
6449 * otherwise, we should have a mux, so call its wake
6450 * method if we didn't woke a tasklet already.
6451 */
6452 if (ctx->conn->xprt_ctx == ctx) {
6453 if (ctx->conn->xprt_done_cb)
6454 ret = ctx->conn->xprt_done_cb(ctx->conn);
6455 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6456 ctx->conn->mux->wake(ctx->conn);
6457 return NULL;
6458 }
6459 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006460#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6461 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006462 else if (b_data(&ctx->early_buf) && ctx->subs &&
6463 ctx->subs->events & SUB_RETRY_RECV) {
6464 tasklet_wakeup(ctx->subs->tasklet);
6465 ctx->subs->events &= ~SUB_RETRY_RECV;
6466 if (!ctx->subs->events)
6467 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006468 }
6469#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006470 return NULL;
6471}
6472
Emeric Brun46591952012-05-18 15:47:34 +02006473/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006474 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006475 * buffer wraps, in which case a second call may be performed. The connection's
6476 * flags are updated with whatever special event is detected (error, read0,
6477 * empty). The caller is responsible for taking care of those events and
6478 * avoiding the call if inappropriate. The function does not call the
6479 * connection's polling update function, so the caller is responsible for this.
6480 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006481static 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 +02006482{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006483 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006484 ssize_t ret;
6485 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006486
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006487 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006488 goto out_error;
6489
Olivier Houchard54907bb2019-12-19 15:02:39 +01006490#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6491 if (b_data(&ctx->early_buf)) {
6492 try = b_contig_space(buf);
6493 if (try > b_data(&ctx->early_buf))
6494 try = b_data(&ctx->early_buf);
6495 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6496 b_add(buf, try);
6497 b_del(&ctx->early_buf, try);
6498 if (b_data(&ctx->early_buf) == 0)
6499 b_free(&ctx->early_buf);
6500 return try;
6501 }
6502#endif
6503
Emeric Brun46591952012-05-18 15:47:34 +02006504 if (conn->flags & CO_FL_HANDSHAKE)
6505 /* a handshake was requested */
6506 return 0;
6507
Emeric Brun46591952012-05-18 15:47:34 +02006508 /* read the largest possible block. For this, we perform only one call
6509 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6510 * in which case we accept to do it once again. A new attempt is made on
6511 * EINTR too.
6512 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006513 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006514
Willy Tarreau591d4452018-06-15 17:21:00 +02006515 try = b_contig_space(buf);
6516 if (!try)
6517 break;
6518
Willy Tarreauabf08d92014-01-14 11:31:27 +01006519 if (try > count)
6520 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006521
Olivier Houchard66ab4982019-02-26 18:37:15 +01006522 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006523
Emeric Brune1f38db2012-09-03 20:36:47 +02006524 if (conn->flags & CO_FL_ERROR) {
6525 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006526 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006527 }
Emeric Brun46591952012-05-18 15:47:34 +02006528 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006529 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006530 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006531 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006532 }
Emeric Brun46591952012-05-18 15:47:34 +02006533 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006534 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006535 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006536 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02006537 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006538 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006539#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006540 /* Async mode can be re-enabled, because we're leaving data state.*/
6541 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006542 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006543#endif
Emeric Brun46591952012-05-18 15:47:34 +02006544 break;
6545 }
6546 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006547 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006548 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6549 SUB_RETRY_RECV,
6550 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01006551 /* handshake is running, and it may need to re-enable read */
6552 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006553#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006554 /* Async mode can be re-enabled, because we're leaving data state.*/
6555 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006556 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006557#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006558 break;
6559 }
Emeric Brun46591952012-05-18 15:47:34 +02006560 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006561 } else if (ret == SSL_ERROR_ZERO_RETURN)
6562 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006563 /* For SSL_ERROR_SYSCALL, make sure to clear the error
6564 * stack before shutting down the connection for
6565 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006566 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
6567 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02006568 /* otherwise it's a real error */
6569 goto out_error;
6570 }
6571 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006572 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006573 return done;
6574
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006575 clear_ssl_error:
6576 /* Clear openssl global errors stack */
6577 ssl_sock_dump_errors(conn);
6578 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02006579 read0:
6580 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006581 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006582
Emeric Brun46591952012-05-18 15:47:34 +02006583 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006584 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01006585 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006586 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006587 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006588 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006589}
6590
6591
Willy Tarreau787db9a2018-06-14 18:31:46 +02006592/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
6593 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
6594 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02006595 * Only one call to send() is performed, unless the buffer wraps, in which case
6596 * a second call may be performed. The connection's flags are updated with
6597 * whatever special event is detected (error, empty). The caller is responsible
6598 * for taking care of those events and avoiding the call if inappropriate. The
6599 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02006600 * is responsible for this. The buffer's output is not adjusted, it's up to the
6601 * caller to take care of this. It's up to the caller to update the buffer's
6602 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02006603 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006604static 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 +02006605{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006606 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006607 ssize_t ret;
6608 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02006609
6610 done = 0;
6611
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006612 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006613 goto out_error;
6614
Olivier Houchard010941f2019-05-03 20:56:19 +02006615 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006616 /* a handshake was requested */
6617 return 0;
6618
6619 /* send the largest possible block. For this we perform only one call
6620 * to send() unless the buffer wraps and we exactly fill the first hunk,
6621 * in which case we accept to do it once again.
6622 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02006623 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02006624#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006625 size_t written_data;
6626#endif
6627
Willy Tarreau787db9a2018-06-14 18:31:46 +02006628 try = b_contig_data(buf, done);
6629 if (try > count)
6630 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01006631
Willy Tarreau7bed9452014-02-02 02:00:24 +01006632 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006633 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01006634 global_ssl.max_record && try > global_ssl.max_record) {
6635 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006636 }
6637 else {
6638 /* we need to keep the information about the fact that
6639 * we're not limiting the upcoming send(), because if it
6640 * fails, we'll have to retry with at least as many data.
6641 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006642 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006643 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01006644
Willy Tarreau5db847a2019-05-09 14:13:35 +02006645#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02006646 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006647 unsigned int max_early;
6648
Olivier Houchard522eea72017-11-03 16:27:47 +01006649 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006650 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006651 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006652 if (SSL_get0_session(ctx->ssl))
6653 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006654 else
6655 max_early = 0;
6656 }
6657
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006658 if (try + ctx->sent_early_data > max_early) {
6659 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006660 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006661 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006662 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006663 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006664 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006665 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006666 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006667 if (ret == 1) {
6668 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006669 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006670 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006671 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006672 /* Initiate the handshake, now */
6673 tasklet_wakeup(ctx->wait_event.tasklet);
6674 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006675
Olivier Houchardc2aae742017-09-22 18:26:28 +02006676 }
6677
6678 } else
6679#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006680 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006681
Emeric Brune1f38db2012-09-03 20:36:47 +02006682 if (conn->flags & CO_FL_ERROR) {
6683 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006684 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006685 }
Emeric Brun46591952012-05-18 15:47:34 +02006686 if (ret > 0) {
Olivier Houchardf24502b2019-01-17 19:09:11 +01006687 /* A send succeeded, so we can consier ourself connected */
6688 conn->flags |= CO_FL_CONNECTED;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006689 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006690 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006691 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006692 }
6693 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006694 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006695
Emeric Brun46591952012-05-18 15:47:34 +02006696 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006697 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006698 /* handshake is running, and it may need to re-enable write */
6699 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006700 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006701#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006702 /* Async mode can be re-enabled, because we're leaving data state.*/
6703 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006704 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006705#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006706 break;
6707 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006708
Emeric Brun46591952012-05-18 15:47:34 +02006709 break;
6710 }
6711 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006712 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006713 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006714 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6715 SUB_RETRY_RECV,
6716 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006717#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006718 /* Async mode can be re-enabled, because we're leaving data state.*/
6719 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006720 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006721#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006722 break;
6723 }
Emeric Brun46591952012-05-18 15:47:34 +02006724 goto out_error;
6725 }
6726 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006727 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006728 return done;
6729
6730 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006731 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006732 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006733 ERR_clear_error();
6734
Emeric Brun46591952012-05-18 15:47:34 +02006735 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006736 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006737}
6738
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006739static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006740
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006741 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006742
Olivier Houchardea8dd942019-05-20 14:02:16 +02006743
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006744 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006745 if (ctx->wait_event.events != 0)
6746 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6747 ctx->wait_event.events,
6748 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01006749 if (ctx->subs) {
6750 ctx->subs->events = 0;
6751 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006752 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006753
Olivier Houchard692c1d02019-05-23 18:41:47 +02006754 if (ctx->xprt->close)
6755 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006756#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006757 if (global_ssl.async) {
6758 OSSL_ASYNC_FD all_fd[32], afd;
6759 size_t num_all_fds = 0;
6760 int i;
6761
Olivier Houchard66ab4982019-02-26 18:37:15 +01006762 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006763 if (num_all_fds > 32) {
6764 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6765 return;
6766 }
6767
Olivier Houchard66ab4982019-02-26 18:37:15 +01006768 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006769
6770 /* If an async job is pending, we must try to
6771 to catch the end using polling before calling
6772 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006773 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006774 for (i=0 ; i < num_all_fds ; i++) {
6775 /* switch on an handler designed to
6776 * handle the SSL_free
6777 */
6778 afd = all_fd[i];
6779 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006780 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006781 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006782 /* To ensure that the fd cache won't be used
6783 * and we'll catch a real RD event.
6784 */
6785 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006786 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006787 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006788 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006789 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006790 return;
6791 }
Emeric Brun3854e012017-05-17 20:42:48 +02006792 /* Else we can remove the fds from the fdtab
6793 * and call SSL_free.
6794 * note: we do a fd_remove and not a delete
6795 * because the fd is owned by the engine.
6796 * the engine is responsible to close
6797 */
6798 for (i=0 ; i < num_all_fds ; i++)
6799 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006800 }
6801#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006802 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01006803 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006804 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006805 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006806 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006807 }
Emeric Brun46591952012-05-18 15:47:34 +02006808}
6809
6810/* This function tries to perform a clean shutdown on an SSL connection, and in
6811 * any case, flags the connection as reusable if no handshake was in progress.
6812 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006813static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006814{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006815 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006816
Emeric Brun46591952012-05-18 15:47:34 +02006817 if (conn->flags & CO_FL_HANDSHAKE)
6818 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006819 if (!clean)
6820 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006821 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006822 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006823 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006824 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006825 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006826 ERR_clear_error();
6827 }
Emeric Brun46591952012-05-18 15:47:34 +02006828}
6829
William Lallemandd4f946c2019-12-05 10:26:40 +01006830/* fill a buffer with the algorithm and size of a public key */
6831static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006832{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006833 int bits = 0;
6834 int sig = TLSEXT_signature_anonymous;
6835 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006836 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006837
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006838 pkey = X509_get_pubkey(crt);
6839 if (pkey) {
6840 bits = EVP_PKEY_bits(pkey);
6841 switch(EVP_PKEY_base_id(pkey)) {
6842 case EVP_PKEY_RSA:
6843 sig = TLSEXT_signature_rsa;
6844 break;
6845 case EVP_PKEY_EC:
6846 sig = TLSEXT_signature_ecdsa;
6847 break;
6848 case EVP_PKEY_DSA:
6849 sig = TLSEXT_signature_dsa;
6850 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006851 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006852 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006853 }
6854
6855 switch(sig) {
6856 case TLSEXT_signature_rsa:
6857 len = chunk_printf(out, "RSA%d", bits);
6858 break;
6859 case TLSEXT_signature_ecdsa:
6860 len = chunk_printf(out, "EC%d", bits);
6861 break;
6862 case TLSEXT_signature_dsa:
6863 len = chunk_printf(out, "DSA%d", bits);
6864 break;
6865 default:
6866 return 0;
6867 }
6868 if (len < 0)
6869 return 0;
6870 return 1;
6871}
6872
William Lallemandd4f946c2019-12-05 10:26:40 +01006873/* used for ppv2 pkey alog (can be used for logging) */
6874int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
6875{
6876 struct ssl_sock_ctx *ctx;
6877 X509 *crt;
6878
6879 if (!ssl_sock_is_ssl(conn))
6880 return 0;
6881
6882 ctx = conn->xprt_ctx;
6883
6884 crt = SSL_get_certificate(ctx->ssl);
6885 if (!crt)
6886 return 0;
6887
6888 return cert_get_pkey_algo(crt, out);
6889}
6890
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006891/* used for ppv2 cert signature (can be used for logging) */
6892const char *ssl_sock_get_cert_sig(struct connection *conn)
6893{
Christopher Faulet82004142019-09-10 10:12:03 +02006894 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006895
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006896 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6897 X509 *crt;
6898
6899 if (!ssl_sock_is_ssl(conn))
6900 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006901 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006902 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006903 if (!crt)
6904 return NULL;
6905 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6906 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6907}
6908
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006909/* used for ppv2 authority */
6910const char *ssl_sock_get_sni(struct connection *conn)
6911{
6912#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02006913 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006914
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006915 if (!ssl_sock_is_ssl(conn))
6916 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006917 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006918 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006919#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006920 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006921#endif
6922}
6923
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006924/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006925const char *ssl_sock_get_cipher_name(struct connection *conn)
6926{
Christopher Faulet82004142019-09-10 10:12:03 +02006927 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006928
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006929 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006930 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006931 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006932 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006933}
6934
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006935/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006936const char *ssl_sock_get_proto_version(struct connection *conn)
6937{
Christopher Faulet82004142019-09-10 10:12:03 +02006938 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006939
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006940 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006941 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006942 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006943 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006944}
6945
Willy Tarreau8d598402012-10-22 17:58:39 +02006946/* Extract a serial from a cert, and copy it to a chunk.
6947 * Returns 1 if serial is found and copied, 0 if no serial found and
6948 * -1 if output is not large enough.
6949 */
6950static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006951ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006952{
6953 ASN1_INTEGER *serial;
6954
6955 serial = X509_get_serialNumber(crt);
6956 if (!serial)
6957 return 0;
6958
6959 if (out->size < serial->length)
6960 return -1;
6961
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006962 memcpy(out->area, serial->data, serial->length);
6963 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006964 return 1;
6965}
6966
Emeric Brun43e79582014-10-29 19:03:26 +01006967/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006968 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6969 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006970 */
6971static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006972ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006973{
6974 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006975 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01006976
6977 len =i2d_X509(crt, NULL);
6978 if (len <= 0)
6979 return 1;
6980
6981 if (out->size < len)
6982 return -1;
6983
6984 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006985 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01006986 return 1;
6987}
6988
Emeric Brunce5ad802012-10-22 14:11:22 +02006989
Willy Tarreau83061a82018-07-13 11:56:34 +02006990/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02006991 * Returns 1 if serial is found and copied, 0 if no valid time found
6992 * and -1 if output is not large enough.
6993 */
6994static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006995ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02006996{
6997 if (tm->type == V_ASN1_GENERALIZEDTIME) {
6998 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
6999
7000 if (gentm->length < 12)
7001 return 0;
7002 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7003 return 0;
7004 if (out->size < gentm->length-2)
7005 return -1;
7006
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007007 memcpy(out->area, gentm->data+2, gentm->length-2);
7008 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007009 return 1;
7010 }
7011 else if (tm->type == V_ASN1_UTCTIME) {
7012 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7013
7014 if (utctm->length < 10)
7015 return 0;
7016 if (utctm->data[0] >= 0x35)
7017 return 0;
7018 if (out->size < utctm->length)
7019 return -1;
7020
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007021 memcpy(out->area, utctm->data, utctm->length);
7022 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007023 return 1;
7024 }
7025
7026 return 0;
7027}
7028
Emeric Brun87855892012-10-17 17:39:35 +02007029/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7030 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7031 */
7032static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007033ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7034 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007035{
7036 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007037 ASN1_OBJECT *obj;
7038 ASN1_STRING *data;
7039 const unsigned char *data_ptr;
7040 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007041 int i, j, n;
7042 int cur = 0;
7043 const char *s;
7044 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007045 int name_count;
7046
7047 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007048
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007049 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007050 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007051 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007052 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007053 else
7054 j = i;
7055
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007056 ne = X509_NAME_get_entry(a, j);
7057 obj = X509_NAME_ENTRY_get_object(ne);
7058 data = X509_NAME_ENTRY_get_data(ne);
7059 data_ptr = ASN1_STRING_get0_data(data);
7060 data_len = ASN1_STRING_length(data);
7061 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007062 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007063 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007064 s = tmp;
7065 }
7066
7067 if (chunk_strcasecmp(entry, s) != 0)
7068 continue;
7069
7070 if (pos < 0)
7071 cur--;
7072 else
7073 cur++;
7074
7075 if (cur != pos)
7076 continue;
7077
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007078 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007079 return -1;
7080
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007081 memcpy(out->area, data_ptr, data_len);
7082 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007083 return 1;
7084 }
7085
7086 return 0;
7087
William Lallemandd4f946c2019-12-05 10:26:40 +01007088}
7089
7090/*
7091 * Extract and format the DNS SAN extensions and copy result into a chuink
7092 * Return 0;
7093 */
7094#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7095static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7096{
7097 int i;
7098 char *str;
7099 STACK_OF(GENERAL_NAME) *names = NULL;
7100
7101 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7102 if (names) {
7103 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7104 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7105 if (i > 0)
7106 chunk_appendf(out, ", ");
7107 if (name->type == GEN_DNS) {
7108 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7109 chunk_appendf(out, "DNS:%s", str);
7110 OPENSSL_free(str);
7111 }
7112 }
7113 }
7114 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7115 }
7116 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007117}
William Lallemandd4f946c2019-12-05 10:26:40 +01007118#endif
Emeric Brun87855892012-10-17 17:39:35 +02007119
Elliot Otchet71f82972020-01-15 08:12:14 -05007120/*
7121 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7122 * Currently supports rfc2253 for returning LDAP V3 DNs.
7123 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7124 */
7125static int
7126ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7127{
7128 BIO *bio = NULL;
7129 int ret = 0;
7130 int data_len = 0;
7131
7132 if (chunk_strcmp(format, "rfc2253") == 0) {
7133 bio = BIO_new(BIO_s_mem());
7134 if (bio == NULL)
7135 goto out;
7136
7137 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7138 goto out;
7139
7140 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7141 goto out;
7142
7143 out->data = data_len;
7144
7145 ret = 1;
7146 }
7147out:
7148 if (bio)
7149 BIO_free(bio);
7150 return ret;
7151}
7152
Emeric Brun87855892012-10-17 17:39:35 +02007153/* Extract and format full DN from a X509_NAME and copy result into a chunk
7154 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7155 */
7156static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007157ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007158{
7159 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007160 ASN1_OBJECT *obj;
7161 ASN1_STRING *data;
7162 const unsigned char *data_ptr;
7163 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007164 int i, n, ln;
7165 int l = 0;
7166 const char *s;
7167 char *p;
7168 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007169 int name_count;
7170
7171
7172 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007173
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007174 out->data = 0;
7175 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007176 for (i = 0; i < name_count; i++) {
7177 ne = X509_NAME_get_entry(a, i);
7178 obj = X509_NAME_ENTRY_get_object(ne);
7179 data = X509_NAME_ENTRY_get_data(ne);
7180 data_ptr = ASN1_STRING_get0_data(data);
7181 data_len = ASN1_STRING_length(data);
7182 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007183 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007184 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007185 s = tmp;
7186 }
7187 ln = strlen(s);
7188
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007189 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007190 if (l > out->size)
7191 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007192 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007193
7194 *(p++)='/';
7195 memcpy(p, s, ln);
7196 p += ln;
7197 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007198 memcpy(p, data_ptr, data_len);
7199 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007200 }
7201
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007202 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007203 return 0;
7204
7205 return 1;
7206}
7207
Olivier Houchardab28a322018-12-21 19:45:40 +01007208void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7209{
7210#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007211 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007212
Olivier Houcharde488ea82019-06-28 14:10:33 +02007213 if (!ssl_sock_is_ssl(conn))
7214 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007215 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007216 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007217#endif
7218}
7219
Willy Tarreau119a4082016-12-22 21:58:38 +01007220/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7221 * to disable SNI.
7222 */
Willy Tarreau63076412015-07-10 11:33:32 +02007223void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7224{
7225#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007226 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007227
Willy Tarreau119a4082016-12-22 21:58:38 +01007228 char *prev_name;
7229
Willy Tarreau63076412015-07-10 11:33:32 +02007230 if (!ssl_sock_is_ssl(conn))
7231 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007232 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007233
Willy Tarreau119a4082016-12-22 21:58:38 +01007234 /* if the SNI changes, we must destroy the reusable context so that a
7235 * new connection will present a new SNI. As an optimization we could
7236 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7237 * server.
7238 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007239 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007240 if ((!prev_name && hostname) ||
7241 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007242 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007243
Olivier Houchard66ab4982019-02-26 18:37:15 +01007244 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007245#endif
7246}
7247
Emeric Brun0abf8362014-06-24 18:26:41 +02007248/* Extract peer certificate's common name into the chunk dest
7249 * Returns
7250 * the len of the extracted common name
7251 * or 0 if no CN found in DN
7252 * or -1 on error case (i.e. no peer certificate)
7253 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007254int ssl_sock_get_remote_common_name(struct connection *conn,
7255 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007256{
Christopher Faulet82004142019-09-10 10:12:03 +02007257 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007258 X509 *crt = NULL;
7259 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007260 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007261 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007262 .area = (char *)&find_cn,
7263 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007264 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007265 int result = -1;
David Safb76832014-05-08 23:42:08 -04007266
7267 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007268 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007269 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007270
7271 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007272 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007273 if (!crt)
7274 goto out;
7275
7276 name = X509_get_subject_name(crt);
7277 if (!name)
7278 goto out;
David Safb76832014-05-08 23:42:08 -04007279
Emeric Brun0abf8362014-06-24 18:26:41 +02007280 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7281out:
David Safb76832014-05-08 23:42:08 -04007282 if (crt)
7283 X509_free(crt);
7284
7285 return result;
7286}
7287
Dave McCowan328fb582014-07-30 10:39:13 -04007288/* returns 1 if client passed a certificate for this session, 0 if not */
7289int ssl_sock_get_cert_used_sess(struct connection *conn)
7290{
Christopher Faulet82004142019-09-10 10:12:03 +02007291 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007292 X509 *crt = NULL;
7293
7294 if (!ssl_sock_is_ssl(conn))
7295 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007296 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007297
7298 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007299 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007300 if (!crt)
7301 return 0;
7302
7303 X509_free(crt);
7304 return 1;
7305}
7306
7307/* returns 1 if client passed a certificate for this connection, 0 if not */
7308int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007309{
Christopher Faulet82004142019-09-10 10:12:03 +02007310 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007311
David Safb76832014-05-08 23:42:08 -04007312 if (!ssl_sock_is_ssl(conn))
7313 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007314 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007315 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007316}
7317
7318/* returns result from SSL verify */
7319unsigned int ssl_sock_get_verify_result(struct connection *conn)
7320{
Christopher Faulet82004142019-09-10 10:12:03 +02007321 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007322
David Safb76832014-05-08 23:42:08 -04007323 if (!ssl_sock_is_ssl(conn))
7324 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007325 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007326 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007327}
7328
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007329/* Returns the application layer protocol name in <str> and <len> when known.
7330 * Zero is returned if the protocol name was not found, otherwise non-zero is
7331 * returned. The string is allocated in the SSL context and doesn't have to be
7332 * freed by the caller. NPN is also checked if available since older versions
7333 * of openssl (1.0.1) which are more common in field only support this one.
7334 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007335static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007336{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007337#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7338 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007339 struct ssl_sock_ctx *ctx = xprt_ctx;
7340 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007341 return 0;
7342
7343 *str = NULL;
7344
7345#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007346 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007347 if (*str)
7348 return 1;
7349#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007350#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007351 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007352 if (*str)
7353 return 1;
7354#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007355#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007356 return 0;
7357}
7358
Willy Tarreau7875d092012-09-10 08:20:03 +02007359/***** Below are some sample fetching functions for ACL/patterns *****/
7360
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007361static int
7362smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7363{
7364 struct connection *conn;
7365
7366 conn = objt_conn(smp->sess->origin);
7367 if (!conn || conn->xprt != &ssl_sock)
7368 return 0;
7369
7370 smp->flags = 0;
7371 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007372#ifdef OPENSSL_IS_BORINGSSL
7373 {
7374 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7375 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7376 SSL_early_data_accepted(ctx->ssl));
7377 }
7378#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007379 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
7380 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007381#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007382 return 1;
7383}
7384
Emeric Brune64aef12012-09-21 13:15:06 +02007385/* boolean, returns true if client cert was present */
7386static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007387smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007388{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007389 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007390 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007391
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007392 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007393 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007394 return 0;
7395
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007396 ctx = conn->xprt_ctx;
7397
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007398 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02007399 smp->flags |= SMP_F_MAY_CHANGE;
7400 return 0;
7401 }
7402
7403 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007404 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007405 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007406
7407 return 1;
7408}
7409
Emeric Brun43e79582014-10-29 19:03:26 +01007410/* binary, returns a certificate in a binary chunk (der/raw).
7411 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7412 * should be use.
7413 */
7414static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007415smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007416{
7417 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7418 X509 *crt = NULL;
7419 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007420 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007421 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007422 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007423
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007424 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007425 if (!conn || conn->xprt != &ssl_sock)
7426 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007427 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007428
7429 if (!(conn->flags & CO_FL_CONNECTED)) {
7430 smp->flags |= SMP_F_MAY_CHANGE;
7431 return 0;
7432 }
7433
7434 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007435 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007436 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007437 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007438
7439 if (!crt)
7440 goto out;
7441
7442 smp_trash = get_trash_chunk();
7443 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7444 goto out;
7445
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007446 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007447 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007448 ret = 1;
7449out:
7450 /* SSL_get_peer_certificate, it increase X509 * ref count */
7451 if (cert_peer && crt)
7452 X509_free(crt);
7453 return ret;
7454}
7455
Emeric Brunba841a12014-04-30 17:05:08 +02007456/* binary, returns serial of certificate in a binary chunk.
7457 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7458 * should be use.
7459 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007460static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007461smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007462{
Emeric Brunba841a12014-04-30 17:05:08 +02007463 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007464 X509 *crt = NULL;
7465 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007466 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007467 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007468 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007469
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007470 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007471 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007472 return 0;
7473
Olivier Houchard66ab4982019-02-26 18:37:15 +01007474 ctx = conn->xprt_ctx;
7475
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007476 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007477 smp->flags |= SMP_F_MAY_CHANGE;
7478 return 0;
7479 }
7480
Emeric Brunba841a12014-04-30 17:05:08 +02007481 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007482 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007483 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007484 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007485
Willy Tarreau8d598402012-10-22 17:58:39 +02007486 if (!crt)
7487 goto out;
7488
Willy Tarreau47ca5452012-12-23 20:22:19 +01007489 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007490 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7491 goto out;
7492
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007493 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007494 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007495 ret = 1;
7496out:
Emeric Brunba841a12014-04-30 17:05:08 +02007497 /* SSL_get_peer_certificate, it increase X509 * ref count */
7498 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007499 X509_free(crt);
7500 return ret;
7501}
Emeric Brune64aef12012-09-21 13:15:06 +02007502
Emeric Brunba841a12014-04-30 17:05:08 +02007503/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7504 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7505 * should be use.
7506 */
James Votha051b4a2013-05-14 20:37:59 +02007507static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007508smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007509{
Emeric Brunba841a12014-04-30 17:05:08 +02007510 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007511 X509 *crt = NULL;
7512 const EVP_MD *digest;
7513 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007514 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007515 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007516 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02007517
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007518 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007519 if (!conn || conn->xprt != &ssl_sock)
7520 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007521 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007522
7523 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02007524 smp->flags |= SMP_F_MAY_CHANGE;
7525 return 0;
7526 }
7527
Emeric Brunba841a12014-04-30 17:05:08 +02007528 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007529 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007530 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007531 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02007532 if (!crt)
7533 goto out;
7534
7535 smp_trash = get_trash_chunk();
7536 digest = EVP_sha1();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007537 X509_digest(crt, digest, (unsigned char *) smp_trash->area,
7538 (unsigned int *)&smp_trash->data);
James Votha051b4a2013-05-14 20:37:59 +02007539
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007540 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007541 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02007542 ret = 1;
7543out:
Emeric Brunba841a12014-04-30 17:05:08 +02007544 /* SSL_get_peer_certificate, it increase X509 * ref count */
7545 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02007546 X509_free(crt);
7547 return ret;
7548}
7549
Emeric Brunba841a12014-04-30 17:05:08 +02007550/* string, returns certificate's notafter date in ASN1_UTCTIME format.
7551 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7552 * should be use.
7553 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007554static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007555smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007556{
Emeric Brunba841a12014-04-30 17:05:08 +02007557 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007558 X509 *crt = NULL;
7559 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007560 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007561 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007562 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007563
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007564 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007565 if (!conn || conn->xprt != &ssl_sock)
7566 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007567 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007568
7569 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007570 smp->flags |= SMP_F_MAY_CHANGE;
7571 return 0;
7572 }
7573
Emeric Brunba841a12014-04-30 17:05:08 +02007574 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007575 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007576 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007577 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007578 if (!crt)
7579 goto out;
7580
Willy Tarreau47ca5452012-12-23 20:22:19 +01007581 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007582 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007583 goto out;
7584
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007585 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007586 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007587 ret = 1;
7588out:
Emeric Brunba841a12014-04-30 17:05:08 +02007589 /* SSL_get_peer_certificate, it increase X509 * ref count */
7590 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007591 X509_free(crt);
7592 return ret;
7593}
7594
Emeric Brunba841a12014-04-30 17:05:08 +02007595/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
7596 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7597 * should be use.
7598 */
Emeric Brun87855892012-10-17 17:39:35 +02007599static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007600smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007601{
Emeric Brunba841a12014-04-30 17:05:08 +02007602 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007603 X509 *crt = NULL;
7604 X509_NAME *name;
7605 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007606 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007607 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007608 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007609
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007610 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007611 if (!conn || conn->xprt != &ssl_sock)
7612 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007613 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007614
7615 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007616 smp->flags |= SMP_F_MAY_CHANGE;
7617 return 0;
7618 }
7619
Emeric Brunba841a12014-04-30 17:05:08 +02007620 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007621 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007622 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007623 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007624 if (!crt)
7625 goto out;
7626
7627 name = X509_get_issuer_name(crt);
7628 if (!name)
7629 goto out;
7630
Willy Tarreau47ca5452012-12-23 20:22:19 +01007631 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007632 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007633 int pos = 1;
7634
7635 if (args[1].type == ARGT_SINT)
7636 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007637
7638 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7639 goto out;
7640 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007641 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7642 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7643 goto out;
7644 }
Emeric Brun87855892012-10-17 17:39:35 +02007645 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7646 goto out;
7647
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007648 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007649 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007650 ret = 1;
7651out:
Emeric Brunba841a12014-04-30 17:05:08 +02007652 /* SSL_get_peer_certificate, it increase X509 * ref count */
7653 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007654 X509_free(crt);
7655 return ret;
7656}
7657
Emeric Brunba841a12014-04-30 17:05:08 +02007658/* string, returns notbefore date in ASN1_UTCTIME format.
7659 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7660 * should be use.
7661 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007662static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007663smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007664{
Emeric Brunba841a12014-04-30 17:05:08 +02007665 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007666 X509 *crt = NULL;
7667 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007668 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007669 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007670 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007671
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007672 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007673 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02007674 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007675 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007676
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007677 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007678 smp->flags |= SMP_F_MAY_CHANGE;
7679 return 0;
7680 }
7681
Emeric Brunba841a12014-04-30 17:05:08 +02007682 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007683 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007684 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007685 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007686 if (!crt)
7687 goto out;
7688
Willy Tarreau47ca5452012-12-23 20:22:19 +01007689 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007690 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007691 goto out;
7692
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007693 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007694 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007695 ret = 1;
7696out:
Emeric Brunba841a12014-04-30 17:05:08 +02007697 /* SSL_get_peer_certificate, it increase X509 * ref count */
7698 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007699 X509_free(crt);
7700 return ret;
7701}
7702
Emeric Brunba841a12014-04-30 17:05:08 +02007703/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
7704 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7705 * should be use.
7706 */
Emeric Brun87855892012-10-17 17:39:35 +02007707static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007708smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007709{
Emeric Brunba841a12014-04-30 17:05:08 +02007710 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007711 X509 *crt = NULL;
7712 X509_NAME *name;
7713 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007714 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007715 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007716 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007717
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007718 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007719 if (!conn || conn->xprt != &ssl_sock)
7720 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007721 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007722
7723 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007724 smp->flags |= SMP_F_MAY_CHANGE;
7725 return 0;
7726 }
7727
Emeric Brunba841a12014-04-30 17:05:08 +02007728 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007729 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007730 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007731 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007732 if (!crt)
7733 goto out;
7734
7735 name = X509_get_subject_name(crt);
7736 if (!name)
7737 goto out;
7738
Willy Tarreau47ca5452012-12-23 20:22:19 +01007739 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007740 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007741 int pos = 1;
7742
7743 if (args[1].type == ARGT_SINT)
7744 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007745
7746 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7747 goto out;
7748 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007749 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7750 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7751 goto out;
7752 }
Emeric Brun87855892012-10-17 17:39:35 +02007753 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7754 goto out;
7755
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007756 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007757 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007758 ret = 1;
7759out:
Emeric Brunba841a12014-04-30 17:05:08 +02007760 /* SSL_get_peer_certificate, it increase X509 * ref count */
7761 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007762 X509_free(crt);
7763 return ret;
7764}
Emeric Brun9143d372012-12-20 15:44:16 +01007765
7766/* integer, returns true if current session use a client certificate */
7767static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007768smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007769{
7770 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007771 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007772 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007773
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007774 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007775 if (!conn || conn->xprt != &ssl_sock)
7776 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007777 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007778
7779 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01007780 smp->flags |= SMP_F_MAY_CHANGE;
7781 return 0;
7782 }
7783
7784 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007785 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007786 if (crt) {
7787 X509_free(crt);
7788 }
7789
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007790 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007791 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007792 return 1;
7793}
7794
Emeric Brunba841a12014-04-30 17:05:08 +02007795/* integer, returns the certificate version
7796 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7797 * should be use.
7798 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007799static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007800smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007801{
Emeric Brunba841a12014-04-30 17:05:08 +02007802 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007803 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007804 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007805 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007806
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007807 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007808 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007809 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007810 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007811
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007812 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007813 smp->flags |= SMP_F_MAY_CHANGE;
7814 return 0;
7815 }
7816
Emeric Brunba841a12014-04-30 17:05:08 +02007817 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007818 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007819 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007820 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007821 if (!crt)
7822 return 0;
7823
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007824 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007825 /* SSL_get_peer_certificate increase X509 * ref count */
7826 if (cert_peer)
7827 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007828 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007829
7830 return 1;
7831}
7832
Emeric Brunba841a12014-04-30 17:05:08 +02007833/* string, returns the certificate's signature algorithm.
7834 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7835 * should be use.
7836 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007837static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007838smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007839{
Emeric Brunba841a12014-04-30 17:05:08 +02007840 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007841 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007842 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007843 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007844 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007845 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007846
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007847 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007848 if (!conn || conn->xprt != &ssl_sock)
7849 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007850 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007851
7852 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007853 smp->flags |= SMP_F_MAY_CHANGE;
7854 return 0;
7855 }
7856
Emeric Brunba841a12014-04-30 17:05:08 +02007857 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007858 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007859 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007860 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007861 if (!crt)
7862 return 0;
7863
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007864 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7865 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007866
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007867 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7868 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007869 /* SSL_get_peer_certificate increase X509 * ref count */
7870 if (cert_peer)
7871 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007872 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007873 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007874
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007875 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007876 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007877 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007878 /* SSL_get_peer_certificate increase X509 * ref count */
7879 if (cert_peer)
7880 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007881
7882 return 1;
7883}
7884
Emeric Brunba841a12014-04-30 17:05:08 +02007885/* string, returns the certificate's key algorithm.
7886 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7887 * should be use.
7888 */
Emeric Brun521a0112012-10-22 12:22:55 +02007889static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007890smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02007891{
Emeric Brunba841a12014-04-30 17:05:08 +02007892 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02007893 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007894 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02007895 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007896 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007897 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02007898
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007899 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007900 if (!conn || conn->xprt != &ssl_sock)
7901 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007902 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007903
7904 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02007905 smp->flags |= SMP_F_MAY_CHANGE;
7906 return 0;
7907 }
7908
Emeric Brunba841a12014-04-30 17:05:08 +02007909 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007910 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007911 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007912 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02007913 if (!crt)
7914 return 0;
7915
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007916 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
7917 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02007918
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007919 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7920 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007921 /* SSL_get_peer_certificate increase X509 * ref count */
7922 if (cert_peer)
7923 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007924 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007925 }
Emeric Brun521a0112012-10-22 12:22:55 +02007926
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007927 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007928 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007929 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007930 if (cert_peer)
7931 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007932
7933 return 1;
7934}
7935
Emeric Brun645ae792014-04-30 14:21:06 +02007936/* boolean, returns true if front conn. transport layer is SSL.
7937 * This function is also usable on backend conn if the fetch keyword 5th
7938 * char is 'b'.
7939 */
Willy Tarreau7875d092012-09-10 08:20:03 +02007940static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007941smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007942{
Emeric Bruneb8def92018-02-19 15:59:48 +01007943 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7944 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007945
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007946 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007947 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007948 return 1;
7949}
7950
Emeric Brun2525b6b2012-10-18 15:59:43 +02007951/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007952static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007953smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007954{
7955#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007956 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007957 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007958
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007959 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007960 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007961 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007962 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007963 return 1;
7964#else
7965 return 0;
7966#endif
7967}
7968
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007969/* boolean, returns true if client session has been resumed.
7970 * This function is also usable on backend conn if the fetch keyword 5th
7971 * char is 'b'.
7972 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007973static int
7974smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
7975{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007976 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7977 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007978 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007979
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007980
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007981 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007982 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007983 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007984 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007985 return 1;
7986}
7987
Emeric Brun645ae792014-04-30 14:21:06 +02007988/* string, returns the used cipher if front conn. transport layer is SSL.
7989 * This function is also usable on backend conn if the fetch keyword 5th
7990 * char is 'b'.
7991 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007992static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007993smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007994{
Emeric Bruneb8def92018-02-19 15:59:48 +01007995 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7996 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007997 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007998
Willy Tarreaube508f12016-03-10 11:47:01 +01007999 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008000 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008001 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008002 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008003
Olivier Houchard66ab4982019-02-26 18:37:15 +01008004 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008005 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008006 return 0;
8007
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008008 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008009 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008010 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008011
8012 return 1;
8013}
8014
Emeric Brun645ae792014-04-30 14:21:06 +02008015/* integer, returns the algoritm's keysize if front conn. transport layer
8016 * is SSL.
8017 * This function is also usable on backend conn if the fetch keyword 5th
8018 * char is 'b'.
8019 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008020static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008021smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008022{
Emeric Bruneb8def92018-02-19 15:59:48 +01008023 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8024 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008025 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008026 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008027
Emeric Brun589fcad2012-10-16 14:13:26 +02008028 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008029 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008030 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008031 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008032
Olivier Houchard66ab4982019-02-26 18:37:15 +01008033 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008034 return 0;
8035
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008036 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008037 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008038
8039 return 1;
8040}
8041
Emeric Brun645ae792014-04-30 14:21:06 +02008042/* integer, returns the used keysize if front conn. transport layer is SSL.
8043 * This function is also usable on backend conn if the fetch keyword 5th
8044 * char is 'b'.
8045 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008046static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008047smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008048{
Emeric Bruneb8def92018-02-19 15:59:48 +01008049 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8050 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008051 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008052
Emeric Brun589fcad2012-10-16 14:13:26 +02008053 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008054 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8055 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008056 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008057
Olivier Houchard66ab4982019-02-26 18:37:15 +01008058 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008059 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008060 return 0;
8061
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008062 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008063
8064 return 1;
8065}
8066
Bernard Spil13c53f82018-02-15 13:34:58 +01008067#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008068static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008069smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008070{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008071 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008072 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008073
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008074 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008075 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008076
Olivier Houchard6b77f492018-11-22 18:18:29 +01008077 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8078 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008079 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8080 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008081 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008082
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008083 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008084 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008085 (const unsigned char **)&smp->data.u.str.area,
8086 (unsigned *)&smp->data.u.str.data);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008087
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008088 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008089 return 0;
8090
8091 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008092}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008093#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008094
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008095#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008096static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008097smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008098{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008099 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008100 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008101
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008102 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008103 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008104
Olivier Houchard6b77f492018-11-22 18:18:29 +01008105 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8106 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8107
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008108 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008109 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008110 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008111
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008112 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008113 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008114 (const unsigned char **)&smp->data.u.str.area,
8115 (unsigned *)&smp->data.u.str.data);
Willy Tarreauab861d32013-04-02 02:30:41 +02008116
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008117 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008118 return 0;
8119
8120 return 1;
8121}
8122#endif
8123
Emeric Brun645ae792014-04-30 14:21:06 +02008124/* string, returns the used protocol if front conn. transport layer is SSL.
8125 * This function is also usable on backend conn if the fetch keyword 5th
8126 * char is 'b'.
8127 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008128static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008129smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008130{
Emeric Bruneb8def92018-02-19 15:59:48 +01008131 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8132 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008133 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008134
Emeric Brun589fcad2012-10-16 14:13:26 +02008135 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008136 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8137 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008138 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008139
Olivier Houchard66ab4982019-02-26 18:37:15 +01008140 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008141 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008142 return 0;
8143
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008144 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008145 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008146 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008147
8148 return 1;
8149}
8150
Willy Tarreau87b09662015-04-03 00:22:06 +02008151/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008152 * This function is also usable on backend conn if the fetch keyword 5th
8153 * char is 'b'.
8154 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008155#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008156static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008157smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008158{
Emeric Bruneb8def92018-02-19 15:59:48 +01008159 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8160 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008161 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008162 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008163
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008164 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008165 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008166
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008167 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8168 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008169 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008170
Olivier Houchard66ab4982019-02-26 18:37:15 +01008171 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008172 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008173 return 0;
8174
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008175 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess,
8176 (unsigned int *)&smp->data.u.str.data);
8177 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008178 return 0;
8179
8180 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008181}
Patrick Hemmer41966772018-04-28 19:15:48 -04008182#endif
8183
Emeric Brunfe68f682012-10-16 14:59:28 +02008184
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008185#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008186static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008187smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8188{
8189 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8190 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8191 struct buffer *data;
8192 struct ssl_sock_ctx *ctx;
8193
8194 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8195 return 0;
8196 ctx = conn->xprt_ctx;
8197
8198 data = get_trash_chunk();
8199 if (kw[7] == 'c')
8200 data->data = SSL_get_client_random(ctx->ssl,
8201 (unsigned char *) data->area,
8202 data->size);
8203 else
8204 data->data = SSL_get_server_random(ctx->ssl,
8205 (unsigned char *) data->area,
8206 data->size);
8207 if (!data->data)
8208 return 0;
8209
8210 smp->flags = 0;
8211 smp->data.type = SMP_T_BIN;
8212 smp->data.u.str = *data;
8213
8214 return 1;
8215}
8216
8217static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008218smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8219{
8220 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8221 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8222 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008223 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008224 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008225
8226 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8227 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008228 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008229
Olivier Houchard66ab4982019-02-26 18:37:15 +01008230 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008231 if (!ssl_sess)
8232 return 0;
8233
8234 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008235 data->data = SSL_SESSION_get_master_key(ssl_sess,
8236 (unsigned char *) data->area,
8237 data->size);
8238 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008239 return 0;
8240
8241 smp->flags = 0;
8242 smp->data.type = SMP_T_BIN;
8243 smp->data.u.str = *data;
8244
8245 return 1;
8246}
8247#endif
8248
Patrick Hemmer41966772018-04-28 19:15:48 -04008249#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008250static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008251smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008252{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008253 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008254 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008255
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008256 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008257 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008258
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008259 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008260 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8261 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008262 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008263
Olivier Houchard66ab4982019-02-26 18:37:15 +01008264 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008265 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008266 return 0;
8267
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008268 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008269 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008270}
Patrick Hemmer41966772018-04-28 19:15:48 -04008271#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008272
David Sc1ad52e2014-04-08 18:48:47 -04008273static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008274smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8275{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008276 struct connection *conn;
8277 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008278 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008279
8280 conn = objt_conn(smp->sess->origin);
8281 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8282 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008283 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008284
Olivier Houchard66ab4982019-02-26 18:37:15 +01008285 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008286 if (!capture)
8287 return 0;
8288
8289 smp->flags = SMP_F_CONST;
8290 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008291 smp->data.u.str.area = capture->ciphersuite;
8292 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008293 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008294}
8295
8296static int
8297smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8298{
Willy Tarreau83061a82018-07-13 11:56:34 +02008299 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008300
8301 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8302 return 0;
8303
8304 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008305 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008306 smp->data.type = SMP_T_BIN;
8307 smp->data.u.str = *data;
8308 return 1;
8309}
8310
8311static int
8312smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8313{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008314 struct connection *conn;
8315 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008316 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008317
8318 conn = objt_conn(smp->sess->origin);
8319 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8320 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008321 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008322
Olivier Houchard66ab4982019-02-26 18:37:15 +01008323 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008324 if (!capture)
8325 return 0;
8326
8327 smp->data.type = SMP_T_SINT;
8328 smp->data.u.sint = capture->xxh64;
8329 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008330}
8331
8332static int
8333smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8334{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008335#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008336 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008337 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008338
8339 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8340 return 0;
8341
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008342 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008343 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008344 const char *str;
8345 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008346 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008347 uint16_t id = (bin[0] << 8) | bin[1];
8348#if defined(OPENSSL_IS_BORINGSSL)
8349 cipher = SSL_get_cipher_by_value(id);
8350#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008351 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008352 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8353 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008354#endif
8355 str = SSL_CIPHER_get_name(cipher);
8356 if (!str || strcmp(str, "(NONE)") == 0)
8357 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008358 else
8359 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8360 }
8361 smp->data.type = SMP_T_STR;
8362 smp->data.u.str = *data;
8363 return 1;
8364#else
8365 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8366#endif
8367}
8368
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008369#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008370static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008371smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008372{
Emeric Bruneb8def92018-02-19 15:59:48 +01008373 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8374 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008375 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008376 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008377 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008378
8379 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008380 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8381 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008382 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008383
8384 if (!(conn->flags & CO_FL_CONNECTED)) {
8385 smp->flags |= SMP_F_MAY_CHANGE;
8386 return 0;
8387 }
8388
8389 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008390 if (!SSL_session_reused(ctx->ssl))
8391 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008392 finished_trash->area,
8393 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008394 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008395 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008396 finished_trash->area,
8397 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008398
8399 if (!finished_len)
8400 return 0;
8401
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008402 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008403 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008404 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008405
8406 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008407}
Patrick Hemmer41966772018-04-28 19:15:48 -04008408#endif
David Sc1ad52e2014-04-08 18:48:47 -04008409
Emeric Brun2525b6b2012-10-18 15:59:43 +02008410/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008411static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008412smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008413{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008414 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008415 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008416
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008417 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008418 if (!conn || conn->xprt != &ssl_sock)
8419 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008420 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008421
8422 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008423 smp->flags = SMP_F_MAY_CHANGE;
8424 return 0;
8425 }
8426
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008427 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008428 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008429 smp->flags = 0;
8430
8431 return 1;
8432}
8433
Emeric Brun2525b6b2012-10-18 15:59:43 +02008434/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008435static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008436smp_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 +02008437{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008438 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008439 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008440
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008441 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008442 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008443 return 0;
8444
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008445 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008446 smp->flags = SMP_F_MAY_CHANGE;
8447 return 0;
8448 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008449 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008450
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008451 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008452 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008453 smp->flags = 0;
8454
8455 return 1;
8456}
8457
Emeric Brun2525b6b2012-10-18 15:59:43 +02008458/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008459static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008460smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008461{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008462 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008463 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008464
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008465 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008466 if (!conn || conn->xprt != &ssl_sock)
8467 return 0;
8468
8469 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008470 smp->flags = SMP_F_MAY_CHANGE;
8471 return 0;
8472 }
8473
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008474 ctx = conn->xprt_ctx;
8475
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008476 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008477 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008478 smp->flags = 0;
8479
8480 return 1;
8481}
8482
Emeric Brun2525b6b2012-10-18 15:59:43 +02008483/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008484static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008485smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008486{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008487 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008488 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008489
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008490 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008491 if (!conn || conn->xprt != &ssl_sock)
8492 return 0;
8493
8494 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008495 smp->flags = SMP_F_MAY_CHANGE;
8496 return 0;
8497 }
8498
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008499 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008500 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008501 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008502
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008503 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008504 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008505 smp->flags = 0;
8506
8507 return 1;
8508}
8509
Emeric Brunfb510ea2012-10-05 12:00:26 +02008510/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008511static 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 +02008512{
8513 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008514 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008515 return ERR_ALERT | ERR_FATAL;
8516 }
8517
Willy Tarreauef934602016-12-22 23:12:01 +01008518 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8519 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008520 else
8521 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008522
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02008523 if (!ssl_store_load_locations_file(conf->ca_file)) {
8524 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->ca_file);
8525 return ERR_ALERT | ERR_FATAL;
8526 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02008527 return 0;
8528}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008529static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8530{
8531 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
8532}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008533
Christopher Faulet31af49d2015-06-09 17:29:50 +02008534/* parse the "ca-sign-file" bind keyword */
8535static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8536{
8537 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008538 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008539 return ERR_ALERT | ERR_FATAL;
8540 }
8541
Willy Tarreauef934602016-12-22 23:12:01 +01008542 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8543 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008544 else
8545 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
8546
8547 return 0;
8548}
8549
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008550/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02008551static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8552{
8553 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008554 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008555 return ERR_ALERT | ERR_FATAL;
8556 }
8557 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
8558 return 0;
8559}
8560
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008561/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008562static 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 +02008563{
8564 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008565 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008566 return ERR_ALERT | ERR_FATAL;
8567 }
8568
Emeric Brun76d88952012-10-05 15:47:31 +02008569 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02008570 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008571 return 0;
8572}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008573static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8574{
8575 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
8576}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008577
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008578#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008579/* parse the "ciphersuites" bind keyword */
8580static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8581{
8582 if (!*args[cur_arg + 1]) {
8583 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
8584 return ERR_ALERT | ERR_FATAL;
8585 }
8586
8587 free(conf->ciphersuites);
8588 conf->ciphersuites = strdup(args[cur_arg + 1]);
8589 return 0;
8590}
8591static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8592{
8593 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
8594}
8595#endif
8596
Willy Tarreaubbc91962019-10-16 16:42:19 +02008597/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008598static 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 +02008599{
Willy Tarreau38011032013-08-13 16:59:39 +02008600 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02008601
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008602 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008603 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008604 return ERR_ALERT | ERR_FATAL;
8605 }
8606
Willy Tarreauef934602016-12-22 23:12:01 +01008607 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
8608 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02008609 memprintf(err, "'%s' : path too long", args[cur_arg]);
8610 return ERR_ALERT | ERR_FATAL;
8611 }
Willy Tarreauef934602016-12-22 23:12:01 +01008612 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02008613 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008614 }
8615
Willy Tarreaubbc91962019-10-16 16:42:19 +02008616 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008617}
8618
Willy Tarreaubbc91962019-10-16 16:42:19 +02008619/* 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 +01008620static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8621{
Willy Tarreaubbc91962019-10-16 16:42:19 +02008622 int err_code;
8623
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008624 if (!*args[cur_arg + 1]) {
8625 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
8626 return ERR_ALERT | ERR_FATAL;
8627 }
8628
Willy Tarreaubbc91962019-10-16 16:42:19 +02008629 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err);
8630 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02008631 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008632
Willy Tarreaubbc91962019-10-16 16:42:19 +02008633 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008634}
8635
Emeric Brunfb510ea2012-10-05 12:00:26 +02008636/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008637static 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 +02008638{
Emeric Brun051cdab2012-10-02 19:25:50 +02008639#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01008640 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02008641 return ERR_ALERT | ERR_FATAL;
8642#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02008643 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008644 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008645 return ERR_ALERT | ERR_FATAL;
8646 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008647
Willy Tarreauef934602016-12-22 23:12:01 +01008648 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8649 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008650 else
8651 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008652
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01008653 if (!ssl_store_load_locations_file(conf->crl_file)) {
8654 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
8655 return ERR_ALERT | ERR_FATAL;
8656 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008657 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02008658#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02008659}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008660static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8661{
8662 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
8663}
Emeric Brun2b58d042012-09-20 17:10:03 +02008664
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008665/* parse the "curves" bind keyword keyword */
8666static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8667{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01008668#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008669 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008670 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008671 return ERR_ALERT | ERR_FATAL;
8672 }
8673 conf->curves = strdup(args[cur_arg + 1]);
8674 return 0;
8675#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008676 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008677 return ERR_ALERT | ERR_FATAL;
8678#endif
8679}
8680static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8681{
8682 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
8683}
8684
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008685/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008686static 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 +02008687{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008688#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01008689 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008690 return ERR_ALERT | ERR_FATAL;
8691#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01008692 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 +02008693 return ERR_ALERT | ERR_FATAL;
8694#else
8695 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008696 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008697 return ERR_ALERT | ERR_FATAL;
8698 }
8699
8700 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008701
8702 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02008703#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008704}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008705static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8706{
8707 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
8708}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008709
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008710/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02008711static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8712{
8713 int code;
8714 char *p = args[cur_arg + 1];
8715 unsigned long long *ignerr = &conf->crt_ignerr;
8716
8717 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008718 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008719 return ERR_ALERT | ERR_FATAL;
8720 }
8721
8722 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
8723 ignerr = &conf->ca_ignerr;
8724
8725 if (strcmp(p, "all") == 0) {
8726 *ignerr = ~0ULL;
8727 return 0;
8728 }
8729
8730 while (p) {
8731 code = atoi(p);
8732 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008733 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
8734 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008735 return ERR_ALERT | ERR_FATAL;
8736 }
8737 *ignerr |= 1ULL << code;
8738 p = strchr(p, ',');
8739 if (p)
8740 p++;
8741 }
8742
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008743 return 0;
8744}
8745
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008746/* parse tls_method_options "no-xxx" and "force-xxx" */
8747static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008748{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008749 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008750 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008751 p = strchr(arg, '-');
8752 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008753 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008754 p++;
8755 if (!strcmp(p, "sslv3"))
8756 v = CONF_SSLV3;
8757 else if (!strcmp(p, "tlsv10"))
8758 v = CONF_TLSV10;
8759 else if (!strcmp(p, "tlsv11"))
8760 v = CONF_TLSV11;
8761 else if (!strcmp(p, "tlsv12"))
8762 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008763 else if (!strcmp(p, "tlsv13"))
8764 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008765 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008766 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008767 if (!strncmp(arg, "no-", 3))
8768 methods->flags |= methodVersions[v].flag;
8769 else if (!strncmp(arg, "force-", 6))
8770 methods->min = methods->max = v;
8771 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008772 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008773 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008774 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01008775 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008776 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008777}
8778
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008779static 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 +02008780{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008781 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008782}
8783
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008784static 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 +02008785{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008786 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8787}
8788
8789/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8790static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8791{
8792 uint16_t i, v = 0;
8793 char *argv = args[cur_arg + 1];
8794 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008795 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008796 return ERR_ALERT | ERR_FATAL;
8797 }
8798 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8799 if (!strcmp(argv, methodVersions[i].name))
8800 v = i;
8801 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008802 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008803 return ERR_ALERT | ERR_FATAL;
8804 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008805 if (!strcmp("ssl-min-ver", args[cur_arg]))
8806 methods->min = v;
8807 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8808 methods->max = v;
8809 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01008810 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008811 return ERR_ALERT | ERR_FATAL;
8812 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008813 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008814}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008815
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008816static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8817{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008818#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008819 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 +02008820#endif
8821 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8822}
8823
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008824static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8825{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008826 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008827}
8828
8829static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8830{
8831 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8832}
8833
Emeric Brun2d0c4822012-10-02 13:45:20 +02008834/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008835static 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 +02008836{
Emeric Brun89675492012-10-05 13:48:26 +02008837 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008838 return 0;
8839}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008840
Olivier Houchardc2aae742017-09-22 18:26:28 +02008841/* parse the "allow-0rtt" bind keyword */
8842static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8843{
8844 conf->early_data = 1;
8845 return 0;
8846}
8847
8848static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8849{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008850 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008851 return 0;
8852}
8853
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008854/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008855static 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 +02008856{
Bernard Spil13c53f82018-02-15 13:34:58 +01008857#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008858 char *p1, *p2;
8859
8860 if (!*args[cur_arg + 1]) {
8861 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8862 return ERR_ALERT | ERR_FATAL;
8863 }
8864
8865 free(conf->npn_str);
8866
Willy Tarreau3724da12016-02-12 17:11:12 +01008867 /* the NPN string is built as a suite of (<len> <name>)*,
8868 * so we reuse each comma to store the next <len> and need
8869 * one more for the end of the string.
8870 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008871 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008872 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008873 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8874
8875 /* replace commas with the name length */
8876 p1 = conf->npn_str;
8877 p2 = p1 + 1;
8878 while (1) {
8879 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8880 if (!p2)
8881 p2 = p1 + 1 + strlen(p1 + 1);
8882
8883 if (p2 - (p1 + 1) > 255) {
8884 *p2 = '\0';
8885 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8886 return ERR_ALERT | ERR_FATAL;
8887 }
8888
8889 *p1 = p2 - (p1 + 1);
8890 p1 = p2;
8891
8892 if (!*p2)
8893 break;
8894
8895 *(p2++) = '\0';
8896 }
8897 return 0;
8898#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008899 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008900 return ERR_ALERT | ERR_FATAL;
8901#endif
8902}
8903
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008904static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8905{
8906 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8907}
8908
Willy Tarreauab861d32013-04-02 02:30:41 +02008909/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008910static 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 +02008911{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008912#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008913 char *p1, *p2;
8914
8915 if (!*args[cur_arg + 1]) {
8916 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
8917 return ERR_ALERT | ERR_FATAL;
8918 }
8919
8920 free(conf->alpn_str);
8921
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008922 /* the ALPN string is built as a suite of (<len> <name>)*,
8923 * so we reuse each comma to store the next <len> and need
8924 * one more for the end of the string.
8925 */
Willy Tarreauab861d32013-04-02 02:30:41 +02008926 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008927 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02008928 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
8929
8930 /* replace commas with the name length */
8931 p1 = conf->alpn_str;
8932 p2 = p1 + 1;
8933 while (1) {
8934 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
8935 if (!p2)
8936 p2 = p1 + 1 + strlen(p1 + 1);
8937
8938 if (p2 - (p1 + 1) > 255) {
8939 *p2 = '\0';
8940 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8941 return ERR_ALERT | ERR_FATAL;
8942 }
8943
8944 *p1 = p2 - (p1 + 1);
8945 p1 = p2;
8946
8947 if (!*p2)
8948 break;
8949
8950 *(p2++) = '\0';
8951 }
8952 return 0;
8953#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008954 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02008955 return ERR_ALERT | ERR_FATAL;
8956#endif
8957}
8958
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008959static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8960{
8961 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8962}
8963
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008964/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008965static 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 +02008966{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01008967 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02008968 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02008969
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008970 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
8971 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008972#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008973 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
8974 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
8975#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008976 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008977 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
8978 if (!conf->ssl_conf.ssl_methods.min)
8979 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
8980 if (!conf->ssl_conf.ssl_methods.max)
8981 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02008982
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008983 return 0;
8984}
8985
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008986/* parse the "prefer-client-ciphers" bind keyword */
8987static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8988{
8989 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
8990 return 0;
8991}
8992
Christopher Faulet31af49d2015-06-09 17:29:50 +02008993/* parse the "generate-certificates" bind keyword */
8994static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8995{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01008996#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02008997 conf->generate_certs = 1;
8998#else
8999 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9000 err && *err ? *err : "");
9001#endif
9002 return 0;
9003}
9004
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009005/* parse the "strict-sni" bind keyword */
9006static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9007{
9008 conf->strict_sni = 1;
9009 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009010}
9011
9012/* parse the "tls-ticket-keys" bind keyword */
9013static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9014{
9015#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009016 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009017 int i = 0;
9018 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009019 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009020
9021 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009022 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009023 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009024 }
9025
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009026 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009027 if (keys_ref) {
9028 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009029 conf->keys_ref = keys_ref;
9030 return 0;
9031 }
9032
Christopher Faulete566f3d2019-10-21 09:55:49 +02009033 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009034 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009035 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009036 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009037 }
9038
Emeric Brun9e754772019-01-10 17:51:55 +01009039 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009040 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009041 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009042 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009043 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009044
9045 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009046 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009047 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009048 }
9049
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009050 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009051 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009052 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009053 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009054 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009055
Emeric Brun9e754772019-01-10 17:51:55 +01009056 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009057 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9058 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009059 int dec_size;
9060
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009061 /* Strip newline characters from the end */
9062 if(thisline[len - 1] == '\n')
9063 thisline[--len] = 0;
9064
9065 if(thisline[len - 1] == '\r')
9066 thisline[--len] = 0;
9067
Emeric Brun9e754772019-01-10 17:51:55 +01009068 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9069 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009070 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009071 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009072 }
Emeric Brun9e754772019-01-10 17:51:55 +01009073 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9074 keys_ref->key_size_bits = 128;
9075 }
9076 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9077 keys_ref->key_size_bits = 256;
9078 }
9079 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9080 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9081 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009082 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009083 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009084 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009085 i++;
9086 }
9087
9088 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009089 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 +02009090 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009091 }
9092
9093 fclose(f);
9094
9095 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009096 i -= 2;
9097 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009098 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009099 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009100 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009101 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009102
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009103 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9104
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009105 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009106
9107 fail:
9108 if (f)
9109 fclose(f);
9110 if (keys_ref) {
9111 free(keys_ref->filename);
9112 free(keys_ref->tlskeys);
9113 free(keys_ref);
9114 }
9115 return ERR_ALERT | ERR_FATAL;
9116
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009117#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009118 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009119 return ERR_ALERT | ERR_FATAL;
9120#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009121}
9122
Emeric Brund94b3fe2012-09-20 18:23:56 +02009123/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009124static 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 +02009125{
9126 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009127 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009128 return ERR_ALERT | ERR_FATAL;
9129 }
9130
9131 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009132 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009133 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009134 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009135 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009136 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009137 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009138 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9139 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009140 return ERR_ALERT | ERR_FATAL;
9141 }
9142
9143 return 0;
9144}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009145static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9146{
9147 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9148}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009149
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009150/* parse the "no-ca-names" bind keyword */
9151static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9152{
9153 conf->no_ca_names = 1;
9154 return 0;
9155}
9156static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9157{
9158 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9159}
9160
Willy Tarreau92faadf2012-10-10 23:04:25 +02009161/************** "server" keywords ****************/
9162
Olivier Houchardc7566002018-11-20 23:33:50 +01009163/* parse the "npn" bind keyword */
9164static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9165{
9166#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9167 char *p1, *p2;
9168
9169 if (!*args[*cur_arg + 1]) {
9170 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9171 return ERR_ALERT | ERR_FATAL;
9172 }
9173
9174 free(newsrv->ssl_ctx.npn_str);
9175
9176 /* the NPN string is built as a suite of (<len> <name>)*,
9177 * so we reuse each comma to store the next <len> and need
9178 * one more for the end of the string.
9179 */
9180 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9181 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9182 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9183 newsrv->ssl_ctx.npn_len);
9184
9185 /* replace commas with the name length */
9186 p1 = newsrv->ssl_ctx.npn_str;
9187 p2 = p1 + 1;
9188 while (1) {
9189 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9190 newsrv->ssl_ctx.npn_len - (p1 + 1));
9191 if (!p2)
9192 p2 = p1 + 1 + strlen(p1 + 1);
9193
9194 if (p2 - (p1 + 1) > 255) {
9195 *p2 = '\0';
9196 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9197 return ERR_ALERT | ERR_FATAL;
9198 }
9199
9200 *p1 = p2 - (p1 + 1);
9201 p1 = p2;
9202
9203 if (!*p2)
9204 break;
9205
9206 *(p2++) = '\0';
9207 }
9208 return 0;
9209#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009210 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009211 return ERR_ALERT | ERR_FATAL;
9212#endif
9213}
9214
Olivier Houchard92150142018-12-21 19:47:01 +01009215/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009216static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9217{
9218#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9219 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009220 char **alpn_str;
9221 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009222
Olivier Houchard92150142018-12-21 19:47:01 +01009223 if (*args[*cur_arg] == 'c') {
9224 alpn_str = &newsrv->check.alpn_str;
9225 alpn_len = &newsrv->check.alpn_len;
9226 } else {
9227 alpn_str = &newsrv->ssl_ctx.alpn_str;
9228 alpn_len = &newsrv->ssl_ctx.alpn_len;
9229
9230 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009231 if (!*args[*cur_arg + 1]) {
9232 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9233 return ERR_ALERT | ERR_FATAL;
9234 }
9235
Olivier Houchard92150142018-12-21 19:47:01 +01009236 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009237
9238 /* the ALPN string is built as a suite of (<len> <name>)*,
9239 * so we reuse each comma to store the next <len> and need
9240 * one more for the end of the string.
9241 */
Olivier Houchard92150142018-12-21 19:47:01 +01009242 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9243 *alpn_str = calloc(1, *alpn_len + 1);
9244 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009245
9246 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009247 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009248 p2 = p1 + 1;
9249 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009250 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009251 if (!p2)
9252 p2 = p1 + 1 + strlen(p1 + 1);
9253
9254 if (p2 - (p1 + 1) > 255) {
9255 *p2 = '\0';
9256 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9257 return ERR_ALERT | ERR_FATAL;
9258 }
9259
9260 *p1 = p2 - (p1 + 1);
9261 p1 = p2;
9262
9263 if (!*p2)
9264 break;
9265
9266 *(p2++) = '\0';
9267 }
9268 return 0;
9269#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009270 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009271 return ERR_ALERT | ERR_FATAL;
9272#endif
9273}
9274
Emeric Brunef42d922012-10-11 16:11:36 +02009275/* parse the "ca-file" server keyword */
9276static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9277{
9278 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009279 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009280 return ERR_ALERT | ERR_FATAL;
9281 }
9282
Willy Tarreauef934602016-12-22 23:12:01 +01009283 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9284 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009285 else
9286 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9287
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009288 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9289 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9290 return ERR_ALERT | ERR_FATAL;
9291 }
Emeric Brunef42d922012-10-11 16:11:36 +02009292 return 0;
9293}
9294
Olivier Houchard9130a962017-10-17 17:33:43 +02009295/* parse the "check-sni" server keyword */
9296static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9297{
9298 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009299 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009300 return ERR_ALERT | ERR_FATAL;
9301 }
9302
9303 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9304 if (!newsrv->check.sni) {
9305 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9306 return ERR_ALERT | ERR_FATAL;
9307 }
9308 return 0;
9309
9310}
9311
Willy Tarreau92faadf2012-10-10 23:04:25 +02009312/* parse the "check-ssl" server keyword */
9313static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9314{
9315 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009316 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9317 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009318#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009319 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9320 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9321#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009322 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009323 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9324 if (!newsrv->ssl_ctx.methods.min)
9325 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9326 if (!newsrv->ssl_ctx.methods.max)
9327 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9328
Willy Tarreau92faadf2012-10-10 23:04:25 +02009329 return 0;
9330}
9331
9332/* parse the "ciphers" server keyword */
9333static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9334{
9335 if (!*args[*cur_arg + 1]) {
9336 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9337 return ERR_ALERT | ERR_FATAL;
9338 }
9339
9340 free(newsrv->ssl_ctx.ciphers);
9341 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9342 return 0;
9343}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009344
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009345#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009346/* parse the "ciphersuites" server keyword */
9347static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9348{
9349 if (!*args[*cur_arg + 1]) {
9350 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9351 return ERR_ALERT | ERR_FATAL;
9352 }
9353
9354 free(newsrv->ssl_ctx.ciphersuites);
9355 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9356 return 0;
9357}
9358#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009359
Emeric Brunef42d922012-10-11 16:11:36 +02009360/* parse the "crl-file" server keyword */
9361static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9362{
9363#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009364 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009365 return ERR_ALERT | ERR_FATAL;
9366#else
9367 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009368 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009369 return ERR_ALERT | ERR_FATAL;
9370 }
9371
Willy Tarreauef934602016-12-22 23:12:01 +01009372 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9373 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009374 else
9375 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9376
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009377 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9378 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9379 return ERR_ALERT | ERR_FATAL;
9380 }
Emeric Brunef42d922012-10-11 16:11:36 +02009381 return 0;
9382#endif
9383}
9384
Emeric Bruna7aa3092012-10-26 12:58:00 +02009385/* parse the "crt" server keyword */
9386static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9387{
9388 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009389 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009390 return ERR_ALERT | ERR_FATAL;
9391 }
9392
Willy Tarreauef934602016-12-22 23:12:01 +01009393 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009394 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009395 else
9396 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9397
9398 return 0;
9399}
Emeric Brunef42d922012-10-11 16:11:36 +02009400
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009401/* parse the "no-check-ssl" server keyword */
9402static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9403{
9404 newsrv->check.use_ssl = 0;
9405 free(newsrv->ssl_ctx.ciphers);
9406 newsrv->ssl_ctx.ciphers = NULL;
9407 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9408 return 0;
9409}
9410
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009411/* parse the "no-send-proxy-v2-ssl" server keyword */
9412static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9413{
9414 newsrv->pp_opts &= ~SRV_PP_V2;
9415 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9416 return 0;
9417}
9418
9419/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9420static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9421{
9422 newsrv->pp_opts &= ~SRV_PP_V2;
9423 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9424 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9425 return 0;
9426}
9427
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009428/* parse the "no-ssl" server keyword */
9429static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9430{
9431 newsrv->use_ssl = 0;
9432 free(newsrv->ssl_ctx.ciphers);
9433 newsrv->ssl_ctx.ciphers = NULL;
9434 return 0;
9435}
9436
Olivier Houchard522eea72017-11-03 16:27:47 +01009437/* parse the "allow-0rtt" server keyword */
9438static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9439{
9440 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9441 return 0;
9442}
9443
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009444/* parse the "no-ssl-reuse" server keyword */
9445static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9446{
9447 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9448 return 0;
9449}
9450
Emeric Brunf9c5c472012-10-11 15:28:34 +02009451/* parse the "no-tls-tickets" server keyword */
9452static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9453{
9454 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9455 return 0;
9456}
David Safb76832014-05-08 23:42:08 -04009457/* parse the "send-proxy-v2-ssl" server keyword */
9458static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9459{
9460 newsrv->pp_opts |= SRV_PP_V2;
9461 newsrv->pp_opts |= SRV_PP_V2_SSL;
9462 return 0;
9463}
9464
9465/* parse the "send-proxy-v2-ssl-cn" server keyword */
9466static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9467{
9468 newsrv->pp_opts |= SRV_PP_V2;
9469 newsrv->pp_opts |= SRV_PP_V2_SSL;
9470 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9471 return 0;
9472}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009473
Willy Tarreau732eac42015-07-09 11:40:25 +02009474/* parse the "sni" server keyword */
9475static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9476{
9477#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9478 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9479 return ERR_ALERT | ERR_FATAL;
9480#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009481 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009482
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009483 arg = args[*cur_arg + 1];
9484 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009485 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9486 return ERR_ALERT | ERR_FATAL;
9487 }
9488
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009489 free(newsrv->sni_expr);
9490 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02009491
Willy Tarreau732eac42015-07-09 11:40:25 +02009492 return 0;
9493#endif
9494}
9495
Willy Tarreau92faadf2012-10-10 23:04:25 +02009496/* parse the "ssl" server keyword */
9497static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9498{
9499 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009500 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9501 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009502#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009503 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9504 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9505#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009506 return 0;
9507}
9508
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009509/* parse the "ssl-reuse" server keyword */
9510static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9511{
9512 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
9513 return 0;
9514}
9515
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009516/* parse the "tls-tickets" server keyword */
9517static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9518{
9519 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
9520 return 0;
9521}
9522
Emeric Brunef42d922012-10-11 16:11:36 +02009523/* parse the "verify" server keyword */
9524static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9525{
9526 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009527 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009528 return ERR_ALERT | ERR_FATAL;
9529 }
9530
9531 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009532 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02009533 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009534 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02009535 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009536 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
9537 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009538 return ERR_ALERT | ERR_FATAL;
9539 }
9540
Evan Broderbe554312013-06-27 00:05:25 -07009541 return 0;
9542}
9543
9544/* parse the "verifyhost" server keyword */
9545static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9546{
9547 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009548 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -07009549 return ERR_ALERT | ERR_FATAL;
9550 }
9551
Frédéric Lécaille273f3212017-03-13 15:52:01 +01009552 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07009553 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
9554
Emeric Brunef42d922012-10-11 16:11:36 +02009555 return 0;
9556}
9557
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009558/* parse the "ssl-default-bind-options" keyword in global section */
9559static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
9560 struct proxy *defpx, const char *file, int line,
9561 char **err) {
9562 int i = 1;
9563
9564 if (*(args[i]) == 0) {
9565 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9566 return -1;
9567 }
9568 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009569 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009570 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009571 else if (!strcmp(args[i], "prefer-client-ciphers"))
9572 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009573 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9574 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
9575 i++;
9576 else {
9577 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9578 return -1;
9579 }
9580 }
9581 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009582 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9583 return -1;
9584 }
9585 i++;
9586 }
9587 return 0;
9588}
9589
9590/* parse the "ssl-default-server-options" keyword in global section */
9591static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
9592 struct proxy *defpx, const char *file, int line,
9593 char **err) {
9594 int i = 1;
9595
9596 if (*(args[i]) == 0) {
9597 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9598 return -1;
9599 }
9600 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009601 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009602 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009603 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9604 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
9605 i++;
9606 else {
9607 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9608 return -1;
9609 }
9610 }
9611 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009612 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9613 return -1;
9614 }
9615 i++;
9616 }
9617 return 0;
9618}
9619
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009620/* parse the "ca-base" / "crt-base" keywords in global section.
9621 * Returns <0 on alert, >0 on warning, 0 on success.
9622 */
9623static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
9624 struct proxy *defpx, const char *file, int line,
9625 char **err)
9626{
9627 char **target;
9628
Willy Tarreauef934602016-12-22 23:12:01 +01009629 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009630
9631 if (too_many_args(1, args, err, NULL))
9632 return -1;
9633
9634 if (*target) {
9635 memprintf(err, "'%s' already specified.", args[0]);
9636 return -1;
9637 }
9638
9639 if (*(args[1]) == 0) {
9640 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
9641 return -1;
9642 }
9643 *target = strdup(args[1]);
9644 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009645}
9646
9647/* parse the "ssl-mode-async" keyword in global section.
9648 * Returns <0 on alert, >0 on warning, 0 on success.
9649 */
9650static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
9651 struct proxy *defpx, const char *file, int line,
9652 char **err)
9653{
Willy Tarreau5db847a2019-05-09 14:13:35 +02009654#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009655 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01009656 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009657 return 0;
9658#else
9659 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
9660 return -1;
9661#endif
9662}
9663
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009664#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009665static int ssl_check_async_engine_count(void) {
9666 int err_code = 0;
9667
Emeric Brun3854e012017-05-17 20:42:48 +02009668 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009669 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009670 err_code = ERR_ABORT;
9671 }
9672 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009673}
9674
Grant Zhang872f9c22017-01-21 01:10:18 +00009675/* parse the "ssl-engine" keyword in global section.
9676 * Returns <0 on alert, >0 on warning, 0 on success.
9677 */
9678static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9679 struct proxy *defpx, const char *file, int line,
9680 char **err)
9681{
9682 char *algo;
9683 int ret = -1;
9684
9685 if (*(args[1]) == 0) {
9686 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9687 return ret;
9688 }
9689
9690 if (*(args[2]) == 0) {
9691 /* if no list of algorithms is given, it defaults to ALL */
9692 algo = strdup("ALL");
9693 goto add_engine;
9694 }
9695
9696 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
9697 if (strcmp(args[2], "algo") != 0) {
9698 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
9699 return ret;
9700 }
9701
9702 if (*(args[3]) == 0) {
9703 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
9704 return ret;
9705 }
9706 algo = strdup(args[3]);
9707
9708add_engine:
9709 if (ssl_init_single_engine(args[1], algo)==0) {
9710 openssl_engines_initialized++;
9711 ret = 0;
9712 }
9713 free(algo);
9714 return ret;
9715}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009716#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00009717
Willy Tarreauf22e9682016-12-21 23:23:19 +01009718/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
9719 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9720 */
9721static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
9722 struct proxy *defpx, const char *file, int line,
9723 char **err)
9724{
9725 char **target;
9726
Willy Tarreauef934602016-12-22 23:12:01 +01009727 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01009728
9729 if (too_many_args(1, args, err, NULL))
9730 return -1;
9731
9732 if (*(args[1]) == 0) {
9733 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9734 return -1;
9735 }
9736
9737 free(*target);
9738 *target = strdup(args[1]);
9739 return 0;
9740}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009741
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009742#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009743/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9744 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9745 */
9746static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9747 struct proxy *defpx, const char *file, int line,
9748 char **err)
9749{
9750 char **target;
9751
9752 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9753
9754 if (too_many_args(1, args, err, NULL))
9755 return -1;
9756
9757 if (*(args[1]) == 0) {
9758 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9759 return -1;
9760 }
9761
9762 free(*target);
9763 *target = strdup(args[1]);
9764 return 0;
9765}
9766#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009767
Willy Tarreau9ceda382016-12-21 23:13:03 +01009768/* parse various global tune.ssl settings consisting in positive integers.
9769 * Returns <0 on alert, >0 on warning, 0 on success.
9770 */
9771static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9772 struct proxy *defpx, const char *file, int line,
9773 char **err)
9774{
9775 int *target;
9776
9777 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9778 target = &global.tune.sslcachesize;
9779 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009780 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009781 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009782 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009783 else if (strcmp(args[0], "maxsslconn") == 0)
9784 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009785 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9786 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009787 else {
9788 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9789 return -1;
9790 }
9791
9792 if (too_many_args(1, args, err, NULL))
9793 return -1;
9794
9795 if (*(args[1]) == 0) {
9796 memprintf(err, "'%s' expects an integer argument.", args[0]);
9797 return -1;
9798 }
9799
9800 *target = atoi(args[1]);
9801 if (*target < 0) {
9802 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9803 return -1;
9804 }
9805 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009806}
9807
9808static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9809 struct proxy *defpx, const char *file, int line,
9810 char **err)
9811{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009812 int ret;
9813
9814 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9815 if (ret != 0)
9816 return ret;
9817
Willy Tarreaubafbe012017-11-24 17:34:44 +01009818 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009819 memprintf(err, "'%s' is already configured.", args[0]);
9820 return -1;
9821 }
9822
Willy Tarreaubafbe012017-11-24 17:34:44 +01009823 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9824 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009825 memprintf(err, "Out of memory error.");
9826 return -1;
9827 }
9828 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009829}
9830
9831/* parse "ssl.force-private-cache".
9832 * Returns <0 on alert, >0 on warning, 0 on success.
9833 */
9834static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9835 struct proxy *defpx, const char *file, int line,
9836 char **err)
9837{
9838 if (too_many_args(0, args, err, NULL))
9839 return -1;
9840
Willy Tarreauef934602016-12-22 23:12:01 +01009841 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009842 return 0;
9843}
9844
9845/* parse "ssl.lifetime".
9846 * Returns <0 on alert, >0 on warning, 0 on success.
9847 */
9848static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9849 struct proxy *defpx, const char *file, int line,
9850 char **err)
9851{
9852 const char *res;
9853
9854 if (too_many_args(1, args, err, NULL))
9855 return -1;
9856
9857 if (*(args[1]) == 0) {
9858 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9859 return -1;
9860 }
9861
Willy Tarreauef934602016-12-22 23:12:01 +01009862 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009863 if (res == PARSE_TIME_OVER) {
9864 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9865 args[1], args[0]);
9866 return -1;
9867 }
9868 else if (res == PARSE_TIME_UNDER) {
9869 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9870 args[1], args[0]);
9871 return -1;
9872 }
9873 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009874 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9875 return -1;
9876 }
9877 return 0;
9878}
9879
9880#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009881/* parse "ssl-dh-param-file".
9882 * Returns <0 on alert, >0 on warning, 0 on success.
9883 */
9884static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9885 struct proxy *defpx, const char *file, int line,
9886 char **err)
9887{
9888 if (too_many_args(1, args, err, NULL))
9889 return -1;
9890
9891 if (*(args[1]) == 0) {
9892 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9893 return -1;
9894 }
9895
9896 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9897 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9898 return -1;
9899 }
9900 return 0;
9901}
9902
Willy Tarreau9ceda382016-12-21 23:13:03 +01009903/* parse "ssl.default-dh-param".
9904 * Returns <0 on alert, >0 on warning, 0 on success.
9905 */
9906static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9907 struct proxy *defpx, const char *file, int line,
9908 char **err)
9909{
9910 if (too_many_args(1, args, err, NULL))
9911 return -1;
9912
9913 if (*(args[1]) == 0) {
9914 memprintf(err, "'%s' expects an integer argument.", args[0]);
9915 return -1;
9916 }
9917
Willy Tarreauef934602016-12-22 23:12:01 +01009918 global_ssl.default_dh_param = atoi(args[1]);
9919 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009920 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9921 return -1;
9922 }
9923 return 0;
9924}
9925#endif
9926
9927
William Lallemand32af2032016-10-29 18:09:35 +02009928/* This function is used with TLS ticket keys management. It permits to browse
9929 * each reference. The variable <getnext> must contain the current node,
9930 * <end> point to the root node.
9931 */
9932#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9933static inline
9934struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
9935{
9936 struct tls_keys_ref *ref = getnext;
9937
9938 while (1) {
9939
9940 /* Get next list entry. */
9941 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
9942
9943 /* If the entry is the last of the list, return NULL. */
9944 if (&ref->list == end)
9945 return NULL;
9946
9947 return ref;
9948 }
9949}
9950
9951static inline
9952struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
9953{
9954 int id;
9955 char *error;
9956
9957 /* If the reference starts by a '#', this is numeric id. */
9958 if (reference[0] == '#') {
9959 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
9960 id = strtol(reference + 1, &error, 10);
9961 if (*error != '\0')
9962 return NULL;
9963
9964 /* Perform the unique id lookup. */
9965 return tlskeys_ref_lookupid(id);
9966 }
9967
9968 /* Perform the string lookup. */
9969 return tlskeys_ref_lookup(reference);
9970}
9971#endif
9972
9973
9974#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9975
9976static int cli_io_handler_tlskeys_files(struct appctx *appctx);
9977
9978static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
9979 return cli_io_handler_tlskeys_files(appctx);
9980}
9981
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009982/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
9983 * (next index to be dumped), and cli.p0 (next key reference).
9984 */
William Lallemand32af2032016-10-29 18:09:35 +02009985static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
9986
9987 struct stream_interface *si = appctx->owner;
9988
9989 switch (appctx->st2) {
9990 case STAT_ST_INIT:
9991 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08009992 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +02009993 * later and restart at the state "STAT_ST_INIT".
9994 */
9995 chunk_reset(&trash);
9996
9997 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
9998 chunk_appendf(&trash, "# id secret\n");
9999 else
10000 chunk_appendf(&trash, "# id (file)\n");
10001
Willy Tarreau06d80a92017-10-19 14:32:15 +020010002 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010003 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010004 return 0;
10005 }
10006
William Lallemand32af2032016-10-29 18:09:35 +020010007 /* Now, we start the browsing of the references lists.
10008 * Note that the following call to LIST_ELEM return bad pointer. The only
10009 * available field of this pointer is <list>. It is used with the function
10010 * tlskeys_list_get_next() for retruning the first available entry
10011 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010012 if (appctx->ctx.cli.p0 == NULL) {
10013 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10014 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010015 }
10016
10017 appctx->st2 = STAT_ST_LIST;
10018 /* fall through */
10019
10020 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010021 while (appctx->ctx.cli.p0) {
10022 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010023
10024 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010025 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010026 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010027
10028 if (appctx->ctx.cli.i1 == 0)
10029 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10030
William Lallemand32af2032016-10-29 18:09:35 +020010031 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010032 int head;
10033
10034 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10035 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010036 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010037 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010038
10039 chunk_reset(t2);
10040 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010041 if (ref->key_size_bits == 128) {
10042 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10043 sizeof(struct tls_sess_key_128),
10044 t2->area, t2->size);
10045 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10046 t2->area);
10047 }
10048 else if (ref->key_size_bits == 256) {
10049 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10050 sizeof(struct tls_sess_key_256),
10051 t2->area, t2->size);
10052 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10053 t2->area);
10054 }
10055 else {
10056 /* This case should never happen */
10057 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10058 }
William Lallemand32af2032016-10-29 18:09:35 +020010059
Willy Tarreau06d80a92017-10-19 14:32:15 +020010060 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010061 /* let's try again later from this stream. We add ourselves into
10062 * this stream's users so that it can remove us upon termination.
10063 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010064 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010065 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010066 return 0;
10067 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010068 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010069 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010070 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010071 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010072 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010073 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010074 /* let's try again later from this stream. We add ourselves into
10075 * this stream's users so that it can remove us upon termination.
10076 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010077 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010078 return 0;
10079 }
10080
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010081 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010082 break;
10083
10084 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010085 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010086 }
10087
10088 appctx->st2 = STAT_ST_FIN;
10089 /* fall through */
10090
10091 default:
10092 appctx->st2 = STAT_ST_FIN;
10093 return 1;
10094 }
10095 return 0;
10096}
10097
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010098/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010099static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010100{
William Lallemand32af2032016-10-29 18:09:35 +020010101 /* no parameter, shows only file list */
10102 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010103 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010104 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010105 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010106 }
10107
10108 if (args[2][0] == '*') {
10109 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010110 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010111 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010112 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010113 if (!appctx->ctx.cli.p0)
10114 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010115 }
William Lallemand32af2032016-10-29 18:09:35 +020010116 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010117 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010118}
10119
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010120static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010121{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010122 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010123 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010124
William Lallemand32af2032016-10-29 18:09:35 +020010125 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010126 if (!*args[3] || !*args[4])
10127 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 +020010128
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010129 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010130 if (!ref)
10131 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010132
Willy Tarreau1c913e42018-08-22 05:26:57 +020010133 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010134 if (ret < 0)
10135 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010136
Willy Tarreau1c913e42018-08-22 05:26:57 +020010137 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010138 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10139 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010140
Willy Tarreau9d008692019-08-09 11:21:01 +020010141 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010142}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010143#endif
William Lallemand32af2032016-10-29 18:09:35 +020010144
William Lallemand44b35322019-10-17 16:28:40 +020010145
10146/* Type of SSL payloads that can be updated over the CLI */
10147
10148enum {
10149 CERT_TYPE_PEM = 0,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010150#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010151 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010152#endif
William Lallemand44b35322019-10-17 16:28:40 +020010153 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010154#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010155 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010156#endif
William Lallemand44b35322019-10-17 16:28:40 +020010157 CERT_TYPE_MAX,
10158};
10159
10160struct {
10161 const char *ext;
10162 int type;
10163 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
10164 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020010165} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020010166 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand541a5342019-10-23 14:11:54 +020010167#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010168 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010169#endif
10170#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010171 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010172#endif
William Lallemand44b35322019-10-17 16:28:40 +020010173 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020010174 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020010175};
10176
William Lallemand430413e2019-10-28 14:30:47 +010010177/* states of the CLI IO handler for 'set ssl cert' */
10178enum {
10179 SETCERT_ST_INIT = 0,
10180 SETCERT_ST_GEN,
10181 SETCERT_ST_INSERT,
10182 SETCERT_ST_FIN,
10183};
William Lallemand8f840d72019-10-23 10:53:05 +020010184
William Lallemandd4f946c2019-12-05 10:26:40 +010010185/* release function of the `show ssl cert' command */
10186static void cli_release_show_cert(struct appctx *appctx)
10187{
10188 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10189}
10190
10191/* IO handler of "show ssl cert <filename>" */
10192static int cli_io_handler_show_cert(struct appctx *appctx)
10193{
10194 struct buffer *trash = alloc_trash_chunk();
10195 struct ebmb_node *node;
10196 struct stream_interface *si = appctx->owner;
10197 struct ckch_store *ckchs;
10198 int n;
10199
10200 if (trash == NULL)
10201 return 1;
10202
10203 if (!appctx->ctx.ssl.old_ckchs) {
10204 if (ckchs_transaction.old_ckchs) {
10205 ckchs = ckchs_transaction.old_ckchs;
10206 chunk_appendf(trash, "# transaction\n");
10207 if (!ckchs->multi) {
10208 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010209#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010210 } else {
10211 chunk_appendf(trash, "*%s:", ckchs->path);
10212 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10213 if (ckchs->ckch[n].cert)
10214 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10215 }
10216 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010217#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010218 }
10219 }
10220 }
10221
10222 if (!appctx->ctx.cli.p0) {
10223 chunk_appendf(trash, "# filename\n");
10224 node = ebmb_first(&ckchs_tree);
10225 } else {
10226 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
10227 }
10228 while (node) {
10229 ckchs = ebmb_entry(node, struct ckch_store, node);
10230 if (!ckchs->multi) {
10231 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010232#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010233 } else {
10234 chunk_appendf(trash, "%s:", ckchs->path);
10235 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10236 if (ckchs->ckch[n].cert)
10237 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10238 }
10239 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010240#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010241 }
10242
10243 node = ebmb_next(node);
10244 if (ci_putchk(si_ic(si), trash) == -1) {
10245 si_rx_room_blk(si);
10246 goto yield;
10247 }
10248 }
10249
10250 appctx->ctx.cli.p0 = NULL;
10251 free_trash_chunk(trash);
10252 return 1;
10253yield:
10254
10255 free_trash_chunk(trash);
10256 appctx->ctx.cli.p0 = ckchs;
10257 return 0; /* should come back */
10258}
10259
10260/* IO handler of the details "show ssl cert <filename>" */
10261static int cli_io_handler_show_cert_detail(struct appctx *appctx)
10262{
10263 struct stream_interface *si = appctx->owner;
10264 struct ckch_store *ckchs = appctx->ctx.cli.p0;
10265 struct buffer *out = alloc_trash_chunk();
10266 struct buffer *tmp = alloc_trash_chunk();
10267 X509_NAME *name = NULL;
10268 int write = -1;
10269 BIO *bio = NULL;
10270
10271 if (!tmp || !out)
10272 goto end;
10273
10274 if (!ckchs->multi) {
10275 chunk_appendf(out, "Filename: ");
10276 if (ckchs == ckchs_transaction.new_ckchs)
10277 chunk_appendf(out, "*");
10278 chunk_appendf(out, "%s\n", ckchs->path);
10279 chunk_appendf(out, "Serial: ");
10280 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
10281 goto end;
10282 dump_binary(out, tmp->area, tmp->data);
10283 chunk_appendf(out, "\n");
10284
10285 chunk_appendf(out, "notBefore: ");
10286 chunk_reset(tmp);
10287 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10288 goto end;
10289 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
10290 goto end;
10291 write = BIO_read(bio, tmp->area, tmp->size-1);
10292 tmp->area[write] = '\0';
10293 BIO_free(bio);
10294 chunk_appendf(out, "%s\n", tmp->area);
10295
10296 chunk_appendf(out, "notAfter: ");
10297 chunk_reset(tmp);
10298 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10299 goto end;
10300 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
10301 goto end;
10302 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
10303 goto end;
10304 tmp->area[write] = '\0';
10305 BIO_free(bio);
10306 chunk_appendf(out, "%s\n", tmp->area);
10307
10308
10309 chunk_appendf(out, "Issuer: ");
10310 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
10311 goto end;
10312 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10313 goto end;
10314 *(tmp->area + tmp->data) = '\0';
10315 chunk_appendf(out, "%s\n", tmp->area);
10316
10317 chunk_appendf(out, "Subject: ");
10318 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
10319 goto end;
10320 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10321 goto end;
10322 *(tmp->area + tmp->data) = '\0';
10323 chunk_appendf(out, "%s\n", tmp->area);
10324
10325
10326#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10327 chunk_appendf(out, "Subject Alternative Name: ");
10328 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
10329 goto end;
10330 *(out->area + out->data) = '\0';
10331 chunk_appendf(out, "\n");
10332#endif
10333 chunk_reset(tmp);
10334 chunk_appendf(out, "Algorithm: ");
10335 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
10336 goto end;
10337 chunk_appendf(out, "%s\n", tmp->area);
10338
10339 chunk_reset(tmp);
10340 chunk_appendf(out, "SHA1 FingerPrint: ");
10341 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area,
10342 (unsigned int *)&tmp->data) == 0)
10343 goto end;
10344 dump_binary(out, tmp->area, tmp->data);
10345 chunk_appendf(out, "\n");
10346 }
10347
10348 if (ci_putchk(si_ic(si), out) == -1) {
10349 si_rx_room_blk(si);
10350 goto yield;
10351 }
10352
10353end:
10354 free_trash_chunk(tmp);
10355 free_trash_chunk(out);
10356 return 1;
10357yield:
10358 free_trash_chunk(tmp);
10359 free_trash_chunk(out);
10360 return 0; /* should come back */
10361}
10362
10363/* parsing function for 'show ssl cert [certfile]' */
10364static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
10365{
10366 struct ckch_store *ckchs;
10367
10368 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
10369 return cli_err(appctx, "Can't allocate memory!\n");
10370
10371 /* The operations on the CKCH architecture are locked so we can
10372 * manipulate ckch_store and ckch_inst */
10373 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10374 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
10375
10376 /* check if there is a certificate to lookup */
10377 if (*args[3]) {
10378 if (*args[3] == '*') {
10379 if (!ckchs_transaction.new_ckchs)
10380 goto error;
10381
10382 ckchs = ckchs_transaction.new_ckchs;
10383
10384 if (strcmp(args[3] + 1, ckchs->path))
10385 goto error;
10386
10387 } else {
10388 if ((ckchs = ckchs_lookup(args[3])) == NULL)
10389 goto error;
10390
10391 }
10392
10393 if (ckchs->multi)
10394 goto error;
10395
10396 appctx->ctx.cli.p0 = ckchs;
10397 /* use the IO handler that shows details */
10398 appctx->io_handler = cli_io_handler_show_cert_detail;
10399 }
10400
10401 return 0;
10402
10403error:
10404 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10405 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
10406}
10407
William Lallemand430413e2019-10-28 14:30:47 +010010408/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010409static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010410{
10411 struct ckch_store *new_ckchs;
10412 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010413
William Lallemand430413e2019-10-28 14:30:47 +010010414 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020010415
William Lallemand430413e2019-10-28 14:30:47 +010010416 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020010417 /* 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 +010010418 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010419
William Lallemandbeea2a42019-10-30 17:45:33 +010010420 if (!new_ckchs)
10421 return;
William Lallemand8f840d72019-10-23 10:53:05 +020010422
William Lallemandbeea2a42019-10-30 17:45:33 +010010423 /* if the allocation failed, we need to free everything from the temporary list */
10424 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10425 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010426
William Lallemandbeea2a42019-10-30 17:45:33 +010010427 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10428 if (sc0->order == 0) /* we only free if it's the first inserted */
10429 SSL_CTX_free(sc0->ctx);
10430 LIST_DEL(&sc0->by_ckch_inst);
10431 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020010432 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010433 LIST_DEL(&ckchi->by_ckchs);
10434 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020010435 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010436 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010437 }
10438}
10439
10440
10441/*
10442 * This function tries to create the new ckch_inst and their SNIs
10443 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010444static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010445{
10446 struct stream_interface *si = appctx->owner;
10447 int y = 0;
10448 char *err = NULL;
10449 int errcode = 0;
10450 struct ckch_store *old_ckchs, *new_ckchs = NULL;
10451 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010452 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010453 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010454
William Lallemand33cc76f2019-10-31 11:43:45 +010010455 if (trash == NULL)
10456 goto error;
10457
William Lallemand8f840d72019-10-23 10:53:05 +020010458 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
10459 goto error;
10460
William Lallemand430413e2019-10-28 14:30:47 +010010461 while (1) {
10462 switch (appctx->st2) {
10463 case SETCERT_ST_INIT:
10464 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010465 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010010466 if (ci_putchk(si_ic(si), trash) == -1) {
10467 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020010468 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010010469 }
10470 appctx->st2 = SETCERT_ST_GEN;
10471 /* fallthrough */
10472 case SETCERT_ST_GEN:
10473 /*
10474 * This state generates the ckch instances with their
10475 * sni_ctxs and SSL_CTX.
10476 *
William Lallemand430413e2019-10-28 14:30:47 +010010477 * Since the SSL_CTX generation can be CPU consumer, we
10478 * yield every 10 instances.
10479 */
William Lallemand8f840d72019-10-23 10:53:05 +020010480
William Lallemandbeea2a42019-10-30 17:45:33 +010010481 old_ckchs = appctx->ctx.ssl.old_ckchs;
10482 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010483
William Lallemandbeea2a42019-10-30 17:45:33 +010010484 if (!new_ckchs)
10485 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020010486
William Lallemandbeea2a42019-10-30 17:45:33 +010010487 /* get the next ckchi to regenerate */
10488 ckchi = appctx->ctx.ssl.next_ckchi;
10489 /* we didn't start yet, set it to the first elem */
10490 if (ckchi == NULL)
10491 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010492
William Lallemandbeea2a42019-10-30 17:45:33 +010010493 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
10494 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
10495 struct ckch_inst *new_inst;
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010496 int verify = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020010497
William Lallemandbeea2a42019-10-30 17:45:33 +010010498 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
10499 if (y >= 10) {
10500 /* save the next ckchi to compute */
10501 appctx->ctx.ssl.next_ckchi = ckchi;
10502 goto yield;
10503 }
William Lallemand8f840d72019-10-23 10:53:05 +020010504
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010505 /* prevent ssl_sock_prepare_ctx() to do file access which is only for verify (crl/ca file) */
10506 verify = (ckchi->ssl_conf && ckchi->ssl_conf->verify) ? ckchi->ssl_conf->verify : ckchi->bind_conf->ssl_conf.verify;
10507 if (verify & SSL_VERIFY_PEER) {
10508 memprintf(&err, "%sCan't commit a certificate which use the 'verify' bind SSL option [%s:%d]\n", err ? err : "", ckchi->bind_conf->file, ckchi->bind_conf->line);
10509 errcode |= ERR_FATAL | ERR_ABORT;
10510 goto error;
10511 }
10512
10513
William Lallemandbeea2a42019-10-30 17:45:33 +010010514 if (new_ckchs->multi)
10515 errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
10516 else
10517 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 +020010518
William Lallemandbeea2a42019-10-30 17:45:33 +010010519 if (errcode & ERR_CODE)
10520 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020010521
William Lallemand21724f02019-11-04 17:56:13 +010010522 /* if the previous ckchi was used as the default */
10523 if (ckchi->is_default)
10524 new_inst->is_default = 1;
10525
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010526 /* we need to initialize the SSL_CTX generated */
10527 /* TODO: the prepare_ctx function need to be reworked to be safer there */
10528 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10529 if (!sc0->order) { /* we initiliazed only the first SSL_CTX because it's the same in the other sni_ctx's */
10530 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
10531 if (errcode & ERR_CODE)
10532 goto error;
10533 }
10534 }
10535
10536
William Lallemandbeea2a42019-10-30 17:45:33 +010010537 /* display one dot per new instance */
10538 chunk_appendf(trash, ".");
10539 /* link the new ckch_inst to the duplicate */
10540 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
10541 y++;
10542 }
William Lallemand430413e2019-10-28 14:30:47 +010010543 appctx->st2 = SETCERT_ST_INSERT;
10544 /* fallthrough */
10545 case SETCERT_ST_INSERT:
10546 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020010547
William Lallemandbeea2a42019-10-30 17:45:33 +010010548 old_ckchs = appctx->ctx.ssl.old_ckchs;
10549 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010550
William Lallemandbeea2a42019-10-30 17:45:33 +010010551 if (!new_ckchs)
10552 continue;
William Lallemand430413e2019-10-28 14:30:47 +010010553
William Lallemand21724f02019-11-04 17:56:13 +010010554 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010010555 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10556 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10557 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
10558 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10559 }
William Lallemand8f840d72019-10-23 10:53:05 +020010560
William Lallemandbeea2a42019-10-30 17:45:33 +010010561 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
10562 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemand430413e2019-10-28 14:30:47 +010010563
William Lallemandbeea2a42019-10-30 17:45:33 +010010564 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10565 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10566 ebmb_delete(&sc0->name);
10567 LIST_DEL(&sc0->by_ckch_inst);
10568 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010010569 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010570 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10571 LIST_DEL(&ckchi->by_ckchs);
10572 free(ckchi);
10573 }
William Lallemand8f840d72019-10-23 10:53:05 +020010574
William Lallemandbeea2a42019-10-30 17:45:33 +010010575 /* Replace the old ckchs by the new one */
10576 ebmb_delete(&old_ckchs->node);
10577 ckchs_free(old_ckchs);
10578 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010010579 appctx->st2 = SETCERT_ST_FIN;
10580 /* fallthrough */
10581 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010582 /* we achieved the transaction, we can set everything to NULL */
10583 free(ckchs_transaction.path);
10584 ckchs_transaction.path = NULL;
10585 ckchs_transaction.new_ckchs = NULL;
10586 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010010587 goto end;
10588 }
William Lallemand8f840d72019-10-23 10:53:05 +020010589 }
William Lallemand430413e2019-10-28 14:30:47 +010010590end:
William Lallemand8f840d72019-10-23 10:53:05 +020010591
William Lallemanded442432019-11-21 16:41:07 +010010592 chunk_appendf(trash, "\n");
10593 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010010594 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010010595 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010010596 if (ci_putchk(si_ic(si), trash) == -1)
10597 si_rx_room_blk(si);
10598 free_trash_chunk(trash);
10599 /* success: call the release function and don't come back */
10600 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010601yield:
10602 /* store the state */
10603 if (ci_putchk(si_ic(si), trash) == -1)
10604 si_rx_room_blk(si);
10605 free_trash_chunk(trash);
10606 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020010607 return 0; /* should come back */
10608
10609error:
10610 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010010611 if (trash) {
10612 chunk_appendf(trash, "\n%sFailed!\n", err);
10613 if (ci_putchk(si_ic(si), trash) == -1)
10614 si_rx_room_blk(si);
10615 free_trash_chunk(trash);
10616 }
William Lallemand430413e2019-10-28 14:30:47 +010010617 /* error: call the release function and don't come back */
10618 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010619}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010620
10621/*
10622 * Parsing function of 'commit ssl cert'
10623 */
10624static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
10625{
10626 char *err = NULL;
10627
William Lallemand230662a2019-12-03 13:32:54 +010010628 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10629 return 1;
10630
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010631 if (!*args[3])
10632 return cli_err(appctx, "'commit ssl cert expects a filename\n");
10633
10634 /* The operations on the CKCH architecture are locked so we can
10635 * manipulate ckch_store and ckch_inst */
10636 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10637 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
10638
10639 if (!ckchs_transaction.path) {
10640 memprintf(&err, "No ongoing transaction! !\n");
10641 goto error;
10642 }
10643
10644 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10645 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
10646 goto error;
10647 }
10648
10649 /* init the appctx structure */
10650 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010651 appctx->ctx.ssl.next_ckchi = NULL;
10652 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
10653 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
10654
10655 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
10656 return 0;
10657
10658error:
10659
10660 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10661 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
10662
10663 return cli_dynerr(appctx, err);
10664}
10665
10666
William Lallemand8f840d72019-10-23 10:53:05 +020010667/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010668 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020010669 */
William Lallemand150bfa82019-09-19 17:12:49 +020010670static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
10671{
William Lallemand0c3b7d92019-10-18 11:27:07 +020010672 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020010673 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020010674 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020010675 int i;
William Lallemand849eed62019-10-17 16:23:50 +020010676 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010677 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020010678 char *end;
10679 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010680 struct cert_key_and_chain *ckch;
10681 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020010682
William Lallemand230662a2019-12-03 13:32:54 +010010683 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10684 return 1;
10685
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010686 if ((buf = alloc_trash_chunk()) == NULL)
10687 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020010688
10689 if (!*args[3] || !payload)
10690 return cli_err(appctx, "'set ssl cert expects a filename and a certificat as a payload\n");
10691
10692 /* The operations on the CKCH architecture are locked so we can
10693 * manipulate ckch_store and ckch_inst */
10694 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10695 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
10696
William Lallemand8f840d72019-10-23 10:53:05 +020010697 if (!chunk_strcpy(buf, args[3])) {
10698 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10699 errcode |= ERR_ALERT | ERR_FATAL;
10700 goto end;
10701 }
10702
William Lallemand44b35322019-10-17 16:28:40 +020010703 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020010704 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020010705 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020010706 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
10707 *end = '\0';
10708 type = cert_exts[i].type;
10709 break;
10710 }
10711 }
10712
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010713 appctx->ctx.ssl.old_ckchs = NULL;
10714 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020010715
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010716 /* if there is an ongoing transaction */
10717 if (ckchs_transaction.path) {
10718 /* 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 +020010719#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010720 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010721 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020010722 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020010723
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010724 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020010725 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020010726 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010727 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020010728 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10729 bundle = j; /* keep the type of certificate so we insert it at the right place */
10730 *end = '\0'; /* it's a bundle let's end the string*/
10731 break;
10732 }
William Lallemand150bfa82019-09-19 17:12:49 +020010733 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010734 if (bundle < 0) {
10735 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);
10736 errcode |= ERR_ALERT | ERR_FATAL;
10737 goto end;
10738 }
10739 }
10740#endif
10741
10742 /* if there is an ongoing transaction, check if this is the same file */
10743 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
10744 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
10745 errcode |= ERR_ALERT | ERR_FATAL;
10746 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010747 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010748
10749 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
10750
10751 } else {
10752 struct ckch_store *find_ckchs[2] = { NULL, NULL };
10753
10754 /* lookup for the certificate in the tree:
10755 * check if this is used as a bundle AND as a unique certificate */
10756 for (i = 0; i < 2; i++) {
10757
10758 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
10759 /* only the bundle name is in the tree and you should
10760 * never update a bundle name, only a filename */
10761 if (bundle < 0 && find_ckchs[i]->multi) {
10762 /* we tried to look for a non-bundle and we found a bundle */
10763 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
10764 err ? err : "", args[3], args[3]);
10765 errcode |= ERR_ALERT | ERR_FATAL;
10766 goto end;
10767 }
William Lallemand3246d942019-11-04 14:02:11 +010010768 /* If we want a bundle but this is not a bundle
10769 * example: When you try to update <file>.rsa, but
10770 * <file> is a regular file */
10771 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
10772 find_ckchs[i] = NULL;
10773 break;
10774 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010775 }
10776#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
10777 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010778 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010779 int j;
10780
10781 /* check if it was used in a bundle by removing the
10782 * .dsa/.rsa/.ecdsa at the end of the filename */
10783 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010784 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010785 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10786 bundle = j; /* keep the type of certificate so we insert it at the right place */
10787 *end = '\0'; /* it's a bundle let's end the string*/
10788 break;
10789 }
10790 }
William Lallemand37031b82019-11-04 13:38:53 +010010791 if (bundle < 0) /* we didn't find a bundle extension */
10792 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010793 }
William Lallemand963b2e72019-10-14 11:38:36 +020010794#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010795 /* bundles are not supported here, so we don't need to lookup again */
10796 break;
William Lallemand963b2e72019-10-14 11:38:36 +020010797#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010798 }
10799
10800 if (find_ckchs[0] && find_ckchs[1]) {
10801 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",
10802 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
10803 errcode |= ERR_ALERT | ERR_FATAL;
10804 goto end;
10805 }
10806
10807 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020010808 }
10809
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010810 if (!appctx->ctx.ssl.old_ckchs) {
10811 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020010812 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010813 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020010814 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010815 }
10816
William Lallemand8a7fdf02019-11-04 10:59:32 +010010817 if (!appctx->ctx.ssl.path) {
10818 /* this is a new transaction, set the path of the transaction */
10819 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
10820 if (!appctx->ctx.ssl.path) {
10821 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10822 errcode |= ERR_ALERT | ERR_FATAL;
10823 goto end;
10824 }
10825 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010826
10827 old_ckchs = appctx->ctx.ssl.old_ckchs;
10828
10829 /* TODO: handle filters */
10830 if (old_ckchs->filters) {
10831 memprintf(&err, "%sCertificates used in crt-list with filters are not supported!\n",
10832 err ? err : "");
10833 errcode |= ERR_ALERT | ERR_FATAL;
10834 goto end;
10835 }
10836
10837 /* duplicate the ckch store */
10838 new_ckchs = ckchs_dup(old_ckchs);
10839 if (!new_ckchs) {
10840 memprintf(&err, "%sCannot allocate memory!\n",
10841 err ? err : "");
10842 errcode |= ERR_ALERT | ERR_FATAL;
10843 goto end;
10844 }
10845
10846 if (!new_ckchs->multi)
10847 ckch = new_ckchs->ckch;
10848 else
10849 ckch = &new_ckchs->ckch[bundle];
10850
10851 /* appply the change on the duplicate */
10852 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
10853 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
10854 errcode |= ERR_ALERT | ERR_FATAL;
10855 goto end;
10856 }
10857
10858 appctx->ctx.ssl.new_ckchs = new_ckchs;
10859
10860 /* we succeed, we can save the ckchs in the transaction */
10861
10862 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010010863 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010864 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
10865 ckchs_transaction.path = appctx->ctx.ssl.path;
10866 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
10867 } else {
10868 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
10869
10870 }
10871
10872 /* free the previous ckchs if there was a transaction */
10873 ckchs_free(ckchs_transaction.new_ckchs);
10874
10875 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
10876
10877
William Lallemand8f840d72019-10-23 10:53:05 +020010878 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010879
William Lallemand8f840d72019-10-23 10:53:05 +020010880end:
10881 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020010882
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010883 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010884
10885 ckchs_free(appctx->ctx.ssl.new_ckchs);
10886 appctx->ctx.ssl.new_ckchs = NULL;
10887
10888 appctx->ctx.ssl.old_ckchs = NULL;
10889
10890 free(appctx->ctx.ssl.path);
10891 appctx->ctx.ssl.path = NULL;
10892
10893 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020010894 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010010895 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010896
10897 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10898 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010010899 }
William Lallemand8f840d72019-10-23 10:53:05 +020010900 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010901}
10902
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010903/* parsing function of 'abort ssl cert' */
10904static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
10905{
10906 char *err = NULL;
10907
William Lallemand230662a2019-12-03 13:32:54 +010010908 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10909 return 1;
10910
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010911 if (!*args[3])
10912 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
10913
10914 /* The operations on the CKCH architecture are locked so we can
10915 * manipulate ckch_store and ckch_inst */
10916 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10917 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
10918
10919 if (!ckchs_transaction.path) {
10920 memprintf(&err, "No ongoing transaction!\n");
10921 goto error;
10922 }
10923
10924 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10925 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
10926 goto error;
10927 }
10928
10929 /* Only free the ckchs there, because the SNI and instances were not generated yet */
10930 ckchs_free(ckchs_transaction.new_ckchs);
10931 ckchs_transaction.new_ckchs = NULL;
10932 ckchs_free(ckchs_transaction.old_ckchs);
10933 ckchs_transaction.old_ckchs = NULL;
10934 free(ckchs_transaction.path);
10935 ckchs_transaction.path = NULL;
10936
10937 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10938
10939 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
10940 return cli_dynmsg(appctx, LOG_NOTICE, err);
10941
10942error:
10943 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10944
10945 return cli_dynerr(appctx, err);
10946}
10947
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010948static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010949{
10950#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
10951 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010952 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010953
10954 if (!payload)
10955 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020010956
10957 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010958 if (!*payload)
10959 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010960
10961 /* remove \r and \n from the payload */
10962 for (i = 0, j = 0; payload[i]; i++) {
10963 if (payload[i] == '\r' || payload[i] == '\n')
10964 continue;
10965 payload[j++] = payload[i];
10966 }
10967 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010968
Willy Tarreau1c913e42018-08-22 05:26:57 +020010969 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010970 if (ret < 0)
10971 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010972
Willy Tarreau1c913e42018-08-22 05:26:57 +020010973 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020010974 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020010975 if (err)
10976 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
10977 else
10978 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010979 }
Willy Tarreau9d008692019-08-09 11:21:01 +020010980
10981 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010982#else
Willy Tarreau9d008692019-08-09 11:21:01 +020010983 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 +020010984#endif
10985
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010986}
10987
Willy Tarreau86a394e2019-05-09 14:15:32 +020010988#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010989static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
10990{
10991 switch (arg->type) {
10992 case ARGT_STR:
10993 smp->data.type = SMP_T_STR;
10994 smp->data.u.str = arg->data.str;
10995 return 1;
10996 case ARGT_VAR:
10997 if (!vars_get_by_desc(&arg->data.var, smp))
10998 return 0;
10999 if (!sample_casts[smp->data.type][SMP_T_STR])
11000 return 0;
11001 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
11002 return 0;
11003 return 1;
11004 default:
11005 return 0;
11006 }
11007}
11008
11009static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
11010 const char *file, int line, char **err)
11011{
11012 switch(args[0].data.sint) {
11013 case 128:
11014 case 192:
11015 case 256:
11016 break;
11017 default:
11018 memprintf(err, "key size must be 128, 192 or 256 (bits).");
11019 return 0;
11020 }
11021 /* Try to decode a variable. */
11022 vars_check_arg(&args[1], NULL);
11023 vars_check_arg(&args[2], NULL);
11024 vars_check_arg(&args[3], NULL);
11025 return 1;
11026}
11027
11028/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
11029static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
11030{
11031 struct sample nonce, key, aead_tag;
11032 struct buffer *smp_trash, *smp_trash_alloc;
11033 EVP_CIPHER_CTX *ctx;
11034 int dec_size, ret;
11035
11036 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
11037 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
11038 return 0;
11039
11040 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
11041 if (!sample_conv_var2smp_str(&arg_p[2], &key))
11042 return 0;
11043
11044 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
11045 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
11046 return 0;
11047
11048 smp_trash = get_trash_chunk();
11049 smp_trash_alloc = alloc_trash_chunk();
11050 if (!smp_trash_alloc)
11051 return 0;
11052
11053 ctx = EVP_CIPHER_CTX_new();
11054
11055 if (!ctx)
11056 goto err;
11057
11058 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
11059 if (dec_size < 0)
11060 goto err;
11061 smp_trash->data = dec_size;
11062
11063 /* Set cipher type and mode */
11064 switch(arg_p[0].data.sint) {
11065 case 128:
11066 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
11067 break;
11068 case 192:
11069 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
11070 break;
11071 case 256:
11072 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
11073 break;
11074 }
11075
11076 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
11077
11078 /* Initialise IV */
11079 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
11080 goto err;
11081
11082 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
11083 if (dec_size < 0)
11084 goto err;
11085 smp_trash->data = dec_size;
11086
11087 /* Initialise key */
11088 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
11089 goto err;
11090
11091 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
11092 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
11093 goto err;
11094
11095 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
11096 if (dec_size < 0)
11097 goto err;
11098 smp_trash_alloc->data = dec_size;
11099 dec_size = smp_trash->data;
11100
11101 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
11102 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
11103
11104 if (ret <= 0)
11105 goto err;
11106
11107 smp->data.u.str.data = dec_size + smp_trash->data;
11108 smp->data.u.str.area = smp_trash->area;
11109 smp->data.type = SMP_T_BIN;
11110 smp->flags &= ~SMP_F_CONST;
11111 free_trash_chunk(smp_trash_alloc);
11112 return 1;
11113
11114err:
11115 free_trash_chunk(smp_trash_alloc);
11116 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020011117}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011118# endif
William Lallemand32af2032016-10-29 18:09:35 +020011119
Elliot Otchet71f82972020-01-15 08:12:14 -050011120/* Argument validation functions */
11121
11122/* This function is used to validate the arguments passed to any "x_dn" ssl
11123 * keywords. These keywords support specifying a third parameter that must be
11124 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
11125 */
11126int val_dnfmt(struct arg *arg, char **err_msg)
11127{
11128 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
11129 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
11130 return 0;
11131 }
11132 return 1;
11133}
11134
William Lallemand32af2032016-10-29 18:09:35 +020011135/* register cli keywords */
11136static struct cli_kw_list cli_kws = {{ },{
11137#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11138 { { "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 +020011139 { { "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 +020011140#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010011141 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011142 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
11143 { { "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 +010011144 { { "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 +010011145 { { "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 +020011146 { { NULL }, NULL, NULL, NULL }
11147}};
11148
Willy Tarreau0108d902018-11-25 19:14:37 +010011149INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020011150
Willy Tarreau7875d092012-09-10 08:20:03 +020011151/* Note: must not be declared <const> as its list will be overwritten.
11152 * Please take care of keeping this list alphabetically sorted.
11153 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011154static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020011155 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011156 { "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 +010011157#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010011158 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011159#endif
Emeric Brun645ae792014-04-30 14:21:06 +020011160 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011161#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
11162 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
11163#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010011164 { "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 +020011165 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020011166 { "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 +020011167 { "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 +020011168#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020011169 { "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 -040011170#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011171#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011172 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11173 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040011174 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11175#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011176 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11177 { "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 +010011178 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011179 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011180 { "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 +020011181 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11182 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11183 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11184 { "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 -050011185 { "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 +020011186 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11187 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011188 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011189 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11190 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010011191 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011192 { "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 +020011193 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11194 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11195 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11196 { "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 -050011197 { "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 +020011198 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020011199 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011200 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011201 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011202 { "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 +010011203 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011204 { "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 +020011205 { "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 +010011206 { "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 +020011207 { "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 +010011208#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011209 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020011210#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010011211#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011212 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020011213#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011214 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011215#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020011216 { "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 -040011217#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011218 { "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 +020011219#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011220 { "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 -040011221#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011222#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011223 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11224 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040011225 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11226#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040011227#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011228 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040011229#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011230 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11231 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11232 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11233 { "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 +020011234 { NULL, NULL, 0, 0, 0 },
11235}};
11236
Willy Tarreau0108d902018-11-25 19:14:37 +010011237INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
11238
Willy Tarreau7875d092012-09-10 08:20:03 +020011239/* Note: must not be declared <const> as its list will be overwritten.
11240 * Please take care of keeping this list alphabetically sorted.
11241 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011242static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011243 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
11244 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011245 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020011246}};
11247
Willy Tarreau0108d902018-11-25 19:14:37 +010011248INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
11249
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011250/* Note: must not be declared <const> as its list will be overwritten.
11251 * Please take care of keeping this list alphabetically sorted, doing so helps
11252 * all code contributors.
11253 * Optional keywords are also declared with a NULL ->parse() function so that
11254 * the config parser can report an appropriate error when a known keyword was
11255 * not enabled.
11256 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011257static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011258 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011259 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11260 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11261 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011262#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011263 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11264#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011265 { "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 +010011266 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011267 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011268 { "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 +010011269 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020011270 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
11271 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011272 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
11273 { NULL, NULL, 0 },
11274};
11275
Willy Tarreau0108d902018-11-25 19:14:37 +010011276/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
11277
Willy Tarreau51fb7652012-09-18 18:24:39 +020011278static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011279 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011280 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11281 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11282 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
11283 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
11284 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
11285 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011286#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011287 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11288#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011289 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
11290 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
11291 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
11292 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
11293 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
11294 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
11295 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
11296 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
11297 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
11298 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011299 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011300 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011301 { "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 +020011302 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
11303 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
11304 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
11305 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011306 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011307 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
11308 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011309 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
11310 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011311 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
11312 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
11313 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
11314 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
11315 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011316 { NULL, NULL, 0 },
11317}};
Emeric Brun46591952012-05-18 15:47:34 +020011318
Willy Tarreau0108d902018-11-25 19:14:37 +010011319INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
11320
Willy Tarreau92faadf2012-10-10 23:04:25 +020011321/* Note: must not be declared <const> as its list will be overwritten.
11322 * Please take care of keeping this list alphabetically sorted, doing so helps
11323 * all code contributors.
11324 * Optional keywords are also declared with a NULL ->parse() function so that
11325 * the config parser can report an appropriate error when a known keyword was
11326 * not enabled.
11327 */
11328static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010011329 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010011330 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011331 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010011332 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020011333 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011334 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
11335 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011336#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011337 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
11338#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011339 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
11340 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
11341 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
11342 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
11343 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
11344 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
11345 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
11346 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
11347 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
11348 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
11349 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
11350 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
11351 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
11352 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
11353 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
11354 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
11355 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
11356 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010011357 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011358 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
11359 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
11360 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
11361 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
11362 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
11363 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
11364 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
11365 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
11366 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
11367 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020011368 { NULL, NULL, 0, 0 },
11369}};
11370
Willy Tarreau0108d902018-11-25 19:14:37 +010011371INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
11372
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011373static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010011374 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
11375 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010011376 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011377 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
11378 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010011379#ifndef OPENSSL_NO_DH
11380 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
11381#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011382 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011383#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011384 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011385#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010011386 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
11387#ifndef OPENSSL_NO_DH
11388 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
11389#endif
11390 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
11391 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
11392 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
11393 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011394 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010011395 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
11396 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011397#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011398 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
11399 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
11400#endif
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011401 { 0, NULL, NULL },
11402}};
11403
Willy Tarreau0108d902018-11-25 19:14:37 +010011404INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
11405
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011406/* Note: must not be declared <const> as its list will be overwritten */
11407static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020011408#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011409 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
11410#endif
11411 { NULL, NULL, 0, 0, 0 },
11412}};
11413
11414INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
11415
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020011416/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010011417static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020011418 .snd_buf = ssl_sock_from_buf,
11419 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010011420 .subscribe = ssl_subscribe,
11421 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020011422 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020011423 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020011424 .rcv_pipe = NULL,
11425 .snd_pipe = NULL,
11426 .shutr = NULL,
11427 .shutw = ssl_sock_shutw,
11428 .close = ssl_sock_close,
11429 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010011430 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010011431 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010011432 .prepare_srv = ssl_sock_prepare_srv_ctx,
11433 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010011434 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010011435 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020011436};
11437
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011438enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
11439 struct session *sess, struct stream *s, int flags)
11440{
11441 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011442 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011443
11444 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011445 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011446
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011447 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011448 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011449 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011450 s->req.flags |= CF_READ_NULL;
11451 return ACT_RET_YIELD;
11452 }
11453 }
11454 return (ACT_RET_CONT);
11455}
11456
11457static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
11458{
11459 rule->action_ptr = ssl_action_wait_for_hs;
11460
11461 return ACT_RET_PRS_OK;
11462}
11463
11464static struct action_kw_list http_req_actions = {ILH, {
11465 { "wait-for-handshake", ssl_parse_wait_for_hs },
11466 { /* END */ }
11467}};
11468
Willy Tarreau0108d902018-11-25 19:14:37 +010011469INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
11470
Willy Tarreau5db847a2019-05-09 14:13:35 +020011471#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011472
11473static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11474{
11475 if (ptr) {
11476 chunk_destroy(ptr);
11477 free(ptr);
11478 }
11479}
11480
11481#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011482static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11483{
Willy Tarreaubafbe012017-11-24 17:34:44 +010011484 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011485}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011486
Emeric Brun46591952012-05-18 15:47:34 +020011487__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020011488static void __ssl_sock_init(void)
11489{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011490#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011491 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011492 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011493#endif
Emeric Brun46591952012-05-18 15:47:34 +020011494
Willy Tarreauef934602016-12-22 23:12:01 +010011495 if (global_ssl.listen_default_ciphers)
11496 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
11497 if (global_ssl.connect_default_ciphers)
11498 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011499#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011500 if (global_ssl.listen_default_ciphersuites)
11501 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
11502 if (global_ssl.connect_default_ciphersuites)
11503 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
11504#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010011505
Willy Tarreau13e14102016-12-22 20:25:26 +010011506 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011507#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020011508 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080011509#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011510#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011511 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011512 n = sk_SSL_COMP_num(cm);
11513 while (n--) {
11514 (void) sk_SSL_COMP_pop(cm);
11515 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011516#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011517
Willy Tarreau5db847a2019-05-09 14:13:35 +020011518#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011519 ssl_locking_init();
11520#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020011521#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011522 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
11523#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020011524 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020011525 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 +020011526#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011527 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011528 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011529#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010011530#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11531 hap_register_post_check(tlskeys_finalize_config);
11532#endif
Willy Tarreau80713382018-11-26 10:19:54 +010011533
11534 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
11535 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
11536
11537#ifndef OPENSSL_NO_DH
11538 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
11539 hap_register_post_deinit(ssl_free_dh);
11540#endif
11541#ifndef OPENSSL_NO_ENGINE
11542 hap_register_post_deinit(ssl_free_engines);
11543#endif
11544 /* Load SSL string for the verbose & debug mode. */
11545 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020011546 ha_meth = BIO_meth_new(0x666, "ha methods");
11547 BIO_meth_set_write(ha_meth, ha_ssl_write);
11548 BIO_meth_set_read(ha_meth, ha_ssl_read);
11549 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
11550 BIO_meth_set_create(ha_meth, ha_ssl_new);
11551 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
11552 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
11553 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020011554
11555 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010011556}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010011557
Willy Tarreau80713382018-11-26 10:19:54 +010011558/* Compute and register the version string */
11559static void ssl_register_build_options()
11560{
11561 char *ptr = NULL;
11562 int i;
11563
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011564 memprintf(&ptr, "Built with OpenSSL version : "
11565#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011566 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011567#else /* OPENSSL_IS_BORINGSSL */
11568 OPENSSL_VERSION_TEXT
11569 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080011570 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020011571 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011572#endif
11573 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011574#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011575 "no (library version too old)"
11576#elif defined(OPENSSL_NO_TLSEXT)
11577 "no (disabled via OPENSSL_NO_TLSEXT)"
11578#else
11579 "yes"
11580#endif
11581 "", ptr);
11582
11583 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
11584#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11585 "yes"
11586#else
11587#ifdef OPENSSL_NO_TLSEXT
11588 "no (because of OPENSSL_NO_TLSEXT)"
11589#else
11590 "no (version might be too old, 0.9.8f min needed)"
11591#endif
11592#endif
11593 "", ptr);
11594
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020011595 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
11596 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
11597 if (methodVersions[i].option)
11598 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011599
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011600 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010011601}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011602
Willy Tarreau80713382018-11-26 10:19:54 +010011603INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020011604
Emeric Brun46591952012-05-18 15:47:34 +020011605
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011606#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011607void ssl_free_engines(void) {
11608 struct ssl_engine_list *wl, *wlb;
11609 /* free up engine list */
11610 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
11611 ENGINE_finish(wl->e);
11612 ENGINE_free(wl->e);
11613 LIST_DEL(&wl->list);
11614 free(wl);
11615 }
11616}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011617#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020011618
Remi Gacogned3a23c32015-05-28 16:39:47 +020011619#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000011620void ssl_free_dh(void) {
11621 if (local_dh_1024) {
11622 DH_free(local_dh_1024);
11623 local_dh_1024 = NULL;
11624 }
11625 if (local_dh_2048) {
11626 DH_free(local_dh_2048);
11627 local_dh_2048 = NULL;
11628 }
11629 if (local_dh_4096) {
11630 DH_free(local_dh_4096);
11631 local_dh_4096 = NULL;
11632 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020011633 if (global_dh) {
11634 DH_free(global_dh);
11635 global_dh = NULL;
11636 }
Grant Zhang872f9c22017-01-21 01:10:18 +000011637}
11638#endif
11639
11640__attribute__((destructor))
11641static void __ssl_sock_deinit(void)
11642{
11643#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011644 if (ssl_ctx_lru_tree) {
11645 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010011646 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020011647 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020011648#endif
11649
Willy Tarreau5db847a2019-05-09 14:13:35 +020011650#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011651 ERR_remove_state(0);
11652 ERR_free_strings();
11653
11654 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080011655#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020011656
Willy Tarreau5db847a2019-05-09 14:13:35 +020011657#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011658 CRYPTO_cleanup_all_ex_data();
11659#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020011660 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020011661}
11662
11663
Emeric Brun46591952012-05-18 15:47:34 +020011664/*
11665 * Local variables:
11666 * c-indent-level: 8
11667 * c-basic-offset: 8
11668 * End:
11669 */