blob: 1aac1caa44c0a6ca6a75ca62f4b59f2fcf5cf156 [file] [log] [blame]
yanbzhu08ce6ab2015-12-02 13:01:29 -05001
Emeric Brun46591952012-05-18 15:47:34 +02002/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003 * SSL/TLS transport layer over SOCK_STREAM sockets
Emeric Brun46591952012-05-18 15:47:34 +02004 *
5 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Willy Tarreau69845df2012-09-10 09:43:09 +020012 * Acknowledgement:
13 * We'd like to specially thank the Stud project authors for a very clean
14 * and well documented code which helped us understand how the OpenSSL API
15 * ought to be used in non-blocking mode. This is one difficult part which
16 * is not easy to get from the OpenSSL doc, and reading the Stud code made
17 * it much more obvious than the examples in the OpenSSL package. Keep up
18 * the good works, guys !
19 *
20 * Stud is an extremely efficient and scalable SSL/TLS proxy which combines
21 * particularly well with haproxy. For more info about this project, visit :
22 * https://github.com/bumptech/stud
23 *
Emeric Brun46591952012-05-18 15:47:34 +020024 */
25
Willy Tarreau8d164dc2019-05-10 09:35:00 +020026/* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */
Emeric Brun46591952012-05-18 15:47:34 +020027#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020028#include <ctype.h>
29#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020030#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020034#include <string.h>
35#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020036
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020040#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020041#include <netinet/tcp.h>
42
Christopher Faulet31af49d2015-06-09 17:29:50 +020043#include <import/lru.h>
44#include <import/xxhash.h>
45
Emeric Brun46591952012-05-18 15:47:34 +020046#include <common/buffer.h>
Willy Tarreau843b7cb2018-07-13 10:54:26 +020047#include <common/chunk.h>
Emeric Brun46591952012-05-18 15:47:34 +020048#include <common/compat.h>
49#include <common/config.h>
50#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020051#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010052#include <common/initcall.h>
Willy Tarreau55994562019-05-09 14:52:44 +020053#include <common/openssl-compat.h>
Emeric Brun46591952012-05-18 15:47:34 +020054#include <common/standard.h>
55#include <common/ticks.h>
56#include <common/time.h>
Emeric Brun2c86cbf2014-10-30 15:56:50 +010057#include <common/cfgparse.h>
Nenad Merdanovic05552d42015-02-27 19:56:49 +010058#include <common/base64.h>
Emeric Brun46591952012-05-18 15:47:34 +020059
Emeric Brunfc0421f2012-09-07 17:30:07 +020060#include <ebsttree.h>
61
William Lallemand32af2032016-10-29 18:09:35 +020062#include <types/applet.h>
63#include <types/cli.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020064#include <types/global.h>
65#include <types/ssl_sock.h>
William Lallemand32af2032016-10-29 18:09:35 +020066#include <types/stats.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020067
Willy Tarreau7875d092012-09-10 08:20:03 +020068#include <proto/acl.h>
69#include <proto/arg.h>
William Lallemand32af2032016-10-29 18:09:35 +020070#include <proto/channel.h>
Emeric Brun46591952012-05-18 15:47:34 +020071#include <proto/connection.h>
William Lallemand32af2032016-10-29 18:09:35 +020072#include <proto/cli.h>
Emeric Brun46591952012-05-18 15:47:34 +020073#include <proto/fd.h>
74#include <proto/freq_ctr.h>
75#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020076#include <proto/http_rules.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020077#include <proto/listener.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010078#include <proto/pattern.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020079#include <proto/proto_tcp.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020080#include <proto/http_ana.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020081#include <proto/server.h>
William Lallemand32af2032016-10-29 18:09:35 +020082#include <proto/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020083#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020084#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020085#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020086#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020087#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020088#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010089#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020090
Willy Tarreau9356dac2019-05-10 09:22:53 +020091/* ***** READ THIS before adding code here! *****
92 *
93 * Due to API incompatibilities between multiple OpenSSL versions and their
94 * derivatives, it's often tempting to add macros to (re-)define certain
95 * symbols. Please do not do this here, and do it in common/openssl-compat.h
96 * exclusively so that the whole code consistently uses the same macros.
97 *
98 * Whenever possible if a macro is missing in certain versions, it's better
99 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
100 */
101
Willy Tarreau518cedd2014-02-17 15:43:01 +0100102/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +0200103#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +0100104#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +0100105#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +0200106#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
107
Emeric Brunf282a812012-09-21 15:27:54 +0200108/* bits 0xFFFF0000 are reserved to store verify errors */
109
110/* Verify errors macros */
111#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
112#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
113#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
114
115#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
116#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
117#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +0200118
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200119/* ssl_methods flags for ssl options */
120#define MC_SSL_O_ALL 0x0000
121#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
122#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
123#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
124#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200125#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200126
127/* ssl_methods versions */
128enum {
129 CONF_TLSV_NONE = 0,
130 CONF_TLSV_MIN = 1,
131 CONF_SSLV3 = 1,
132 CONF_TLSV10 = 2,
133 CONF_TLSV11 = 3,
134 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200135 CONF_TLSV13 = 5,
136 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200137};
138
Emeric Brun850efd52014-01-29 12:24:34 +0100139/* server and bind verify method, it uses a global value as default */
140enum {
141 SSL_SOCK_VERIFY_DEFAULT = 0,
142 SSL_SOCK_VERIFY_REQUIRED = 1,
143 SSL_SOCK_VERIFY_OPTIONAL = 2,
144 SSL_SOCK_VERIFY_NONE = 3,
145};
146
William Lallemand3f85c9a2017-10-09 16:30:50 +0200147
Willy Tarreau71b734c2014-01-28 15:19:44 +0100148int sslconns = 0;
149int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100150static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100151int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200152
Willy Tarreauef934602016-12-22 23:12:01 +0100153static struct {
154 char *crt_base; /* base directory path for certificates */
155 char *ca_base; /* base directory path for CAs and CRLs */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000156 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100157
158 char *listen_default_ciphers;
159 char *connect_default_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200160#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200161 char *listen_default_ciphersuites;
162 char *connect_default_ciphersuites;
163#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100164 int listen_default_ssloptions;
165 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200166 struct tls_version_filter listen_default_sslmethods;
167 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100168
169 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
170 unsigned int life_time; /* SSL session lifetime in seconds */
171 unsigned int max_record; /* SSL max record size */
172 unsigned int default_dh_param; /* SSL maximum DH parameter size */
173 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100174 int capture_cipherlist; /* Size of the cipherlist buffer. */
Willy Tarreauef934602016-12-22 23:12:01 +0100175} global_ssl = {
176#ifdef LISTEN_DEFAULT_CIPHERS
177 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
178#endif
179#ifdef CONNECT_DEFAULT_CIPHERS
180 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
181#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200182#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200183#ifdef LISTEN_DEFAULT_CIPHERSUITES
184 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
185#endif
186#ifdef CONNECT_DEFAULT_CIPHERSUITES
187 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
188#endif
189#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100190 .listen_default_ssloptions = BC_SSL_O_NONE,
191 .connect_default_ssloptions = SRV_SSL_O_NONE,
192
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200193 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
194 .listen_default_sslmethods.min = CONF_TLSV_NONE,
195 .listen_default_sslmethods.max = CONF_TLSV_NONE,
196 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
197 .connect_default_sslmethods.min = CONF_TLSV_NONE,
198 .connect_default_sslmethods.max = CONF_TLSV_NONE,
199
Willy Tarreauef934602016-12-22 23:12:01 +0100200#ifdef DEFAULT_SSL_MAX_RECORD
201 .max_record = DEFAULT_SSL_MAX_RECORD,
202#endif
203 .default_dh_param = SSL_DEFAULT_DH_PARAM,
204 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100205 .capture_cipherlist = 0,
Willy Tarreauef934602016-12-22 23:12:01 +0100206};
207
Olivier Houcharda8955d52019-04-07 22:00:38 +0200208static BIO_METHOD *ha_meth;
209
Olivier Houchard66ab4982019-02-26 18:37:15 +0100210struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200211 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100212 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200213 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200214 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100215 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200216 struct wait_event wait_event;
Willy Tarreau113d52b2020-01-10 09:20:26 +0100217 struct wait_event *subs;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100218 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100219 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100220 int sent_early_data; /* Amount of early data we sent so far */
221
Olivier Houchard66ab4982019-02-26 18:37:15 +0100222};
223
224DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
225
Olivier Houchardea8dd942019-05-20 14:02:16 +0200226static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200227static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200228
Olivier Houcharda8955d52019-04-07 22:00:38 +0200229/* Methods to implement OpenSSL BIO */
230static int ha_ssl_write(BIO *h, const char *buf, int num)
231{
232 struct buffer tmpbuf;
233 struct ssl_sock_ctx *ctx;
234 int ret;
235
236 ctx = BIO_get_data(h);
237 tmpbuf.size = num;
238 tmpbuf.area = (void *)(uintptr_t)buf;
239 tmpbuf.data = num;
240 tmpbuf.head = 0;
241 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200242 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200243 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200244 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200245 } else if (ret == 0)
246 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200247 return ret;
248}
249
250static int ha_ssl_gets(BIO *h, char *buf, int size)
251{
252
253 return 0;
254}
255
256static int ha_ssl_puts(BIO *h, const char *str)
257{
258
259 return ha_ssl_write(h, str, strlen(str));
260}
261
262static int ha_ssl_read(BIO *h, char *buf, int size)
263{
264 struct buffer tmpbuf;
265 struct ssl_sock_ctx *ctx;
266 int ret;
267
268 ctx = BIO_get_data(h);
269 tmpbuf.size = size;
270 tmpbuf.area = buf;
271 tmpbuf.data = 0;
272 tmpbuf.head = 0;
273 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200274 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200275 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200276 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200277 } else if (ret == 0)
278 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200279
280 return ret;
281}
282
283static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
284{
285 int ret = 0;
286 switch (cmd) {
287 case BIO_CTRL_DUP:
288 case BIO_CTRL_FLUSH:
289 ret = 1;
290 break;
291 }
292 return ret;
293}
294
295static int ha_ssl_new(BIO *h)
296{
297 BIO_set_init(h, 1);
298 BIO_set_data(h, NULL);
299 BIO_clear_flags(h, ~0);
300 return 1;
301}
302
303static int ha_ssl_free(BIO *data)
304{
305
306 return 1;
307}
308
309
Willy Tarreau5db847a2019-05-09 14:13:35 +0200310#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100311
Emeric Brun821bb9b2017-06-15 16:37:39 +0200312static HA_RWLOCK_T *ssl_rwlocks;
313
314
315unsigned long ssl_id_function(void)
316{
317 return (unsigned long)tid;
318}
319
320void ssl_locking_function(int mode, int n, const char * file, int line)
321{
322 if (mode & CRYPTO_LOCK) {
323 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100324 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200325 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100326 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200327 }
328 else {
329 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100330 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200331 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100332 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200333 }
334}
335
336static int ssl_locking_init(void)
337{
338 int i;
339
340 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
341 if (!ssl_rwlocks)
342 return -1;
343
344 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100345 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200346
347 CRYPTO_set_id_callback(ssl_id_function);
348 CRYPTO_set_locking_callback(ssl_locking_function);
349
350 return 0;
351}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100352
Emeric Brun821bb9b2017-06-15 16:37:39 +0200353#endif
354
William Lallemand150bfa82019-09-19 17:12:49 +0200355__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200356
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100357/* Uncommitted CKCH transaction */
358
359static struct {
360 struct ckch_store *new_ckchs;
361 struct ckch_store *old_ckchs;
362 char *path;
363} ckchs_transaction;
364
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200365/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100366 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200367 */
368struct cafile_entry {
369 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200370 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200371 struct ebmb_node node;
372 char path[0];
373};
374
375static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
376
377static X509_STORE* ssl_store_get0_locations_file(char *path)
378{
379 struct ebmb_node *eb;
380
381 eb = ebst_lookup(&cafile_tree, path);
382 if (eb) {
383 struct cafile_entry *ca_e;
384 ca_e = ebmb_entry(eb, struct cafile_entry, node);
385 return ca_e->ca_store;
386 }
387 return NULL;
388}
389
390static int ssl_store_load_locations_file(char *path)
391{
392 if (ssl_store_get0_locations_file(path) == NULL) {
393 struct cafile_entry *ca_e;
394 X509_STORE *store = X509_STORE_new();
395 if (X509_STORE_load_locations(store, path, NULL)) {
396 int pathlen;
397 pathlen = strlen(path);
398 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
399 if (ca_e) {
400 memcpy(ca_e->path, path, pathlen + 1);
401 ca_e->ca_store = store;
402 ebst_insert(&cafile_tree, &ca_e->node);
403 return 1;
404 }
405 }
406 X509_STORE_free(store);
407 return 0;
408 }
409 return 1;
410}
411
412/* mimic what X509_STORE_load_locations do with store_ctx */
413static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
414{
415 X509_STORE *store;
416 store = ssl_store_get0_locations_file(path);
417 if (store_ctx && store) {
418 int i;
419 X509_OBJECT *obj;
420 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
421 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
422 obj = sk_X509_OBJECT_value(objs, i);
423 switch (X509_OBJECT_get_type(obj)) {
424 case X509_LU_X509:
425 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
426 break;
427 case X509_LU_CRL:
428 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
429 break;
430 default:
431 break;
432 }
433 }
434 return 1;
435 }
436 return 0;
437}
438
439/* SSL_CTX_load_verify_locations substitute, internaly call X509_STORE_load_locations */
440static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
441{
442 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
443 return ssl_set_cert_crl_file(store_ctx, path);
444}
445
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200446/*
447 Extract CA_list from CA_file already in tree.
448 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
449 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
450*/
451static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
452{
453 struct ebmb_node *eb;
454 struct cafile_entry *ca_e;
455
456 eb = ebst_lookup(&cafile_tree, path);
457 if (!eb)
458 return NULL;
459 ca_e = ebmb_entry(eb, struct cafile_entry, node);
460
461 if (ca_e->ca_list == NULL) {
462 int i;
463 unsigned long key;
464 struct eb_root ca_name_tree = EB_ROOT;
465 struct eb64_node *node, *back;
466 struct {
467 struct eb64_node node;
468 X509_NAME *xname;
469 } *ca_name;
470 STACK_OF(X509_OBJECT) *objs;
471 STACK_OF(X509_NAME) *skn;
472 X509 *x;
473 X509_NAME *xn;
474
475 skn = sk_X509_NAME_new_null();
476 /* take x509 from cafile_tree */
477 objs = X509_STORE_get0_objects(ca_e->ca_store);
478 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
479 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
480 if (!x)
481 continue;
482 xn = X509_get_subject_name(x);
483 if (!xn)
484 continue;
485 /* Check for duplicates. */
486 key = X509_NAME_hash(xn);
487 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
488 node && ca_name == NULL;
489 node = eb64_next(node)) {
490 ca_name = container_of(node, typeof(*ca_name), node);
491 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
492 ca_name = NULL;
493 }
494 /* find a duplicate */
495 if (ca_name)
496 continue;
497 ca_name = calloc(1, sizeof *ca_name);
498 xn = X509_NAME_dup(xn);
499 if (!ca_name ||
500 !xn ||
501 !sk_X509_NAME_push(skn, xn)) {
502 free(ca_name);
503 X509_NAME_free(xn);
504 sk_X509_NAME_pop_free(skn, X509_NAME_free);
505 sk_X509_NAME_free(skn);
506 skn = NULL;
507 break;
508 }
509 ca_name->node.key = key;
510 ca_name->xname = xn;
511 eb64_insert(&ca_name_tree, &ca_name->node);
512 }
513 ca_e->ca_list = skn;
514 /* remove temporary ca_name tree */
515 node = eb64_first(&ca_name_tree);
516 while (node) {
517 ca_name = container_of(node, typeof(*ca_name), node);
518 back = eb64_next(node);
519 eb64_delete(node);
520 free(ca_name);
521 node = back;
522 }
523 }
524 return ca_e->ca_list;
525}
526
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100527/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100528struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100529 unsigned long long int xxh64;
530 unsigned char ciphersuite_len;
531 char ciphersuite[0];
532};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100533struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100534static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200535static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100536
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200537#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
538struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
539#endif
540
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200541#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000542static unsigned int openssl_engines_initialized;
543struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
544struct ssl_engine_list {
545 struct list list;
546 ENGINE *e;
547};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200548#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000549
Remi Gacogne8de54152014-07-15 11:36:40 +0200550#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200551static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200552static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200553static DH *local_dh_1024 = NULL;
554static DH *local_dh_2048 = NULL;
555static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100556static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200557#endif /* OPENSSL_NO_DH */
558
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100559#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200560/* X509V3 Extensions that will be added on generated certificates */
561#define X509V3_EXT_SIZE 5
562static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
563 "basicConstraints",
564 "nsComment",
565 "subjectKeyIdentifier",
566 "authorityKeyIdentifier",
567 "keyUsage",
568};
569static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
570 "CA:FALSE",
571 "\"OpenSSL Generated Certificate\"",
572 "hash",
573 "keyid,issuer:always",
574 "nonRepudiation,digitalSignature,keyEncipherment"
575};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200576/* LRU cache to store generated certificate */
577static struct lru64_head *ssl_ctx_lru_tree = NULL;
578static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200579static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100580__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200581
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200582#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
583
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100584static struct ssl_bind_kw ssl_bind_kws[];
585
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200586#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500587/* The order here matters for picking a default context,
588 * keep the most common keytype at the bottom of the list
589 */
590const char *SSL_SOCK_KEYTYPE_NAMES[] = {
591 "dsa",
592 "ecdsa",
593 "rsa"
594};
595#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100596#else
597#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500598#endif
599
William Lallemandc3cd35f2017-11-28 11:04:43 +0100600static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100601static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
602
603#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
604
605#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
606 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
607
608#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
609 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200610
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100611/*
612 * This function gives the detail of the SSL error. It is used only
613 * if the debug mode and the verbose mode are activated. It dump all
614 * the SSL error until the stack was empty.
615 */
616static forceinline void ssl_sock_dump_errors(struct connection *conn)
617{
618 unsigned long ret;
619
620 if (unlikely(global.mode & MODE_DEBUG)) {
621 while(1) {
622 ret = ERR_get_error();
623 if (ret == 0)
624 return;
625 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200626 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100627 ERR_func_error_string(ret), ERR_reason_error_string(ret));
628 }
629 }
630}
631
yanbzhube2774d2015-12-10 15:07:30 -0500632
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200633#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000634static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
635{
636 int err_code = ERR_ABORT;
637 ENGINE *engine;
638 struct ssl_engine_list *el;
639
640 /* grab the structural reference to the engine */
641 engine = ENGINE_by_id(engine_id);
642 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100643 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000644 goto fail_get;
645 }
646
647 if (!ENGINE_init(engine)) {
648 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100649 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000650 goto fail_init;
651 }
652
653 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100654 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000655 goto fail_set_method;
656 }
657
658 el = calloc(1, sizeof(*el));
659 el->e = engine;
660 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100661 nb_engines++;
662 if (global_ssl.async)
663 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000664 return 0;
665
666fail_set_method:
667 /* release the functional reference from ENGINE_init() */
668 ENGINE_finish(engine);
669
670fail_init:
671 /* release the structural reference from ENGINE_by_id() */
672 ENGINE_free(engine);
673
674fail_get:
675 return err_code;
676}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200677#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000678
Willy Tarreau5db847a2019-05-09 14:13:35 +0200679#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200680/*
681 * openssl async fd handler
682 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200683void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000684{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200685 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000686
Emeric Brun3854e012017-05-17 20:42:48 +0200687 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000688 * to poll this fd until it is requested
689 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000690 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000691 fd_cant_recv(fd);
692
693 /* crypto engine is available, let's notify the associated
694 * connection that it can pursue its processing.
695 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200696 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000697}
698
Emeric Brun3854e012017-05-17 20:42:48 +0200699/*
700 * openssl async delayed SSL_free handler
701 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200702void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000703{
704 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200705 OSSL_ASYNC_FD all_fd[32];
706 size_t num_all_fds = 0;
707 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000708
Emeric Brun3854e012017-05-17 20:42:48 +0200709 /* We suppose that the async job for a same SSL *
710 * are serialized. So if we are awake it is
711 * because the running job has just finished
712 * and we can remove all async fds safely
713 */
714 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
715 if (num_all_fds > 32) {
716 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
717 return;
718 }
719
720 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
721 for (i=0 ; i < num_all_fds ; i++)
722 fd_remove(all_fd[i]);
723
724 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000725 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100726 _HA_ATOMIC_SUB(&sslconns, 1);
727 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000728}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000729/*
Emeric Brun3854e012017-05-17 20:42:48 +0200730 * function used to manage a returned SSL_ERROR_WANT_ASYNC
731 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000732 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200733static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000734{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100735 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200736 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200737 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000738 size_t num_add_fds = 0;
739 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200740 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000741
742 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
743 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200744 if (num_add_fds > 32 || num_del_fds > 32) {
745 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000746 return;
747 }
748
Emeric Brun3854e012017-05-17 20:42:48 +0200749 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000750
Emeric Brun3854e012017-05-17 20:42:48 +0200751 /* We remove unused fds from the fdtab */
752 for (i=0 ; i < num_del_fds ; i++)
753 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000754
Emeric Brun3854e012017-05-17 20:42:48 +0200755 /* We add new fds to the fdtab */
756 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200757 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000758 }
759
Emeric Brun3854e012017-05-17 20:42:48 +0200760 num_add_fds = 0;
761 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
762 if (num_add_fds > 32) {
763 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
764 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000765 }
Emeric Brun3854e012017-05-17 20:42:48 +0200766
767 /* We activate the polling for all known async fds */
768 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000769 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200770 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000771 /* To ensure that the fd cache won't be used
772 * We'll prefer to catch a real RD event
773 * because handling an EAGAIN on this fd will
774 * result in a context switch and also
775 * some engines uses a fd in blocking mode.
776 */
777 fd_cant_recv(add_fd[i]);
778 }
Emeric Brun3854e012017-05-17 20:42:48 +0200779
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000780}
781#endif
782
William Lallemand104a7a62019-10-14 14:14:59 +0200783#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200784/*
785 * This function returns the number of seconds elapsed
786 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
787 * date presented un ASN1_GENERALIZEDTIME.
788 *
789 * In parsing error case, it returns -1.
790 */
791static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
792{
793 long epoch;
794 char *p, *end;
795 const unsigned short month_offset[12] = {
796 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
797 };
798 int year, month;
799
800 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
801
802 p = (char *)d->data;
803 end = p + d->length;
804
805 if (end - p < 4) return -1;
806 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
807 p += 4;
808 if (end - p < 2) return -1;
809 month = 10 * (p[0] - '0') + p[1] - '0';
810 if (month < 1 || month > 12) return -1;
811 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
812 We consider leap years and the current month (<marsh or not) */
813 epoch = ( ((year - 1970) * 365)
814 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
815 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
816 + month_offset[month-1]
817 ) * 24 * 60 * 60;
818 p += 2;
819 if (end - p < 2) return -1;
820 /* Add the number of seconds of completed days of current month */
821 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
822 p += 2;
823 if (end - p < 2) return -1;
824 /* Add the completed hours of the current day */
825 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
826 p += 2;
827 if (end - p < 2) return -1;
828 /* Add the completed minutes of the current hour */
829 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
830 p += 2;
831 if (p == end) return -1;
832 /* Test if there is available seconds */
833 if (p[0] < '0' || p[0] > '9')
834 goto nosec;
835 if (end - p < 2) return -1;
836 /* Add the seconds of the current minute */
837 epoch += 10 * (p[0] - '0') + p[1] - '0';
838 p += 2;
839 if (p == end) return -1;
840 /* Ignore seconds float part if present */
841 if (p[0] == '.') {
842 do {
843 if (++p == end) return -1;
844 } while (p[0] >= '0' && p[0] <= '9');
845 }
846
847nosec:
848 if (p[0] == 'Z') {
849 if (end - p != 1) return -1;
850 return epoch;
851 }
852 else if (p[0] == '+') {
853 if (end - p != 5) return -1;
854 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700855 return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200856 }
857 else if (p[0] == '-') {
858 if (end - p != 5) return -1;
859 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700860 return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200861 }
862
863 return -1;
864}
865
William Lallemand104a7a62019-10-14 14:14:59 +0200866/*
867 * struct alignment works here such that the key.key is the same as key_data
868 * Do not change the placement of key_data
869 */
870struct certificate_ocsp {
871 struct ebmb_node key;
872 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
873 struct buffer response;
874 long expire;
875};
876
877struct ocsp_cbk_arg {
878 int is_single;
879 int single_kt;
880 union {
881 struct certificate_ocsp *s_ocsp;
882 /*
883 * m_ocsp will have multiple entries dependent on key type
884 * Entry 0 - DSA
885 * Entry 1 - ECDSA
886 * Entry 2 - RSA
887 */
888 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
889 };
890};
891
Emeric Brun1d3865b2014-06-20 15:37:32 +0200892static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200893
894/* This function starts to check if the OCSP response (in DER format) contained
895 * in chunk 'ocsp_response' is valid (else exits on error).
896 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
897 * contained in the OCSP Response and exits on error if no match.
898 * If it's a valid OCSP Response:
899 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
900 * pointed by 'ocsp'.
901 * If 'ocsp' is NULL, the function looks up into the OCSP response's
902 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
903 * from the response) and exits on error if not found. Finally, If an OCSP response is
904 * already present in the container, it will be overwritten.
905 *
906 * Note: OCSP response containing more than one OCSP Single response is not
907 * considered valid.
908 *
909 * Returns 0 on success, 1 in error case.
910 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200911static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
912 struct certificate_ocsp *ocsp,
913 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200914{
915 OCSP_RESPONSE *resp;
916 OCSP_BASICRESP *bs = NULL;
917 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200918 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200919 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200920 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200921 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200922 int reason;
923 int ret = 1;
924
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200925 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
926 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200927 if (!resp) {
928 memprintf(err, "Unable to parse OCSP response");
929 goto out;
930 }
931
932 rc = OCSP_response_status(resp);
933 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
934 memprintf(err, "OCSP response status not successful");
935 goto out;
936 }
937
938 bs = OCSP_response_get1_basic(resp);
939 if (!bs) {
940 memprintf(err, "Failed to get basic response from OCSP Response");
941 goto out;
942 }
943
944 count_sr = OCSP_resp_count(bs);
945 if (count_sr > 1) {
946 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
947 goto out;
948 }
949
950 sr = OCSP_resp_get0(bs, 0);
951 if (!sr) {
952 memprintf(err, "Failed to get OCSP single response");
953 goto out;
954 }
955
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200956 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
957
Emeric Brun4147b2e2014-06-16 18:36:30 +0200958 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200959 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200960 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200961 goto out;
962 }
963
Emeric Brun13a6b482014-06-20 15:44:34 +0200964 if (!nextupd) {
965 memprintf(err, "OCSP single response: missing nextupdate");
966 goto out;
967 }
968
Emeric Brunc8b27b62014-06-19 14:16:17 +0200969 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200970 if (!rc) {
971 memprintf(err, "OCSP single response: no longer valid.");
972 goto out;
973 }
974
975 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200976 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200977 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
978 goto out;
979 }
980 }
981
982 if (!ocsp) {
983 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
984 unsigned char *p;
985
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200986 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200987 if (!rc) {
988 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
989 goto out;
990 }
991
992 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
993 memprintf(err, "OCSP single response: Certificate ID too long");
994 goto out;
995 }
996
997 p = key;
998 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200999 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001000 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
1001 if (!ocsp) {
1002 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
1003 goto out;
1004 }
1005 }
1006
1007 /* According to comments on "chunk_dup", the
1008 previous chunk buffer will be freed */
1009 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1010 memprintf(err, "OCSP response: Memory allocation error");
1011 goto out;
1012 }
1013
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001014 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1015
Emeric Brun4147b2e2014-06-16 18:36:30 +02001016 ret = 0;
1017out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001018 ERR_clear_error();
1019
Emeric Brun4147b2e2014-06-16 18:36:30 +02001020 if (bs)
1021 OCSP_BASICRESP_free(bs);
1022
1023 if (resp)
1024 OCSP_RESPONSE_free(resp);
1025
1026 return ret;
1027}
1028/*
1029 * External function use to update the OCSP response in the OCSP response's
1030 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1031 * to update in DER format.
1032 *
1033 * Returns 0 on success, 1 in error case.
1034 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001035int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001036{
1037 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1038}
1039
William Lallemand4a660132019-10-14 14:51:41 +02001040#endif
1041
1042#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001043/*
1044 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +02001045 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +02001046 *
1047 * Returns 0 on success, 1 in error case.
1048 */
William Lallemand3b5f3602019-10-16 18:05:05 +02001049static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001050{
1051 int fd = -1;
1052 int r = 0;
1053 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +02001054 struct buffer *ocsp_response;
1055 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001056
William Lallemand3b5f3602019-10-16 18:05:05 +02001057 if (buf) {
1058 int i, j;
1059 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001060
William Lallemand3b5f3602019-10-16 18:05:05 +02001061 /* remove \r and \n from the payload */
1062 for (i = 0, j = 0; buf[i]; i++) {
1063 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +02001064 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +02001065 buf[j++] = buf[i];
1066 }
1067 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001068
William Lallemand3b5f3602019-10-16 18:05:05 +02001069 ret = base64dec(buf, j, trash.area, trash.size);
1070 if (ret < 0) {
1071 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +02001072 goto end;
1073 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001074 trash.data = ret;
1075 src = &trash;
1076 } else {
1077 fd = open(ocsp_path, O_RDONLY);
1078 if (fd == -1) {
1079 memprintf(err, "Error opening OCSP response file");
1080 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001081 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001082
1083 trash.data = 0;
1084 while (trash.data < trash.size) {
1085 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1086 if (r < 0) {
1087 if (errno == EINTR)
1088 continue;
1089
1090 memprintf(err, "Error reading OCSP response from file");
1091 goto end;
1092 }
1093 else if (r == 0) {
1094 break;
1095 }
1096 trash.data += r;
1097 }
1098 close(fd);
1099 fd = -1;
1100 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001101 }
1102
William Lallemand3b5f3602019-10-16 18:05:05 +02001103 ocsp_response = calloc(1, sizeof(*ocsp_response));
1104 if (!chunk_dup(ocsp_response, src)) {
1105 free(ocsp_response);
1106 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001107 goto end;
1108 }
Emmanuel Hocdet0667fae2020-01-16 14:41:36 +01001109 /* no error, fill ckch with new context, old context must be free */
1110 if (ckch->ocsp_response) {
1111 free(ckch->ocsp_response->area);
1112 ckch->ocsp_response->area = NULL;
1113 free(ckch->ocsp_response);
1114 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001115 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001116 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001117end:
1118 if (fd != -1)
1119 close(fd);
1120
1121 return ret;
1122}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001123#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001124
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001125#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1126static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc)
1127{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001128 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001129 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001130 struct connection *conn;
1131 int head;
1132 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001133 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001134
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001135 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001136 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001137 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1138
1139 keys = ref->tlskeys;
1140 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001141
1142 if (enc) {
1143 memcpy(key_name, keys[head].name, 16);
1144
1145 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001146 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001147
Emeric Brun9e754772019-01-10 17:51:55 +01001148 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001149
Emeric Brun9e754772019-01-10 17:51:55 +01001150 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1151 goto end;
1152
Willy Tarreau9356dac2019-05-10 09:22:53 +02001153 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001154 ret = 1;
1155 }
1156 else if (ref->key_size_bits == 256 ) {
1157
1158 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1159 goto end;
1160
Willy Tarreau9356dac2019-05-10 09:22:53 +02001161 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001162 ret = 1;
1163 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001164 } else {
1165 for (i = 0; i < TLS_TICKETS_NO; i++) {
1166 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1167 goto found;
1168 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001169 ret = 0;
1170 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001171
Christopher Faulet16f45c82018-02-16 11:23:49 +01001172 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001173 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001174 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001175 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1176 goto end;
1177 /* 2 for key renewal, 1 if current key is still valid */
1178 ret = i ? 2 : 1;
1179 }
1180 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001181 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001182 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1183 goto end;
1184 /* 2 for key renewal, 1 if current key is still valid */
1185 ret = i ? 2 : 1;
1186 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001187 }
Emeric Brun9e754772019-01-10 17:51:55 +01001188
Christopher Faulet16f45c82018-02-16 11:23:49 +01001189 end:
1190 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1191 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001192}
1193
1194struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1195{
1196 struct tls_keys_ref *ref;
1197
1198 list_for_each_entry(ref, &tlskeys_reference, list)
1199 if (ref->filename && strcmp(filename, ref->filename) == 0)
1200 return ref;
1201 return NULL;
1202}
1203
1204struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1205{
1206 struct tls_keys_ref *ref;
1207
1208 list_for_each_entry(ref, &tlskeys_reference, list)
1209 if (ref->unique_id == unique_id)
1210 return ref;
1211 return NULL;
1212}
1213
Emeric Brun9e754772019-01-10 17:51:55 +01001214/* Update the key into ref: if keysize doesnt
1215 * match existing ones, this function returns -1
1216 * else it returns 0 on success.
1217 */
1218int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001219 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001220{
Emeric Brun9e754772019-01-10 17:51:55 +01001221 if (ref->key_size_bits == 128) {
1222 if (tlskey->data != sizeof(struct tls_sess_key_128))
1223 return -1;
1224 }
1225 else if (ref->key_size_bits == 256) {
1226 if (tlskey->data != sizeof(struct tls_sess_key_256))
1227 return -1;
1228 }
1229 else
1230 return -1;
1231
Christopher Faulet16f45c82018-02-16 11:23:49 +01001232 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001233 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1234 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001235 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1236 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001237
1238 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001239}
1240
Willy Tarreau83061a82018-07-13 11:56:34 +02001241int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001242{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001243 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1244
1245 if(!ref) {
1246 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1247 return 1;
1248 }
Emeric Brun9e754772019-01-10 17:51:55 +01001249 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1250 memprintf(err, "Invalid key size");
1251 return 1;
1252 }
1253
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001254 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001255}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001256
1257/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001258 * automatic ids. It's called just after the basic checks. It returns
1259 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001260 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001261static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001262{
1263 int i = 0;
1264 struct tls_keys_ref *ref, *ref2, *ref3;
1265 struct list tkr = LIST_HEAD_INIT(tkr);
1266
1267 list_for_each_entry(ref, &tlskeys_reference, list) {
1268 if (ref->unique_id == -1) {
1269 /* Look for the first free id. */
1270 while (1) {
1271 list_for_each_entry(ref2, &tlskeys_reference, list) {
1272 if (ref2->unique_id == i) {
1273 i++;
1274 break;
1275 }
1276 }
1277 if (&ref2->list == &tlskeys_reference)
1278 break;
1279 }
1280
1281 /* Uses the unique id and increment it for the next entry. */
1282 ref->unique_id = i;
1283 i++;
1284 }
1285 }
1286
1287 /* This sort the reference list by id. */
1288 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1289 LIST_DEL(&ref->list);
1290 list_for_each_entry(ref3, &tkr, list) {
1291 if (ref->unique_id < ref3->unique_id) {
1292 LIST_ADDQ(&ref3->list, &ref->list);
1293 break;
1294 }
1295 }
1296 if (&ref3->list == &tkr)
1297 LIST_ADDQ(&tkr, &ref->list);
1298 }
1299
1300 /* swap root */
1301 LIST_ADD(&tkr, &tlskeys_reference);
1302 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001303 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001304}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001305#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1306
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001307#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001308int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1309{
1310 switch (evp_keytype) {
1311 case EVP_PKEY_RSA:
1312 return 2;
1313 case EVP_PKEY_DSA:
1314 return 0;
1315 case EVP_PKEY_EC:
1316 return 1;
1317 }
1318
1319 return -1;
1320}
1321
Emeric Brun4147b2e2014-06-16 18:36:30 +02001322/*
1323 * Callback used to set OCSP status extension content in server hello.
1324 */
1325int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1326{
yanbzhube2774d2015-12-10 15:07:30 -05001327 struct certificate_ocsp *ocsp;
1328 struct ocsp_cbk_arg *ocsp_arg;
1329 char *ssl_buf;
1330 EVP_PKEY *ssl_pkey;
1331 int key_type;
1332 int index;
1333
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001334 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001335
1336 ssl_pkey = SSL_get_privatekey(ssl);
1337 if (!ssl_pkey)
1338 return SSL_TLSEXT_ERR_NOACK;
1339
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001340 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001341
1342 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1343 ocsp = ocsp_arg->s_ocsp;
1344 else {
1345 /* For multiple certs per context, we have to find the correct OCSP response based on
1346 * the certificate type
1347 */
1348 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1349
1350 if (index < 0)
1351 return SSL_TLSEXT_ERR_NOACK;
1352
1353 ocsp = ocsp_arg->m_ocsp[index];
1354
1355 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001356
1357 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001358 !ocsp->response.area ||
1359 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001360 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001361 return SSL_TLSEXT_ERR_NOACK;
1362
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001363 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001364 if (!ssl_buf)
1365 return SSL_TLSEXT_ERR_NOACK;
1366
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001367 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1368 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001369
1370 return SSL_TLSEXT_ERR_OK;
1371}
1372
William Lallemand4a660132019-10-14 14:51:41 +02001373#endif
1374
1375#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001376/*
1377 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001378 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1379 * status extension, the issuer's certificate is mandatory. It should be
1380 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001381 *
William Lallemand246c0242019-10-11 08:59:13 +02001382 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1383 * OCSP response. If file is empty or content is not a valid OCSP response,
1384 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1385 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001386 *
1387 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001388 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001389 */
William Lallemand4a660132019-10-14 14:51:41 +02001390#ifndef OPENSSL_IS_BORINGSSL
William Lallemand246c0242019-10-11 08:59:13 +02001391static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001392{
William Lallemand246c0242019-10-11 08:59:13 +02001393 X509 *x = NULL, *issuer = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001394 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001395 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001396 struct certificate_ocsp *ocsp = NULL, *iocsp;
1397 char *warn = NULL;
1398 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001399 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001400
Emeric Brun4147b2e2014-06-16 18:36:30 +02001401
William Lallemand246c0242019-10-11 08:59:13 +02001402 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001403 if (!x)
1404 goto out;
1405
William Lallemand246c0242019-10-11 08:59:13 +02001406 issuer = ckch->ocsp_issuer;
1407 if (!issuer)
1408 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001409
1410 cid = OCSP_cert_to_id(0, x, issuer);
1411 if (!cid)
1412 goto out;
1413
1414 i = i2d_OCSP_CERTID(cid, NULL);
1415 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1416 goto out;
1417
Vincent Bernat02779b62016-04-03 13:48:43 +02001418 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001419 if (!ocsp)
1420 goto out;
1421
1422 p = ocsp->key_data;
1423 i2d_OCSP_CERTID(cid, &p);
1424
1425 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1426 if (iocsp == ocsp)
1427 ocsp = NULL;
1428
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001429#ifndef SSL_CTX_get_tlsext_status_cb
1430# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1431 *cb = (void (*) (void))ctx->tlsext_status_cb;
1432#endif
1433 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1434
1435 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001436 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001437 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001438
1439 cb_arg->is_single = 1;
1440 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001441
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001442 pkey = X509_get_pubkey(x);
1443 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1444 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001445
1446 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1447 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1448 } else {
1449 /*
1450 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1451 * Update that cb_arg with the new cert's staple
1452 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001453 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001454 struct certificate_ocsp *tmp_ocsp;
1455 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001456 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001457 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001458
1459#ifdef SSL_CTX_get_tlsext_status_arg
1460 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1461#else
1462 cb_arg = ctx->tlsext_status_arg;
1463#endif
yanbzhube2774d2015-12-10 15:07:30 -05001464
1465 /*
1466 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1467 * the order of operations below matter, take care when changing it
1468 */
1469 tmp_ocsp = cb_arg->s_ocsp;
1470 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1471 cb_arg->s_ocsp = NULL;
1472 cb_arg->m_ocsp[index] = tmp_ocsp;
1473 cb_arg->is_single = 0;
1474 cb_arg->single_kt = 0;
1475
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001476 pkey = X509_get_pubkey(x);
1477 key_type = EVP_PKEY_base_id(pkey);
1478 EVP_PKEY_free(pkey);
1479
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001480 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001481 if (index >= 0 && !cb_arg->m_ocsp[index])
1482 cb_arg->m_ocsp[index] = iocsp;
1483
1484 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001485
1486 ret = 0;
1487
1488 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001489 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001490 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001491 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001492 }
1493
1494out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001495 if (cid)
1496 OCSP_CERTID_free(cid);
1497
1498 if (ocsp)
1499 free(ocsp);
1500
1501 if (warn)
1502 free(warn);
1503
Emeric Brun4147b2e2014-06-16 18:36:30 +02001504 return ret;
1505}
William Lallemand4a660132019-10-14 14:51:41 +02001506#else /* OPENSSL_IS_BORINGSSL */
1507static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001508{
William Lallemand4a660132019-10-14 14:51:41 +02001509 return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)ckch->ocsp_response->area, ckch->ocsp_response->data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001510}
1511#endif
1512
William Lallemand4a660132019-10-14 14:51:41 +02001513#endif
1514
1515
Willy Tarreau5db847a2019-05-09 14:13:35 +02001516#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001517
1518#define CT_EXTENSION_TYPE 18
1519
1520static int sctl_ex_index = -1;
1521
1522/*
1523 * Try to parse Signed Certificate Timestamp List structure. This function
1524 * makes only basic test if the data seems like SCTL. No signature validation
1525 * is performed.
1526 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001527static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001528{
1529 int ret = 1;
1530 int len, pos, sct_len;
1531 unsigned char *data;
1532
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001533 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001534 goto out;
1535
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001536 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001537 len = (data[0] << 8) | data[1];
1538
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001539 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001540 goto out;
1541
1542 data = data + 2;
1543 pos = 0;
1544 while (pos < len) {
1545 if (len - pos < 2)
1546 goto out;
1547
1548 sct_len = (data[pos] << 8) | data[pos + 1];
1549 if (pos + sct_len + 2 > len)
1550 goto out;
1551
1552 pos += sct_len + 2;
1553 }
1554
1555 ret = 0;
1556
1557out:
1558 return ret;
1559}
1560
William Lallemand0dfae6c2019-10-16 18:06:58 +02001561/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1562 * It fills the ckch->sctl buffer
1563 * return 0 on success or != 0 on failure */
1564static int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001565{
1566 int fd = -1;
1567 int r = 0;
1568 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001569 struct buffer tmp;
1570 struct buffer *src;
1571 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001572
William Lallemand0dfae6c2019-10-16 18:06:58 +02001573 if (buf) {
1574 tmp.area = buf;
1575 tmp.data = strlen(buf);
1576 tmp.size = tmp.data + 1;
1577 src = &tmp;
1578 } else {
1579 fd = open(sctl_path, O_RDONLY);
1580 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001581 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001582
1583 trash.data = 0;
1584 while (trash.data < trash.size) {
1585 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1586 if (r < 0) {
1587 if (errno == EINTR)
1588 continue;
1589 goto end;
1590 }
1591 else if (r == 0) {
1592 break;
1593 }
1594 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001595 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001596 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001597 }
1598
William Lallemand0dfae6c2019-10-16 18:06:58 +02001599 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001600 if (ret)
1601 goto end;
1602
William Lallemand0dfae6c2019-10-16 18:06:58 +02001603 sctl = calloc(1, sizeof(*sctl));
1604 if (!chunk_dup(sctl, src)) {
1605 free(sctl);
1606 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001607 goto end;
1608 }
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001609 /* no error, fill ckch with new context, old context must be free */
1610 if (ckch->sctl) {
1611 free(ckch->sctl->area);
1612 ckch->sctl->area = NULL;
1613 free(ckch->sctl);
1614 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001615 ckch->sctl = sctl;
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001616 ret = 0;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001617end:
1618 if (fd != -1)
1619 close(fd);
1620
1621 return ret;
1622}
1623
1624int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1625{
Willy Tarreau83061a82018-07-13 11:56:34 +02001626 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001627
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001628 *out = (unsigned char *) sctl->area;
1629 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001630
1631 return 1;
1632}
1633
1634int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1635{
1636 return 1;
1637}
1638
William Lallemanda17f4112019-10-10 15:16:44 +02001639static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001640{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001641 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001642
William Lallemanda17f4112019-10-10 15:16:44 +02001643 if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL))
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001644 goto out;
1645
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001646 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1647
1648 ret = 0;
1649
1650out:
1651 return ret;
1652}
1653
1654#endif
1655
Emeric Brune1f38db2012-09-03 20:36:47 +02001656void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1657{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001658 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001659 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001660 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001661 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001662
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001663#ifndef SSL_OP_NO_RENEGOTIATION
1664 /* Please note that BoringSSL defines this macro to zero so don't
1665 * change this to #if and do not assign a default value to this macro!
1666 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001667 if (where & SSL_CB_HANDSHAKE_START) {
1668 /* Disable renegotiation (CVE-2009-3555) */
Olivier Houchard90084a12017-11-23 18:21:29 +01001669 if ((conn->flags & (CO_FL_CONNECTED | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_CONNECTED) {
Emeric Brune1f38db2012-09-03 20:36:47 +02001670 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001671 conn->err_code = CO_ER_SSL_RENEG;
1672 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001673 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001674#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001675
1676 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001677 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001678 /* Long certificate chains optimz
1679 If write and read bios are differents, we
1680 consider that the buffering was activated,
1681 so we rise the output buffer size from 4k
1682 to 16k */
1683 write_bio = SSL_get_wbio(ssl);
1684 if (write_bio != SSL_get_rbio(ssl)) {
1685 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001686 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001687 }
1688 }
1689 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001690}
1691
Emeric Brune64aef12012-09-21 13:15:06 +02001692/* Callback is called for each certificate of the chain during a verify
1693 ok is set to 1 if preverify detect no error on current certificate.
1694 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001695int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001696{
1697 SSL *ssl;
1698 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001699 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001700 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001701
1702 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001703 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001704
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001705 ctx = conn->xprt_ctx;
1706
1707 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001708
Emeric Brun81c00f02012-09-21 14:31:21 +02001709 if (ok) /* no errors */
1710 return ok;
1711
1712 depth = X509_STORE_CTX_get_error_depth(x_store);
1713 err = X509_STORE_CTX_get_error(x_store);
1714
1715 /* check if CA error needs to be ignored */
1716 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001717 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1718 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1719 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001720 }
1721
Willy Tarreau07d94e42018-09-20 10:57:52 +02001722 if (__objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001723 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001724 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001725 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001726 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001727
Willy Tarreau20879a02012-12-03 16:32:10 +01001728 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001729 return 0;
1730 }
1731
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001732 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1733 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001734
Emeric Brun81c00f02012-09-21 14:31:21 +02001735 /* check if certificate error needs to be ignored */
Willy Tarreau07d94e42018-09-20 10:57:52 +02001736 if (__objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001737 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001738 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001739 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001740 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001741
Willy Tarreau20879a02012-12-03 16:32:10 +01001742 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001743 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001744}
1745
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001746static inline
1747void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001748 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001749{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001750 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001751 unsigned char *msg;
1752 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001753 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001754
1755 /* This function is called for "from client" and "to server"
1756 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001757 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001758 */
1759
1760 /* "write_p" is set to 0 is the bytes are received messages,
1761 * otherwise it is set to 1.
1762 */
1763 if (write_p != 0)
1764 return;
1765
1766 /* content_type contains the type of message received or sent
1767 * according with the SSL/TLS protocol spec. This message is
1768 * encoded with one byte. The value 256 (two bytes) is used
1769 * for designing the SSL/TLS record layer. According with the
1770 * rfc6101, the expected message (other than 256) are:
1771 * - change_cipher_spec(20)
1772 * - alert(21)
1773 * - handshake(22)
1774 * - application_data(23)
1775 * - (255)
1776 * We are interessed by the handshake and specially the client
1777 * hello.
1778 */
1779 if (content_type != 22)
1780 return;
1781
1782 /* The message length is at least 4 bytes, containing the
1783 * message type and the message length.
1784 */
1785 if (len < 4)
1786 return;
1787
1788 /* First byte of the handshake message id the type of
1789 * message. The konwn types are:
1790 * - hello_request(0)
1791 * - client_hello(1)
1792 * - server_hello(2)
1793 * - certificate(11)
1794 * - server_key_exchange (12)
1795 * - certificate_request(13)
1796 * - server_hello_done(14)
1797 * We are interested by the client hello.
1798 */
1799 msg = (unsigned char *)buf;
1800 if (msg[0] != 1)
1801 return;
1802
1803 /* Next three bytes are the length of the message. The total length
1804 * must be this decoded length + 4. If the length given as argument
1805 * is not the same, we abort the protocol dissector.
1806 */
1807 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1808 if (len < rec_len + 4)
1809 return;
1810 msg += 4;
1811 end = msg + rec_len;
1812 if (end < msg)
1813 return;
1814
1815 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1816 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001817 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1818 */
1819 msg += 1 + 1 + 4 + 28;
1820 if (msg > end)
1821 return;
1822
1823 /* Next, is session id:
1824 * if present, we have to jump by length + 1 for the size information
1825 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001826 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001827 if (msg[0] > 0)
1828 msg += msg[0];
1829 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001830 if (msg > end)
1831 return;
1832
1833 /* Next two bytes are the ciphersuite length. */
1834 if (msg + 2 > end)
1835 return;
1836 rec_len = (msg[0] << 8) + msg[1];
1837 msg += 2;
1838 if (msg + rec_len > end || msg + rec_len < msg)
1839 return;
1840
Willy Tarreaubafbe012017-11-24 17:34:44 +01001841 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001842 if (!capture)
1843 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001844 /* Compute the xxh64 of the ciphersuite. */
1845 capture->xxh64 = XXH64(msg, rec_len, 0);
1846
1847 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001848 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1849 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001850 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001851
1852 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001853}
1854
Emeric Brun29f037d2014-04-25 19:05:36 +02001855/* Callback is called for ssl protocol analyse */
1856void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1857{
Emeric Brun29f037d2014-04-25 19:05:36 +02001858#ifdef TLS1_RT_HEARTBEAT
1859 /* test heartbeat received (write_p is set to 0
1860 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001861 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001862 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001863 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001864 const unsigned char *p = buf;
1865 unsigned int payload;
1866
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001867 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001868
1869 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1870 if (*p != TLS1_HB_REQUEST)
1871 return;
1872
Willy Tarreauaeed6722014-04-25 23:59:58 +02001873 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001874 goto kill_it;
1875
1876 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001877 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001878 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001879 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001880 /* We have a clear heartbleed attack (CVE-2014-0160), the
1881 * advertised payload is larger than the advertised packet
1882 * length, so we have garbage in the buffer between the
1883 * payload and the end of the buffer (p+len). We can't know
1884 * if the SSL stack is patched, and we don't know if we can
1885 * safely wipe out the area between p+3+len and payload.
1886 * So instead, we prevent the response from being sent by
1887 * setting the max_send_fragment to 0 and we report an SSL
1888 * error, which will kill this connection. It will be reported
1889 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001890 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1891 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001892 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001893 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1894 return;
1895 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001896#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001897 if (global_ssl.capture_cipherlist > 0)
1898 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001899}
1900
Bernard Spil13c53f82018-02-15 13:34:58 +01001901#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001902static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1903 const unsigned char *in, unsigned int inlen,
1904 void *arg)
1905{
1906 struct server *srv = arg;
1907
1908 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1909 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1910 return SSL_TLSEXT_ERR_OK;
1911 return SSL_TLSEXT_ERR_NOACK;
1912}
1913#endif
1914
1915#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001916/* This callback is used so that the server advertises the list of
1917 * negociable protocols for NPN.
1918 */
1919static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1920 unsigned int *len, void *arg)
1921{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001922 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001923
1924 *data = (const unsigned char *)conf->npn_str;
1925 *len = conf->npn_len;
1926 return SSL_TLSEXT_ERR_OK;
1927}
1928#endif
1929
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001930#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001931/* This callback is used so that the server advertises the list of
1932 * negociable protocols for ALPN.
1933 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001934static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1935 unsigned char *outlen,
1936 const unsigned char *server,
1937 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001938{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001939 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001940
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001941 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1942 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1943 return SSL_TLSEXT_ERR_NOACK;
1944 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001945 return SSL_TLSEXT_ERR_OK;
1946}
1947#endif
1948
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001949#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001950#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001951
Christopher Faulet30548802015-06-11 13:39:32 +02001952/* Create a X509 certificate with the specified servername and serial. This
1953 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001954static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001955ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001956{
Christopher Faulet7969a332015-10-09 11:15:03 +02001957 X509 *cacert = bind_conf->ca_sign_cert;
1958 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001959 SSL_CTX *ssl_ctx = NULL;
1960 X509 *newcrt = NULL;
1961 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001962 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001963 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001964 X509_NAME *name;
1965 const EVP_MD *digest;
1966 X509V3_CTX ctx;
1967 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001968 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001969
Christopher Faulet48a83322017-07-28 16:56:09 +02001970 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001971#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001972 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1973#else
1974 tmp_ssl = SSL_new(bind_conf->default_ctx);
1975 if (tmp_ssl)
1976 pkey = SSL_get_privatekey(tmp_ssl);
1977#endif
1978 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001979 goto mkcert_error;
1980
1981 /* Create the certificate */
1982 if (!(newcrt = X509_new()))
1983 goto mkcert_error;
1984
1985 /* Set version number for the certificate (X509v3) and the serial
1986 * number */
1987 if (X509_set_version(newcrt, 2L) != 1)
1988 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001989 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001990
1991 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001992 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1993 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001994 goto mkcert_error;
1995
1996 /* set public key in the certificate */
1997 if (X509_set_pubkey(newcrt, pkey) != 1)
1998 goto mkcert_error;
1999
2000 /* Set issuer name from the CA */
2001 if (!(name = X509_get_subject_name(cacert)))
2002 goto mkcert_error;
2003 if (X509_set_issuer_name(newcrt, name) != 1)
2004 goto mkcert_error;
2005
2006 /* Set the subject name using the same, but the CN */
2007 name = X509_NAME_dup(name);
2008 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2009 (const unsigned char *)servername,
2010 -1, -1, 0) != 1) {
2011 X509_NAME_free(name);
2012 goto mkcert_error;
2013 }
2014 if (X509_set_subject_name(newcrt, name) != 1) {
2015 X509_NAME_free(name);
2016 goto mkcert_error;
2017 }
2018 X509_NAME_free(name);
2019
2020 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002021 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002022 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2023 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2024 X509_EXTENSION *ext;
2025
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002026 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002027 goto mkcert_error;
2028 if (!X509_add_ext(newcrt, ext, -1)) {
2029 X509_EXTENSION_free(ext);
2030 goto mkcert_error;
2031 }
2032 X509_EXTENSION_free(ext);
2033 }
2034
2035 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002036
2037 key_type = EVP_PKEY_base_id(capkey);
2038
2039 if (key_type == EVP_PKEY_DSA)
2040 digest = EVP_sha1();
2041 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002042 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002043 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002044 digest = EVP_sha256();
2045 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002046#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002047 int nid;
2048
2049 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2050 goto mkcert_error;
2051 if (!(digest = EVP_get_digestbynid(nid)))
2052 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002053#else
2054 goto mkcert_error;
2055#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002056 }
2057
Christopher Faulet31af49d2015-06-09 17:29:50 +02002058 if (!(X509_sign(newcrt, capkey, digest)))
2059 goto mkcert_error;
2060
2061 /* Create and set the new SSL_CTX */
2062 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2063 goto mkcert_error;
2064 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2065 goto mkcert_error;
2066 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2067 goto mkcert_error;
2068 if (!SSL_CTX_check_private_key(ssl_ctx))
2069 goto mkcert_error;
2070
2071 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002072
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002073#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002074 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002075#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002076#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2077 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002078 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002079 EC_KEY *ecc;
2080 int nid;
2081
2082 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2083 goto end;
2084 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2085 goto end;
2086 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2087 EC_KEY_free(ecc);
2088 }
2089#endif
2090 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002091 return ssl_ctx;
2092
2093 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002094 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002095 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002096 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2097 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002098 return NULL;
2099}
2100
Christopher Faulet7969a332015-10-09 11:15:03 +02002101SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002102ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002103{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002104 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002105 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002106
Olivier Houchard66ab4982019-02-26 18:37:15 +01002107 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002108}
2109
Christopher Faulet30548802015-06-11 13:39:32 +02002110/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002111 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002112SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002113ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002114{
2115 struct lru64 *lru = NULL;
2116
2117 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002118 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002119 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002120 if (lru && lru->domain) {
2121 if (ssl)
2122 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002123 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002124 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002125 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002126 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002127 }
2128 return NULL;
2129}
2130
Emeric Brun821bb9b2017-06-15 16:37:39 +02002131/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2132 * function is not thread-safe, it should only be used to check if a certificate
2133 * exists in the lru cache (with no warranty it will not be removed by another
2134 * thread). It is kept for backward compatibility. */
2135SSL_CTX *
2136ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2137{
2138 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2139}
2140
Christopher Fauletd2cab922015-07-28 16:03:47 +02002141/* Set a certificate int the LRU cache used to store generated
2142 * certificate. Return 0 on success, otherwise -1 */
2143int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002144ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002145{
2146 struct lru64 *lru = NULL;
2147
2148 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002149 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002150 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002151 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002152 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002153 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002154 }
Christopher Faulet30548802015-06-11 13:39:32 +02002155 if (lru->domain && lru->data)
2156 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002157 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002158 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002159 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002160 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002161 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002162}
2163
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002164/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002165unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002166ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002167{
2168 return XXH32(data, len, ssl_ctx_lru_seed);
2169}
2170
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002171/* Generate a cert and immediately assign it to the SSL session so that the cert's
2172 * refcount is maintained regardless of the cert's presence in the LRU cache.
2173 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002174static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002175ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002176{
2177 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002178 SSL_CTX *ssl_ctx = NULL;
2179 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002180 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002181
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002182 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002183 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002184 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002185 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002186 if (lru && lru->domain)
2187 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002188 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002189 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002190 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002191 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002192 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002193 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002194 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002195 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002196 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002197 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002198 SSL_set_SSL_CTX(ssl, ssl_ctx);
2199 /* No LRU cache, this CTX will be released as soon as the session dies */
2200 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002201 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002202 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002203 return 0;
2204}
2205static int
2206ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2207{
2208 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002209 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002210
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002211 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002212 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002213 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002214 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002215 }
2216 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002217}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002218#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002219
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002220#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002221typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2222
2223static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002224{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002225#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002226 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002227 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2228#endif
2229}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002230static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2231 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002232 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2233}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002234static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002235#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002236 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002237 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2238#endif
2239}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002240static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002241#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002242 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002243 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2244#endif
2245}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002246/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002247static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2248/* Unusable in this context. */
2249static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2250static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2251static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2252static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2253static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002254#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002255typedef enum { SET_MIN, SET_MAX } set_context_func;
2256
2257static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2258 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002259 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2260}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002261static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2262 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2263 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2264}
2265static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2266 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002267 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2268}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002269static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2270 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2271 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2272}
2273static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2274 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002275 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2276}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002277static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2278 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2279 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2280}
2281static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2282 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002283 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2284}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002285static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2286 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2287 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2288}
2289static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002290#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002291 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002292 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2293#endif
2294}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002295static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2296#if SSL_OP_NO_TLSv1_3
2297 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2298 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002299#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002300}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002301#endif
2302static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2303static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002304
2305static struct {
2306 int option;
2307 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002308 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2309 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002310 const char *name;
2311} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002312 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2313 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2314 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2315 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2316 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2317 {SSL_OP_NO_TLSv1_3, MC_SSL_O_NO_TLSV13, ctx_set_TLSv13_func, ssl_set_TLSv13_func, "TLSv1.3"}, /* CONF_TLSV13 */
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002318};
2319
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002320static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2321{
2322 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2323 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2324 SSL_set_SSL_CTX(ssl, ctx);
2325}
2326
Willy Tarreau5db847a2019-05-09 14:13:35 +02002327#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002328
2329static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2330{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002331 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002332 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002333
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002334 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2335 return SSL_TLSEXT_ERR_OK;
2336 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002337}
2338
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002339#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002340static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2341{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002342 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002343#else
2344static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2345{
2346#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002347 struct connection *conn;
2348 struct bind_conf *s;
2349 const uint8_t *extension_data;
2350 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002351 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002352
2353 char *wildp = NULL;
2354 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002355 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002356 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002357 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002358 int i;
2359
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002360 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002361 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002362
Olivier Houchard9679ac92017-10-27 14:58:08 +02002363 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002364 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002365#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002366 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2367 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002368#else
2369 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2370#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002371 /*
2372 * The server_name extension was given too much extensibility when it
2373 * was written, so parsing the normal case is a bit complex.
2374 */
2375 size_t len;
2376 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002377 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002378 /* Extract the length of the supplied list of names. */
2379 len = (*extension_data++) << 8;
2380 len |= *extension_data++;
2381 if (len + 2 != extension_len)
2382 goto abort;
2383 /*
2384 * The list in practice only has a single element, so we only consider
2385 * the first one.
2386 */
2387 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2388 goto abort;
2389 extension_len = len - 1;
2390 /* Now we can finally pull out the byte array with the actual hostname. */
2391 if (extension_len <= 2)
2392 goto abort;
2393 len = (*extension_data++) << 8;
2394 len |= *extension_data++;
2395 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2396 || memchr(extension_data, 0, len) != NULL)
2397 goto abort;
2398 servername = extension_data;
2399 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002400 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002401#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2402 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002403 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002404 }
2405#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002406 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002407 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002408 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002409 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002410 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002411 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002412 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002413 goto abort;
2414 }
2415
2416 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002417#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002418 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002419#else
2420 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2421#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002422 uint8_t sign;
2423 size_t len;
2424 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002425 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002426 len = (*extension_data++) << 8;
2427 len |= *extension_data++;
2428 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002429 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002430 if (len % 2 != 0)
2431 goto abort;
2432 for (; len > 0; len -= 2) {
2433 extension_data++; /* hash */
2434 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002435 switch (sign) {
2436 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002437 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002438 break;
2439 case TLSEXT_signature_ecdsa:
2440 has_ecdsa_sig = 1;
2441 break;
2442 default:
2443 continue;
2444 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002445 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002446 break;
2447 }
2448 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002449 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002450 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002451 }
2452 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002453 const SSL_CIPHER *cipher;
2454 size_t len;
2455 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002456 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002457#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002458 len = ctx->cipher_suites_len;
2459 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002460#else
2461 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2462#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002463 if (len % 2 != 0)
2464 goto abort;
2465 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002466#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002467 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002468 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002469#else
2470 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2471#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002472 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002473 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002474 break;
2475 }
2476 }
2477 }
2478
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002479 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002480 trash.area[i] = tolower(servername[i]);
2481 if (!wildp && (trash.area[i] == '.'))
2482 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002483 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002484 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002485
William Lallemand150bfa82019-09-19 17:12:49 +02002486 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002487
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002488 for (i = 0; i < 2; i++) {
2489 if (i == 0) /* lookup in full qualified names */
2490 node = ebst_lookup(&s->sni_ctx, trash.area);
2491 else if (i == 1 && wildp) /* lookup in wildcards names */
2492 node = ebst_lookup(&s->sni_w_ctx, wildp);
2493 else
2494 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002495 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002496 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002497 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002498 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002499 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002500 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002501 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002502 break;
2503 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002504 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002505 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002506 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002507 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002508 if (!node_anonymous)
2509 node_anonymous = n;
2510 break;
2511 }
2512 }
2513 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002514 /* select by key_signature priority order */
2515 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2516 : ((has_rsa_sig && node_rsa) ? node_rsa
2517 : (node_anonymous ? node_anonymous
2518 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2519 : node_rsa /* no rsa signature case (far far away) */
2520 )));
2521 if (node) {
2522 /* switch ctx */
2523 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2524 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002525 if (conf) {
2526 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2527 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2528 if (conf->early_data)
2529 allow_early = 1;
2530 }
William Lallemand02010472019-10-18 11:02:19 +02002531 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002532 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002533 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002534 }
William Lallemand150bfa82019-09-19 17:12:49 +02002535
William Lallemand02010472019-10-18 11:02:19 +02002536 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002537#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002538 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002539 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002540 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002541 }
2542#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002543 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002544 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002545 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002546 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002547 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002548 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002549allow_early:
2550#ifdef OPENSSL_IS_BORINGSSL
2551 if (allow_early)
2552 SSL_set_early_data_enabled(ssl, 1);
2553#else
2554 if (!allow_early)
2555 SSL_set_max_early_data(ssl, 0);
2556#endif
2557 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002558 abort:
2559 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2560 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002561#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002562 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002563#else
2564 *al = SSL_AD_UNRECOGNIZED_NAME;
2565 return 0;
2566#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002567}
2568
2569#else /* OPENSSL_IS_BORINGSSL */
2570
Emeric Brunfc0421f2012-09-07 17:30:07 +02002571/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2572 * warning when no match is found, which implies the default (first) cert
2573 * will keep being used.
2574 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002575static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002576{
2577 const char *servername;
2578 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002579 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002580 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002581 int i;
2582 (void)al; /* shut gcc stupid warning */
2583
2584 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002585 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002586#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002587 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2588 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002589#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002590 if (s->strict_sni)
2591 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002592 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002593 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002594 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002595 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002596 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002597
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002598 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002599 if (!servername[i])
2600 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002601 trash.area[i] = tolower(servername[i]);
2602 if (!wildp && (trash.area[i] == '.'))
2603 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002604 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002605 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002606
William Lallemand150bfa82019-09-19 17:12:49 +02002607 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002608 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002609 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002610 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2611 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002612 if (!container_of(n, struct sni_ctx, name)->neg) {
2613 node = n;
2614 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002615 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002616 }
2617 if (!node && wildp) {
2618 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002619 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2620 /* lookup a not neg filter */
2621 if (!container_of(n, struct sni_ctx, name)->neg) {
2622 node = n;
2623 break;
2624 }
2625 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002626 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002627 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002628#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002629 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2630 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002631 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002632 return SSL_TLSEXT_ERR_OK;
2633 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002634#endif
William Lallemand21724f02019-11-04 17:56:13 +01002635 if (s->strict_sni) {
2636 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002637 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002638 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002639 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002640 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002641 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002642 }
2643
2644 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002645 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002646 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002647 return SSL_TLSEXT_ERR_OK;
2648}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002649#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002650#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2651
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002652#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002653
2654static DH * ssl_get_dh_1024(void)
2655{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002656 static unsigned char dh1024_p[]={
2657 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2658 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2659 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2660 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2661 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2662 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2663 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2664 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2665 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2666 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2667 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2668 };
2669 static unsigned char dh1024_g[]={
2670 0x02,
2671 };
2672
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002673 BIGNUM *p;
2674 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002675 DH *dh = DH_new();
2676 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002677 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2678 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002679
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002680 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002681 DH_free(dh);
2682 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002683 } else {
2684 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002685 }
2686 }
2687 return dh;
2688}
2689
2690static DH *ssl_get_dh_2048(void)
2691{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002692 static unsigned char dh2048_p[]={
2693 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2694 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2695 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2696 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2697 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2698 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2699 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2700 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2701 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2702 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2703 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2704 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2705 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2706 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2707 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2708 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2709 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2710 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2711 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2712 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2713 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2714 0xB7,0x1F,0x77,0xF3,
2715 };
2716 static unsigned char dh2048_g[]={
2717 0x02,
2718 };
2719
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002720 BIGNUM *p;
2721 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002722 DH *dh = DH_new();
2723 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002724 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2725 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002726
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002727 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002728 DH_free(dh);
2729 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002730 } else {
2731 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002732 }
2733 }
2734 return dh;
2735}
2736
2737static DH *ssl_get_dh_4096(void)
2738{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002739 static unsigned char dh4096_p[]={
2740 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2741 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2742 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2743 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2744 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2745 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2746 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2747 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2748 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2749 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2750 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2751 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2752 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2753 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2754 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2755 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2756 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2757 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2758 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2759 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2760 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2761 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2762 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2763 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2764 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2765 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2766 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2767 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2768 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2769 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2770 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2771 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2772 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2773 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2774 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2775 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2776 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2777 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2778 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2779 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2780 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2781 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2782 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002783 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002784 static unsigned char dh4096_g[]={
2785 0x02,
2786 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002787
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002788 BIGNUM *p;
2789 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002790 DH *dh = DH_new();
2791 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002792 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2793 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002794
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002795 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002796 DH_free(dh);
2797 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002798 } else {
2799 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002800 }
2801 }
2802 return dh;
2803}
2804
2805/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002806 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002807static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2808{
2809 DH *dh = NULL;
2810 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002811 int type;
2812
2813 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002814
2815 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2816 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2817 */
2818 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2819 keylen = EVP_PKEY_bits(pkey);
2820 }
2821
Willy Tarreauef934602016-12-22 23:12:01 +01002822 if (keylen > global_ssl.default_dh_param) {
2823 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002824 }
2825
Remi Gacogned3a341a2015-05-29 16:26:17 +02002826 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002827 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002828 }
2829 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002830 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002831 }
2832 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002833 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002834 }
2835
2836 return dh;
2837}
2838
Remi Gacogne47783ef2015-05-29 15:53:22 +02002839static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002840{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002841 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002842 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002843
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002844 if (in == NULL)
2845 goto end;
2846
Remi Gacogne47783ef2015-05-29 15:53:22 +02002847 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002848 goto end;
2849
Remi Gacogne47783ef2015-05-29 15:53:22 +02002850 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2851
2852end:
2853 if (in)
2854 BIO_free(in);
2855
Emeric Brune1b4ed42018-08-16 15:14:12 +02002856 ERR_clear_error();
2857
Remi Gacogne47783ef2015-05-29 15:53:22 +02002858 return dh;
2859}
2860
2861int ssl_sock_load_global_dh_param_from_file(const char *filename)
2862{
2863 global_dh = ssl_sock_get_dh_from_file(filename);
2864
2865 if (global_dh) {
2866 return 0;
2867 }
2868
2869 return -1;
2870}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002871#endif
2872
William Lallemand9117de92019-10-04 00:29:42 +02002873/* Alloc and init a ckch_inst */
2874static struct ckch_inst *ckch_inst_new()
2875{
2876 struct ckch_inst *ckch_inst;
2877
2878 ckch_inst = calloc(1, sizeof *ckch_inst);
2879 if (ckch_inst)
2880 LIST_INIT(&ckch_inst->sni_ctx);
2881
2882 return ckch_inst;
2883}
2884
2885
2886/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002887static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002888 struct bind_conf *s, struct ssl_bind_conf *conf,
2889 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002890{
2891 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002892 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002893
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002894 if (*name == '!') {
2895 neg = 1;
2896 name++;
2897 }
2898 if (*name == '*') {
2899 wild = 1;
2900 name++;
2901 }
2902 /* !* filter is a nop */
2903 if (neg && wild)
2904 return order;
2905 if (*name) {
2906 int j, len;
2907 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002908 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002909 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002910 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002911 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002912 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002913
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002914 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002915 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002916 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002917 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002918 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002919 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002920 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002921 sc->order = order++;
2922 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002923 sc->wild = wild;
2924 sc->name.node.leaf_p = NULL;
William Lallemand1d29c742019-10-04 00:53:29 +02002925 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002926 }
2927 return order;
2928}
2929
William Lallemand6af03992019-07-23 15:00:54 +02002930/*
William Lallemand1d29c742019-10-04 00:53:29 +02002931 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2932 * This function can't return an error.
2933 *
2934 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2935 */
2936static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2937{
2938
2939 struct sni_ctx *sc0, *sc0b, *sc1;
2940 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002941 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002942
2943 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2944
2945 /* ignore if sc0 was already inserted in a tree */
2946 if (sc0->name.node.leaf_p)
2947 continue;
2948
2949 /* Check for duplicates. */
2950 if (sc0->wild)
2951 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2952 else
2953 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2954
2955 for (; node; node = ebmb_next_dup(node)) {
2956 sc1 = ebmb_entry(node, struct sni_ctx, name);
2957 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2958 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2959 /* it's a duplicate, we should remove and free it */
2960 LIST_DEL(&sc0->by_ckch_inst);
2961 free(sc0);
2962 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002963 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002964 }
2965 }
2966
2967 /* if duplicate, ignore the insertion */
2968 if (!sc0)
2969 continue;
2970
2971 if (sc0->wild)
2972 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
2973 else
2974 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01002975
2976 /* replace the default_ctx if required with the first ctx */
2977 if (ckch_inst->is_default && !def) {
2978 /* we don't need to free the default_ctx because the refcount was not incremented */
2979 bind_conf->default_ctx = sc0->ctx;
2980 def = 1;
2981 }
William Lallemand1d29c742019-10-04 00:53:29 +02002982 }
2983}
2984
2985/*
William Lallemande3af8fb2019-10-08 11:36:53 +02002986 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02002987 */
William Lallemande3af8fb2019-10-08 11:36:53 +02002988struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02002989
William Lallemandfa892222019-07-23 16:06:08 +02002990
Emeric Brun7a883362019-10-17 13:27:40 +02002991/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
2992 * If there is no DH paramater availaible in the ckchs, the global
2993 * DH parameter is loaded into the SSL_CTX and if there is no
2994 * DH parameter available in ckchs nor in global, the default
2995 * DH parameters are applied on the SSL_CTX.
2996 * Returns a bitfield containing the flags:
2997 * ERR_FATAL in any fatal error case
2998 * ERR_ALERT if a reason of the error is availabine in err
2999 * ERR_WARN if a warning is available into err
3000 * The value 0 means there is no error nor warning and
3001 * the operation succeed.
3002 */
William Lallemandfa892222019-07-23 16:06:08 +02003003#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003004static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3005 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003006{
Emeric Brun7a883362019-10-17 13:27:40 +02003007 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003008 DH *dh = NULL;
3009
William Lallemanda8c73742019-07-31 18:31:34 +02003010 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003011 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003012 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3013 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3014 err && *err ? *err : "", path);
3015#if defined(SSL_CTX_set_dh_auto)
3016 SSL_CTX_set_dh_auto(ctx, 1);
3017 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3018 err && *err ? *err : "");
3019#else
3020 memprintf(err, "%s, DH ciphers won't be available.\n",
3021 err && *err ? *err : "");
3022#endif
3023 ret |= ERR_WARN;
3024 goto end;
3025 }
William Lallemandfa892222019-07-23 16:06:08 +02003026
3027 if (ssl_dh_ptr_index >= 0) {
3028 /* store a pointer to the DH params to avoid complaining about
3029 ssl-default-dh-param not being set for this SSL_CTX */
3030 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3031 }
3032 }
3033 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003034 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3035 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3036 err && *err ? *err : "", path);
3037#if defined(SSL_CTX_set_dh_auto)
3038 SSL_CTX_set_dh_auto(ctx, 1);
3039 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3040 err && *err ? *err : "");
3041#else
3042 memprintf(err, "%s, DH ciphers won't be available.\n",
3043 err && *err ? *err : "");
3044#endif
3045 ret |= ERR_WARN;
3046 goto end;
3047 }
William Lallemandfa892222019-07-23 16:06:08 +02003048 }
3049 else {
3050 /* Clear openssl global errors stack */
3051 ERR_clear_error();
3052
3053 if (global_ssl.default_dh_param <= 1024) {
3054 /* we are limited to DH parameter of 1024 bits anyway */
3055 if (local_dh_1024 == NULL)
3056 local_dh_1024 = ssl_get_dh_1024();
3057
Emeric Brun7a883362019-10-17 13:27:40 +02003058 if (local_dh_1024 == NULL) {
3059 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3060 err && *err ? *err : "", path);
3061 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003062 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003063 }
William Lallemandfa892222019-07-23 16:06:08 +02003064
Emeric Bruna9363eb2019-10-17 14:53:03 +02003065 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3066 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3067 err && *err ? *err : "", path);
3068#if defined(SSL_CTX_set_dh_auto)
3069 SSL_CTX_set_dh_auto(ctx, 1);
3070 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3071 err && *err ? *err : "");
3072#else
3073 memprintf(err, "%s, DH ciphers won't be available.\n",
3074 err && *err ? *err : "");
3075#endif
3076 ret |= ERR_WARN;
3077 goto end;
3078 }
William Lallemandfa892222019-07-23 16:06:08 +02003079 }
3080 else {
3081 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3082 }
William Lallemandfa892222019-07-23 16:06:08 +02003083 }
3084
3085end:
William Lallemandfa892222019-07-23 16:06:08 +02003086 return ret;
3087}
3088#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003089
yanbzhu488a4d22015-12-01 15:16:07 -05003090/* Frees the contents of a cert_key_and_chain
3091 */
3092static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3093{
yanbzhu488a4d22015-12-01 15:16:07 -05003094 if (!ckch)
3095 return;
3096
3097 /* Free the certificate and set pointer to NULL */
3098 if (ckch->cert)
3099 X509_free(ckch->cert);
3100 ckch->cert = NULL;
3101
3102 /* Free the key and set pointer to NULL */
3103 if (ckch->key)
3104 EVP_PKEY_free(ckch->key);
3105 ckch->key = NULL;
3106
3107 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003108 if (ckch->chain)
3109 sk_X509_pop_free(ckch->chain, X509_free);
3110 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003111
William Lallemand455af502019-10-17 18:04:45 +02003112 if (ckch->dh)
3113 DH_free(ckch->dh);
3114 ckch->dh = NULL;
3115
3116 if (ckch->sctl) {
3117 free(ckch->sctl->area);
3118 ckch->sctl->area = NULL;
3119 free(ckch->sctl);
3120 ckch->sctl = NULL;
3121 }
3122
3123 if (ckch->ocsp_response) {
3124 free(ckch->ocsp_response->area);
3125 ckch->ocsp_response->area = NULL;
3126 free(ckch->ocsp_response);
3127 ckch->ocsp_response = NULL;
3128 }
yanbzhu488a4d22015-12-01 15:16:07 -05003129}
3130
William Lallemand8d0f8932019-10-17 18:03:58 +02003131/*
3132 *
3133 * This function copy a cert_key_and_chain in memory
3134 *
3135 * It's used to try to apply changes on a ckch before committing them, because
3136 * most of the time it's not possible to revert those changes
3137 *
3138 * Return a the dst or NULL
3139 */
3140static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3141 struct cert_key_and_chain *dst)
3142{
3143 if (src->cert) {
3144 dst->cert = src->cert;
3145 X509_up_ref(src->cert);
3146 }
3147
3148 if (src->key) {
3149 dst->key = src->key;
3150 EVP_PKEY_up_ref(src->key);
3151 }
3152
3153 if (src->chain) {
3154 dst->chain = X509_chain_up_ref(src->chain);
3155 }
3156
3157 if (src->dh) {
3158 DH_up_ref(src->dh);
3159 dst->dh = src->dh;
3160 }
3161
3162 if (src->sctl) {
3163 struct buffer *sctl;
3164
3165 sctl = calloc(1, sizeof(*sctl));
3166 if (!chunk_dup(sctl, src->sctl)) {
3167 free(sctl);
3168 sctl = NULL;
3169 goto error;
3170 }
3171 dst->sctl = sctl;
3172 }
3173
3174 if (src->ocsp_response) {
3175 struct buffer *ocsp_response;
3176
3177 ocsp_response = calloc(1, sizeof(*ocsp_response));
3178 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3179 free(ocsp_response);
3180 ocsp_response = NULL;
3181 goto error;
3182 }
3183 dst->ocsp_response = ocsp_response;
3184 }
3185
3186 if (src->ocsp_issuer) {
3187 X509_up_ref(src->ocsp_issuer);
3188 dst->ocsp_issuer = src->ocsp_issuer;
3189 }
3190
3191 return dst;
3192
3193error:
3194
3195 /* free everything */
3196 ssl_sock_free_cert_key_and_chain_contents(dst);
3197
3198 return NULL;
3199}
3200
3201
yanbzhu488a4d22015-12-01 15:16:07 -05003202/* checks if a key and cert exists in the ckch
3203 */
William Lallemand1633e392019-09-30 12:58:13 +02003204#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003205static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3206{
3207 return (ckch->cert != NULL && ckch->key != NULL);
3208}
William Lallemand1633e392019-09-30 12:58:13 +02003209#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003210
William Lallemandf9568fc2019-10-16 18:27:58 +02003211/*
3212 * return 0 on success or != 0 on failure
3213 */
3214static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3215{
3216 int ret = 1;
3217 BIO *in = NULL;
3218 X509 *issuer;
3219
3220 if (buf) {
3221 /* reading from a buffer */
3222 in = BIO_new_mem_buf(buf, -1);
3223 if (in == NULL) {
3224 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3225 goto end;
3226 }
3227
3228 } else {
3229 /* reading from a file */
3230 in = BIO_new(BIO_s_file());
3231 if (in == NULL)
3232 goto end;
3233
3234 if (BIO_read_filename(in, path) <= 0)
3235 goto end;
3236 }
3237
3238 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3239 if (!issuer) {
3240 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003241 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003242 goto end;
3243 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003244 /* no error, fill ckch with new context, old context must be free */
3245 if (ckch->ocsp_issuer)
3246 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003247 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003248 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003249
3250end:
3251
3252 ERR_clear_error();
3253 if (in)
3254 BIO_free(in);
3255
3256 return ret;
3257}
3258
William Lallemand96a9c972019-10-17 11:56:17 +02003259
3260/*
3261 * Try to load a PEM file from a <path> or a buffer <buf>
3262 * The PEM must contain at least a Private Key and a Certificate,
3263 * It could contain a DH and a certificate chain.
yanbzhu488a4d22015-12-01 15:16:07 -05003264 *
William Lallemand96a9c972019-10-17 11:56:17 +02003265 * If it failed you should not attempt to use the ckch but free it.
3266 *
3267 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003268 */
William Lallemand96a9c972019-10-17 11:56:17 +02003269static int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
yanbzhu488a4d22015-12-01 15:16:07 -05003270{
William Lallemandf11365b2019-09-19 14:25:58 +02003271 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003272 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003273 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003274 X509 *cert = NULL;
3275 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003276 DH *dh = NULL;
3277 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003278
3279 if (buf) {
3280 /* reading from a buffer */
3281 in = BIO_new_mem_buf(buf, -1);
3282 if (in == NULL) {
3283 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3284 goto end;
3285 }
yanbzhu488a4d22015-12-01 15:16:07 -05003286
William Lallemand96a9c972019-10-17 11:56:17 +02003287 } else {
3288 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003289 in = BIO_new(BIO_s_file());
3290 if (in == NULL)
3291 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003292
William Lallemandf11365b2019-09-19 14:25:58 +02003293 if (BIO_read_filename(in, path) <= 0)
3294 goto end;
William Lallemandf11365b2019-09-19 14:25:58 +02003295 }
yanbzhu488a4d22015-12-01 15:16:07 -05003296
yanbzhu488a4d22015-12-01 15:16:07 -05003297 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003298 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3299 if (key == NULL) {
yanbzhu488a4d22015-12-01 15:16:07 -05003300 memprintf(err, "%sunable to load private key from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003301 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003302 goto end;
3303 }
3304
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003305#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003306 /* Seek back to beginning of file */
3307 if (BIO_reset(in) == -1) {
3308 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3309 err && *err ? *err : "", path);
3310 goto end;
3311 }
3312
William Lallemand96a9c972019-10-17 11:56:17 +02003313 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3314 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003315#endif
William Lallemandfa892222019-07-23 16:06:08 +02003316
Willy Tarreaubb137a82016-04-06 19:02:38 +02003317 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003318 if (BIO_reset(in) == -1) {
3319 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3320 err && *err ? *err : "", path);
3321 goto end;
3322 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003323
3324 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003325 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3326 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003327 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003328 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003329 goto end;
3330 }
3331
William Lallemand96a9c972019-10-17 11:56:17 +02003332 if (!X509_check_private_key(cert, key)) {
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003333 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003334 err && *err ? *err : "", path);
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003335 goto end;
3336 }
3337
William Lallemand96a9c972019-10-17 11:56:17 +02003338 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003339 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3340 if (chain == NULL)
3341 chain = sk_X509_new_null();
3342 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003343 X509_free(ca);
3344 goto end;
3345 }
3346 }
yanbzhu488a4d22015-12-01 15:16:07 -05003347
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003348 /* no chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003349 if (chain == NULL) {
3350 chain = sk_X509_new_null();
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003351 }
3352
yanbzhu488a4d22015-12-01 15:16:07 -05003353 ret = ERR_get_error();
3354 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3355 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003356 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003357 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003358 }
3359
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003360 /* no error, fill ckch with new context, old context will be free at end: */
3361 SWAP(ckch->key, key);
3362 SWAP(ckch->dh, dh);
3363 SWAP(ckch->cert, cert);
3364 SWAP(ckch->chain, chain);
3365
William Lallemand246c0242019-10-11 08:59:13 +02003366 ret = 0;
3367
William Lallemand96a9c972019-10-17 11:56:17 +02003368end:
William Lallemand246c0242019-10-11 08:59:13 +02003369
3370 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003371 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003372 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003373 if (key)
3374 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003375 if (dh)
3376 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003377 if (cert)
3378 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003379 if (chain)
3380 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003381
William Lallemand96a9c972019-10-17 11:56:17 +02003382 return ret;
3383}
3384
3385/*
3386 * Try to load in a ckch every files related to a ckch.
3387 * (PEM, sctl, ocsp, issuer etc.)
3388 *
3389 * This function is only used to load files during the configuration parsing,
3390 * it is not used with the CLI.
3391 *
3392 * This allows us to carry the contents of the file without having to read the
3393 * file multiple times. The caller must call
3394 * ssl_sock_free_cert_key_and_chain_contents.
3395 *
3396 * returns:
3397 * 0 on Success
3398 * 1 on SSL Failure
3399 */
3400static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3401{
3402 int ret = 1;
3403
3404 /* try to load the PEM */
3405 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3406 goto end;
3407 }
3408
William Lallemanda17f4112019-10-10 15:16:44 +02003409#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3410 /* try to load the sctl file */
3411 {
3412 char fp[MAXPATHLEN+1];
3413 struct stat st;
3414
3415 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3416 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003417 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003418 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003419 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003420 ret = 1;
3421 goto end;
3422 }
3423 }
3424 }
3425#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003426
William Lallemand246c0242019-10-11 08:59:13 +02003427 /* try to load an ocsp response file */
3428 {
3429 char fp[MAXPATHLEN+1];
3430 struct stat st;
3431
3432 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3433 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003434 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003435 ret = 1;
3436 goto end;
3437 }
3438 }
3439 }
3440
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003441#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand246c0242019-10-11 08:59:13 +02003442 if (ckch->ocsp_response) {
3443 X509 *issuer;
3444 int i;
3445
3446 /* check if one of the certificate of the chain is the issuer */
3447 for (i = 0; i < sk_X509_num(ckch->chain); i++) {
3448 issuer = sk_X509_value(ckch->chain, i);
3449 if (X509_check_issued(issuer, ckch->cert) == X509_V_OK) {
3450 ckch->ocsp_issuer = issuer;
3451 break;
3452 } else
3453 issuer = NULL;
3454 }
3455
3456 /* if no issuer was found, try to load an issuer from the .issuer */
3457 if (!issuer) {
3458 struct stat st;
3459 char fp[MAXPATHLEN+1];
3460
3461 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3462 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003463 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003464 ret = 1;
3465 goto end;
3466 }
3467
3468 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003469 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003470 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003471 ret = 1;
3472 goto end;
3473 }
3474 } else {
3475 memprintf(err, "%sNo issuer found, cannot use the OCSP response'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003476 err && *err ? *err : "");
William Lallemand246c0242019-10-11 08:59:13 +02003477 ret = 1;
3478 goto end;
3479 }
3480 }
3481 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003482#endif
William Lallemand246c0242019-10-11 08:59:13 +02003483
yanbzhu488a4d22015-12-01 15:16:07 -05003484 ret = 0;
3485
3486end:
3487
3488 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003489
3490 /* Something went wrong in one of the reads */
3491 if (ret != 0)
3492 ssl_sock_free_cert_key_and_chain_contents(ckch);
3493
3494 return ret;
3495}
3496
3497/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003498 * Returns a bitfield containing the flags:
3499 * ERR_FATAL in any fatal error case
3500 * ERR_ALERT if the reason of the error is available in err
3501 * ERR_WARN if a warning is available into err
3502 * The value 0 means there is no error nor warning and
3503 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003504 */
3505static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3506{
Emeric Bruna96b5822019-10-17 13:25:14 +02003507 int errcode = 0;
3508
yanbzhu488a4d22015-12-01 15:16:07 -05003509 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3510 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3511 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003512 errcode |= ERR_ALERT | ERR_FATAL;
3513 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003514 }
3515
3516 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3517 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3518 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003519 errcode |= ERR_ALERT | ERR_FATAL;
3520 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003521 }
3522
yanbzhu488a4d22015-12-01 15:16:07 -05003523 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003524#ifdef SSL_CTX_set1_chain
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003525 if (!SSL_CTX_set1_chain(ctx, ckch->chain)) {
3526 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3527 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003528 errcode |= ERR_ALERT | ERR_FATAL;
3529 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003530 }
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003531#else
3532 { /* legacy compat (< openssl 1.0.2) */
3533 X509 *ca;
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003534 STACK_OF(X509) *chain;
3535 chain = X509_chain_up_ref(ckch->chain);
3536 while ((ca = sk_X509_shift(chain)))
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003537 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3538 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3539 err && *err ? *err : "", path);
3540 X509_free(ca);
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003541 sk_X509_pop_free(chain, X509_free);
Emeric Bruna96b5822019-10-17 13:25:14 +02003542 errcode |= ERR_ALERT | ERR_FATAL;
3543 goto end;
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003544 }
3545 }
3546#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003547
William Lallemandfa892222019-07-23 16:06:08 +02003548#ifndef OPENSSL_NO_DH
3549 /* store a NULL pointer to indicate we have not yet loaded
3550 a custom DH param file */
3551 if (ssl_dh_ptr_index >= 0) {
3552 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3553 }
3554
Emeric Brun7a883362019-10-17 13:27:40 +02003555 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3556 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003557 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3558 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003559 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003560 }
3561#endif
3562
William Lallemanda17f4112019-10-10 15:16:44 +02003563#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3564 if (sctl_ex_index >= 0 && ckch->sctl) {
3565 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3566 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003567 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003568 errcode |= ERR_ALERT | ERR_FATAL;
3569 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003570 }
3571 }
3572#endif
3573
William Lallemand4a660132019-10-14 14:51:41 +02003574#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003575 /* Load OCSP Info into context */
3576 if (ckch->ocsp_response) {
3577 if (ssl_sock_load_ocsp(ctx, ckch) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003578 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",
3579 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003580 errcode |= ERR_ALERT | ERR_FATAL;
3581 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003582 }
3583 }
William Lallemand246c0242019-10-11 08:59:13 +02003584#endif
3585
Emeric Bruna96b5822019-10-17 13:25:14 +02003586 end:
3587 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003588}
3589
William Lallemandc4ecddf2019-07-31 16:50:08 +02003590#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003591
William Lallemand28a8fce2019-10-04 17:36:55 +02003592static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003593{
3594 struct sni_keytype *s_kt = NULL;
3595 struct ebmb_node *node;
3596 int i;
3597
3598 for (i = 0; i < trash.size; i++) {
3599 if (!str[i])
3600 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003601 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003602 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003603 trash.area[i] = 0;
3604 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003605 if (!node) {
3606 /* CN not found in tree */
3607 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3608 /* Using memcpy here instead of strncpy.
3609 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3610 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3611 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003612 if (!s_kt)
3613 return -1;
3614
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003615 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003616 s_kt->keytypes = 0;
3617 ebst_insert(sni_keytypes, &s_kt->name);
3618 } else {
3619 /* CN found in tree */
3620 s_kt = container_of(node, struct sni_keytype, name);
3621 }
3622
3623 /* Mark that this CN has the keytype of key_index via keytypes mask */
3624 s_kt->keytypes |= 1<<key_index;
3625
William Lallemand28a8fce2019-10-04 17:36:55 +02003626 return 0;
3627
yanbzhu08ce6ab2015-12-02 13:01:29 -05003628}
3629
William Lallemandc4ecddf2019-07-31 16:50:08 +02003630#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003631/*
3632 * Free a ckch_store and its ckch(s)
3633 * The linked ckch_inst are not free'd
3634 */
3635void ckchs_free(struct ckch_store *ckchs)
3636{
3637 if (!ckchs)
3638 return;
3639
3640#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3641 if (ckchs->multi) {
3642 int n;
3643
3644 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3645 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
3646 } else
3647#endif
3648 {
3649 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
3650 ckchs->ckch = NULL;
3651 }
3652
3653 free(ckchs);
3654}
3655
3656/* allocate and duplicate a ckch_store
3657 * Return a new ckch_store or NULL */
3658static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3659{
3660 struct ckch_store *dst;
3661 int pathlen;
3662
3663 pathlen = strlen(src->path);
3664 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3665 if (!dst)
3666 return NULL;
3667 /* copy previous key */
3668 memcpy(dst->path, src->path, pathlen + 1);
3669 dst->multi = src->multi;
3670 LIST_INIT(&dst->ckch_inst);
3671
3672 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3673 if (!dst->ckch)
3674 goto error;
3675
3676#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3677 if (src->multi) {
3678 int n;
3679
3680 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3681 if (&src->ckch[n]) {
3682 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3683 goto error;
3684 }
3685 }
3686 } else
3687#endif
3688 {
3689 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3690 goto error;
3691 }
3692
3693 return dst;
3694
3695error:
3696 ckchs_free(dst);
3697
3698 return NULL;
3699}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003700
William Lallemand36b84632019-07-18 19:28:17 +02003701/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003702 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003703 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003704static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003705{
3706 struct ebmb_node *eb;
3707
William Lallemande3af8fb2019-10-08 11:36:53 +02003708 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003709 if (!eb)
3710 return NULL;
3711
William Lallemande3af8fb2019-10-08 11:36:53 +02003712 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003713}
3714
3715/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003716 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003717 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003718static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003719{
William Lallemande3af8fb2019-10-08 11:36:53 +02003720 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003721
William Lallemande3af8fb2019-10-08 11:36:53 +02003722 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3723 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003724 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3725 goto end;
3726 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003727 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003728
William Lallemande3af8fb2019-10-08 11:36:53 +02003729 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003730 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3731 goto end;
3732 }
3733
William Lallemand9117de92019-10-04 00:29:42 +02003734 LIST_INIT(&ckchs->ckch_inst);
3735
William Lallemand36b84632019-07-18 19:28:17 +02003736 if (!multi) {
3737
William Lallemand96a9c972019-10-17 11:56:17 +02003738 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003739 goto end;
3740
William Lallemande3af8fb2019-10-08 11:36:53 +02003741 /* insert into the ckchs tree */
3742 memcpy(ckchs->path, path, strlen(path) + 1);
3743 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003744 } else {
3745 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003746#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3747 char fp[MAXPATHLEN+1] = {0};
3748 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003749
3750 /* Load all possible certs and keys */
3751 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3752 struct stat buf;
3753 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3754 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003755 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003756 goto end;
3757 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003758 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003759 }
3760 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003761#endif
William Lallemand36b84632019-07-18 19:28:17 +02003762
3763 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003764 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003765 goto end;
3766 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003767 /* insert into the ckchs tree */
3768 memcpy(ckchs->path, path, strlen(path) + 1);
3769 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003770 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003771 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003772
William Lallemand36b84632019-07-18 19:28:17 +02003773end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003774 if (ckchs) {
3775 free(ckchs->ckch);
3776 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003777 }
3778
William Lallemande3af8fb2019-10-08 11:36:53 +02003779 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003780
3781 return NULL;
3782}
3783
William Lallemandc4ecddf2019-07-31 16:50:08 +02003784#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3785
William Lallemand36b84632019-07-18 19:28:17 +02003786/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003787 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003788 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003789 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3790 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003791 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003792 *
Emeric Brun054563d2019-10-17 13:16:58 +02003793 * Returns a bitfield containing the flags:
3794 * ERR_FATAL in any fatal error case
3795 * ERR_ALERT if the reason of the error is available in err
3796 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003797 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003798 */
Emeric Brun054563d2019-10-17 13:16:58 +02003799static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3800 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3801 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003802{
William Lallemand36b84632019-07-18 19:28:17 +02003803 int i = 0, n = 0;
3804 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003805 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003806 struct ebmb_node *node;
3807 struct ebmb_node *next;
3808 /* Array of SSL_CTX pointers corresponding to each possible combo
3809 * of keytypes
3810 */
3811 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003812 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003813 X509_NAME *xname = NULL;
3814 char *str = NULL;
3815#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3816 STACK_OF(GENERAL_NAME) *names = NULL;
3817#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003818 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003819
Emeric Brun054563d2019-10-17 13:16:58 +02003820 *ckchi = NULL;
3821
William Lallemande3af8fb2019-10-08 11:36:53 +02003822 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003823 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3824 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003825 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003826 }
3827
3828 ckch_inst = ckch_inst_new();
3829 if (!ckch_inst) {
3830 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3831 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003832 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003833 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003834 }
3835
William Lallemande3af8fb2019-10-08 11:36:53 +02003836 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003837
William Lallemand150bfa82019-09-19 17:12:49 +02003838 /* at least one of the instances is using filters during the config
3839 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01003840 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02003841
yanbzhu08ce6ab2015-12-02 13:01:29 -05003842 /* Process each ckch and update keytypes for each CN/SAN
3843 * for example, if CN/SAN www.a.com is associated with
3844 * certs with keytype 0 and 2, then at the end of the loop,
3845 * www.a.com will have:
3846 * keyindex = 0 | 1 | 4 = 5
3847 */
3848 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003849 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003850
3851 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3852 continue;
3853
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003854 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003855 for (i = 0; i < fcount; i++) {
3856 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3857 if (ret < 0) {
3858 memprintf(err, "%sunable to allocate SSL context.\n",
3859 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003860 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003861 goto end;
3862 }
3863 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003864 } else {
3865 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3866 * so the line that contains logic is marked via comments
3867 */
3868 xname = X509_get_subject_name(certs_and_keys[n].cert);
3869 i = -1;
3870 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3871 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003872 ASN1_STRING *value;
3873 value = X509_NAME_ENTRY_get_data(entry);
3874 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003875 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003876 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003877
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003878 OPENSSL_free(str);
3879 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003880 if (ret < 0) {
3881 memprintf(err, "%sunable to allocate SSL context.\n",
3882 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003883 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003884 goto end;
3885 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003886 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003887 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003888
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003889 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003890#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003891 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3892 if (names) {
3893 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3894 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003895
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003896 if (name->type == GEN_DNS) {
3897 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3898 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003899 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003900
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003901 OPENSSL_free(str);
3902 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003903 if (ret < 0) {
3904 memprintf(err, "%sunable to allocate SSL context.\n",
3905 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003906 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003907 goto end;
3908 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003909 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003910 }
3911 }
3912 }
3913 }
3914#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3915 }
3916
3917 /* If no files found, return error */
3918 if (eb_is_empty(&sni_keytypes_map)) {
3919 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3920 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003921 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003922 goto end;
3923 }
3924
3925 /* We now have a map of CN/SAN to keytypes that are loaded in
3926 * Iterate through the map to create the SSL_CTX's (if needed)
3927 * and add each CTX to the SNI tree
3928 *
3929 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003930 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003931 * combination is denoted by the key in the map. Each key
3932 * has a value between 1 and 2^n - 1. Conveniently, the array
3933 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3934 * entry in the array to correspond to the unique combo (key)
3935 * associated with i. This unique key combo (i) will be associated
3936 * with combos[i-1]
3937 */
3938
3939 node = ebmb_first(&sni_keytypes_map);
3940 while (node) {
3941 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003942 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003943 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003944
3945 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3946 i = container_of(node, struct sni_keytype, name)->keytypes;
3947 cur_ctx = key_combos[i-1].ctx;
3948
3949 if (cur_ctx == NULL) {
3950 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003951 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003952 if (cur_ctx == NULL) {
3953 memprintf(err, "%sunable to allocate SSL context.\n",
3954 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003955 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003956 goto end;
3957 }
3958
yanbzhube2774d2015-12-10 15:07:30 -05003959 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003960 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3961 if (i & (1<<n)) {
3962 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003963 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02003964 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3965 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003966 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003967 }
3968 }
3969
yanbzhu08ce6ab2015-12-02 13:01:29 -05003970 /* Update key_combos */
3971 key_combos[i-1].ctx = cur_ctx;
3972 }
3973
3974 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02003975
William Lallemand1d29c742019-10-04 00:53:29 +02003976 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 +02003977 kinfo, str, key_combos[i-1].order);
3978 if (key_combos[i-1].order < 0) {
3979 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003980 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02003981 goto end;
3982 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003983 node = ebmb_next(node);
3984 }
3985
3986
3987 /* Mark a default context if none exists, using the ctx that has the most shared keys */
3988 if (!bind_conf->default_ctx) {
3989 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
3990 if (key_combos[i].ctx) {
3991 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003992 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01003993 ckch_inst->is_default = 1;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003994 break;
3995 }
3996 }
3997 }
3998
William Lallemand614ca0d2019-10-07 13:52:11 +02003999 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004000 ckch_inst->ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004001end:
4002
4003 if (names)
4004 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4005
yanbzhu08ce6ab2015-12-02 13:01:29 -05004006 node = ebmb_first(&sni_keytypes_map);
4007 while (node) {
4008 next = ebmb_next(node);
4009 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004010 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004011 node = next;
4012 }
4013
Emeric Brun054563d2019-10-17 13:16:58 +02004014 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02004015 struct sni_ctx *sc0, *sc0b;
4016
4017 /* free the SSL_CTX in case of error */
4018 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4019 if (key_combos[i].ctx)
4020 SSL_CTX_free(key_combos[i].ctx);
4021 }
4022
4023 /* free the sni_ctx in case of error */
4024 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4025
4026 ebmb_delete(&sc0->name);
4027 LIST_DEL(&sc0->by_ckch_inst);
4028 free(sc0);
4029 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004030 free(ckch_inst);
4031 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004032 }
4033
Emeric Brun054563d2019-10-17 13:16:58 +02004034 *ckchi = ckch_inst;
4035 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004036}
4037#else
4038/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004039static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4040 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4041 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004042{
4043 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4044 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004045 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004046}
4047
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004048#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004049
William Lallemand614ca0d2019-10-07 13:52:11 +02004050/*
4051 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004052 *
4053 * Returns a bitfield containing the flags:
4054 * ERR_FATAL in any fatal error case
4055 * ERR_ALERT if the reason of the error is available in err
4056 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004057 */
Emeric Brun054563d2019-10-17 13:16:58 +02004058static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4059 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004060{
William Lallemandc9402072019-05-15 15:33:54 +02004061 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004062 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004063 int order = 0;
4064 X509_NAME *xname;
4065 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004066 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004067 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004068#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4069 STACK_OF(GENERAL_NAME) *names;
4070#endif
William Lallemand36b84632019-07-18 19:28:17 +02004071 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004072 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004073 int errcode = 0;
4074
4075 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004076
William Lallemande3af8fb2019-10-08 11:36:53 +02004077 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004078 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004079
William Lallemande3af8fb2019-10-08 11:36:53 +02004080 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004081
William Lallemand150bfa82019-09-19 17:12:49 +02004082 /* at least one of the instances is using filters during the config
4083 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01004084 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02004085
William Lallemandc9402072019-05-15 15:33:54 +02004086 ctx = SSL_CTX_new(SSLv23_server_method());
4087 if (!ctx) {
4088 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4089 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004090 errcode |= ERR_ALERT | ERR_FATAL;
4091 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004092 }
4093
Emeric Bruna96b5822019-10-17 13:25:14 +02004094 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4095 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004096 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004097
4098 ckch_inst = ckch_inst_new();
4099 if (!ckch_inst) {
4100 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4101 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004102 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004103 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004104 }
4105
William Lallemand36b84632019-07-18 19:28:17 +02004106 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004107 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004108 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004109 switch(EVP_PKEY_base_id(pkey)) {
4110 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004111 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004112 break;
4113 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004114 kinfo.sig = TLSEXT_signature_ecdsa;
4115 break;
4116 case EVP_PKEY_DSA:
4117 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004118 break;
4119 }
4120 EVP_PKEY_free(pkey);
4121 }
4122
Emeric Brun50bcecc2013-04-22 13:05:23 +02004123 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004124 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004125 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 +02004126 if (order < 0) {
4127 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004128 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004129 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004130 }
4131 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004132 }
4133 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004134#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004135 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004136 if (names) {
4137 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4138 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4139 if (name->type == GEN_DNS) {
4140 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004141 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004142 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004143 if (order < 0) {
4144 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004145 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004146 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004147 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004148 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004149 }
4150 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004151 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004152 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004153#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004154 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004155 i = -1;
4156 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4157 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004158 ASN1_STRING *value;
4159
4160 value = X509_NAME_ENTRY_get_data(entry);
4161 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004162 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004163 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004164 if (order < 0) {
4165 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004166 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004167 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004168 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004169 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004170 }
4171 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004172 /* we must not free the SSL_CTX anymore below, since it's already in
4173 * the tree, so it will be discovered and cleaned in time.
4174 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004175
Emeric Brunfc0421f2012-09-07 17:30:07 +02004176#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004177 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004178 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4179 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004180 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004181 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004182 }
4183#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004184 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004185 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004186 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004187 ckch_inst->is_default = 1;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004188 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004189
William Lallemand9117de92019-10-04 00:29:42 +02004190 /* everything succeed, the ckch instance can be used */
4191 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004192 ckch_inst->ssl_conf = ssl_conf;
William Lallemand9117de92019-10-04 00:29:42 +02004193
Emeric Brun054563d2019-10-17 13:16:58 +02004194 *ckchi = ckch_inst;
4195 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004196
4197error:
4198 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004199 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004200 struct sni_ctx *sc0, *sc0b;
4201
4202 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4203
4204 ebmb_delete(&sc0->name);
4205 LIST_DEL(&sc0->by_ckch_inst);
4206 free(sc0);
4207 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004208 free(ckch_inst);
4209 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004210 }
4211 /* We only created 1 SSL_CTX so we can free it there */
4212 SSL_CTX_free(ctx);
4213
Emeric Brun054563d2019-10-17 13:16:58 +02004214 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004215}
4216
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004217/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004218static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4219 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4220 char **sni_filter, int fcount, char **err)
4221{
4222 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004223 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004224
4225 /* we found the ckchs in the tree, we can use it directly */
4226 if (ckchs->multi)
Emeric Brun054563d2019-10-17 13:16:58 +02004227 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 +02004228 else
Emeric Brun054563d2019-10-17 13:16:58 +02004229 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 +02004230
Emeric Brun054563d2019-10-17 13:16:58 +02004231 if (errcode & ERR_CODE)
4232 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004233
4234 ssl_sock_load_cert_sni(ckch_inst, bind_conf);
4235
4236 /* succeed, add the instance to the ckch_store's list of instance */
4237 LIST_ADDQ(&ckchs->ckch_inst, &ckch_inst->by_ckchs);
Emeric Brun054563d2019-10-17 13:16:58 +02004238 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004239}
4240
4241
Willy Tarreaubbc91962019-10-16 16:42:19 +02004242/* Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau03209342016-12-22 17:08:28 +01004243int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004244{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004245 struct dirent **de_list;
4246 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004247 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004248 char *end;
4249 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004250 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004251 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004252#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004253 int is_bundle;
4254 int j;
4255#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004256 if ((ckchs = ckchs_lookup(path))) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004257 /* we found the ckchs in the tree, we can use it directly */
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004258 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand6af03992019-07-23 15:00:54 +02004259 }
4260
yanbzhu08ce6ab2015-12-02 13:01:29 -05004261 if (stat(path, &buf) == 0) {
William Dauchy9a8ef7f2020-01-13 17:52:49 +01004262 if (S_ISDIR(buf.st_mode) == 0) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004263 ckchs = ckchs_load_cert_file(path, 0, err);
4264 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004265 return ERR_ALERT | ERR_FATAL;
4266
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004267 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004268 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004269
yanbzhu08ce6ab2015-12-02 13:01:29 -05004270 /* strip trailing slashes, including first one */
4271 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
4272 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004273
yanbzhu08ce6ab2015-12-02 13:01:29 -05004274 n = scandir(path, &de_list, 0, alphasort);
4275 if (n < 0) {
4276 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4277 err && *err ? *err : "", path, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004278 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004279 }
4280 else {
4281 for (i = 0; i < n; i++) {
4282 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004283
yanbzhu08ce6ab2015-12-02 13:01:29 -05004284 end = strrchr(de->d_name, '.');
4285 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
4286 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004287
yanbzhu08ce6ab2015-12-02 13:01:29 -05004288 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4289 if (stat(fp, &buf) != 0) {
4290 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4291 err && *err ? *err : "", fp, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004292 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004293 goto ignore_entry;
4294 }
4295 if (!S_ISREG(buf.st_mode))
4296 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004297
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004298#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004299 is_bundle = 0;
4300 /* Check if current entry in directory is part of a multi-cert bundle */
4301
4302 if (end) {
4303 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4304 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4305 is_bundle = 1;
4306 break;
4307 }
4308 }
4309
4310 if (is_bundle) {
yanbzhu63ea8462015-12-09 13:35:14 -05004311 int dp_len;
4312
4313 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004314
4315 /* increment i and free de until we get to a non-bundle cert
4316 * Note here that we look at de_list[i + 1] before freeing de
Willy Tarreau05800522019-10-29 10:48:50 +01004317 * this is important since ignore_entry will free de. This also
4318 * guarantees that de->d_name continues to hold the same prefix.
yanbzhu63ea8462015-12-09 13:35:14 -05004319 */
Willy Tarreau05800522019-10-29 10:48:50 +01004320 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
yanbzhu63ea8462015-12-09 13:35:14 -05004321 free(de);
4322 i++;
4323 de = de_list[i];
4324 }
4325
Willy Tarreau05800522019-10-29 10:48:50 +01004326 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemande3af8fb2019-10-08 11:36:53 +02004327 if ((ckchs = ckchs_lookup(fp)) == NULL)
4328 ckchs = ckchs_load_cert_file(fp, 1, err);
4329 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004330 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004331 else
4332 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05004333 /* Successfully processed the bundle */
4334 goto ignore_entry;
4335 }
4336 }
4337
4338#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004339 if ((ckchs = ckchs_lookup(fp)) == NULL)
4340 ckchs = ckchs_load_cert_file(fp, 0, err);
4341 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004342 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004343 else
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004344 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004345
yanbzhu08ce6ab2015-12-02 13:01:29 -05004346ignore_entry:
4347 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004348 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004349 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004350 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004351 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004352 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004353
William Lallemande3af8fb2019-10-08 11:36:53 +02004354 ckchs = ckchs_load_cert_file(path, 1, err);
4355 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004356 return ERR_ALERT | ERR_FATAL;
4357
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004358 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004359
Emeric Brunfc0421f2012-09-07 17:30:07 +02004360 return cfgerr;
4361}
4362
Thierry Fournier383085f2013-01-24 14:15:43 +01004363/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4364 * done once. Zero is returned if the operation fails. No error is returned
4365 * if the random is said as not implemented, because we expect that openssl
4366 * will use another method once needed.
4367 */
4368static int ssl_initialize_random()
4369{
4370 unsigned char random;
4371 static int random_initialized = 0;
4372
4373 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4374 random_initialized = 1;
4375
4376 return random_initialized;
4377}
4378
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004379/* release ssl bind conf */
4380void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004381{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004382 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01004383#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004384 free(conf->npn_str);
4385 conf->npn_str = NULL;
4386#endif
4387#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4388 free(conf->alpn_str);
4389 conf->alpn_str = NULL;
4390#endif
4391 free(conf->ca_file);
4392 conf->ca_file = NULL;
4393 free(conf->crl_file);
4394 conf->crl_file = NULL;
4395 free(conf->ciphers);
4396 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004397#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004398 free(conf->ciphersuites);
4399 conf->ciphersuites = NULL;
4400#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004401 free(conf->curves);
4402 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004403 free(conf->ecdhe);
4404 conf->ecdhe = NULL;
4405 }
4406}
4407
Willy Tarreaubbc91962019-10-16 16:42:19 +02004408/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004409int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
4410{
4411 char thisline[CRT_LINESIZE];
4412 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004413 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004414 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004415 int linenum = 0;
4416 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004417 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004418
Willy Tarreauad1731d2013-04-02 17:35:58 +02004419 if ((f = fopen(file, "r")) == NULL) {
4420 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004421 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004422 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004423
4424 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004425 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004426 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004427 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004428 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004429 char *crt_path;
4430 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004431
4432 linenum++;
4433 end = line + strlen(line);
4434 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4435 /* Check if we reached the limit and the last char is not \n.
4436 * Watch out for the last line without the terminating '\n'!
4437 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004438 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4439 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004440 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004441 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004442 }
4443
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004444 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004445 newarg = 1;
4446 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004447 if (*line == '#' || *line == '\n' || *line == '\r') {
4448 /* end of string, end of loop */
4449 *line = 0;
4450 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004451 } else if (isspace(*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02004452 newarg = 1;
4453 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004454 } else if (*line == '[') {
4455 if (ssl_b) {
4456 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004457 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004458 break;
4459 }
4460 if (!arg) {
4461 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004462 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004463 break;
4464 }
4465 ssl_b = arg;
4466 newarg = 1;
4467 *line = 0;
4468 } else if (*line == ']') {
4469 if (ssl_e) {
4470 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004471 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004472 break;
4473 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004474 if (!ssl_b) {
4475 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004476 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004477 break;
4478 }
4479 ssl_e = arg;
4480 newarg = 1;
4481 *line = 0;
4482 } else if (newarg) {
4483 if (arg == MAX_CRT_ARGS) {
4484 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004485 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004486 break;
4487 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004488 newarg = 0;
4489 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004490 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004491 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004492 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004493 if (cfgerr)
4494 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004495 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004496
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004497 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004498 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004499 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004500
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004501 crt_path = args[0];
4502 if (*crt_path != '/' && global_ssl.crt_base) {
4503 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4504 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4505 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004506 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004507 break;
4508 }
4509 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4510 crt_path = path;
4511 }
4512
4513 ssl_conf = calloc(1, sizeof *ssl_conf);
4514 cur_arg = ssl_b ? ssl_b : 1;
4515 while (cur_arg < ssl_e) {
4516 newarg = 0;
4517 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4518 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4519 newarg = 1;
Willy Tarreaubbc91962019-10-16 16:42:19 +02004520 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004521 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4522 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4523 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004524 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004525 }
4526 cur_arg += 1 + ssl_bind_kws[i].skip;
4527 break;
4528 }
4529 }
4530 if (!cfgerr && !newarg) {
4531 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4532 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004533 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004534 break;
4535 }
4536 }
Willy Tarreaubbc91962019-10-16 16:42:19 +02004537
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004538 if (cfgerr) {
4539 ssl_sock_free_ssl_conf(ssl_conf);
4540 free(ssl_conf);
4541 ssl_conf = NULL;
4542 break;
4543 }
4544
William Lallemande3af8fb2019-10-08 11:36:53 +02004545 if ((ckchs = ckchs_lookup(crt_path)) == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004546 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004547 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004548 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004549 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004550 }
4551
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004552 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004553 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004554 else
4555 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 +02004556
Willy Tarreauad1731d2013-04-02 17:35:58 +02004557 if (cfgerr) {
4558 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004559 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004560 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004561 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004562 fclose(f);
4563 return cfgerr;
4564}
4565
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004566/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004567static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004568ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004569{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004570 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004571 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004572 SSL_OP_ALL | /* all known workarounds for bugs */
4573 SSL_OP_NO_SSLv2 |
4574 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004575 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02004576 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004577 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02004578 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004579 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004580 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004581 SSL_MODE_ENABLE_PARTIAL_WRITE |
4582 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004583 SSL_MODE_RELEASE_BUFFERS |
4584 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004585 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004586 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004587 int flags = MC_SSL_O_ALL;
4588 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004589
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004590 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004591 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004592
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004593 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004594 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
4595 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4596 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004597 else
4598 flags = conf_ssl_methods->flags;
4599
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004600 min = conf_ssl_methods->min;
4601 max = conf_ssl_methods->max;
4602 /* start with TLSv10 to remove SSLv3 per default */
4603 if (!min && (!max || max >= CONF_TLSV10))
4604 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004605 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004606 if (min)
4607 flags |= (methodVersions[min].flag - 1);
4608 if (max)
4609 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004610 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004611 min = max = CONF_TLSV_NONE;
4612 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004613 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004614 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004615 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004616 if (min) {
4617 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004618 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
4619 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4620 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
4621 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004622 hole = 0;
4623 }
4624 max = i;
4625 }
4626 else {
4627 min = max = i;
4628 }
4629 }
4630 else {
4631 if (min)
4632 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004633 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004634 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004635 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4636 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004637 cfgerr += 1;
4638 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004639 /* save real min/max in bind_conf */
4640 conf_ssl_methods->min = min;
4641 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004642
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004643#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004644 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004645 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004646 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004647 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004648 else
4649 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4650 if (flags & methodVersions[i].flag)
4651 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004652#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004653 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004654 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4655 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004656#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004657
4658 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4659 options |= SSL_OP_NO_TICKET;
4660 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4661 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004662
4663#ifdef SSL_OP_NO_RENEGOTIATION
4664 options |= SSL_OP_NO_RENEGOTIATION;
4665#endif
4666
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004667 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004668
Willy Tarreau5db847a2019-05-09 14:13:35 +02004669#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004670 if (global_ssl.async)
4671 mode |= SSL_MODE_ASYNC;
4672#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004673 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004674 if (global_ssl.life_time)
4675 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004676
4677#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4678#ifdef OPENSSL_IS_BORINGSSL
4679 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4680 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05004681#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01004682 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004683 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004684 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4685 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004686#else
4687 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004688#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004689 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004690#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004691 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004692}
4693
William Lallemand4f45bb92017-10-30 20:08:51 +01004694
4695static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4696{
4697 if (first == block) {
4698 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4699 if (first->len > 0)
4700 sh_ssl_sess_tree_delete(sh_ssl_sess);
4701 }
4702}
4703
4704/* return first block from sh_ssl_sess */
4705static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4706{
4707 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4708
4709}
4710
4711/* store a session into the cache
4712 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4713 * data: asn1 encoded session
4714 * data_len: asn1 encoded session length
4715 * Returns 1 id session was stored (else 0)
4716 */
4717static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4718{
4719 struct shared_block *first;
4720 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4721
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004722 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004723 if (!first) {
4724 /* Could not retrieve enough free blocks to store that session */
4725 return 0;
4726 }
4727
4728 /* STORE the key in the first elem */
4729 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4730 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4731 first->len = sizeof(struct sh_ssl_sess_hdr);
4732
4733 /* it returns the already existing node
4734 or current node if none, never returns null */
4735 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4736 if (oldsh_ssl_sess != sh_ssl_sess) {
4737 /* NOTE: Row couldn't be in use because we lock read & write function */
4738 /* release the reserved row */
4739 shctx_row_dec_hot(ssl_shctx, first);
4740 /* replace the previous session already in the tree */
4741 sh_ssl_sess = oldsh_ssl_sess;
4742 /* ignore the previous session data, only use the header */
4743 first = sh_ssl_sess_first_block(sh_ssl_sess);
4744 shctx_row_inc_hot(ssl_shctx, first);
4745 first->len = sizeof(struct sh_ssl_sess_hdr);
4746 }
4747
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004748 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004749 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004750 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004751 }
4752
4753 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004754
4755 return 1;
4756}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004757
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004758/* SSL callback used when a new session is created while connecting to a server */
4759static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4760{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004761 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004762 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004763
Willy Tarreau07d94e42018-09-20 10:57:52 +02004764 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004765
Olivier Houcharde6060c52017-11-16 17:42:52 +01004766 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4767 int len;
4768 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004769
Olivier Houcharde6060c52017-11-16 17:42:52 +01004770 len = i2d_SSL_SESSION(sess, NULL);
4771 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4772 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4773 } else {
4774 free(s->ssl_ctx.reused_sess[tid].ptr);
4775 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4776 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4777 }
4778 if (s->ssl_ctx.reused_sess[tid].ptr) {
4779 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4780 &ptr);
4781 }
4782 } else {
4783 free(s->ssl_ctx.reused_sess[tid].ptr);
4784 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4785 }
4786
4787 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004788}
4789
Olivier Houcharde6060c52017-11-16 17:42:52 +01004790
William Lallemanded0b5ad2017-10-30 19:36:36 +01004791/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004792int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004793{
4794 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4795 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4796 unsigned char *p;
4797 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02004798 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004799 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004800
4801 /* Session id is already stored in to key and session id is known
4802 * so we dont store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02004803 * note: SSL_SESSION_set1_id is using
4804 * a memcpy so we need to use a different pointer
4805 * than sid_data or sid_ctx_data to avoid valgrind
4806 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004807 */
4808
4809 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02004810
4811 /* copy value in an other buffer */
4812 memcpy(encid, sid_data, sid_length);
4813
4814 /* pad with 0 */
4815 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4816 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4817
4818 /* force length to zero to avoid ASN1 encoding */
4819 SSL_SESSION_set1_id(sess, encid, 0);
4820
4821 /* force length to zero to avoid ASN1 encoding */
4822 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004823
4824 /* check if buffer is large enough for the ASN1 encoded session */
4825 data_len = i2d_SSL_SESSION(sess, NULL);
4826 if (data_len > SHSESS_MAX_DATA_LEN)
4827 goto err;
4828
4829 p = encsess;
4830
4831 /* process ASN1 session encoding before the lock */
4832 i2d_SSL_SESSION(sess, &p);
4833
William Lallemanded0b5ad2017-10-30 19:36:36 +01004834
William Lallemanda3c77cf2017-10-30 23:44:40 +01004835 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004836 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004837 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004838 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004839err:
4840 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02004841 SSL_SESSION_set1_id(sess, encid, sid_length);
4842 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004843
4844 return 0; /* do not increment session reference count */
4845}
4846
4847/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004848SSL_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 +01004849{
William Lallemand4f45bb92017-10-30 20:08:51 +01004850 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004851 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4852 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004853 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004854 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004855
4856 global.shctx_lookups++;
4857
4858 /* allow the session to be freed automatically by openssl */
4859 *do_copy = 0;
4860
4861 /* tree key is zeros padded sessionid */
4862 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4863 memcpy(tmpkey, key, key_len);
4864 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4865 key = tmpkey;
4866 }
4867
4868 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004869 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004870
4871 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004872 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4873 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004874 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004875 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004876 global.shctx_misses++;
4877 return NULL;
4878 }
4879
William Lallemand4f45bb92017-10-30 20:08:51 +01004880 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4881 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004882
William Lallemand4f45bb92017-10-30 20:08:51 +01004883 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 +01004884
William Lallemanda3c77cf2017-10-30 23:44:40 +01004885 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004886
4887 /* decode ASN1 session */
4888 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004889 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004890 /* Reset session id and session id contenxt */
4891 if (sess) {
4892 SSL_SESSION_set1_id(sess, key, key_len);
4893 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4894 }
4895
4896 return sess;
4897}
4898
William Lallemand4f45bb92017-10-30 20:08:51 +01004899
William Lallemanded0b5ad2017-10-30 19:36:36 +01004900/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004901void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004902{
William Lallemand4f45bb92017-10-30 20:08:51 +01004903 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004904 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4905 unsigned int sid_length;
4906 const unsigned char *sid_data;
4907 (void)ctx;
4908
4909 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4910 /* tree key is zeros padded sessionid */
4911 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4912 memcpy(tmpkey, sid_data, sid_length);
4913 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4914 sid_data = tmpkey;
4915 }
4916
William Lallemanda3c77cf2017-10-30 23:44:40 +01004917 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004918
4919 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004920 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4921 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004922 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004923 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004924 }
4925
4926 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004927 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004928}
4929
4930/* Set session cache mode to server and disable openssl internal cache.
4931 * Set shared cache callbacks on an ssl context.
4932 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004933void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004934{
4935 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4936
4937 if (!ssl_shctx) {
4938 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4939 return;
4940 }
4941
4942 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4943 SSL_SESS_CACHE_NO_INTERNAL |
4944 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4945
4946 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004947 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4948 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4949 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004950}
4951
William Lallemand8b453912019-11-21 15:48:10 +01004952/*
4953 * This function applies the SSL configuration on a SSL_CTX
4954 * It returns an error code and fills the <err> buffer
4955 */
4956int 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 +01004957{
4958 struct proxy *curproxy = bind_conf->frontend;
4959 int cfgerr = 0;
4960 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004961 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004962 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004963#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004964 const char *conf_ciphersuites;
4965#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004966 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004967
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004968 if (ssl_conf) {
4969 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4970 int i, min, max;
4971 int flags = MC_SSL_O_ALL;
4972
4973 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004974 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4975 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004976 if (min)
4977 flags |= (methodVersions[min].flag - 1);
4978 if (max)
4979 flags |= ~((methodVersions[max].flag << 1) - 1);
4980 min = max = CONF_TLSV_NONE;
4981 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4982 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4983 if (min)
4984 max = i;
4985 else
4986 min = max = i;
4987 }
4988 /* save real min/max */
4989 conf_ssl_methods->min = min;
4990 conf_ssl_methods->max = max;
4991 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004992 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4993 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004994 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004995 }
4996 }
4997
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004998 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01004999 case SSL_SOCK_VERIFY_NONE:
5000 verify = SSL_VERIFY_NONE;
5001 break;
5002 case SSL_SOCK_VERIFY_OPTIONAL:
5003 verify = SSL_VERIFY_PEER;
5004 break;
5005 case SSL_SOCK_VERIFY_REQUIRED:
5006 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5007 break;
5008 }
5009 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5010 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005011 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
5012 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
5013 if (ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005014 /* set CAfile to verify */
5015 if (!ssl_set_verify_locations_file(ctx, ca_file)) {
5016 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005017 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005018 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005019 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005020 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
5021 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005022 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 +02005023 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005024 }
Emeric Brun850efd52014-01-29 12:24:34 +01005025 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005026 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5027 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005028 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005029 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005030#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005031 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005032 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5033
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005034 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005035 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5036 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005037 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005038 }
Emeric Brun561e5742012-10-02 15:20:55 +02005039 else {
5040 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5041 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005042 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005043#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005044 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005045 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005046#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005047 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005048 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005049 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5050 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005051 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005052 }
5053 }
5054#endif
5055
William Lallemand4f45bb92017-10-30 20:08:51 +01005056 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005057 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5058 if (conf_ciphers &&
5059 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005060 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5061 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005062 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005063 }
5064
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005065#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005066 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5067 if (conf_ciphersuites &&
5068 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005069 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5070 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005071 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005072 }
5073#endif
5074
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005075#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005076 /* If tune.ssl.default-dh-param has not been set,
5077 neither has ssl-default-dh-file and no static DH
5078 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005079 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005080 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005081 (ssl_dh_ptr_index == -1 ||
5082 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005083 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5084 const SSL_CIPHER * cipher = NULL;
5085 char cipher_description[128];
5086 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5087 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5088 which is not ephemeral DH. */
5089 const char dhe_description[] = " Kx=DH ";
5090 const char dhe_export_description[] = " Kx=DH(";
5091 int idx = 0;
5092 int dhe_found = 0;
5093 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005094
Remi Gacogne23d5d372014-10-10 17:04:26 +02005095 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005096
Remi Gacogne23d5d372014-10-10 17:04:26 +02005097 if (ssl) {
5098 ciphers = SSL_get_ciphers(ssl);
5099
5100 if (ciphers) {
5101 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5102 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5103 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5104 if (strstr(cipher_description, dhe_description) != NULL ||
5105 strstr(cipher_description, dhe_export_description) != NULL) {
5106 dhe_found = 1;
5107 break;
5108 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005109 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005110 }
5111 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005112 SSL_free(ssl);
5113 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005114 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005115
Lukas Tribus90132722014-08-18 00:56:33 +02005116 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005117 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",
5118 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005119 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005120 }
5121
Willy Tarreauef934602016-12-22 23:12:01 +01005122 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005123 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005124
Willy Tarreauef934602016-12-22 23:12:01 +01005125 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005126 if (local_dh_1024 == NULL) {
5127 local_dh_1024 = ssl_get_dh_1024();
5128 }
Willy Tarreauef934602016-12-22 23:12:01 +01005129 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005130 if (local_dh_2048 == NULL) {
5131 local_dh_2048 = ssl_get_dh_2048();
5132 }
Willy Tarreauef934602016-12-22 23:12:01 +01005133 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005134 if (local_dh_4096 == NULL) {
5135 local_dh_4096 = ssl_get_dh_4096();
5136 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005137 }
5138 }
5139 }
5140#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005141
Emeric Brunfc0421f2012-09-07 17:30:07 +02005142 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005143#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005144 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005145#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005146
Bernard Spil13c53f82018-02-15 13:34:58 +01005147#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005148 ssl_conf_cur = NULL;
5149 if (ssl_conf && ssl_conf->npn_str)
5150 ssl_conf_cur = ssl_conf;
5151 else if (bind_conf->ssl_conf.npn_str)
5152 ssl_conf_cur = &bind_conf->ssl_conf;
5153 if (ssl_conf_cur)
5154 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005155#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005156#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005157 ssl_conf_cur = NULL;
5158 if (ssl_conf && ssl_conf->alpn_str)
5159 ssl_conf_cur = ssl_conf;
5160 else if (bind_conf->ssl_conf.alpn_str)
5161 ssl_conf_cur = &bind_conf->ssl_conf;
5162 if (ssl_conf_cur)
5163 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005164#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005165#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005166 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5167 if (conf_curves) {
5168 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005169 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5170 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005171 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005172 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005173 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005174 }
5175#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005176#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005177 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005178 int i;
5179 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005180#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005181 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005182 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5183 NULL);
5184
5185 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005186 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005187 return cfgerr;
5188 }
5189#else
5190 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5191 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5192 ECDHE_DEFAULT_CURVE);
5193#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005194
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005195 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005196 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005197 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5198 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005199 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005200 }
5201 else {
5202 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5203 EC_KEY_free(ecdh);
5204 }
5205 }
5206#endif
5207
Emeric Brunfc0421f2012-09-07 17:30:07 +02005208 return cfgerr;
5209}
5210
Evan Broderbe554312013-06-27 00:05:25 -07005211static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5212{
5213 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5214 size_t prefixlen, suffixlen;
5215
5216 /* Trivial case */
5217 if (strcmp(pattern, hostname) == 0)
5218 return 1;
5219
Evan Broderbe554312013-06-27 00:05:25 -07005220 /* The rest of this logic is based on RFC 6125, section 6.4.3
5221 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5222
Emeric Bruna848dae2013-10-08 11:27:28 +02005223 pattern_wildcard = NULL;
5224 pattern_left_label_end = pattern;
5225 while (*pattern_left_label_end != '.') {
5226 switch (*pattern_left_label_end) {
5227 case 0:
5228 /* End of label not found */
5229 return 0;
5230 case '*':
5231 /* If there is more than one wildcards */
5232 if (pattern_wildcard)
5233 return 0;
5234 pattern_wildcard = pattern_left_label_end;
5235 break;
5236 }
5237 pattern_left_label_end++;
5238 }
5239
5240 /* If it's not trivial and there is no wildcard, it can't
5241 * match */
5242 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005243 return 0;
5244
5245 /* Make sure all labels match except the leftmost */
5246 hostname_left_label_end = strchr(hostname, '.');
5247 if (!hostname_left_label_end
5248 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5249 return 0;
5250
5251 /* Make sure the leftmost label of the hostname is long enough
5252 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005253 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005254 return 0;
5255
5256 /* Finally compare the string on either side of the
5257 * wildcard */
5258 prefixlen = pattern_wildcard - pattern;
5259 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005260 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5261 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005262 return 0;
5263
5264 return 1;
5265}
5266
5267static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5268{
5269 SSL *ssl;
5270 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005271 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005272 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005273 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005274
5275 int depth;
5276 X509 *cert;
5277 STACK_OF(GENERAL_NAME) *alt_names;
5278 int i;
5279 X509_NAME *cert_subject;
5280 char *str;
5281
5282 if (ok == 0)
5283 return ok;
5284
5285 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005286 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005287 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005288
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005289 /* We're checking if the provided hostnames match the desired one. The
5290 * desired hostname comes from the SNI we presented if any, or if not
5291 * provided then it may have been explicitly stated using a "verifyhost"
5292 * directive. If neither is set, we don't care about the name so the
5293 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005294 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005295 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005296 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005297 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005298 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005299 if (!servername)
5300 return ok;
5301 }
Evan Broderbe554312013-06-27 00:05:25 -07005302
5303 /* We only need to verify the CN on the actual server cert,
5304 * not the indirect CAs */
5305 depth = X509_STORE_CTX_get_error_depth(ctx);
5306 if (depth != 0)
5307 return ok;
5308
5309 /* At this point, the cert is *not* OK unless we can find a
5310 * hostname match */
5311 ok = 0;
5312
5313 cert = X509_STORE_CTX_get_current_cert(ctx);
5314 /* It seems like this might happen if verify peer isn't set */
5315 if (!cert)
5316 return ok;
5317
5318 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5319 if (alt_names) {
5320 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5321 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5322 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005323#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005324 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5325#else
Evan Broderbe554312013-06-27 00:05:25 -07005326 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005327#endif
Evan Broderbe554312013-06-27 00:05:25 -07005328 ok = ssl_sock_srv_hostcheck(str, servername);
5329 OPENSSL_free(str);
5330 }
5331 }
5332 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005333 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005334 }
5335
5336 cert_subject = X509_get_subject_name(cert);
5337 i = -1;
5338 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5339 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005340 ASN1_STRING *value;
5341 value = X509_NAME_ENTRY_get_data(entry);
5342 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005343 ok = ssl_sock_srv_hostcheck(str, servername);
5344 OPENSSL_free(str);
5345 }
5346 }
5347
Willy Tarreau71d058c2017-07-26 20:09:56 +02005348 /* report the mismatch and indicate if SNI was used or not */
5349 if (!ok && !conn->err_code)
5350 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005351 return ok;
5352}
5353
Emeric Brun94324a42012-10-11 14:00:19 +02005354/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005355int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005356{
Willy Tarreau03209342016-12-22 17:08:28 +01005357 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005358 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005359 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005360 SSL_OP_ALL | /* all known workarounds for bugs */
5361 SSL_OP_NO_SSLv2 |
5362 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005363 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005364 SSL_MODE_ENABLE_PARTIAL_WRITE |
5365 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005366 SSL_MODE_RELEASE_BUFFERS |
5367 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005368 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005369 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005370 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005371 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005372 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005373
Thierry Fournier383085f2013-01-24 14:15:43 +01005374 /* Make sure openssl opens /dev/urandom before the chroot */
5375 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005376 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005377 cfgerr++;
5378 }
5379
Willy Tarreaufce03112015-01-15 21:32:40 +01005380 /* Automatic memory computations need to know we use SSL there */
5381 global.ssl_used_backend = 1;
5382
5383 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005384 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005385 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005386 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5387 curproxy->id, srv->id,
5388 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005389 cfgerr++;
5390 return cfgerr;
5391 }
5392 }
Emeric Brun94324a42012-10-11 14:00:19 +02005393 if (srv->use_ssl)
5394 srv->xprt = &ssl_sock;
5395 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005396 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005397
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005398 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005399 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005400 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5401 proxy_type_str(curproxy), curproxy->id,
5402 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005403 cfgerr++;
5404 return cfgerr;
5405 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005406
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005407 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005408 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5409 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5410 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005411 else
5412 flags = conf_ssl_methods->flags;
5413
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005414 /* Real min and max should be determinate with configuration and openssl's capabilities */
5415 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005416 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005417 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005418 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005419
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005420 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005421 min = max = CONF_TLSV_NONE;
5422 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005423 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005424 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005425 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005426 if (min) {
5427 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005428 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5429 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5430 proxy_type_str(curproxy), curproxy->id, srv->id,
5431 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005432 hole = 0;
5433 }
5434 max = i;
5435 }
5436 else {
5437 min = max = i;
5438 }
5439 }
5440 else {
5441 if (min)
5442 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005443 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005444 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005445 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5446 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005447 cfgerr += 1;
5448 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005449
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005450#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005451 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005452 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005453 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005454 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005455 else
5456 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5457 if (flags & methodVersions[i].flag)
5458 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005459#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005460 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005461 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5462 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005463#endif
5464
5465 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5466 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005467 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005468
Willy Tarreau5db847a2019-05-09 14:13:35 +02005469#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005470 if (global_ssl.async)
5471 mode |= SSL_MODE_ASYNC;
5472#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005473 SSL_CTX_set_mode(ctx, mode);
5474 srv->ssl_ctx.ctx = ctx;
5475
Emeric Bruna7aa3092012-10-26 12:58:00 +02005476 if (srv->ssl_ctx.client_crt) {
5477 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 +01005478 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5479 proxy_type_str(curproxy), curproxy->id,
5480 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005481 cfgerr++;
5482 }
5483 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 +01005484 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5485 proxy_type_str(curproxy), curproxy->id,
5486 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005487 cfgerr++;
5488 }
5489 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005490 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5491 proxy_type_str(curproxy), curproxy->id,
5492 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005493 cfgerr++;
5494 }
5495 }
Emeric Brun94324a42012-10-11 14:00:19 +02005496
Emeric Brun850efd52014-01-29 12:24:34 +01005497 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5498 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005499 switch (srv->ssl_ctx.verify) {
5500 case SSL_SOCK_VERIFY_NONE:
5501 verify = SSL_VERIFY_NONE;
5502 break;
5503 case SSL_SOCK_VERIFY_REQUIRED:
5504 verify = SSL_VERIFY_PEER;
5505 break;
5506 }
Evan Broderbe554312013-06-27 00:05:25 -07005507 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005508 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005509 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005510 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005511 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005512 /* set CAfile to verify */
5513 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
5514 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01005515 curproxy->id, srv->id,
5516 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005517 cfgerr++;
5518 }
5519 }
Emeric Brun850efd52014-01-29 12:24:34 +01005520 else {
5521 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01005522 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",
5523 curproxy->id, srv->id,
5524 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005525 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01005526 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
5527 curproxy->id, srv->id,
5528 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005529 cfgerr++;
5530 }
Emeric Brunef42d922012-10-11 16:11:36 +02005531#ifdef X509_V_FLAG_CRL_CHECK
5532 if (srv->ssl_ctx.crl_file) {
5533 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
5534
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005535 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005536 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
5537 curproxy->id, srv->id,
5538 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005539 cfgerr++;
5540 }
5541 else {
5542 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5543 }
5544 }
5545#endif
5546 }
5547
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005548 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
5549 SSL_SESS_CACHE_NO_INTERNAL_STORE);
5550 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02005551 if (srv->ssl_ctx.ciphers &&
5552 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005553 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
5554 curproxy->id, srv->id,
5555 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02005556 cfgerr++;
5557 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005558
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005559#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005560 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00005561 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005562 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
5563 curproxy->id, srv->id,
5564 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
5565 cfgerr++;
5566 }
5567#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01005568#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
5569 if (srv->ssl_ctx.npn_str)
5570 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
5571#endif
5572#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5573 if (srv->ssl_ctx.alpn_str)
5574 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
5575#endif
5576
Emeric Brun94324a42012-10-11 14:00:19 +02005577
5578 return cfgerr;
5579}
5580
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005581/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005582 * be NULL, in which case nothing is done. Returns the number of errors
5583 * encountered.
5584 */
Willy Tarreau03209342016-12-22 17:08:28 +01005585int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005586{
5587 struct ebmb_node *node;
5588 struct sni_ctx *sni;
5589 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01005590 int errcode = 0;
5591 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005592
Willy Tarreaufce03112015-01-15 21:32:40 +01005593 /* Automatic memory computations need to know we use SSL there */
5594 global.ssl_used_frontend = 1;
5595
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005596 /* Make sure openssl opens /dev/urandom before the chroot */
5597 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005598 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005599 err++;
5600 }
5601 /* Create initial_ctx used to start the ssl connection before do switchctx */
5602 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005603 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005604 /* It should not be necessary to call this function, but it's
5605 necessary first to check and move all initialisation related
5606 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01005607 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005608 }
Emeric Brun0bed9942014-10-30 19:25:24 +01005609 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01005610 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01005611
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005612 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005613 while (node) {
5614 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01005615 if (!sni->order && sni->ctx != bind_conf->default_ctx)
5616 /* only initialize the CTX on its first occurrence and
5617 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005618 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005619 node = ebmb_next(node);
5620 }
5621
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005622 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005623 while (node) {
5624 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01005625 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01005626 /* only initialize the CTX on its first occurrence and
5627 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005628 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
5629 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005630 node = ebmb_next(node);
5631 }
William Lallemand8b453912019-11-21 15:48:10 +01005632
5633 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005634 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005635 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005636 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005637 err++;
5638 }
5639
5640 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005641 return err;
5642}
5643
Willy Tarreau55d37912016-12-21 23:38:39 +01005644/* Prepares all the contexts for a bind_conf and allocates the shared SSL
5645 * context if needed. Returns < 0 on error, 0 on success. The warnings and
5646 * alerts are directly emitted since the rest of the stack does it below.
5647 */
5648int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
5649{
5650 struct proxy *px = bind_conf->frontend;
5651 int alloc_ctx;
5652 int err;
5653
5654 if (!bind_conf->is_ssl) {
5655 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005656 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
5657 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01005658 }
5659 return 0;
5660 }
5661 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005662 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005663 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
5664 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005665 }
5666 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005667 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
5668 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005669 return -1;
5670 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005671 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005672 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005673 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005674 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01005675 sizeof(*sh_ssl_sess_tree),
5676 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005677 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005678 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5679 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");
5680 else
5681 ha_alert("Unable to allocate SSL session cache.\n");
5682 return -1;
5683 }
5684 /* free block callback */
5685 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5686 /* init the root tree within the extra space */
5687 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5688 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005689 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005690 err = 0;
5691 /* initialize all certificate contexts */
5692 err += ssl_sock_prepare_all_ctx(bind_conf);
5693
5694 /* initialize CA variables if the certificates generation is enabled */
5695 err += ssl_sock_load_ca(bind_conf);
5696
5697 return -err;
5698}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005699
5700/* release ssl context allocated for servers. */
5701void ssl_sock_free_srv_ctx(struct server *srv)
5702{
Olivier Houchardc7566002018-11-20 23:33:50 +01005703#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5704 if (srv->ssl_ctx.alpn_str)
5705 free(srv->ssl_ctx.alpn_str);
5706#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005707#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01005708 if (srv->ssl_ctx.npn_str)
5709 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005710#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005711 if (srv->ssl_ctx.ctx)
5712 SSL_CTX_free(srv->ssl_ctx.ctx);
5713}
5714
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005715/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005716 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5717 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005718void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005719{
5720 struct ebmb_node *node, *back;
5721 struct sni_ctx *sni;
5722
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005723 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005724 while (node) {
5725 sni = ebmb_entry(node, struct sni_ctx, name);
5726 back = ebmb_next(node);
5727 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005728 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005729 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005730 ssl_sock_free_ssl_conf(sni->conf);
5731 free(sni->conf);
5732 sni->conf = NULL;
5733 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005734 free(sni);
5735 node = back;
5736 }
5737
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005738 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005739 while (node) {
5740 sni = ebmb_entry(node, struct sni_ctx, name);
5741 back = ebmb_next(node);
5742 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005743 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005744 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005745 ssl_sock_free_ssl_conf(sni->conf);
5746 free(sni->conf);
5747 sni->conf = NULL;
5748 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005749 free(sni);
5750 node = back;
5751 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005752 SSL_CTX_free(bind_conf->initial_ctx);
5753 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005754 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005755 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005756}
5757
Willy Tarreau795cdab2016-12-22 17:30:54 +01005758/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5759void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5760{
5761 ssl_sock_free_ca(bind_conf);
5762 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005763 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005764 free(bind_conf->ca_sign_file);
5765 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005766 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005767 free(bind_conf->keys_ref->filename);
5768 free(bind_conf->keys_ref->tlskeys);
5769 LIST_DEL(&bind_conf->keys_ref->list);
5770 free(bind_conf->keys_ref);
5771 }
5772 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005773 bind_conf->ca_sign_pass = NULL;
5774 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005775}
5776
Christopher Faulet31af49d2015-06-09 17:29:50 +02005777/* Load CA cert file and private key used to generate certificates */
5778int
Willy Tarreau03209342016-12-22 17:08:28 +01005779ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005780{
Willy Tarreau03209342016-12-22 17:08:28 +01005781 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005782 FILE *fp;
5783 X509 *cacert = NULL;
5784 EVP_PKEY *capkey = NULL;
5785 int err = 0;
5786
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005787 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005788 return err;
5789
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005790#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005791 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005792 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005793 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005794 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005795 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005796#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005797
Christopher Faulet31af49d2015-06-09 17:29:50 +02005798 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005799 ha_alert("Proxy '%s': cannot enable certificate generation, "
5800 "no CA certificate File configured at [%s:%d].\n",
5801 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005802 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005803 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005804
5805 /* read in the CA certificate */
5806 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005807 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5808 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005809 goto load_error;
5810 }
5811 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005812 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5813 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005814 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005815 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005816 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005817 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005818 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5819 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005820 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005821 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005822
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005823 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005824 bind_conf->ca_sign_cert = cacert;
5825 bind_conf->ca_sign_pkey = capkey;
5826 return err;
5827
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005828 read_error:
5829 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005830 if (capkey) EVP_PKEY_free(capkey);
5831 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005832 load_error:
5833 bind_conf->generate_certs = 0;
5834 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005835 return err;
5836}
5837
5838/* Release CA cert and private key used to generate certificated */
5839void
5840ssl_sock_free_ca(struct bind_conf *bind_conf)
5841{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005842 if (bind_conf->ca_sign_pkey)
5843 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5844 if (bind_conf->ca_sign_cert)
5845 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005846 bind_conf->ca_sign_pkey = NULL;
5847 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005848}
5849
Emeric Brun46591952012-05-18 15:47:34 +02005850/*
5851 * This function is called if SSL * context is not yet allocated. The function
5852 * is designed to be called before any other data-layer operation and sets the
5853 * handshake flag on the connection. It is safe to call it multiple times.
5854 * It returns 0 on success and -1 in error case.
5855 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005856static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005857{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005858 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005859 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005860 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005861 return 0;
5862
Willy Tarreau3c728722014-01-23 13:50:42 +01005863 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005864 return 0;
5865
Olivier Houchard66ab4982019-02-26 18:37:15 +01005866 ctx = pool_alloc(ssl_sock_ctx_pool);
5867 if (!ctx) {
5868 conn->err_code = CO_ER_SSL_NO_MEM;
5869 return -1;
5870 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005871 ctx->wait_event.tasklet = tasklet_new();
5872 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005873 conn->err_code = CO_ER_SSL_NO_MEM;
5874 pool_free(ssl_sock_ctx_pool, ctx);
5875 return -1;
5876 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005877 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5878 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005879 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005880 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01005881 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005882 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01005883 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02005884 ctx->xprt_st = 0;
5885 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005886
5887 /* Only work with sockets for now, this should be adapted when we'll
5888 * add QUIC support.
5889 */
5890 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005891 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005892 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5893 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005894 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005895
Willy Tarreau20879a02012-12-03 16:32:10 +01005896 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5897 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005898 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005899 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005900
Emeric Brun46591952012-05-18 15:47:34 +02005901 /* If it is in client mode initiate SSL session
5902 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005903 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005904 int may_retry = 1;
5905
5906 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005907 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005908 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5909 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005910 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005911 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005912 goto retry_connect;
5913 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005914 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005915 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005916 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005917 ctx->bio = BIO_new(ha_meth);
5918 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005919 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005920 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005921 goto retry_connect;
5922 }
Emeric Brun55476152014-11-12 17:35:37 +01005923 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005924 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005925 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005926 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005927 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005928
Evan Broderbe554312013-06-27 00:05:25 -07005929 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005930 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5931 SSL_free(ctx->ssl);
5932 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005933 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005934 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005935 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005936 goto retry_connect;
5937 }
Emeric Brun55476152014-11-12 17:35:37 +01005938 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005939 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005940 }
5941
Olivier Houchard66ab4982019-02-26 18:37:15 +01005942 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005943 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5944 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5945 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 +01005946 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005947 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005948 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5949 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005950 } else if (sess) {
5951 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005952 }
5953 }
Evan Broderbe554312013-06-27 00:05:25 -07005954
Emeric Brun46591952012-05-18 15:47:34 +02005955 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005956 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005957
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005958 _HA_ATOMIC_ADD(&sslconns, 1);
5959 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005960 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005961 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005962 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005963 if (conn->flags & CO_FL_ERROR)
5964 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005965 return 0;
5966 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005967 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005968 int may_retry = 1;
5969
5970 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005971 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005972 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5973 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005974 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005975 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005976 goto retry_accept;
5977 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005978 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005979 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005980 }
Olivier Houchard545989f2019-12-17 15:39:54 +01005981#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard54907bb2019-12-19 15:02:39 +01005982 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
5983 b_alloc(&ctx->early_buf);
5984 SSL_set_max_early_data(ctx->ssl,
5985 /* Only allow early data if we managed to allocate
5986 * a buffer.
5987 */
5988 (!b_is_null(&ctx->early_buf)) ?
5989 global.tune.bufsize - global.tune.maxrewrite : 0);
5990 }
Olivier Houchard545989f2019-12-17 15:39:54 +01005991#endif
Emeric Brun46591952012-05-18 15:47:34 +02005992
Olivier Houcharda8955d52019-04-07 22:00:38 +02005993 ctx->bio = BIO_new(ha_meth);
5994 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005995 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005996 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005997 goto retry_accept;
5998 }
Emeric Brun55476152014-11-12 17:35:37 +01005999 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006000 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006001 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006002 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006003 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006004
Emeric Brune1f38db2012-09-03 20:36:47 +02006005 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006006 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6007 SSL_free(ctx->ssl);
6008 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006009 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006010 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006011 goto retry_accept;
6012 }
Emeric Brun55476152014-11-12 17:35:37 +01006013 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006014 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006015 }
6016
Olivier Houchard66ab4982019-02-26 18:37:15 +01006017 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006018
Emeric Brun46591952012-05-18 15:47:34 +02006019 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006020 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006021#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006022 conn->flags |= CO_FL_EARLY_SSL_HS;
6023#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006024
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006025 _HA_ATOMIC_ADD(&sslconns, 1);
6026 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006027 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006028 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006029 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006030 if (conn->flags & CO_FL_ERROR)
6031 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02006032 return 0;
6033 }
6034 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006035 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006036err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006037 if (ctx && ctx->wait_event.tasklet)
6038 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006039 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006040 return -1;
6041}
6042
6043
6044/* This is the callback which is used when an SSL handshake is pending. It
6045 * updates the FD status if it wants some polling before being called again.
6046 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6047 * otherwise it returns non-zero and removes itself from the connection's
6048 * flags (the bit is provided in <flag> by the caller).
6049 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006050static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006051{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006052 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006053 int ret;
6054
Willy Tarreau3c728722014-01-23 13:50:42 +01006055 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006056 return 0;
6057
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006058 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006059 goto out_error;
6060
Willy Tarreau5db847a2019-05-09 14:13:35 +02006061#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006062 /*
6063 * Check if we have early data. If we do, we have to read them
6064 * before SSL_do_handshake() is called, And there's no way to
6065 * detect early data, except to try to read them
6066 */
6067 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006068 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006069
Olivier Houchard54907bb2019-12-19 15:02:39 +01006070 while (1) {
6071 ret = SSL_read_early_data(ctx->ssl,
6072 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6073 &read_data);
6074 if (ret == SSL_READ_EARLY_DATA_ERROR)
6075 goto check_error;
6076 if (read_data > 0) {
6077 conn->flags |= CO_FL_EARLY_DATA;
6078 b_add(&ctx->early_buf, read_data);
6079 }
6080 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6081 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6082 if (!b_data(&ctx->early_buf))
6083 b_free(&ctx->early_buf);
6084 break;
6085 }
6086 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006087 }
6088#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006089 /* If we use SSL_do_handshake to process a reneg initiated by
6090 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6091 * Usually SSL_write and SSL_read are used and process implicitly
6092 * the reneg handshake.
6093 * Here we use SSL_peek as a workaround for reneg.
6094 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006095 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006096 char c;
6097
Olivier Houchard66ab4982019-02-26 18:37:15 +01006098 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006099 if (ret <= 0) {
6100 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006101 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006102
Emeric Brun674b7432012-11-08 19:21:55 +01006103 if (ret == SSL_ERROR_WANT_WRITE) {
6104 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006105 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006106 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006107 return 0;
6108 }
6109 else if (ret == SSL_ERROR_WANT_READ) {
6110 /* handshake may have been completed but we have
6111 * no more data to read.
6112 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006113 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006114 ret = 1;
6115 goto reneg_ok;
6116 }
6117 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006118 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006119 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006120 return 0;
6121 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006122#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006123 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006124 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006125 return 0;
6126 }
6127#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006128 else if (ret == SSL_ERROR_SYSCALL) {
6129 /* if errno is null, then connection was successfully established */
6130 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6131 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006132 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006133#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6134 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006135 conn->err_code = CO_ER_SSL_HANDSHAKE;
6136#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006137 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006138#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006139 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006140 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006141 empty_handshake = state == TLS_ST_BEFORE;
6142#else
Lukas Tribus49799162019-07-08 14:29:15 +02006143 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6144 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006145#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006146 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006147 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006148 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006149 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6150 else
6151 conn->err_code = CO_ER_SSL_EMPTY;
6152 }
6153 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006154 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006155 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6156 else
6157 conn->err_code = CO_ER_SSL_ABORT;
6158 }
6159 }
6160 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006161 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006162 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006163 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006164 conn->err_code = CO_ER_SSL_HANDSHAKE;
6165 }
Lukas Tribus49799162019-07-08 14:29:15 +02006166#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006167 }
Emeric Brun674b7432012-11-08 19:21:55 +01006168 goto out_error;
6169 }
6170 else {
6171 /* Fail on all other handshake errors */
6172 /* Note: OpenSSL may leave unread bytes in the socket's
6173 * buffer, causing an RST to be emitted upon close() on
6174 * TCP sockets. We first try to drain possibly pending
6175 * data to avoid this as much as possible.
6176 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006177 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006178 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006179 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006180 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006181 goto out_error;
6182 }
6183 }
6184 /* read some data: consider handshake completed */
6185 goto reneg_ok;
6186 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006187 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006188check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006189 if (ret != 1) {
6190 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006191 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006192
6193 if (ret == SSL_ERROR_WANT_WRITE) {
6194 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006195 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006196 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006197 return 0;
6198 }
6199 else if (ret == SSL_ERROR_WANT_READ) {
6200 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006201 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006202 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6203 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006204 return 0;
6205 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006206#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006207 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006208 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006209 return 0;
6210 }
6211#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006212 else if (ret == SSL_ERROR_SYSCALL) {
6213 /* if errno is null, then connection was successfully established */
6214 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6215 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006216 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006217#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6218 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006219 conn->err_code = CO_ER_SSL_HANDSHAKE;
6220#else
6221 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006222#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006223 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006224 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006225 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006226#else
Lukas Tribus49799162019-07-08 14:29:15 +02006227 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6228 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006229#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006230 if (empty_handshake) {
6231 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006232 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006233 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6234 else
6235 conn->err_code = CO_ER_SSL_EMPTY;
6236 }
6237 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006238 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006239 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6240 else
6241 conn->err_code = CO_ER_SSL_ABORT;
6242 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006243 }
6244 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006245 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006246 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6247 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006248 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006249 }
Lukas Tribus49799162019-07-08 14:29:15 +02006250#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006251 }
Willy Tarreau89230192012-09-28 20:22:13 +02006252 goto out_error;
6253 }
Emeric Brun46591952012-05-18 15:47:34 +02006254 else {
6255 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006256 /* Note: OpenSSL may leave unread bytes in the socket's
6257 * buffer, causing an RST to be emitted upon close() on
6258 * TCP sockets. We first try to drain possibly pending
6259 * data to avoid this as much as possible.
6260 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006261 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006262 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006263 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006264 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006265 goto out_error;
6266 }
6267 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006268#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006269 else {
6270 /*
6271 * If the server refused the early data, we have to send a
6272 * 425 to the client, as we no longer have the data to sent
6273 * them again.
6274 */
6275 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006276 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006277 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6278 goto out_error;
6279 }
6280 }
6281 }
6282#endif
6283
Emeric Brun46591952012-05-18 15:47:34 +02006284
Emeric Brun674b7432012-11-08 19:21:55 +01006285reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006286
Willy Tarreau5db847a2019-05-09 14:13:35 +02006287#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006288 /* ASYNC engine API doesn't support moving read/write
6289 * buffers. So we disable ASYNC mode right after
6290 * the handshake to avoid buffer oveflows.
6291 */
6292 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006293 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006294#endif
Emeric Brun46591952012-05-18 15:47:34 +02006295 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006296 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006297 if (objt_server(conn->target)) {
6298 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6299 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6300 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006301 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006302 else {
6303 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6304 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6305 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6306 }
Emeric Brun46591952012-05-18 15:47:34 +02006307 }
6308
6309 /* The connection is now established at both layers, it's time to leave */
6310 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6311 return 1;
6312
6313 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006314 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006315 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006316 ERR_clear_error();
6317
Emeric Brun9fa89732012-10-04 17:09:56 +02006318 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006319 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6320 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6321 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006322 }
6323
Emeric Brun46591952012-05-18 15:47:34 +02006324 /* Fail on all other handshake errors */
6325 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006326 if (!conn->err_code)
6327 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006328 return 0;
6329}
6330
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006331/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6332 * event subscriber <es> is not allowed to change from a previous call as long
6333 * as at least one event is still subscribed. The <event_type> must only be a
6334 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6335 * unless the transport layer was already released.
6336 */
6337static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006338{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006339 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006340
Olivier Houchard0ff28652019-06-24 18:57:39 +02006341 if (!ctx)
6342 return -1;
6343
Willy Tarreau113d52b2020-01-10 09:20:26 +01006344 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6345 BUG_ON(ctx->subs && ctx->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006346 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006347
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006348 ctx->subs = es;
6349 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006350
6351 /* we may have to subscribe to lower layers for new events */
6352 event_type &= ~ctx->wait_event.events;
6353 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6354 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006355 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006356}
6357
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006358/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6359 * The <es> pointer is not allowed to differ from the one passed to the
6360 * subscribe() call. It always returns zero.
6361 */
6362static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006363{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006364 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006365
Willy Tarreau113d52b2020-01-10 09:20:26 +01006366 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006367 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006368
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006369 es->events &= ~event_type;
6370 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006371 ctx->subs = NULL;
6372
6373 /* If we subscribed, and we're not doing the handshake,
6374 * then we subscribed because the upper layer asked for it,
6375 * as the upper layer is no longer interested, we can
6376 * unsubscribe too.
6377 */
6378 event_type &= ctx->wait_event.events;
6379 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6380 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006381
6382 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006383}
6384
Olivier Houchard2e055482019-05-27 19:50:12 +02006385/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6386 * Returns 0 on success, and non-zero on failure.
6387 */
6388static 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)
6389{
6390 struct ssl_sock_ctx *ctx = xprt_ctx;
6391
6392 if (oldxprt_ops != NULL)
6393 *oldxprt_ops = ctx->xprt;
6394 if (oldxprt_ctx != NULL)
6395 *oldxprt_ctx = ctx->xprt_ctx;
6396 ctx->xprt = toadd_ops;
6397 ctx->xprt_ctx = toadd_ctx;
6398 return 0;
6399}
6400
Olivier Houchard5149b592019-05-23 17:47:36 +02006401/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6402 * return 0, otherwise just call the remove_xprt method from the underlying
6403 * XPRT.
6404 */
6405static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6406{
6407 struct ssl_sock_ctx *ctx = xprt_ctx;
6408
6409 if (ctx->xprt_ctx == toremove_ctx) {
6410 ctx->xprt_ctx = newctx;
6411 ctx->xprt = newops;
6412 return 0;
6413 }
6414 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6415}
6416
Olivier Houchardea8dd942019-05-20 14:02:16 +02006417static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6418{
6419 struct ssl_sock_ctx *ctx = context;
6420
6421 /* First if we're doing an handshake, try that */
6422 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6423 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6424 /* If we had an error, or the handshake is done and I/O is available,
6425 * let the upper layer know.
6426 * If no mux was set up yet, and nobody subscribed, then call
6427 * xprt_done_cb() ourself if it's set, or destroy the connection,
6428 * we can't be sure conn_fd_handler() will be called again.
6429 */
6430 if ((ctx->conn->flags & CO_FL_ERROR) ||
6431 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6432 int ret = 0;
6433 int woke = 0;
6434
6435 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006436 if (ctx->subs) {
6437 tasklet_wakeup(ctx->subs->tasklet);
6438 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006439 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006440 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006441 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006442
Olivier Houchardea8dd942019-05-20 14:02:16 +02006443 /* If we're the first xprt for the connection, let the
6444 * upper layers know. If xprt_done_cb() is set, call it,
6445 * otherwise, we should have a mux, so call its wake
6446 * method if we didn't woke a tasklet already.
6447 */
6448 if (ctx->conn->xprt_ctx == ctx) {
6449 if (ctx->conn->xprt_done_cb)
6450 ret = ctx->conn->xprt_done_cb(ctx->conn);
6451 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6452 ctx->conn->mux->wake(ctx->conn);
6453 return NULL;
6454 }
6455 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006456#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6457 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006458 else if (b_data(&ctx->early_buf) && ctx->subs &&
6459 ctx->subs->events & SUB_RETRY_RECV) {
6460 tasklet_wakeup(ctx->subs->tasklet);
6461 ctx->subs->events &= ~SUB_RETRY_RECV;
6462 if (!ctx->subs->events)
6463 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006464 }
6465#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006466 return NULL;
6467}
6468
Emeric Brun46591952012-05-18 15:47:34 +02006469/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006470 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006471 * buffer wraps, in which case a second call may be performed. The connection's
6472 * flags are updated with whatever special event is detected (error, read0,
6473 * empty). The caller is responsible for taking care of those events and
6474 * avoiding the call if inappropriate. The function does not call the
6475 * connection's polling update function, so the caller is responsible for this.
6476 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006477static 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 +02006478{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006479 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006480 ssize_t ret;
6481 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006482
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006483 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006484 goto out_error;
6485
Olivier Houchard54907bb2019-12-19 15:02:39 +01006486#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6487 if (b_data(&ctx->early_buf)) {
6488 try = b_contig_space(buf);
6489 if (try > b_data(&ctx->early_buf))
6490 try = b_data(&ctx->early_buf);
6491 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6492 b_add(buf, try);
6493 b_del(&ctx->early_buf, try);
6494 if (b_data(&ctx->early_buf) == 0)
6495 b_free(&ctx->early_buf);
6496 return try;
6497 }
6498#endif
6499
Emeric Brun46591952012-05-18 15:47:34 +02006500 if (conn->flags & CO_FL_HANDSHAKE)
6501 /* a handshake was requested */
6502 return 0;
6503
Emeric Brun46591952012-05-18 15:47:34 +02006504 /* read the largest possible block. For this, we perform only one call
6505 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6506 * in which case we accept to do it once again. A new attempt is made on
6507 * EINTR too.
6508 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006509 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006510
Willy Tarreau591d4452018-06-15 17:21:00 +02006511 try = b_contig_space(buf);
6512 if (!try)
6513 break;
6514
Willy Tarreauabf08d92014-01-14 11:31:27 +01006515 if (try > count)
6516 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006517
Olivier Houchard66ab4982019-02-26 18:37:15 +01006518 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006519
Emeric Brune1f38db2012-09-03 20:36:47 +02006520 if (conn->flags & CO_FL_ERROR) {
6521 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006522 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006523 }
Emeric Brun46591952012-05-18 15:47:34 +02006524 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006525 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006526 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006527 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006528 }
Emeric Brun46591952012-05-18 15:47:34 +02006529 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006530 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006531 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006532 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02006533 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006534 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006535#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006536 /* Async mode can be re-enabled, because we're leaving data state.*/
6537 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006538 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006539#endif
Emeric Brun46591952012-05-18 15:47:34 +02006540 break;
6541 }
6542 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006543 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006544 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6545 SUB_RETRY_RECV,
6546 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01006547 /* handshake is running, and it may need to re-enable read */
6548 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006549#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006550 /* Async mode can be re-enabled, because we're leaving data state.*/
6551 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006552 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006553#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006554 break;
6555 }
Emeric Brun46591952012-05-18 15:47:34 +02006556 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006557 } else if (ret == SSL_ERROR_ZERO_RETURN)
6558 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006559 /* For SSL_ERROR_SYSCALL, make sure to clear the error
6560 * stack before shutting down the connection for
6561 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006562 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
6563 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02006564 /* otherwise it's a real error */
6565 goto out_error;
6566 }
6567 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006568 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006569 return done;
6570
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006571 clear_ssl_error:
6572 /* Clear openssl global errors stack */
6573 ssl_sock_dump_errors(conn);
6574 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02006575 read0:
6576 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006577 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006578
Emeric Brun46591952012-05-18 15:47:34 +02006579 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006580 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01006581 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006582 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006583 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006584 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006585}
6586
6587
Willy Tarreau787db9a2018-06-14 18:31:46 +02006588/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
6589 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
6590 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02006591 * Only one call to send() is performed, unless the buffer wraps, in which case
6592 * a second call may be performed. The connection's flags are updated with
6593 * whatever special event is detected (error, empty). The caller is responsible
6594 * for taking care of those events and avoiding the call if inappropriate. The
6595 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02006596 * is responsible for this. The buffer's output is not adjusted, it's up to the
6597 * caller to take care of this. It's up to the caller to update the buffer's
6598 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02006599 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006600static 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 +02006601{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006602 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006603 ssize_t ret;
6604 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02006605
6606 done = 0;
6607
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006608 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006609 goto out_error;
6610
Olivier Houchard010941f2019-05-03 20:56:19 +02006611 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006612 /* a handshake was requested */
6613 return 0;
6614
6615 /* send the largest possible block. For this we perform only one call
6616 * to send() unless the buffer wraps and we exactly fill the first hunk,
6617 * in which case we accept to do it once again.
6618 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02006619 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02006620#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006621 size_t written_data;
6622#endif
6623
Willy Tarreau787db9a2018-06-14 18:31:46 +02006624 try = b_contig_data(buf, done);
6625 if (try > count)
6626 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01006627
Willy Tarreau7bed9452014-02-02 02:00:24 +01006628 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006629 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01006630 global_ssl.max_record && try > global_ssl.max_record) {
6631 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006632 }
6633 else {
6634 /* we need to keep the information about the fact that
6635 * we're not limiting the upcoming send(), because if it
6636 * fails, we'll have to retry with at least as many data.
6637 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006638 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006639 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01006640
Willy Tarreau5db847a2019-05-09 14:13:35 +02006641#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02006642 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006643 unsigned int max_early;
6644
Olivier Houchard522eea72017-11-03 16:27:47 +01006645 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006646 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006647 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006648 if (SSL_get0_session(ctx->ssl))
6649 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006650 else
6651 max_early = 0;
6652 }
6653
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006654 if (try + ctx->sent_early_data > max_early) {
6655 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006656 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006657 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006658 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006659 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006660 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006661 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006662 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006663 if (ret == 1) {
6664 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006665 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006666 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006667 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006668 /* Initiate the handshake, now */
6669 tasklet_wakeup(ctx->wait_event.tasklet);
6670 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006671
Olivier Houchardc2aae742017-09-22 18:26:28 +02006672 }
6673
6674 } else
6675#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006676 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006677
Emeric Brune1f38db2012-09-03 20:36:47 +02006678 if (conn->flags & CO_FL_ERROR) {
6679 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006680 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006681 }
Emeric Brun46591952012-05-18 15:47:34 +02006682 if (ret > 0) {
Olivier Houchardf24502b2019-01-17 19:09:11 +01006683 /* A send succeeded, so we can consier ourself connected */
6684 conn->flags |= CO_FL_CONNECTED;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006685 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006686 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006687 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006688 }
6689 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006690 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006691
Emeric Brun46591952012-05-18 15:47:34 +02006692 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006693 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006694 /* handshake is running, and it may need to re-enable write */
6695 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006696 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006697#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006698 /* Async mode can be re-enabled, because we're leaving data state.*/
6699 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006700 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006701#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006702 break;
6703 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006704
Emeric Brun46591952012-05-18 15:47:34 +02006705 break;
6706 }
6707 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006708 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006709 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006710 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6711 SUB_RETRY_RECV,
6712 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006713#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006714 /* Async mode can be re-enabled, because we're leaving data state.*/
6715 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006716 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006717#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006718 break;
6719 }
Emeric Brun46591952012-05-18 15:47:34 +02006720 goto out_error;
6721 }
6722 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006723 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006724 return done;
6725
6726 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006727 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006728 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006729 ERR_clear_error();
6730
Emeric Brun46591952012-05-18 15:47:34 +02006731 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006732 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006733}
6734
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006735static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006736
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006737 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006738
Olivier Houchardea8dd942019-05-20 14:02:16 +02006739
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006740 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006741 if (ctx->wait_event.events != 0)
6742 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6743 ctx->wait_event.events,
6744 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01006745 if (ctx->subs) {
6746 ctx->subs->events = 0;
6747 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006748 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006749
Olivier Houchard692c1d02019-05-23 18:41:47 +02006750 if (ctx->xprt->close)
6751 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006752#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006753 if (global_ssl.async) {
6754 OSSL_ASYNC_FD all_fd[32], afd;
6755 size_t num_all_fds = 0;
6756 int i;
6757
Olivier Houchard66ab4982019-02-26 18:37:15 +01006758 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006759 if (num_all_fds > 32) {
6760 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6761 return;
6762 }
6763
Olivier Houchard66ab4982019-02-26 18:37:15 +01006764 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006765
6766 /* If an async job is pending, we must try to
6767 to catch the end using polling before calling
6768 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006769 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006770 for (i=0 ; i < num_all_fds ; i++) {
6771 /* switch on an handler designed to
6772 * handle the SSL_free
6773 */
6774 afd = all_fd[i];
6775 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006776 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006777 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006778 /* To ensure that the fd cache won't be used
6779 * and we'll catch a real RD event.
6780 */
6781 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006782 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006783 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006784 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006785 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006786 return;
6787 }
Emeric Brun3854e012017-05-17 20:42:48 +02006788 /* Else we can remove the fds from the fdtab
6789 * and call SSL_free.
6790 * note: we do a fd_remove and not a delete
6791 * because the fd is owned by the engine.
6792 * the engine is responsible to close
6793 */
6794 for (i=0 ; i < num_all_fds ; i++)
6795 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006796 }
6797#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006798 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01006799 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006800 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006801 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006802 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006803 }
Emeric Brun46591952012-05-18 15:47:34 +02006804}
6805
6806/* This function tries to perform a clean shutdown on an SSL connection, and in
6807 * any case, flags the connection as reusable if no handshake was in progress.
6808 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006809static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006810{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006811 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006812
Emeric Brun46591952012-05-18 15:47:34 +02006813 if (conn->flags & CO_FL_HANDSHAKE)
6814 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006815 if (!clean)
6816 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006817 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006818 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006819 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006820 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006821 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006822 ERR_clear_error();
6823 }
Emeric Brun46591952012-05-18 15:47:34 +02006824}
6825
William Lallemandd4f946c2019-12-05 10:26:40 +01006826/* fill a buffer with the algorithm and size of a public key */
6827static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006828{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006829 int bits = 0;
6830 int sig = TLSEXT_signature_anonymous;
6831 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006832 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006833
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006834 pkey = X509_get_pubkey(crt);
6835 if (pkey) {
6836 bits = EVP_PKEY_bits(pkey);
6837 switch(EVP_PKEY_base_id(pkey)) {
6838 case EVP_PKEY_RSA:
6839 sig = TLSEXT_signature_rsa;
6840 break;
6841 case EVP_PKEY_EC:
6842 sig = TLSEXT_signature_ecdsa;
6843 break;
6844 case EVP_PKEY_DSA:
6845 sig = TLSEXT_signature_dsa;
6846 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006847 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006848 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006849 }
6850
6851 switch(sig) {
6852 case TLSEXT_signature_rsa:
6853 len = chunk_printf(out, "RSA%d", bits);
6854 break;
6855 case TLSEXT_signature_ecdsa:
6856 len = chunk_printf(out, "EC%d", bits);
6857 break;
6858 case TLSEXT_signature_dsa:
6859 len = chunk_printf(out, "DSA%d", bits);
6860 break;
6861 default:
6862 return 0;
6863 }
6864 if (len < 0)
6865 return 0;
6866 return 1;
6867}
6868
William Lallemandd4f946c2019-12-05 10:26:40 +01006869/* used for ppv2 pkey alog (can be used for logging) */
6870int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
6871{
6872 struct ssl_sock_ctx *ctx;
6873 X509 *crt;
6874
6875 if (!ssl_sock_is_ssl(conn))
6876 return 0;
6877
6878 ctx = conn->xprt_ctx;
6879
6880 crt = SSL_get_certificate(ctx->ssl);
6881 if (!crt)
6882 return 0;
6883
6884 return cert_get_pkey_algo(crt, out);
6885}
6886
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006887/* used for ppv2 cert signature (can be used for logging) */
6888const char *ssl_sock_get_cert_sig(struct connection *conn)
6889{
Christopher Faulet82004142019-09-10 10:12:03 +02006890 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006891
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006892 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6893 X509 *crt;
6894
6895 if (!ssl_sock_is_ssl(conn))
6896 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006897 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006898 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006899 if (!crt)
6900 return NULL;
6901 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6902 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6903}
6904
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006905/* used for ppv2 authority */
6906const char *ssl_sock_get_sni(struct connection *conn)
6907{
6908#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02006909 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006910
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006911 if (!ssl_sock_is_ssl(conn))
6912 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006913 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006914 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006915#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006916 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006917#endif
6918}
6919
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006920/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006921const char *ssl_sock_get_cipher_name(struct connection *conn)
6922{
Christopher Faulet82004142019-09-10 10:12:03 +02006923 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006924
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006925 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006926 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006927 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006928 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006929}
6930
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006931/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006932const char *ssl_sock_get_proto_version(struct connection *conn)
6933{
Christopher Faulet82004142019-09-10 10:12:03 +02006934 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006935
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006936 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006937 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006938 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006939 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006940}
6941
Willy Tarreau8d598402012-10-22 17:58:39 +02006942/* Extract a serial from a cert, and copy it to a chunk.
6943 * Returns 1 if serial is found and copied, 0 if no serial found and
6944 * -1 if output is not large enough.
6945 */
6946static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006947ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006948{
6949 ASN1_INTEGER *serial;
6950
6951 serial = X509_get_serialNumber(crt);
6952 if (!serial)
6953 return 0;
6954
6955 if (out->size < serial->length)
6956 return -1;
6957
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006958 memcpy(out->area, serial->data, serial->length);
6959 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006960 return 1;
6961}
6962
Emeric Brun43e79582014-10-29 19:03:26 +01006963/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006964 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6965 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006966 */
6967static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006968ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006969{
6970 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006971 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01006972
6973 len =i2d_X509(crt, NULL);
6974 if (len <= 0)
6975 return 1;
6976
6977 if (out->size < len)
6978 return -1;
6979
6980 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006981 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01006982 return 1;
6983}
6984
Emeric Brunce5ad802012-10-22 14:11:22 +02006985
Willy Tarreau83061a82018-07-13 11:56:34 +02006986/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02006987 * Returns 1 if serial is found and copied, 0 if no valid time found
6988 * and -1 if output is not large enough.
6989 */
6990static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006991ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02006992{
6993 if (tm->type == V_ASN1_GENERALIZEDTIME) {
6994 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
6995
6996 if (gentm->length < 12)
6997 return 0;
6998 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
6999 return 0;
7000 if (out->size < gentm->length-2)
7001 return -1;
7002
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007003 memcpy(out->area, gentm->data+2, gentm->length-2);
7004 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007005 return 1;
7006 }
7007 else if (tm->type == V_ASN1_UTCTIME) {
7008 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7009
7010 if (utctm->length < 10)
7011 return 0;
7012 if (utctm->data[0] >= 0x35)
7013 return 0;
7014 if (out->size < utctm->length)
7015 return -1;
7016
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007017 memcpy(out->area, utctm->data, utctm->length);
7018 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007019 return 1;
7020 }
7021
7022 return 0;
7023}
7024
Emeric Brun87855892012-10-17 17:39:35 +02007025/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7026 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7027 */
7028static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007029ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7030 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007031{
7032 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007033 ASN1_OBJECT *obj;
7034 ASN1_STRING *data;
7035 const unsigned char *data_ptr;
7036 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007037 int i, j, n;
7038 int cur = 0;
7039 const char *s;
7040 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007041 int name_count;
7042
7043 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007044
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007045 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007046 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007047 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007048 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007049 else
7050 j = i;
7051
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007052 ne = X509_NAME_get_entry(a, j);
7053 obj = X509_NAME_ENTRY_get_object(ne);
7054 data = X509_NAME_ENTRY_get_data(ne);
7055 data_ptr = ASN1_STRING_get0_data(data);
7056 data_len = ASN1_STRING_length(data);
7057 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007058 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007059 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007060 s = tmp;
7061 }
7062
7063 if (chunk_strcasecmp(entry, s) != 0)
7064 continue;
7065
7066 if (pos < 0)
7067 cur--;
7068 else
7069 cur++;
7070
7071 if (cur != pos)
7072 continue;
7073
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007074 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007075 return -1;
7076
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007077 memcpy(out->area, data_ptr, data_len);
7078 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007079 return 1;
7080 }
7081
7082 return 0;
7083
William Lallemandd4f946c2019-12-05 10:26:40 +01007084}
7085
7086/*
7087 * Extract and format the DNS SAN extensions and copy result into a chuink
7088 * Return 0;
7089 */
7090#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7091static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7092{
7093 int i;
7094 char *str;
7095 STACK_OF(GENERAL_NAME) *names = NULL;
7096
7097 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7098 if (names) {
7099 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7100 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7101 if (i > 0)
7102 chunk_appendf(out, ", ");
7103 if (name->type == GEN_DNS) {
7104 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7105 chunk_appendf(out, "DNS:%s", str);
7106 OPENSSL_free(str);
7107 }
7108 }
7109 }
7110 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7111 }
7112 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007113}
William Lallemandd4f946c2019-12-05 10:26:40 +01007114#endif
Emeric Brun87855892012-10-17 17:39:35 +02007115
Elliot Otchet71f82972020-01-15 08:12:14 -05007116/*
7117 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7118 * Currently supports rfc2253 for returning LDAP V3 DNs.
7119 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7120 */
7121static int
7122ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7123{
7124 BIO *bio = NULL;
7125 int ret = 0;
7126 int data_len = 0;
7127
7128 if (chunk_strcmp(format, "rfc2253") == 0) {
7129 bio = BIO_new(BIO_s_mem());
7130 if (bio == NULL)
7131 goto out;
7132
7133 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7134 goto out;
7135
7136 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7137 goto out;
7138
7139 out->data = data_len;
7140
7141 ret = 1;
7142 }
7143out:
7144 if (bio)
7145 BIO_free(bio);
7146 return ret;
7147}
7148
Emeric Brun87855892012-10-17 17:39:35 +02007149/* Extract and format full DN from a X509_NAME and copy result into a chunk
7150 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7151 */
7152static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007153ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007154{
7155 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007156 ASN1_OBJECT *obj;
7157 ASN1_STRING *data;
7158 const unsigned char *data_ptr;
7159 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007160 int i, n, ln;
7161 int l = 0;
7162 const char *s;
7163 char *p;
7164 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007165 int name_count;
7166
7167
7168 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007169
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007170 out->data = 0;
7171 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007172 for (i = 0; i < name_count; i++) {
7173 ne = X509_NAME_get_entry(a, i);
7174 obj = X509_NAME_ENTRY_get_object(ne);
7175 data = X509_NAME_ENTRY_get_data(ne);
7176 data_ptr = ASN1_STRING_get0_data(data);
7177 data_len = ASN1_STRING_length(data);
7178 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007179 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007180 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007181 s = tmp;
7182 }
7183 ln = strlen(s);
7184
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007185 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007186 if (l > out->size)
7187 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007188 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007189
7190 *(p++)='/';
7191 memcpy(p, s, ln);
7192 p += ln;
7193 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007194 memcpy(p, data_ptr, data_len);
7195 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007196 }
7197
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007198 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007199 return 0;
7200
7201 return 1;
7202}
7203
Olivier Houchardab28a322018-12-21 19:45:40 +01007204void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7205{
7206#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007207 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007208
Olivier Houcharde488ea82019-06-28 14:10:33 +02007209 if (!ssl_sock_is_ssl(conn))
7210 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007211 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007212 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007213#endif
7214}
7215
Willy Tarreau119a4082016-12-22 21:58:38 +01007216/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7217 * to disable SNI.
7218 */
Willy Tarreau63076412015-07-10 11:33:32 +02007219void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7220{
7221#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007222 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007223
Willy Tarreau119a4082016-12-22 21:58:38 +01007224 char *prev_name;
7225
Willy Tarreau63076412015-07-10 11:33:32 +02007226 if (!ssl_sock_is_ssl(conn))
7227 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007228 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007229
Willy Tarreau119a4082016-12-22 21:58:38 +01007230 /* if the SNI changes, we must destroy the reusable context so that a
7231 * new connection will present a new SNI. As an optimization we could
7232 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7233 * server.
7234 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007235 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007236 if ((!prev_name && hostname) ||
7237 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007238 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007239
Olivier Houchard66ab4982019-02-26 18:37:15 +01007240 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007241#endif
7242}
7243
Emeric Brun0abf8362014-06-24 18:26:41 +02007244/* Extract peer certificate's common name into the chunk dest
7245 * Returns
7246 * the len of the extracted common name
7247 * or 0 if no CN found in DN
7248 * or -1 on error case (i.e. no peer certificate)
7249 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007250int ssl_sock_get_remote_common_name(struct connection *conn,
7251 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007252{
Christopher Faulet82004142019-09-10 10:12:03 +02007253 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007254 X509 *crt = NULL;
7255 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007256 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007257 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007258 .area = (char *)&find_cn,
7259 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007260 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007261 int result = -1;
David Safb76832014-05-08 23:42:08 -04007262
7263 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007264 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007265 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007266
7267 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007268 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007269 if (!crt)
7270 goto out;
7271
7272 name = X509_get_subject_name(crt);
7273 if (!name)
7274 goto out;
David Safb76832014-05-08 23:42:08 -04007275
Emeric Brun0abf8362014-06-24 18:26:41 +02007276 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7277out:
David Safb76832014-05-08 23:42:08 -04007278 if (crt)
7279 X509_free(crt);
7280
7281 return result;
7282}
7283
Dave McCowan328fb582014-07-30 10:39:13 -04007284/* returns 1 if client passed a certificate for this session, 0 if not */
7285int ssl_sock_get_cert_used_sess(struct connection *conn)
7286{
Christopher Faulet82004142019-09-10 10:12:03 +02007287 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007288 X509 *crt = NULL;
7289
7290 if (!ssl_sock_is_ssl(conn))
7291 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007292 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007293
7294 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007295 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007296 if (!crt)
7297 return 0;
7298
7299 X509_free(crt);
7300 return 1;
7301}
7302
7303/* returns 1 if client passed a certificate for this connection, 0 if not */
7304int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007305{
Christopher Faulet82004142019-09-10 10:12:03 +02007306 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007307
David Safb76832014-05-08 23:42:08 -04007308 if (!ssl_sock_is_ssl(conn))
7309 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007310 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007311 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007312}
7313
7314/* returns result from SSL verify */
7315unsigned int ssl_sock_get_verify_result(struct connection *conn)
7316{
Christopher Faulet82004142019-09-10 10:12:03 +02007317 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007318
David Safb76832014-05-08 23:42:08 -04007319 if (!ssl_sock_is_ssl(conn))
7320 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007321 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007322 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007323}
7324
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007325/* Returns the application layer protocol name in <str> and <len> when known.
7326 * Zero is returned if the protocol name was not found, otherwise non-zero is
7327 * returned. The string is allocated in the SSL context and doesn't have to be
7328 * freed by the caller. NPN is also checked if available since older versions
7329 * of openssl (1.0.1) which are more common in field only support this one.
7330 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007331static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007332{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007333#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7334 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007335 struct ssl_sock_ctx *ctx = xprt_ctx;
7336 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007337 return 0;
7338
7339 *str = NULL;
7340
7341#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007342 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007343 if (*str)
7344 return 1;
7345#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007346#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007347 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007348 if (*str)
7349 return 1;
7350#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007351#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007352 return 0;
7353}
7354
Willy Tarreau7875d092012-09-10 08:20:03 +02007355/***** Below are some sample fetching functions for ACL/patterns *****/
7356
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007357static int
7358smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7359{
7360 struct connection *conn;
7361
7362 conn = objt_conn(smp->sess->origin);
7363 if (!conn || conn->xprt != &ssl_sock)
7364 return 0;
7365
7366 smp->flags = 0;
7367 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007368#ifdef OPENSSL_IS_BORINGSSL
7369 {
7370 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7371 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7372 SSL_early_data_accepted(ctx->ssl));
7373 }
7374#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007375 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
7376 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007377#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007378 return 1;
7379}
7380
Emeric Brune64aef12012-09-21 13:15:06 +02007381/* boolean, returns true if client cert was present */
7382static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007383smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007384{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007385 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007386 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007387
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007388 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007389 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007390 return 0;
7391
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007392 ctx = conn->xprt_ctx;
7393
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007394 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02007395 smp->flags |= SMP_F_MAY_CHANGE;
7396 return 0;
7397 }
7398
7399 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007400 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007401 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007402
7403 return 1;
7404}
7405
Emeric Brun43e79582014-10-29 19:03:26 +01007406/* binary, returns a certificate in a binary chunk (der/raw).
7407 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7408 * should be use.
7409 */
7410static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007411smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007412{
7413 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7414 X509 *crt = NULL;
7415 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007416 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007417 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007418 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007419
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007420 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007421 if (!conn || conn->xprt != &ssl_sock)
7422 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007423 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007424
7425 if (!(conn->flags & CO_FL_CONNECTED)) {
7426 smp->flags |= SMP_F_MAY_CHANGE;
7427 return 0;
7428 }
7429
7430 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007431 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007432 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007433 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007434
7435 if (!crt)
7436 goto out;
7437
7438 smp_trash = get_trash_chunk();
7439 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7440 goto out;
7441
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007442 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007443 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007444 ret = 1;
7445out:
7446 /* SSL_get_peer_certificate, it increase X509 * ref count */
7447 if (cert_peer && crt)
7448 X509_free(crt);
7449 return ret;
7450}
7451
Emeric Brunba841a12014-04-30 17:05:08 +02007452/* binary, returns serial of certificate in a binary chunk.
7453 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7454 * should be use.
7455 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007456static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007457smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007458{
Emeric Brunba841a12014-04-30 17:05:08 +02007459 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007460 X509 *crt = NULL;
7461 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007462 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007463 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007464 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007465
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007466 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007467 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007468 return 0;
7469
Olivier Houchard66ab4982019-02-26 18:37:15 +01007470 ctx = conn->xprt_ctx;
7471
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007472 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007473 smp->flags |= SMP_F_MAY_CHANGE;
7474 return 0;
7475 }
7476
Emeric Brunba841a12014-04-30 17:05:08 +02007477 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007478 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007479 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007480 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007481
Willy Tarreau8d598402012-10-22 17:58:39 +02007482 if (!crt)
7483 goto out;
7484
Willy Tarreau47ca5452012-12-23 20:22:19 +01007485 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007486 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7487 goto out;
7488
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007489 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007490 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007491 ret = 1;
7492out:
Emeric Brunba841a12014-04-30 17:05:08 +02007493 /* SSL_get_peer_certificate, it increase X509 * ref count */
7494 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007495 X509_free(crt);
7496 return ret;
7497}
Emeric Brune64aef12012-09-21 13:15:06 +02007498
Emeric Brunba841a12014-04-30 17:05:08 +02007499/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7500 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7501 * should be use.
7502 */
James Votha051b4a2013-05-14 20:37:59 +02007503static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007504smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007505{
Emeric Brunba841a12014-04-30 17:05:08 +02007506 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007507 X509 *crt = NULL;
7508 const EVP_MD *digest;
7509 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007510 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007511 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007512 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02007513
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007514 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007515 if (!conn || conn->xprt != &ssl_sock)
7516 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007517 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007518
7519 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02007520 smp->flags |= SMP_F_MAY_CHANGE;
7521 return 0;
7522 }
7523
Emeric Brunba841a12014-04-30 17:05:08 +02007524 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007525 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007526 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007527 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02007528 if (!crt)
7529 goto out;
7530
7531 smp_trash = get_trash_chunk();
7532 digest = EVP_sha1();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007533 X509_digest(crt, digest, (unsigned char *) smp_trash->area,
7534 (unsigned int *)&smp_trash->data);
James Votha051b4a2013-05-14 20:37:59 +02007535
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007536 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007537 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02007538 ret = 1;
7539out:
Emeric Brunba841a12014-04-30 17:05:08 +02007540 /* SSL_get_peer_certificate, it increase X509 * ref count */
7541 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02007542 X509_free(crt);
7543 return ret;
7544}
7545
Emeric Brunba841a12014-04-30 17:05:08 +02007546/* string, returns certificate's notafter date in ASN1_UTCTIME format.
7547 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7548 * should be use.
7549 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007550static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007551smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007552{
Emeric Brunba841a12014-04-30 17:05:08 +02007553 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007554 X509 *crt = NULL;
7555 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007556 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007557 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007558 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007559
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007560 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007561 if (!conn || conn->xprt != &ssl_sock)
7562 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007563 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007564
7565 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007566 smp->flags |= SMP_F_MAY_CHANGE;
7567 return 0;
7568 }
7569
Emeric Brunba841a12014-04-30 17:05:08 +02007570 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007571 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007572 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007573 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007574 if (!crt)
7575 goto out;
7576
Willy Tarreau47ca5452012-12-23 20:22:19 +01007577 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007578 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007579 goto out;
7580
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007581 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007582 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007583 ret = 1;
7584out:
Emeric Brunba841a12014-04-30 17:05:08 +02007585 /* SSL_get_peer_certificate, it increase X509 * ref count */
7586 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007587 X509_free(crt);
7588 return ret;
7589}
7590
Emeric Brunba841a12014-04-30 17:05:08 +02007591/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
7592 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7593 * should be use.
7594 */
Emeric Brun87855892012-10-17 17:39:35 +02007595static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007596smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007597{
Emeric Brunba841a12014-04-30 17:05:08 +02007598 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007599 X509 *crt = NULL;
7600 X509_NAME *name;
7601 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007602 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007603 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007604 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007605
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007606 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007607 if (!conn || conn->xprt != &ssl_sock)
7608 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007609 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007610
7611 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007612 smp->flags |= SMP_F_MAY_CHANGE;
7613 return 0;
7614 }
7615
Emeric Brunba841a12014-04-30 17:05:08 +02007616 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007617 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007618 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007619 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007620 if (!crt)
7621 goto out;
7622
7623 name = X509_get_issuer_name(crt);
7624 if (!name)
7625 goto out;
7626
Willy Tarreau47ca5452012-12-23 20:22:19 +01007627 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007628 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007629 int pos = 1;
7630
7631 if (args[1].type == ARGT_SINT)
7632 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007633
7634 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7635 goto out;
7636 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007637 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7638 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7639 goto out;
7640 }
Emeric Brun87855892012-10-17 17:39:35 +02007641 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7642 goto out;
7643
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007644 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007645 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007646 ret = 1;
7647out:
Emeric Brunba841a12014-04-30 17:05:08 +02007648 /* SSL_get_peer_certificate, it increase X509 * ref count */
7649 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007650 X509_free(crt);
7651 return ret;
7652}
7653
Emeric Brunba841a12014-04-30 17:05:08 +02007654/* string, returns notbefore date in ASN1_UTCTIME format.
7655 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7656 * should be use.
7657 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007658static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007659smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007660{
Emeric Brunba841a12014-04-30 17:05:08 +02007661 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007662 X509 *crt = NULL;
7663 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007664 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007665 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007666 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007667
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007668 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007669 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02007670 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007671 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007672
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007673 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007674 smp->flags |= SMP_F_MAY_CHANGE;
7675 return 0;
7676 }
7677
Emeric Brunba841a12014-04-30 17:05:08 +02007678 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007679 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007680 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007681 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007682 if (!crt)
7683 goto out;
7684
Willy Tarreau47ca5452012-12-23 20:22:19 +01007685 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007686 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007687 goto out;
7688
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007689 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007690 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007691 ret = 1;
7692out:
Emeric Brunba841a12014-04-30 17:05:08 +02007693 /* SSL_get_peer_certificate, it increase X509 * ref count */
7694 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007695 X509_free(crt);
7696 return ret;
7697}
7698
Emeric Brunba841a12014-04-30 17:05:08 +02007699/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
7700 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7701 * should be use.
7702 */
Emeric Brun87855892012-10-17 17:39:35 +02007703static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007704smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007705{
Emeric Brunba841a12014-04-30 17:05:08 +02007706 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007707 X509 *crt = NULL;
7708 X509_NAME *name;
7709 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007710 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007711 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007712 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007713
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007714 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007715 if (!conn || conn->xprt != &ssl_sock)
7716 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007717 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007718
7719 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007720 smp->flags |= SMP_F_MAY_CHANGE;
7721 return 0;
7722 }
7723
Emeric Brunba841a12014-04-30 17:05:08 +02007724 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007725 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007726 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007727 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007728 if (!crt)
7729 goto out;
7730
7731 name = X509_get_subject_name(crt);
7732 if (!name)
7733 goto out;
7734
Willy Tarreau47ca5452012-12-23 20:22:19 +01007735 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007736 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007737 int pos = 1;
7738
7739 if (args[1].type == ARGT_SINT)
7740 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007741
7742 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7743 goto out;
7744 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007745 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7746 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7747 goto out;
7748 }
Emeric Brun87855892012-10-17 17:39:35 +02007749 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7750 goto out;
7751
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007752 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007753 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007754 ret = 1;
7755out:
Emeric Brunba841a12014-04-30 17:05:08 +02007756 /* SSL_get_peer_certificate, it increase X509 * ref count */
7757 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007758 X509_free(crt);
7759 return ret;
7760}
Emeric Brun9143d372012-12-20 15:44:16 +01007761
7762/* integer, returns true if current session use a client certificate */
7763static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007764smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007765{
7766 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007767 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007768 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007769
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007770 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007771 if (!conn || conn->xprt != &ssl_sock)
7772 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007773 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007774
7775 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01007776 smp->flags |= SMP_F_MAY_CHANGE;
7777 return 0;
7778 }
7779
7780 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007781 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007782 if (crt) {
7783 X509_free(crt);
7784 }
7785
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007786 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007787 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007788 return 1;
7789}
7790
Emeric Brunba841a12014-04-30 17:05:08 +02007791/* integer, returns the certificate version
7792 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7793 * should be use.
7794 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007795static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007796smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007797{
Emeric Brunba841a12014-04-30 17:05:08 +02007798 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007799 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007800 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007801 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007802
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007803 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007804 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007805 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007806 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007807
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007808 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007809 smp->flags |= SMP_F_MAY_CHANGE;
7810 return 0;
7811 }
7812
Emeric Brunba841a12014-04-30 17:05:08 +02007813 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007814 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007815 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007816 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007817 if (!crt)
7818 return 0;
7819
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007820 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007821 /* SSL_get_peer_certificate increase X509 * ref count */
7822 if (cert_peer)
7823 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007824 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007825
7826 return 1;
7827}
7828
Emeric Brunba841a12014-04-30 17:05:08 +02007829/* string, returns the certificate's signature algorithm.
7830 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7831 * should be use.
7832 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007833static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007834smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007835{
Emeric Brunba841a12014-04-30 17:05:08 +02007836 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007837 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007838 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007839 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007840 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007841 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007842
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007843 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007844 if (!conn || conn->xprt != &ssl_sock)
7845 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007846 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007847
7848 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007849 smp->flags |= SMP_F_MAY_CHANGE;
7850 return 0;
7851 }
7852
Emeric Brunba841a12014-04-30 17:05:08 +02007853 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007854 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007855 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007856 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007857 if (!crt)
7858 return 0;
7859
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007860 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7861 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007862
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007863 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7864 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007865 /* SSL_get_peer_certificate increase X509 * ref count */
7866 if (cert_peer)
7867 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007868 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007869 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007870
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007871 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007872 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007873 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007874 /* SSL_get_peer_certificate increase X509 * ref count */
7875 if (cert_peer)
7876 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007877
7878 return 1;
7879}
7880
Emeric Brunba841a12014-04-30 17:05:08 +02007881/* string, returns the certificate's key algorithm.
7882 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7883 * should be use.
7884 */
Emeric Brun521a0112012-10-22 12:22:55 +02007885static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007886smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02007887{
Emeric Brunba841a12014-04-30 17:05:08 +02007888 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02007889 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007890 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02007891 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007892 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007893 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02007894
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007895 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007896 if (!conn || conn->xprt != &ssl_sock)
7897 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007898 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007899
7900 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02007901 smp->flags |= SMP_F_MAY_CHANGE;
7902 return 0;
7903 }
7904
Emeric Brunba841a12014-04-30 17:05:08 +02007905 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007906 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007907 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007908 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02007909 if (!crt)
7910 return 0;
7911
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007912 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
7913 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02007914
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007915 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7916 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007917 /* SSL_get_peer_certificate increase X509 * ref count */
7918 if (cert_peer)
7919 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007920 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007921 }
Emeric Brun521a0112012-10-22 12:22:55 +02007922
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007923 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007924 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007925 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007926 if (cert_peer)
7927 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007928
7929 return 1;
7930}
7931
Emeric Brun645ae792014-04-30 14:21:06 +02007932/* boolean, returns true if front conn. transport layer is SSL.
7933 * This function is also usable on backend conn if the fetch keyword 5th
7934 * char is 'b'.
7935 */
Willy Tarreau7875d092012-09-10 08:20:03 +02007936static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007937smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007938{
Emeric Bruneb8def92018-02-19 15:59:48 +01007939 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7940 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007941
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007942 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007943 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007944 return 1;
7945}
7946
Emeric Brun2525b6b2012-10-18 15:59:43 +02007947/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007948static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007949smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007950{
7951#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007952 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007953 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007954
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007955 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007956 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007957 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007958 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007959 return 1;
7960#else
7961 return 0;
7962#endif
7963}
7964
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007965/* boolean, returns true if client session has been resumed.
7966 * This function is also usable on backend conn if the fetch keyword 5th
7967 * char is 'b'.
7968 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007969static int
7970smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
7971{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007972 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7973 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007974 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007975
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007976
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007977 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007978 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007979 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007980 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007981 return 1;
7982}
7983
Emeric Brun645ae792014-04-30 14:21:06 +02007984/* string, returns the used cipher if front conn. transport layer is SSL.
7985 * This function is also usable on backend conn if the fetch keyword 5th
7986 * char is 'b'.
7987 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007988static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007989smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007990{
Emeric Bruneb8def92018-02-19 15:59:48 +01007991 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7992 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007993 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007994
Willy Tarreaube508f12016-03-10 11:47:01 +01007995 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007996 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007997 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007998 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007999
Olivier Houchard66ab4982019-02-26 18:37:15 +01008000 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008001 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008002 return 0;
8003
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008004 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008005 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008006 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008007
8008 return 1;
8009}
8010
Emeric Brun645ae792014-04-30 14:21:06 +02008011/* integer, returns the algoritm's keysize if front conn. transport layer
8012 * is SSL.
8013 * This function is also usable on backend conn if the fetch keyword 5th
8014 * char is 'b'.
8015 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008016static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008017smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008018{
Emeric Bruneb8def92018-02-19 15:59:48 +01008019 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8020 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008021 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008022 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008023
Emeric Brun589fcad2012-10-16 14:13:26 +02008024 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008025 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008026 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008027 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008028
Olivier Houchard66ab4982019-02-26 18:37:15 +01008029 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008030 return 0;
8031
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008032 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008033 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008034
8035 return 1;
8036}
8037
Emeric Brun645ae792014-04-30 14:21:06 +02008038/* integer, returns the used keysize if front conn. transport layer is SSL.
8039 * This function is also usable on backend conn if the fetch keyword 5th
8040 * char is 'b'.
8041 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008042static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008043smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008044{
Emeric Bruneb8def92018-02-19 15:59:48 +01008045 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8046 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008047 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008048
Emeric Brun589fcad2012-10-16 14:13:26 +02008049 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008050 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8051 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008052 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008053
Olivier Houchard66ab4982019-02-26 18:37:15 +01008054 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008055 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008056 return 0;
8057
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008058 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008059
8060 return 1;
8061}
8062
Bernard Spil13c53f82018-02-15 13:34:58 +01008063#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008064static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008065smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008066{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008067 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008068 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008069
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008070 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008071 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008072
Olivier Houchard6b77f492018-11-22 18:18:29 +01008073 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8074 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008075 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8076 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008077 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008078
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008079 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008080 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008081 (const unsigned char **)&smp->data.u.str.area,
8082 (unsigned *)&smp->data.u.str.data);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008083
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008084 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008085 return 0;
8086
8087 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008088}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008089#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008090
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008091#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008092static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008093smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008094{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008095 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008096 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008097
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008098 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008099 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008100
Olivier Houchard6b77f492018-11-22 18:18:29 +01008101 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8102 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8103
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008104 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008105 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008106 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008107
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008108 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008109 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008110 (const unsigned char **)&smp->data.u.str.area,
8111 (unsigned *)&smp->data.u.str.data);
Willy Tarreauab861d32013-04-02 02:30:41 +02008112
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008113 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008114 return 0;
8115
8116 return 1;
8117}
8118#endif
8119
Emeric Brun645ae792014-04-30 14:21:06 +02008120/* string, returns the used protocol if front conn. transport layer is SSL.
8121 * This function is also usable on backend conn if the fetch keyword 5th
8122 * char is 'b'.
8123 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008124static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008125smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008126{
Emeric Bruneb8def92018-02-19 15:59:48 +01008127 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8128 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008129 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008130
Emeric Brun589fcad2012-10-16 14:13:26 +02008131 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008132 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8133 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008134 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008135
Olivier Houchard66ab4982019-02-26 18:37:15 +01008136 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008137 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008138 return 0;
8139
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008140 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008141 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008142 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008143
8144 return 1;
8145}
8146
Willy Tarreau87b09662015-04-03 00:22:06 +02008147/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008148 * This function is also usable on backend conn if the fetch keyword 5th
8149 * char is 'b'.
8150 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008151#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008152static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008153smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008154{
Emeric Bruneb8def92018-02-19 15:59:48 +01008155 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8156 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008157 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008158 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008159
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008160 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008161 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008162
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008163 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8164 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008165 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008166
Olivier Houchard66ab4982019-02-26 18:37:15 +01008167 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008168 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008169 return 0;
8170
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008171 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess,
8172 (unsigned int *)&smp->data.u.str.data);
8173 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008174 return 0;
8175
8176 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008177}
Patrick Hemmer41966772018-04-28 19:15:48 -04008178#endif
8179
Emeric Brunfe68f682012-10-16 14:59:28 +02008180
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008181#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008182static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008183smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8184{
8185 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8186 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8187 struct buffer *data;
8188 struct ssl_sock_ctx *ctx;
8189
8190 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8191 return 0;
8192 ctx = conn->xprt_ctx;
8193
8194 data = get_trash_chunk();
8195 if (kw[7] == 'c')
8196 data->data = SSL_get_client_random(ctx->ssl,
8197 (unsigned char *) data->area,
8198 data->size);
8199 else
8200 data->data = SSL_get_server_random(ctx->ssl,
8201 (unsigned char *) data->area,
8202 data->size);
8203 if (!data->data)
8204 return 0;
8205
8206 smp->flags = 0;
8207 smp->data.type = SMP_T_BIN;
8208 smp->data.u.str = *data;
8209
8210 return 1;
8211}
8212
8213static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008214smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8215{
8216 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8217 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8218 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008219 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008220 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008221
8222 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8223 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008224 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008225
Olivier Houchard66ab4982019-02-26 18:37:15 +01008226 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008227 if (!ssl_sess)
8228 return 0;
8229
8230 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008231 data->data = SSL_SESSION_get_master_key(ssl_sess,
8232 (unsigned char *) data->area,
8233 data->size);
8234 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008235 return 0;
8236
8237 smp->flags = 0;
8238 smp->data.type = SMP_T_BIN;
8239 smp->data.u.str = *data;
8240
8241 return 1;
8242}
8243#endif
8244
Patrick Hemmer41966772018-04-28 19:15:48 -04008245#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008246static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008247smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008248{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008249 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008250 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008251
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008252 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008253 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008254
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008255 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008256 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8257 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008258 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008259
Olivier Houchard66ab4982019-02-26 18:37:15 +01008260 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008261 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008262 return 0;
8263
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008264 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008265 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008266}
Patrick Hemmer41966772018-04-28 19:15:48 -04008267#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008268
David Sc1ad52e2014-04-08 18:48:47 -04008269static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008270smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8271{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008272 struct connection *conn;
8273 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008274 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008275
8276 conn = objt_conn(smp->sess->origin);
8277 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8278 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008279 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008280
Olivier Houchard66ab4982019-02-26 18:37:15 +01008281 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008282 if (!capture)
8283 return 0;
8284
8285 smp->flags = SMP_F_CONST;
8286 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008287 smp->data.u.str.area = capture->ciphersuite;
8288 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008289 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008290}
8291
8292static int
8293smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8294{
Willy Tarreau83061a82018-07-13 11:56:34 +02008295 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008296
8297 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8298 return 0;
8299
8300 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008301 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008302 smp->data.type = SMP_T_BIN;
8303 smp->data.u.str = *data;
8304 return 1;
8305}
8306
8307static int
8308smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8309{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008310 struct connection *conn;
8311 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008312 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008313
8314 conn = objt_conn(smp->sess->origin);
8315 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8316 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008317 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008318
Olivier Houchard66ab4982019-02-26 18:37:15 +01008319 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008320 if (!capture)
8321 return 0;
8322
8323 smp->data.type = SMP_T_SINT;
8324 smp->data.u.sint = capture->xxh64;
8325 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008326}
8327
8328static int
8329smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8330{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008331#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008332 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008333 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008334
8335 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8336 return 0;
8337
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008338 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008339 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008340 const char *str;
8341 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008342 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008343 uint16_t id = (bin[0] << 8) | bin[1];
8344#if defined(OPENSSL_IS_BORINGSSL)
8345 cipher = SSL_get_cipher_by_value(id);
8346#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008347 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008348 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8349 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008350#endif
8351 str = SSL_CIPHER_get_name(cipher);
8352 if (!str || strcmp(str, "(NONE)") == 0)
8353 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008354 else
8355 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8356 }
8357 smp->data.type = SMP_T_STR;
8358 smp->data.u.str = *data;
8359 return 1;
8360#else
8361 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8362#endif
8363}
8364
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008365#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008366static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008367smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008368{
Emeric Bruneb8def92018-02-19 15:59:48 +01008369 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8370 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008371 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008372 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008373 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008374
8375 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008376 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8377 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008378 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008379
8380 if (!(conn->flags & CO_FL_CONNECTED)) {
8381 smp->flags |= SMP_F_MAY_CHANGE;
8382 return 0;
8383 }
8384
8385 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008386 if (!SSL_session_reused(ctx->ssl))
8387 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008388 finished_trash->area,
8389 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008390 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008391 finished_len = SSL_get_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
8395 if (!finished_len)
8396 return 0;
8397
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008398 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008399 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008400 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008401
8402 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008403}
Patrick Hemmer41966772018-04-28 19:15:48 -04008404#endif
David Sc1ad52e2014-04-08 18:48:47 -04008405
Emeric Brun2525b6b2012-10-18 15:59:43 +02008406/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008407static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008408smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008409{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008410 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008411 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008412
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008413 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008414 if (!conn || conn->xprt != &ssl_sock)
8415 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008416 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008417
8418 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008419 smp->flags = SMP_F_MAY_CHANGE;
8420 return 0;
8421 }
8422
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008423 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008424 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008425 smp->flags = 0;
8426
8427 return 1;
8428}
8429
Emeric Brun2525b6b2012-10-18 15:59:43 +02008430/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008431static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008432smp_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 +02008433{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008434 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008435 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008436
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008437 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008438 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008439 return 0;
8440
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008441 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008442 smp->flags = SMP_F_MAY_CHANGE;
8443 return 0;
8444 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008445 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008446
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008447 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008448 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008449 smp->flags = 0;
8450
8451 return 1;
8452}
8453
Emeric Brun2525b6b2012-10-18 15:59:43 +02008454/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008455static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008456smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008457{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008458 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008459 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008460
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008461 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008462 if (!conn || conn->xprt != &ssl_sock)
8463 return 0;
8464
8465 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008466 smp->flags = SMP_F_MAY_CHANGE;
8467 return 0;
8468 }
8469
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008470 ctx = conn->xprt_ctx;
8471
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008472 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008473 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008474 smp->flags = 0;
8475
8476 return 1;
8477}
8478
Emeric Brun2525b6b2012-10-18 15:59:43 +02008479/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008480static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008481smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008482{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008483 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008484 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008485
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008486 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008487 if (!conn || conn->xprt != &ssl_sock)
8488 return 0;
8489
8490 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008491 smp->flags = SMP_F_MAY_CHANGE;
8492 return 0;
8493 }
8494
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008495 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008496 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008497 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008498
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008499 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008500 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008501 smp->flags = 0;
8502
8503 return 1;
8504}
8505
Emeric Brunfb510ea2012-10-05 12:00:26 +02008506/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008507static 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 +02008508{
8509 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008510 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008511 return ERR_ALERT | ERR_FATAL;
8512 }
8513
Willy Tarreauef934602016-12-22 23:12:01 +01008514 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8515 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008516 else
8517 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008518
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02008519 if (!ssl_store_load_locations_file(conf->ca_file)) {
8520 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->ca_file);
8521 return ERR_ALERT | ERR_FATAL;
8522 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02008523 return 0;
8524}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008525static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8526{
8527 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
8528}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008529
Christopher Faulet31af49d2015-06-09 17:29:50 +02008530/* parse the "ca-sign-file" bind keyword */
8531static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8532{
8533 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008534 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008535 return ERR_ALERT | ERR_FATAL;
8536 }
8537
Willy Tarreauef934602016-12-22 23:12:01 +01008538 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8539 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008540 else
8541 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
8542
8543 return 0;
8544}
8545
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008546/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02008547static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8548{
8549 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008550 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008551 return ERR_ALERT | ERR_FATAL;
8552 }
8553 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
8554 return 0;
8555}
8556
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008557/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008558static 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 +02008559{
8560 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008561 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008562 return ERR_ALERT | ERR_FATAL;
8563 }
8564
Emeric Brun76d88952012-10-05 15:47:31 +02008565 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02008566 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008567 return 0;
8568}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008569static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8570{
8571 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
8572}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008573
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008574#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008575/* parse the "ciphersuites" bind keyword */
8576static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8577{
8578 if (!*args[cur_arg + 1]) {
8579 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
8580 return ERR_ALERT | ERR_FATAL;
8581 }
8582
8583 free(conf->ciphersuites);
8584 conf->ciphersuites = strdup(args[cur_arg + 1]);
8585 return 0;
8586}
8587static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8588{
8589 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
8590}
8591#endif
8592
Willy Tarreaubbc91962019-10-16 16:42:19 +02008593/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008594static 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 +02008595{
Willy Tarreau38011032013-08-13 16:59:39 +02008596 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02008597
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008598 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008599 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008600 return ERR_ALERT | ERR_FATAL;
8601 }
8602
Willy Tarreauef934602016-12-22 23:12:01 +01008603 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
8604 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02008605 memprintf(err, "'%s' : path too long", args[cur_arg]);
8606 return ERR_ALERT | ERR_FATAL;
8607 }
Willy Tarreauef934602016-12-22 23:12:01 +01008608 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02008609 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008610 }
8611
Willy Tarreaubbc91962019-10-16 16:42:19 +02008612 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008613}
8614
Willy Tarreaubbc91962019-10-16 16:42:19 +02008615/* 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 +01008616static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8617{
Willy Tarreaubbc91962019-10-16 16:42:19 +02008618 int err_code;
8619
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008620 if (!*args[cur_arg + 1]) {
8621 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
8622 return ERR_ALERT | ERR_FATAL;
8623 }
8624
Willy Tarreaubbc91962019-10-16 16:42:19 +02008625 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err);
8626 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02008627 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008628
Willy Tarreaubbc91962019-10-16 16:42:19 +02008629 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008630}
8631
Emeric Brunfb510ea2012-10-05 12:00:26 +02008632/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008633static 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 +02008634{
Emeric Brun051cdab2012-10-02 19:25:50 +02008635#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01008636 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02008637 return ERR_ALERT | ERR_FATAL;
8638#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02008639 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008640 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008641 return ERR_ALERT | ERR_FATAL;
8642 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008643
Willy Tarreauef934602016-12-22 23:12:01 +01008644 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8645 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008646 else
8647 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008648
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01008649 if (!ssl_store_load_locations_file(conf->crl_file)) {
8650 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
8651 return ERR_ALERT | ERR_FATAL;
8652 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008653 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02008654#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02008655}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008656static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8657{
8658 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
8659}
Emeric Brun2b58d042012-09-20 17:10:03 +02008660
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008661/* parse the "curves" bind keyword keyword */
8662static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8663{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01008664#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008665 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008666 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008667 return ERR_ALERT | ERR_FATAL;
8668 }
8669 conf->curves = strdup(args[cur_arg + 1]);
8670 return 0;
8671#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008672 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008673 return ERR_ALERT | ERR_FATAL;
8674#endif
8675}
8676static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8677{
8678 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
8679}
8680
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008681/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008682static 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 +02008683{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008684#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01008685 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008686 return ERR_ALERT | ERR_FATAL;
8687#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01008688 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 +02008689 return ERR_ALERT | ERR_FATAL;
8690#else
8691 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008692 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008693 return ERR_ALERT | ERR_FATAL;
8694 }
8695
8696 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008697
8698 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02008699#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008700}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008701static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8702{
8703 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
8704}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008705
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008706/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02008707static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8708{
8709 int code;
8710 char *p = args[cur_arg + 1];
8711 unsigned long long *ignerr = &conf->crt_ignerr;
8712
8713 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008714 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008715 return ERR_ALERT | ERR_FATAL;
8716 }
8717
8718 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
8719 ignerr = &conf->ca_ignerr;
8720
8721 if (strcmp(p, "all") == 0) {
8722 *ignerr = ~0ULL;
8723 return 0;
8724 }
8725
8726 while (p) {
8727 code = atoi(p);
8728 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008729 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
8730 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008731 return ERR_ALERT | ERR_FATAL;
8732 }
8733 *ignerr |= 1ULL << code;
8734 p = strchr(p, ',');
8735 if (p)
8736 p++;
8737 }
8738
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008739 return 0;
8740}
8741
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008742/* parse tls_method_options "no-xxx" and "force-xxx" */
8743static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008744{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008745 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008746 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008747 p = strchr(arg, '-');
8748 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008749 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008750 p++;
8751 if (!strcmp(p, "sslv3"))
8752 v = CONF_SSLV3;
8753 else if (!strcmp(p, "tlsv10"))
8754 v = CONF_TLSV10;
8755 else if (!strcmp(p, "tlsv11"))
8756 v = CONF_TLSV11;
8757 else if (!strcmp(p, "tlsv12"))
8758 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008759 else if (!strcmp(p, "tlsv13"))
8760 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008761 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008762 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008763 if (!strncmp(arg, "no-", 3))
8764 methods->flags |= methodVersions[v].flag;
8765 else if (!strncmp(arg, "force-", 6))
8766 methods->min = methods->max = v;
8767 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008768 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008769 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008770 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01008771 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008772 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008773}
8774
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008775static 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 +02008776{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008777 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008778}
8779
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008780static 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 +02008781{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008782 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8783}
8784
8785/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8786static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8787{
8788 uint16_t i, v = 0;
8789 char *argv = args[cur_arg + 1];
8790 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008791 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008792 return ERR_ALERT | ERR_FATAL;
8793 }
8794 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8795 if (!strcmp(argv, methodVersions[i].name))
8796 v = i;
8797 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008798 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008799 return ERR_ALERT | ERR_FATAL;
8800 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008801 if (!strcmp("ssl-min-ver", args[cur_arg]))
8802 methods->min = v;
8803 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8804 methods->max = v;
8805 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01008806 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008807 return ERR_ALERT | ERR_FATAL;
8808 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008809 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008810}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008811
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008812static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8813{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008814#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008815 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 +02008816#endif
8817 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8818}
8819
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008820static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8821{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008822 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008823}
8824
8825static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8826{
8827 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8828}
8829
Emeric Brun2d0c4822012-10-02 13:45:20 +02008830/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008831static 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 +02008832{
Emeric Brun89675492012-10-05 13:48:26 +02008833 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008834 return 0;
8835}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008836
Olivier Houchardc2aae742017-09-22 18:26:28 +02008837/* parse the "allow-0rtt" bind keyword */
8838static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8839{
8840 conf->early_data = 1;
8841 return 0;
8842}
8843
8844static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8845{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008846 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008847 return 0;
8848}
8849
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008850/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008851static 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 +02008852{
Bernard Spil13c53f82018-02-15 13:34:58 +01008853#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008854 char *p1, *p2;
8855
8856 if (!*args[cur_arg + 1]) {
8857 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8858 return ERR_ALERT | ERR_FATAL;
8859 }
8860
8861 free(conf->npn_str);
8862
Willy Tarreau3724da12016-02-12 17:11:12 +01008863 /* the NPN string is built as a suite of (<len> <name>)*,
8864 * so we reuse each comma to store the next <len> and need
8865 * one more for the end of the string.
8866 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008867 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008868 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008869 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8870
8871 /* replace commas with the name length */
8872 p1 = conf->npn_str;
8873 p2 = p1 + 1;
8874 while (1) {
8875 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8876 if (!p2)
8877 p2 = p1 + 1 + strlen(p1 + 1);
8878
8879 if (p2 - (p1 + 1) > 255) {
8880 *p2 = '\0';
8881 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8882 return ERR_ALERT | ERR_FATAL;
8883 }
8884
8885 *p1 = p2 - (p1 + 1);
8886 p1 = p2;
8887
8888 if (!*p2)
8889 break;
8890
8891 *(p2++) = '\0';
8892 }
8893 return 0;
8894#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008895 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008896 return ERR_ALERT | ERR_FATAL;
8897#endif
8898}
8899
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008900static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8901{
8902 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8903}
8904
Willy Tarreauab861d32013-04-02 02:30:41 +02008905/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008906static 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 +02008907{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008908#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008909 char *p1, *p2;
8910
8911 if (!*args[cur_arg + 1]) {
8912 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
8913 return ERR_ALERT | ERR_FATAL;
8914 }
8915
8916 free(conf->alpn_str);
8917
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008918 /* the ALPN string is built as a suite of (<len> <name>)*,
8919 * so we reuse each comma to store the next <len> and need
8920 * one more for the end of the string.
8921 */
Willy Tarreauab861d32013-04-02 02:30:41 +02008922 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008923 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02008924 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
8925
8926 /* replace commas with the name length */
8927 p1 = conf->alpn_str;
8928 p2 = p1 + 1;
8929 while (1) {
8930 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
8931 if (!p2)
8932 p2 = p1 + 1 + strlen(p1 + 1);
8933
8934 if (p2 - (p1 + 1) > 255) {
8935 *p2 = '\0';
8936 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8937 return ERR_ALERT | ERR_FATAL;
8938 }
8939
8940 *p1 = p2 - (p1 + 1);
8941 p1 = p2;
8942
8943 if (!*p2)
8944 break;
8945
8946 *(p2++) = '\0';
8947 }
8948 return 0;
8949#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008950 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02008951 return ERR_ALERT | ERR_FATAL;
8952#endif
8953}
8954
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008955static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8956{
8957 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8958}
8959
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008960/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008961static 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 +02008962{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01008963 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02008964 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02008965
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008966 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
8967 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008968#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008969 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
8970 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
8971#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008972 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008973 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
8974 if (!conf->ssl_conf.ssl_methods.min)
8975 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
8976 if (!conf->ssl_conf.ssl_methods.max)
8977 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02008978
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008979 return 0;
8980}
8981
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008982/* parse the "prefer-client-ciphers" bind keyword */
8983static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8984{
8985 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
8986 return 0;
8987}
8988
Christopher Faulet31af49d2015-06-09 17:29:50 +02008989/* parse the "generate-certificates" bind keyword */
8990static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8991{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01008992#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02008993 conf->generate_certs = 1;
8994#else
8995 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
8996 err && *err ? *err : "");
8997#endif
8998 return 0;
8999}
9000
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009001/* parse the "strict-sni" bind keyword */
9002static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9003{
9004 conf->strict_sni = 1;
9005 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009006}
9007
9008/* parse the "tls-ticket-keys" bind keyword */
9009static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9010{
9011#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009012 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009013 int i = 0;
9014 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009015 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009016
9017 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009018 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009019 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009020 }
9021
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009022 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009023 if (keys_ref) {
9024 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009025 conf->keys_ref = keys_ref;
9026 return 0;
9027 }
9028
Christopher Faulete566f3d2019-10-21 09:55:49 +02009029 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009030 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009031 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009032 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009033 }
9034
Emeric Brun9e754772019-01-10 17:51:55 +01009035 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009036 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009037 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009038 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009039 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009040
9041 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009042 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009043 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009044 }
9045
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009046 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009047 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009048 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009049 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009050 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009051
Emeric Brun9e754772019-01-10 17:51:55 +01009052 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009053 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9054 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009055 int dec_size;
9056
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009057 /* Strip newline characters from the end */
9058 if(thisline[len - 1] == '\n')
9059 thisline[--len] = 0;
9060
9061 if(thisline[len - 1] == '\r')
9062 thisline[--len] = 0;
9063
Emeric Brun9e754772019-01-10 17:51:55 +01009064 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9065 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009066 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009067 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009068 }
Emeric Brun9e754772019-01-10 17:51:55 +01009069 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9070 keys_ref->key_size_bits = 128;
9071 }
9072 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9073 keys_ref->key_size_bits = 256;
9074 }
9075 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9076 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9077 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009078 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009079 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009080 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009081 i++;
9082 }
9083
9084 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009085 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 +02009086 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009087 }
9088
9089 fclose(f);
9090
9091 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009092 i -= 2;
9093 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009094 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009095 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009096 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009097 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009098
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009099 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9100
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009101 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009102
9103 fail:
9104 if (f)
9105 fclose(f);
9106 if (keys_ref) {
9107 free(keys_ref->filename);
9108 free(keys_ref->tlskeys);
9109 free(keys_ref);
9110 }
9111 return ERR_ALERT | ERR_FATAL;
9112
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009113#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009114 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009115 return ERR_ALERT | ERR_FATAL;
9116#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009117}
9118
Emeric Brund94b3fe2012-09-20 18:23:56 +02009119/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009120static 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 +02009121{
9122 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009123 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009124 return ERR_ALERT | ERR_FATAL;
9125 }
9126
9127 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009128 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009129 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009130 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009131 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009132 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009133 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009134 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9135 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009136 return ERR_ALERT | ERR_FATAL;
9137 }
9138
9139 return 0;
9140}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009141static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9142{
9143 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9144}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009145
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009146/* parse the "no-ca-names" bind keyword */
9147static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9148{
9149 conf->no_ca_names = 1;
9150 return 0;
9151}
9152static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9153{
9154 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9155}
9156
Willy Tarreau92faadf2012-10-10 23:04:25 +02009157/************** "server" keywords ****************/
9158
Olivier Houchardc7566002018-11-20 23:33:50 +01009159/* parse the "npn" bind keyword */
9160static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9161{
9162#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9163 char *p1, *p2;
9164
9165 if (!*args[*cur_arg + 1]) {
9166 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9167 return ERR_ALERT | ERR_FATAL;
9168 }
9169
9170 free(newsrv->ssl_ctx.npn_str);
9171
9172 /* the NPN string is built as a suite of (<len> <name>)*,
9173 * so we reuse each comma to store the next <len> and need
9174 * one more for the end of the string.
9175 */
9176 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9177 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9178 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9179 newsrv->ssl_ctx.npn_len);
9180
9181 /* replace commas with the name length */
9182 p1 = newsrv->ssl_ctx.npn_str;
9183 p2 = p1 + 1;
9184 while (1) {
9185 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9186 newsrv->ssl_ctx.npn_len - (p1 + 1));
9187 if (!p2)
9188 p2 = p1 + 1 + strlen(p1 + 1);
9189
9190 if (p2 - (p1 + 1) > 255) {
9191 *p2 = '\0';
9192 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9193 return ERR_ALERT | ERR_FATAL;
9194 }
9195
9196 *p1 = p2 - (p1 + 1);
9197 p1 = p2;
9198
9199 if (!*p2)
9200 break;
9201
9202 *(p2++) = '\0';
9203 }
9204 return 0;
9205#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009206 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009207 return ERR_ALERT | ERR_FATAL;
9208#endif
9209}
9210
Olivier Houchard92150142018-12-21 19:47:01 +01009211/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009212static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9213{
9214#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9215 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009216 char **alpn_str;
9217 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009218
Olivier Houchard92150142018-12-21 19:47:01 +01009219 if (*args[*cur_arg] == 'c') {
9220 alpn_str = &newsrv->check.alpn_str;
9221 alpn_len = &newsrv->check.alpn_len;
9222 } else {
9223 alpn_str = &newsrv->ssl_ctx.alpn_str;
9224 alpn_len = &newsrv->ssl_ctx.alpn_len;
9225
9226 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009227 if (!*args[*cur_arg + 1]) {
9228 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9229 return ERR_ALERT | ERR_FATAL;
9230 }
9231
Olivier Houchard92150142018-12-21 19:47:01 +01009232 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009233
9234 /* the ALPN string is built as a suite of (<len> <name>)*,
9235 * so we reuse each comma to store the next <len> and need
9236 * one more for the end of the string.
9237 */
Olivier Houchard92150142018-12-21 19:47:01 +01009238 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9239 *alpn_str = calloc(1, *alpn_len + 1);
9240 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009241
9242 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009243 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009244 p2 = p1 + 1;
9245 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009246 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009247 if (!p2)
9248 p2 = p1 + 1 + strlen(p1 + 1);
9249
9250 if (p2 - (p1 + 1) > 255) {
9251 *p2 = '\0';
9252 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9253 return ERR_ALERT | ERR_FATAL;
9254 }
9255
9256 *p1 = p2 - (p1 + 1);
9257 p1 = p2;
9258
9259 if (!*p2)
9260 break;
9261
9262 *(p2++) = '\0';
9263 }
9264 return 0;
9265#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009266 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009267 return ERR_ALERT | ERR_FATAL;
9268#endif
9269}
9270
Emeric Brunef42d922012-10-11 16:11:36 +02009271/* parse the "ca-file" server keyword */
9272static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9273{
9274 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009275 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009276 return ERR_ALERT | ERR_FATAL;
9277 }
9278
Willy Tarreauef934602016-12-22 23:12:01 +01009279 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9280 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009281 else
9282 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9283
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009284 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9285 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9286 return ERR_ALERT | ERR_FATAL;
9287 }
Emeric Brunef42d922012-10-11 16:11:36 +02009288 return 0;
9289}
9290
Olivier Houchard9130a962017-10-17 17:33:43 +02009291/* parse the "check-sni" server keyword */
9292static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9293{
9294 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009295 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009296 return ERR_ALERT | ERR_FATAL;
9297 }
9298
9299 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9300 if (!newsrv->check.sni) {
9301 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9302 return ERR_ALERT | ERR_FATAL;
9303 }
9304 return 0;
9305
9306}
9307
Willy Tarreau92faadf2012-10-10 23:04:25 +02009308/* parse the "check-ssl" server keyword */
9309static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9310{
9311 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009312 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9313 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009314#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009315 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9316 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9317#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009318 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009319 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9320 if (!newsrv->ssl_ctx.methods.min)
9321 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9322 if (!newsrv->ssl_ctx.methods.max)
9323 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9324
Willy Tarreau92faadf2012-10-10 23:04:25 +02009325 return 0;
9326}
9327
9328/* parse the "ciphers" server keyword */
9329static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9330{
9331 if (!*args[*cur_arg + 1]) {
9332 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9333 return ERR_ALERT | ERR_FATAL;
9334 }
9335
9336 free(newsrv->ssl_ctx.ciphers);
9337 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9338 return 0;
9339}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009340
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009341#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009342/* parse the "ciphersuites" server keyword */
9343static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9344{
9345 if (!*args[*cur_arg + 1]) {
9346 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9347 return ERR_ALERT | ERR_FATAL;
9348 }
9349
9350 free(newsrv->ssl_ctx.ciphersuites);
9351 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9352 return 0;
9353}
9354#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009355
Emeric Brunef42d922012-10-11 16:11:36 +02009356/* parse the "crl-file" server keyword */
9357static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9358{
9359#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009360 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009361 return ERR_ALERT | ERR_FATAL;
9362#else
9363 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009364 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009365 return ERR_ALERT | ERR_FATAL;
9366 }
9367
Willy Tarreauef934602016-12-22 23:12:01 +01009368 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9369 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009370 else
9371 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9372
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009373 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9374 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9375 return ERR_ALERT | ERR_FATAL;
9376 }
Emeric Brunef42d922012-10-11 16:11:36 +02009377 return 0;
9378#endif
9379}
9380
Emeric Bruna7aa3092012-10-26 12:58:00 +02009381/* parse the "crt" server keyword */
9382static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9383{
9384 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009385 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009386 return ERR_ALERT | ERR_FATAL;
9387 }
9388
Willy Tarreauef934602016-12-22 23:12:01 +01009389 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009390 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009391 else
9392 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9393
9394 return 0;
9395}
Emeric Brunef42d922012-10-11 16:11:36 +02009396
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009397/* parse the "no-check-ssl" server keyword */
9398static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9399{
9400 newsrv->check.use_ssl = 0;
9401 free(newsrv->ssl_ctx.ciphers);
9402 newsrv->ssl_ctx.ciphers = NULL;
9403 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9404 return 0;
9405}
9406
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009407/* parse the "no-send-proxy-v2-ssl" server keyword */
9408static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9409{
9410 newsrv->pp_opts &= ~SRV_PP_V2;
9411 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9412 return 0;
9413}
9414
9415/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9416static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9417{
9418 newsrv->pp_opts &= ~SRV_PP_V2;
9419 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9420 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9421 return 0;
9422}
9423
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009424/* parse the "no-ssl" server keyword */
9425static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9426{
9427 newsrv->use_ssl = 0;
9428 free(newsrv->ssl_ctx.ciphers);
9429 newsrv->ssl_ctx.ciphers = NULL;
9430 return 0;
9431}
9432
Olivier Houchard522eea72017-11-03 16:27:47 +01009433/* parse the "allow-0rtt" server keyword */
9434static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9435{
9436 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9437 return 0;
9438}
9439
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009440/* parse the "no-ssl-reuse" server keyword */
9441static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9442{
9443 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9444 return 0;
9445}
9446
Emeric Brunf9c5c472012-10-11 15:28:34 +02009447/* parse the "no-tls-tickets" server keyword */
9448static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9449{
9450 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9451 return 0;
9452}
David Safb76832014-05-08 23:42:08 -04009453/* parse the "send-proxy-v2-ssl" server keyword */
9454static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9455{
9456 newsrv->pp_opts |= SRV_PP_V2;
9457 newsrv->pp_opts |= SRV_PP_V2_SSL;
9458 return 0;
9459}
9460
9461/* parse the "send-proxy-v2-ssl-cn" server keyword */
9462static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9463{
9464 newsrv->pp_opts |= SRV_PP_V2;
9465 newsrv->pp_opts |= SRV_PP_V2_SSL;
9466 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9467 return 0;
9468}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009469
Willy Tarreau732eac42015-07-09 11:40:25 +02009470/* parse the "sni" server keyword */
9471static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9472{
9473#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9474 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9475 return ERR_ALERT | ERR_FATAL;
9476#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009477 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009478
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009479 arg = args[*cur_arg + 1];
9480 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009481 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9482 return ERR_ALERT | ERR_FATAL;
9483 }
9484
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009485 free(newsrv->sni_expr);
9486 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02009487
Willy Tarreau732eac42015-07-09 11:40:25 +02009488 return 0;
9489#endif
9490}
9491
Willy Tarreau92faadf2012-10-10 23:04:25 +02009492/* parse the "ssl" server keyword */
9493static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9494{
9495 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009496 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9497 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009498#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009499 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9500 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9501#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009502 return 0;
9503}
9504
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009505/* parse the "ssl-reuse" server keyword */
9506static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9507{
9508 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
9509 return 0;
9510}
9511
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009512/* parse the "tls-tickets" server keyword */
9513static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9514{
9515 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
9516 return 0;
9517}
9518
Emeric Brunef42d922012-10-11 16:11:36 +02009519/* parse the "verify" server keyword */
9520static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9521{
9522 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009523 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009524 return ERR_ALERT | ERR_FATAL;
9525 }
9526
9527 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009528 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02009529 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009530 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02009531 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009532 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
9533 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009534 return ERR_ALERT | ERR_FATAL;
9535 }
9536
Evan Broderbe554312013-06-27 00:05:25 -07009537 return 0;
9538}
9539
9540/* parse the "verifyhost" server keyword */
9541static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9542{
9543 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009544 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -07009545 return ERR_ALERT | ERR_FATAL;
9546 }
9547
Frédéric Lécaille273f3212017-03-13 15:52:01 +01009548 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07009549 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
9550
Emeric Brunef42d922012-10-11 16:11:36 +02009551 return 0;
9552}
9553
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009554/* parse the "ssl-default-bind-options" keyword in global section */
9555static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
9556 struct proxy *defpx, const char *file, int line,
9557 char **err) {
9558 int i = 1;
9559
9560 if (*(args[i]) == 0) {
9561 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9562 return -1;
9563 }
9564 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009565 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009566 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009567 else if (!strcmp(args[i], "prefer-client-ciphers"))
9568 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009569 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9570 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
9571 i++;
9572 else {
9573 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9574 return -1;
9575 }
9576 }
9577 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009578 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9579 return -1;
9580 }
9581 i++;
9582 }
9583 return 0;
9584}
9585
9586/* parse the "ssl-default-server-options" keyword in global section */
9587static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
9588 struct proxy *defpx, const char *file, int line,
9589 char **err) {
9590 int i = 1;
9591
9592 if (*(args[i]) == 0) {
9593 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9594 return -1;
9595 }
9596 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009597 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009598 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009599 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9600 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
9601 i++;
9602 else {
9603 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9604 return -1;
9605 }
9606 }
9607 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009608 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9609 return -1;
9610 }
9611 i++;
9612 }
9613 return 0;
9614}
9615
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009616/* parse the "ca-base" / "crt-base" keywords in global section.
9617 * Returns <0 on alert, >0 on warning, 0 on success.
9618 */
9619static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
9620 struct proxy *defpx, const char *file, int line,
9621 char **err)
9622{
9623 char **target;
9624
Willy Tarreauef934602016-12-22 23:12:01 +01009625 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009626
9627 if (too_many_args(1, args, err, NULL))
9628 return -1;
9629
9630 if (*target) {
9631 memprintf(err, "'%s' already specified.", args[0]);
9632 return -1;
9633 }
9634
9635 if (*(args[1]) == 0) {
9636 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
9637 return -1;
9638 }
9639 *target = strdup(args[1]);
9640 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009641}
9642
9643/* parse the "ssl-mode-async" keyword in global section.
9644 * Returns <0 on alert, >0 on warning, 0 on success.
9645 */
9646static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
9647 struct proxy *defpx, const char *file, int line,
9648 char **err)
9649{
Willy Tarreau5db847a2019-05-09 14:13:35 +02009650#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009651 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01009652 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009653 return 0;
9654#else
9655 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
9656 return -1;
9657#endif
9658}
9659
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009660#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009661static int ssl_check_async_engine_count(void) {
9662 int err_code = 0;
9663
Emeric Brun3854e012017-05-17 20:42:48 +02009664 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009665 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009666 err_code = ERR_ABORT;
9667 }
9668 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009669}
9670
Grant Zhang872f9c22017-01-21 01:10:18 +00009671/* parse the "ssl-engine" keyword in global section.
9672 * Returns <0 on alert, >0 on warning, 0 on success.
9673 */
9674static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9675 struct proxy *defpx, const char *file, int line,
9676 char **err)
9677{
9678 char *algo;
9679 int ret = -1;
9680
9681 if (*(args[1]) == 0) {
9682 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9683 return ret;
9684 }
9685
9686 if (*(args[2]) == 0) {
9687 /* if no list of algorithms is given, it defaults to ALL */
9688 algo = strdup("ALL");
9689 goto add_engine;
9690 }
9691
9692 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
9693 if (strcmp(args[2], "algo") != 0) {
9694 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
9695 return ret;
9696 }
9697
9698 if (*(args[3]) == 0) {
9699 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
9700 return ret;
9701 }
9702 algo = strdup(args[3]);
9703
9704add_engine:
9705 if (ssl_init_single_engine(args[1], algo)==0) {
9706 openssl_engines_initialized++;
9707 ret = 0;
9708 }
9709 free(algo);
9710 return ret;
9711}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009712#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00009713
Willy Tarreauf22e9682016-12-21 23:23:19 +01009714/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
9715 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9716 */
9717static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
9718 struct proxy *defpx, const char *file, int line,
9719 char **err)
9720{
9721 char **target;
9722
Willy Tarreauef934602016-12-22 23:12:01 +01009723 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01009724
9725 if (too_many_args(1, args, err, NULL))
9726 return -1;
9727
9728 if (*(args[1]) == 0) {
9729 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9730 return -1;
9731 }
9732
9733 free(*target);
9734 *target = strdup(args[1]);
9735 return 0;
9736}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009737
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009738#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009739/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9740 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9741 */
9742static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9743 struct proxy *defpx, const char *file, int line,
9744 char **err)
9745{
9746 char **target;
9747
9748 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9749
9750 if (too_many_args(1, args, err, NULL))
9751 return -1;
9752
9753 if (*(args[1]) == 0) {
9754 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9755 return -1;
9756 }
9757
9758 free(*target);
9759 *target = strdup(args[1]);
9760 return 0;
9761}
9762#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009763
Willy Tarreau9ceda382016-12-21 23:13:03 +01009764/* parse various global tune.ssl settings consisting in positive integers.
9765 * Returns <0 on alert, >0 on warning, 0 on success.
9766 */
9767static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9768 struct proxy *defpx, const char *file, int line,
9769 char **err)
9770{
9771 int *target;
9772
9773 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9774 target = &global.tune.sslcachesize;
9775 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009776 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009777 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009778 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009779 else if (strcmp(args[0], "maxsslconn") == 0)
9780 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009781 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9782 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009783 else {
9784 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9785 return -1;
9786 }
9787
9788 if (too_many_args(1, args, err, NULL))
9789 return -1;
9790
9791 if (*(args[1]) == 0) {
9792 memprintf(err, "'%s' expects an integer argument.", args[0]);
9793 return -1;
9794 }
9795
9796 *target = atoi(args[1]);
9797 if (*target < 0) {
9798 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9799 return -1;
9800 }
9801 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009802}
9803
9804static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9805 struct proxy *defpx, const char *file, int line,
9806 char **err)
9807{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009808 int ret;
9809
9810 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9811 if (ret != 0)
9812 return ret;
9813
Willy Tarreaubafbe012017-11-24 17:34:44 +01009814 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009815 memprintf(err, "'%s' is already configured.", args[0]);
9816 return -1;
9817 }
9818
Willy Tarreaubafbe012017-11-24 17:34:44 +01009819 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9820 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009821 memprintf(err, "Out of memory error.");
9822 return -1;
9823 }
9824 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009825}
9826
9827/* parse "ssl.force-private-cache".
9828 * Returns <0 on alert, >0 on warning, 0 on success.
9829 */
9830static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9831 struct proxy *defpx, const char *file, int line,
9832 char **err)
9833{
9834 if (too_many_args(0, args, err, NULL))
9835 return -1;
9836
Willy Tarreauef934602016-12-22 23:12:01 +01009837 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009838 return 0;
9839}
9840
9841/* parse "ssl.lifetime".
9842 * Returns <0 on alert, >0 on warning, 0 on success.
9843 */
9844static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9845 struct proxy *defpx, const char *file, int line,
9846 char **err)
9847{
9848 const char *res;
9849
9850 if (too_many_args(1, args, err, NULL))
9851 return -1;
9852
9853 if (*(args[1]) == 0) {
9854 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9855 return -1;
9856 }
9857
Willy Tarreauef934602016-12-22 23:12:01 +01009858 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009859 if (res == PARSE_TIME_OVER) {
9860 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9861 args[1], args[0]);
9862 return -1;
9863 }
9864 else if (res == PARSE_TIME_UNDER) {
9865 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9866 args[1], args[0]);
9867 return -1;
9868 }
9869 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009870 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9871 return -1;
9872 }
9873 return 0;
9874}
9875
9876#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009877/* parse "ssl-dh-param-file".
9878 * Returns <0 on alert, >0 on warning, 0 on success.
9879 */
9880static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9881 struct proxy *defpx, const char *file, int line,
9882 char **err)
9883{
9884 if (too_many_args(1, args, err, NULL))
9885 return -1;
9886
9887 if (*(args[1]) == 0) {
9888 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9889 return -1;
9890 }
9891
9892 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9893 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9894 return -1;
9895 }
9896 return 0;
9897}
9898
Willy Tarreau9ceda382016-12-21 23:13:03 +01009899/* parse "ssl.default-dh-param".
9900 * Returns <0 on alert, >0 on warning, 0 on success.
9901 */
9902static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9903 struct proxy *defpx, const char *file, int line,
9904 char **err)
9905{
9906 if (too_many_args(1, args, err, NULL))
9907 return -1;
9908
9909 if (*(args[1]) == 0) {
9910 memprintf(err, "'%s' expects an integer argument.", args[0]);
9911 return -1;
9912 }
9913
Willy Tarreauef934602016-12-22 23:12:01 +01009914 global_ssl.default_dh_param = atoi(args[1]);
9915 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009916 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9917 return -1;
9918 }
9919 return 0;
9920}
9921#endif
9922
9923
William Lallemand32af2032016-10-29 18:09:35 +02009924/* This function is used with TLS ticket keys management. It permits to browse
9925 * each reference. The variable <getnext> must contain the current node,
9926 * <end> point to the root node.
9927 */
9928#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9929static inline
9930struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
9931{
9932 struct tls_keys_ref *ref = getnext;
9933
9934 while (1) {
9935
9936 /* Get next list entry. */
9937 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
9938
9939 /* If the entry is the last of the list, return NULL. */
9940 if (&ref->list == end)
9941 return NULL;
9942
9943 return ref;
9944 }
9945}
9946
9947static inline
9948struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
9949{
9950 int id;
9951 char *error;
9952
9953 /* If the reference starts by a '#', this is numeric id. */
9954 if (reference[0] == '#') {
9955 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
9956 id = strtol(reference + 1, &error, 10);
9957 if (*error != '\0')
9958 return NULL;
9959
9960 /* Perform the unique id lookup. */
9961 return tlskeys_ref_lookupid(id);
9962 }
9963
9964 /* Perform the string lookup. */
9965 return tlskeys_ref_lookup(reference);
9966}
9967#endif
9968
9969
9970#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9971
9972static int cli_io_handler_tlskeys_files(struct appctx *appctx);
9973
9974static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
9975 return cli_io_handler_tlskeys_files(appctx);
9976}
9977
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009978/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
9979 * (next index to be dumped), and cli.p0 (next key reference).
9980 */
William Lallemand32af2032016-10-29 18:09:35 +02009981static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
9982
9983 struct stream_interface *si = appctx->owner;
9984
9985 switch (appctx->st2) {
9986 case STAT_ST_INIT:
9987 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08009988 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +02009989 * later and restart at the state "STAT_ST_INIT".
9990 */
9991 chunk_reset(&trash);
9992
9993 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
9994 chunk_appendf(&trash, "# id secret\n");
9995 else
9996 chunk_appendf(&trash, "# id (file)\n");
9997
Willy Tarreau06d80a92017-10-19 14:32:15 +02009998 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01009999 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010000 return 0;
10001 }
10002
William Lallemand32af2032016-10-29 18:09:35 +020010003 /* Now, we start the browsing of the references lists.
10004 * Note that the following call to LIST_ELEM return bad pointer. The only
10005 * available field of this pointer is <list>. It is used with the function
10006 * tlskeys_list_get_next() for retruning the first available entry
10007 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010008 if (appctx->ctx.cli.p0 == NULL) {
10009 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10010 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010011 }
10012
10013 appctx->st2 = STAT_ST_LIST;
10014 /* fall through */
10015
10016 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010017 while (appctx->ctx.cli.p0) {
10018 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010019
10020 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010021 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010022 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010023
10024 if (appctx->ctx.cli.i1 == 0)
10025 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10026
William Lallemand32af2032016-10-29 18:09:35 +020010027 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010028 int head;
10029
10030 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10031 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010032 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010033 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010034
10035 chunk_reset(t2);
10036 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010037 if (ref->key_size_bits == 128) {
10038 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10039 sizeof(struct tls_sess_key_128),
10040 t2->area, t2->size);
10041 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10042 t2->area);
10043 }
10044 else if (ref->key_size_bits == 256) {
10045 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10046 sizeof(struct tls_sess_key_256),
10047 t2->area, t2->size);
10048 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10049 t2->area);
10050 }
10051 else {
10052 /* This case should never happen */
10053 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10054 }
William Lallemand32af2032016-10-29 18:09:35 +020010055
Willy Tarreau06d80a92017-10-19 14:32:15 +020010056 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010057 /* let's try again later from this stream. We add ourselves into
10058 * this stream's users so that it can remove us upon termination.
10059 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010060 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010061 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010062 return 0;
10063 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010064 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010065 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010066 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010067 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010068 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010069 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010070 /* let's try again later from this stream. We add ourselves into
10071 * this stream's users so that it can remove us upon termination.
10072 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010073 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010074 return 0;
10075 }
10076
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010077 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010078 break;
10079
10080 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010081 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010082 }
10083
10084 appctx->st2 = STAT_ST_FIN;
10085 /* fall through */
10086
10087 default:
10088 appctx->st2 = STAT_ST_FIN;
10089 return 1;
10090 }
10091 return 0;
10092}
10093
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010094/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010095static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010096{
William Lallemand32af2032016-10-29 18:09:35 +020010097 /* no parameter, shows only file list */
10098 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010099 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010100 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010101 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010102 }
10103
10104 if (args[2][0] == '*') {
10105 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010106 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010107 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010108 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010109 if (!appctx->ctx.cli.p0)
10110 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010111 }
William Lallemand32af2032016-10-29 18:09:35 +020010112 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010113 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010114}
10115
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010116static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010117{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010118 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010119 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010120
William Lallemand32af2032016-10-29 18:09:35 +020010121 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010122 if (!*args[3] || !*args[4])
10123 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 +020010124
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010125 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010126 if (!ref)
10127 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010128
Willy Tarreau1c913e42018-08-22 05:26:57 +020010129 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010130 if (ret < 0)
10131 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010132
Willy Tarreau1c913e42018-08-22 05:26:57 +020010133 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010134 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10135 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010136
Willy Tarreau9d008692019-08-09 11:21:01 +020010137 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010138}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010139#endif
William Lallemand32af2032016-10-29 18:09:35 +020010140
William Lallemand44b35322019-10-17 16:28:40 +020010141
10142/* Type of SSL payloads that can be updated over the CLI */
10143
10144enum {
10145 CERT_TYPE_PEM = 0,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010146#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010147 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010148#endif
William Lallemand44b35322019-10-17 16:28:40 +020010149 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010150#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010151 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010152#endif
William Lallemand44b35322019-10-17 16:28:40 +020010153 CERT_TYPE_MAX,
10154};
10155
10156struct {
10157 const char *ext;
10158 int type;
10159 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
10160 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020010161} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020010162 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand541a5342019-10-23 14:11:54 +020010163#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010164 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010165#endif
10166#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010167 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010168#endif
William Lallemand44b35322019-10-17 16:28:40 +020010169 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020010170 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020010171};
10172
William Lallemand430413e2019-10-28 14:30:47 +010010173/* states of the CLI IO handler for 'set ssl cert' */
10174enum {
10175 SETCERT_ST_INIT = 0,
10176 SETCERT_ST_GEN,
10177 SETCERT_ST_INSERT,
10178 SETCERT_ST_FIN,
10179};
William Lallemand8f840d72019-10-23 10:53:05 +020010180
William Lallemandd4f946c2019-12-05 10:26:40 +010010181/* release function of the `show ssl cert' command */
10182static void cli_release_show_cert(struct appctx *appctx)
10183{
10184 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10185}
10186
10187/* IO handler of "show ssl cert <filename>" */
10188static int cli_io_handler_show_cert(struct appctx *appctx)
10189{
10190 struct buffer *trash = alloc_trash_chunk();
10191 struct ebmb_node *node;
10192 struct stream_interface *si = appctx->owner;
10193 struct ckch_store *ckchs;
10194 int n;
10195
10196 if (trash == NULL)
10197 return 1;
10198
10199 if (!appctx->ctx.ssl.old_ckchs) {
10200 if (ckchs_transaction.old_ckchs) {
10201 ckchs = ckchs_transaction.old_ckchs;
10202 chunk_appendf(trash, "# transaction\n");
10203 if (!ckchs->multi) {
10204 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010205#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010206 } else {
10207 chunk_appendf(trash, "*%s:", ckchs->path);
10208 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10209 if (ckchs->ckch[n].cert)
10210 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10211 }
10212 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010213#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010214 }
10215 }
10216 }
10217
10218 if (!appctx->ctx.cli.p0) {
10219 chunk_appendf(trash, "# filename\n");
10220 node = ebmb_first(&ckchs_tree);
10221 } else {
10222 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
10223 }
10224 while (node) {
10225 ckchs = ebmb_entry(node, struct ckch_store, node);
10226 if (!ckchs->multi) {
10227 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010228#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010229 } else {
10230 chunk_appendf(trash, "%s:", ckchs->path);
10231 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10232 if (ckchs->ckch[n].cert)
10233 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10234 }
10235 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010236#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010237 }
10238
10239 node = ebmb_next(node);
10240 if (ci_putchk(si_ic(si), trash) == -1) {
10241 si_rx_room_blk(si);
10242 goto yield;
10243 }
10244 }
10245
10246 appctx->ctx.cli.p0 = NULL;
10247 free_trash_chunk(trash);
10248 return 1;
10249yield:
10250
10251 free_trash_chunk(trash);
10252 appctx->ctx.cli.p0 = ckchs;
10253 return 0; /* should come back */
10254}
10255
10256/* IO handler of the details "show ssl cert <filename>" */
10257static int cli_io_handler_show_cert_detail(struct appctx *appctx)
10258{
10259 struct stream_interface *si = appctx->owner;
10260 struct ckch_store *ckchs = appctx->ctx.cli.p0;
10261 struct buffer *out = alloc_trash_chunk();
10262 struct buffer *tmp = alloc_trash_chunk();
10263 X509_NAME *name = NULL;
10264 int write = -1;
10265 BIO *bio = NULL;
10266
10267 if (!tmp || !out)
10268 goto end;
10269
10270 if (!ckchs->multi) {
10271 chunk_appendf(out, "Filename: ");
10272 if (ckchs == ckchs_transaction.new_ckchs)
10273 chunk_appendf(out, "*");
10274 chunk_appendf(out, "%s\n", ckchs->path);
10275 chunk_appendf(out, "Serial: ");
10276 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
10277 goto end;
10278 dump_binary(out, tmp->area, tmp->data);
10279 chunk_appendf(out, "\n");
10280
10281 chunk_appendf(out, "notBefore: ");
10282 chunk_reset(tmp);
10283 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10284 goto end;
10285 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
10286 goto end;
10287 write = BIO_read(bio, tmp->area, tmp->size-1);
10288 tmp->area[write] = '\0';
10289 BIO_free(bio);
10290 chunk_appendf(out, "%s\n", tmp->area);
10291
10292 chunk_appendf(out, "notAfter: ");
10293 chunk_reset(tmp);
10294 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10295 goto end;
10296 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
10297 goto end;
10298 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
10299 goto end;
10300 tmp->area[write] = '\0';
10301 BIO_free(bio);
10302 chunk_appendf(out, "%s\n", tmp->area);
10303
10304
10305 chunk_appendf(out, "Issuer: ");
10306 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
10307 goto end;
10308 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10309 goto end;
10310 *(tmp->area + tmp->data) = '\0';
10311 chunk_appendf(out, "%s\n", tmp->area);
10312
10313 chunk_appendf(out, "Subject: ");
10314 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
10315 goto end;
10316 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10317 goto end;
10318 *(tmp->area + tmp->data) = '\0';
10319 chunk_appendf(out, "%s\n", tmp->area);
10320
10321
10322#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10323 chunk_appendf(out, "Subject Alternative Name: ");
10324 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
10325 goto end;
10326 *(out->area + out->data) = '\0';
10327 chunk_appendf(out, "\n");
10328#endif
10329 chunk_reset(tmp);
10330 chunk_appendf(out, "Algorithm: ");
10331 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
10332 goto end;
10333 chunk_appendf(out, "%s\n", tmp->area);
10334
10335 chunk_reset(tmp);
10336 chunk_appendf(out, "SHA1 FingerPrint: ");
10337 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area,
10338 (unsigned int *)&tmp->data) == 0)
10339 goto end;
10340 dump_binary(out, tmp->area, tmp->data);
10341 chunk_appendf(out, "\n");
10342 }
10343
10344 if (ci_putchk(si_ic(si), out) == -1) {
10345 si_rx_room_blk(si);
10346 goto yield;
10347 }
10348
10349end:
10350 free_trash_chunk(tmp);
10351 free_trash_chunk(out);
10352 return 1;
10353yield:
10354 free_trash_chunk(tmp);
10355 free_trash_chunk(out);
10356 return 0; /* should come back */
10357}
10358
10359/* parsing function for 'show ssl cert [certfile]' */
10360static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
10361{
10362 struct ckch_store *ckchs;
10363
10364 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
10365 return cli_err(appctx, "Can't allocate memory!\n");
10366
10367 /* The operations on the CKCH architecture are locked so we can
10368 * manipulate ckch_store and ckch_inst */
10369 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10370 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
10371
10372 /* check if there is a certificate to lookup */
10373 if (*args[3]) {
10374 if (*args[3] == '*') {
10375 if (!ckchs_transaction.new_ckchs)
10376 goto error;
10377
10378 ckchs = ckchs_transaction.new_ckchs;
10379
10380 if (strcmp(args[3] + 1, ckchs->path))
10381 goto error;
10382
10383 } else {
10384 if ((ckchs = ckchs_lookup(args[3])) == NULL)
10385 goto error;
10386
10387 }
10388
10389 if (ckchs->multi)
10390 goto error;
10391
10392 appctx->ctx.cli.p0 = ckchs;
10393 /* use the IO handler that shows details */
10394 appctx->io_handler = cli_io_handler_show_cert_detail;
10395 }
10396
10397 return 0;
10398
10399error:
10400 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10401 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
10402}
10403
William Lallemand430413e2019-10-28 14:30:47 +010010404/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010405static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010406{
10407 struct ckch_store *new_ckchs;
10408 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010409
William Lallemand430413e2019-10-28 14:30:47 +010010410 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020010411
William Lallemand430413e2019-10-28 14:30:47 +010010412 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020010413 /* 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 +010010414 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010415
William Lallemandbeea2a42019-10-30 17:45:33 +010010416 if (!new_ckchs)
10417 return;
William Lallemand8f840d72019-10-23 10:53:05 +020010418
William Lallemandbeea2a42019-10-30 17:45:33 +010010419 /* if the allocation failed, we need to free everything from the temporary list */
10420 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10421 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010422
William Lallemandbeea2a42019-10-30 17:45:33 +010010423 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10424 if (sc0->order == 0) /* we only free if it's the first inserted */
10425 SSL_CTX_free(sc0->ctx);
10426 LIST_DEL(&sc0->by_ckch_inst);
10427 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020010428 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010429 LIST_DEL(&ckchi->by_ckchs);
10430 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020010431 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010432 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010433 }
10434}
10435
10436
10437/*
10438 * This function tries to create the new ckch_inst and their SNIs
10439 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010440static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010441{
10442 struct stream_interface *si = appctx->owner;
10443 int y = 0;
10444 char *err = NULL;
10445 int errcode = 0;
10446 struct ckch_store *old_ckchs, *new_ckchs = NULL;
10447 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010448 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010449 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010450
William Lallemand33cc76f2019-10-31 11:43:45 +010010451 if (trash == NULL)
10452 goto error;
10453
William Lallemand8f840d72019-10-23 10:53:05 +020010454 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
10455 goto error;
10456
William Lallemand430413e2019-10-28 14:30:47 +010010457 while (1) {
10458 switch (appctx->st2) {
10459 case SETCERT_ST_INIT:
10460 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010461 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010010462 if (ci_putchk(si_ic(si), trash) == -1) {
10463 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020010464 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010010465 }
10466 appctx->st2 = SETCERT_ST_GEN;
10467 /* fallthrough */
10468 case SETCERT_ST_GEN:
10469 /*
10470 * This state generates the ckch instances with their
10471 * sni_ctxs and SSL_CTX.
10472 *
William Lallemand430413e2019-10-28 14:30:47 +010010473 * Since the SSL_CTX generation can be CPU consumer, we
10474 * yield every 10 instances.
10475 */
William Lallemand8f840d72019-10-23 10:53:05 +020010476
William Lallemandbeea2a42019-10-30 17:45:33 +010010477 old_ckchs = appctx->ctx.ssl.old_ckchs;
10478 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010479
William Lallemandbeea2a42019-10-30 17:45:33 +010010480 if (!new_ckchs)
10481 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020010482
William Lallemandbeea2a42019-10-30 17:45:33 +010010483 /* get the next ckchi to regenerate */
10484 ckchi = appctx->ctx.ssl.next_ckchi;
10485 /* we didn't start yet, set it to the first elem */
10486 if (ckchi == NULL)
10487 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010488
William Lallemandbeea2a42019-10-30 17:45:33 +010010489 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
10490 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
10491 struct ckch_inst *new_inst;
William Lallemand8f840d72019-10-23 10:53:05 +020010492
William Lallemandbeea2a42019-10-30 17:45:33 +010010493 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
10494 if (y >= 10) {
10495 /* save the next ckchi to compute */
10496 appctx->ctx.ssl.next_ckchi = ckchi;
10497 goto yield;
10498 }
William Lallemand8f840d72019-10-23 10:53:05 +020010499
William Lallemandbeea2a42019-10-30 17:45:33 +010010500 if (new_ckchs->multi)
10501 errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
10502 else
10503 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 +020010504
William Lallemandbeea2a42019-10-30 17:45:33 +010010505 if (errcode & ERR_CODE)
10506 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020010507
William Lallemand21724f02019-11-04 17:56:13 +010010508 /* if the previous ckchi was used as the default */
10509 if (ckchi->is_default)
10510 new_inst->is_default = 1;
10511
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010512 /* we need to initialize the SSL_CTX generated */
10513 /* TODO: the prepare_ctx function need to be reworked to be safer there */
10514 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10515 if (!sc0->order) { /* we initiliazed only the first SSL_CTX because it's the same in the other sni_ctx's */
10516 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
10517 if (errcode & ERR_CODE)
10518 goto error;
10519 }
10520 }
10521
10522
William Lallemandbeea2a42019-10-30 17:45:33 +010010523 /* display one dot per new instance */
10524 chunk_appendf(trash, ".");
10525 /* link the new ckch_inst to the duplicate */
10526 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
10527 y++;
10528 }
William Lallemand430413e2019-10-28 14:30:47 +010010529 appctx->st2 = SETCERT_ST_INSERT;
10530 /* fallthrough */
10531 case SETCERT_ST_INSERT:
10532 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020010533
William Lallemandbeea2a42019-10-30 17:45:33 +010010534 old_ckchs = appctx->ctx.ssl.old_ckchs;
10535 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010536
William Lallemandbeea2a42019-10-30 17:45:33 +010010537 if (!new_ckchs)
10538 continue;
William Lallemand430413e2019-10-28 14:30:47 +010010539
William Lallemand21724f02019-11-04 17:56:13 +010010540 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010010541 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10542 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10543 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
10544 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10545 }
William Lallemand8f840d72019-10-23 10:53:05 +020010546
William Lallemandbeea2a42019-10-30 17:45:33 +010010547 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
10548 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemand430413e2019-10-28 14:30:47 +010010549
William Lallemandbeea2a42019-10-30 17:45:33 +010010550 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10551 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10552 ebmb_delete(&sc0->name);
10553 LIST_DEL(&sc0->by_ckch_inst);
10554 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010010555 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010556 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10557 LIST_DEL(&ckchi->by_ckchs);
10558 free(ckchi);
10559 }
William Lallemand8f840d72019-10-23 10:53:05 +020010560
William Lallemandbeea2a42019-10-30 17:45:33 +010010561 /* Replace the old ckchs by the new one */
10562 ebmb_delete(&old_ckchs->node);
10563 ckchs_free(old_ckchs);
10564 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010010565 appctx->st2 = SETCERT_ST_FIN;
10566 /* fallthrough */
10567 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010568 /* we achieved the transaction, we can set everything to NULL */
10569 free(ckchs_transaction.path);
10570 ckchs_transaction.path = NULL;
10571 ckchs_transaction.new_ckchs = NULL;
10572 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010010573 goto end;
10574 }
William Lallemand8f840d72019-10-23 10:53:05 +020010575 }
William Lallemand430413e2019-10-28 14:30:47 +010010576end:
William Lallemand8f840d72019-10-23 10:53:05 +020010577
William Lallemanded442432019-11-21 16:41:07 +010010578 chunk_appendf(trash, "\n");
10579 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010010580 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010010581 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010010582 if (ci_putchk(si_ic(si), trash) == -1)
10583 si_rx_room_blk(si);
10584 free_trash_chunk(trash);
10585 /* success: call the release function and don't come back */
10586 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010587yield:
10588 /* store the state */
10589 if (ci_putchk(si_ic(si), trash) == -1)
10590 si_rx_room_blk(si);
10591 free_trash_chunk(trash);
10592 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020010593 return 0; /* should come back */
10594
10595error:
10596 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010010597 if (trash) {
10598 chunk_appendf(trash, "\n%sFailed!\n", err);
10599 if (ci_putchk(si_ic(si), trash) == -1)
10600 si_rx_room_blk(si);
10601 free_trash_chunk(trash);
10602 }
William Lallemand430413e2019-10-28 14:30:47 +010010603 /* error: call the release function and don't come back */
10604 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010605}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010606
10607/*
10608 * Parsing function of 'commit ssl cert'
10609 */
10610static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
10611{
10612 char *err = NULL;
10613
William Lallemand230662a2019-12-03 13:32:54 +010010614 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10615 return 1;
10616
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010617 if (!*args[3])
10618 return cli_err(appctx, "'commit ssl cert expects a filename\n");
10619
10620 /* The operations on the CKCH architecture are locked so we can
10621 * manipulate ckch_store and ckch_inst */
10622 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10623 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
10624
10625 if (!ckchs_transaction.path) {
10626 memprintf(&err, "No ongoing transaction! !\n");
10627 goto error;
10628 }
10629
10630 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10631 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
10632 goto error;
10633 }
10634
10635 /* init the appctx structure */
10636 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010637 appctx->ctx.ssl.next_ckchi = NULL;
10638 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
10639 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
10640
10641 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
10642 return 0;
10643
10644error:
10645
10646 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10647 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
10648
10649 return cli_dynerr(appctx, err);
10650}
10651
10652
William Lallemand8f840d72019-10-23 10:53:05 +020010653/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010654 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020010655 */
William Lallemand150bfa82019-09-19 17:12:49 +020010656static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
10657{
William Lallemand0c3b7d92019-10-18 11:27:07 +020010658 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020010659 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020010660 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020010661 int i;
William Lallemand849eed62019-10-17 16:23:50 +020010662 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010663 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020010664 char *end;
10665 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010666 struct cert_key_and_chain *ckch;
10667 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020010668
William Lallemand230662a2019-12-03 13:32:54 +010010669 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10670 return 1;
10671
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010672 if ((buf = alloc_trash_chunk()) == NULL)
10673 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020010674
10675 if (!*args[3] || !payload)
10676 return cli_err(appctx, "'set ssl cert expects a filename and a certificat as a payload\n");
10677
10678 /* The operations on the CKCH architecture are locked so we can
10679 * manipulate ckch_store and ckch_inst */
10680 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10681 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
10682
William Lallemand8f840d72019-10-23 10:53:05 +020010683 if (!chunk_strcpy(buf, args[3])) {
10684 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10685 errcode |= ERR_ALERT | ERR_FATAL;
10686 goto end;
10687 }
10688
William Lallemand44b35322019-10-17 16:28:40 +020010689 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020010690 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020010691 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020010692 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
10693 *end = '\0';
10694 type = cert_exts[i].type;
10695 break;
10696 }
10697 }
10698
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010699 appctx->ctx.ssl.old_ckchs = NULL;
10700 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020010701
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010702 /* if there is an ongoing transaction */
10703 if (ckchs_transaction.path) {
10704 /* 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 +020010705#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010706 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010707 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020010708 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020010709
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010710 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020010711 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020010712 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010713 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020010714 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10715 bundle = j; /* keep the type of certificate so we insert it at the right place */
10716 *end = '\0'; /* it's a bundle let's end the string*/
10717 break;
10718 }
William Lallemand150bfa82019-09-19 17:12:49 +020010719 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010720 if (bundle < 0) {
10721 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);
10722 errcode |= ERR_ALERT | ERR_FATAL;
10723 goto end;
10724 }
10725 }
10726#endif
10727
10728 /* if there is an ongoing transaction, check if this is the same file */
10729 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
10730 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
10731 errcode |= ERR_ALERT | ERR_FATAL;
10732 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010733 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010734
10735 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
10736
10737 } else {
10738 struct ckch_store *find_ckchs[2] = { NULL, NULL };
10739
10740 /* lookup for the certificate in the tree:
10741 * check if this is used as a bundle AND as a unique certificate */
10742 for (i = 0; i < 2; i++) {
10743
10744 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
10745 /* only the bundle name is in the tree and you should
10746 * never update a bundle name, only a filename */
10747 if (bundle < 0 && find_ckchs[i]->multi) {
10748 /* we tried to look for a non-bundle and we found a bundle */
10749 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
10750 err ? err : "", args[3], args[3]);
10751 errcode |= ERR_ALERT | ERR_FATAL;
10752 goto end;
10753 }
William Lallemand3246d942019-11-04 14:02:11 +010010754 /* If we want a bundle but this is not a bundle
10755 * example: When you try to update <file>.rsa, but
10756 * <file> is a regular file */
10757 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
10758 find_ckchs[i] = NULL;
10759 break;
10760 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010761 }
10762#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
10763 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010764 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010765 int j;
10766
10767 /* check if it was used in a bundle by removing the
10768 * .dsa/.rsa/.ecdsa at the end of the filename */
10769 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010770 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010771 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10772 bundle = j; /* keep the type of certificate so we insert it at the right place */
10773 *end = '\0'; /* it's a bundle let's end the string*/
10774 break;
10775 }
10776 }
William Lallemand37031b82019-11-04 13:38:53 +010010777 if (bundle < 0) /* we didn't find a bundle extension */
10778 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010779 }
William Lallemand963b2e72019-10-14 11:38:36 +020010780#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010781 /* bundles are not supported here, so we don't need to lookup again */
10782 break;
William Lallemand963b2e72019-10-14 11:38:36 +020010783#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010784 }
10785
10786 if (find_ckchs[0] && find_ckchs[1]) {
10787 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",
10788 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
10789 errcode |= ERR_ALERT | ERR_FATAL;
10790 goto end;
10791 }
10792
10793 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020010794 }
10795
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010796 if (!appctx->ctx.ssl.old_ckchs) {
10797 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020010798 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010799 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020010800 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010801 }
10802
William Lallemand8a7fdf02019-11-04 10:59:32 +010010803 if (!appctx->ctx.ssl.path) {
10804 /* this is a new transaction, set the path of the transaction */
10805 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
10806 if (!appctx->ctx.ssl.path) {
10807 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10808 errcode |= ERR_ALERT | ERR_FATAL;
10809 goto end;
10810 }
10811 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010812
10813 old_ckchs = appctx->ctx.ssl.old_ckchs;
10814
10815 /* TODO: handle filters */
10816 if (old_ckchs->filters) {
10817 memprintf(&err, "%sCertificates used in crt-list with filters are not supported!\n",
10818 err ? err : "");
10819 errcode |= ERR_ALERT | ERR_FATAL;
10820 goto end;
10821 }
10822
10823 /* duplicate the ckch store */
10824 new_ckchs = ckchs_dup(old_ckchs);
10825 if (!new_ckchs) {
10826 memprintf(&err, "%sCannot allocate memory!\n",
10827 err ? err : "");
10828 errcode |= ERR_ALERT | ERR_FATAL;
10829 goto end;
10830 }
10831
10832 if (!new_ckchs->multi)
10833 ckch = new_ckchs->ckch;
10834 else
10835 ckch = &new_ckchs->ckch[bundle];
10836
10837 /* appply the change on the duplicate */
10838 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
10839 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
10840 errcode |= ERR_ALERT | ERR_FATAL;
10841 goto end;
10842 }
10843
10844 appctx->ctx.ssl.new_ckchs = new_ckchs;
10845
10846 /* we succeed, we can save the ckchs in the transaction */
10847
10848 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010010849 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010850 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
10851 ckchs_transaction.path = appctx->ctx.ssl.path;
10852 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
10853 } else {
10854 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
10855
10856 }
10857
10858 /* free the previous ckchs if there was a transaction */
10859 ckchs_free(ckchs_transaction.new_ckchs);
10860
10861 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
10862
10863
William Lallemand8f840d72019-10-23 10:53:05 +020010864 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010865
William Lallemand8f840d72019-10-23 10:53:05 +020010866end:
10867 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020010868
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010869 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010870
10871 ckchs_free(appctx->ctx.ssl.new_ckchs);
10872 appctx->ctx.ssl.new_ckchs = NULL;
10873
10874 appctx->ctx.ssl.old_ckchs = NULL;
10875
10876 free(appctx->ctx.ssl.path);
10877 appctx->ctx.ssl.path = NULL;
10878
10879 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020010880 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010010881 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010882
10883 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10884 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010010885 }
William Lallemand8f840d72019-10-23 10:53:05 +020010886 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010887}
10888
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010889/* parsing function of 'abort ssl cert' */
10890static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
10891{
10892 char *err = NULL;
10893
William Lallemand230662a2019-12-03 13:32:54 +010010894 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10895 return 1;
10896
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010897 if (!*args[3])
10898 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
10899
10900 /* The operations on the CKCH architecture are locked so we can
10901 * manipulate ckch_store and ckch_inst */
10902 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10903 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
10904
10905 if (!ckchs_transaction.path) {
10906 memprintf(&err, "No ongoing transaction!\n");
10907 goto error;
10908 }
10909
10910 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10911 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
10912 goto error;
10913 }
10914
10915 /* Only free the ckchs there, because the SNI and instances were not generated yet */
10916 ckchs_free(ckchs_transaction.new_ckchs);
10917 ckchs_transaction.new_ckchs = NULL;
10918 ckchs_free(ckchs_transaction.old_ckchs);
10919 ckchs_transaction.old_ckchs = NULL;
10920 free(ckchs_transaction.path);
10921 ckchs_transaction.path = NULL;
10922
10923 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10924
10925 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
10926 return cli_dynmsg(appctx, LOG_NOTICE, err);
10927
10928error:
10929 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10930
10931 return cli_dynerr(appctx, err);
10932}
10933
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010934static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010935{
10936#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
10937 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010938 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010939
10940 if (!payload)
10941 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020010942
10943 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010944 if (!*payload)
10945 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010946
10947 /* remove \r and \n from the payload */
10948 for (i = 0, j = 0; payload[i]; i++) {
10949 if (payload[i] == '\r' || payload[i] == '\n')
10950 continue;
10951 payload[j++] = payload[i];
10952 }
10953 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010954
Willy Tarreau1c913e42018-08-22 05:26:57 +020010955 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010956 if (ret < 0)
10957 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010958
Willy Tarreau1c913e42018-08-22 05:26:57 +020010959 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020010960 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020010961 if (err)
10962 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
10963 else
10964 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010965 }
Willy Tarreau9d008692019-08-09 11:21:01 +020010966
10967 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010968#else
Willy Tarreau9d008692019-08-09 11:21:01 +020010969 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 +020010970#endif
10971
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010972}
10973
Willy Tarreau86a394e2019-05-09 14:15:32 +020010974#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010975static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
10976{
10977 switch (arg->type) {
10978 case ARGT_STR:
10979 smp->data.type = SMP_T_STR;
10980 smp->data.u.str = arg->data.str;
10981 return 1;
10982 case ARGT_VAR:
10983 if (!vars_get_by_desc(&arg->data.var, smp))
10984 return 0;
10985 if (!sample_casts[smp->data.type][SMP_T_STR])
10986 return 0;
10987 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
10988 return 0;
10989 return 1;
10990 default:
10991 return 0;
10992 }
10993}
10994
10995static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
10996 const char *file, int line, char **err)
10997{
10998 switch(args[0].data.sint) {
10999 case 128:
11000 case 192:
11001 case 256:
11002 break;
11003 default:
11004 memprintf(err, "key size must be 128, 192 or 256 (bits).");
11005 return 0;
11006 }
11007 /* Try to decode a variable. */
11008 vars_check_arg(&args[1], NULL);
11009 vars_check_arg(&args[2], NULL);
11010 vars_check_arg(&args[3], NULL);
11011 return 1;
11012}
11013
11014/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
11015static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
11016{
11017 struct sample nonce, key, aead_tag;
11018 struct buffer *smp_trash, *smp_trash_alloc;
11019 EVP_CIPHER_CTX *ctx;
11020 int dec_size, ret;
11021
11022 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
11023 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
11024 return 0;
11025
11026 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
11027 if (!sample_conv_var2smp_str(&arg_p[2], &key))
11028 return 0;
11029
11030 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
11031 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
11032 return 0;
11033
11034 smp_trash = get_trash_chunk();
11035 smp_trash_alloc = alloc_trash_chunk();
11036 if (!smp_trash_alloc)
11037 return 0;
11038
11039 ctx = EVP_CIPHER_CTX_new();
11040
11041 if (!ctx)
11042 goto err;
11043
11044 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
11045 if (dec_size < 0)
11046 goto err;
11047 smp_trash->data = dec_size;
11048
11049 /* Set cipher type and mode */
11050 switch(arg_p[0].data.sint) {
11051 case 128:
11052 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
11053 break;
11054 case 192:
11055 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
11056 break;
11057 case 256:
11058 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
11059 break;
11060 }
11061
11062 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
11063
11064 /* Initialise IV */
11065 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
11066 goto err;
11067
11068 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
11069 if (dec_size < 0)
11070 goto err;
11071 smp_trash->data = dec_size;
11072
11073 /* Initialise key */
11074 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
11075 goto err;
11076
11077 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
11078 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
11079 goto err;
11080
11081 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
11082 if (dec_size < 0)
11083 goto err;
11084 smp_trash_alloc->data = dec_size;
11085 dec_size = smp_trash->data;
11086
11087 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
11088 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
11089
11090 if (ret <= 0)
11091 goto err;
11092
11093 smp->data.u.str.data = dec_size + smp_trash->data;
11094 smp->data.u.str.area = smp_trash->area;
11095 smp->data.type = SMP_T_BIN;
11096 smp->flags &= ~SMP_F_CONST;
11097 free_trash_chunk(smp_trash_alloc);
11098 return 1;
11099
11100err:
11101 free_trash_chunk(smp_trash_alloc);
11102 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020011103}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011104# endif
William Lallemand32af2032016-10-29 18:09:35 +020011105
Elliot Otchet71f82972020-01-15 08:12:14 -050011106/* Argument validation functions */
11107
11108/* This function is used to validate the arguments passed to any "x_dn" ssl
11109 * keywords. These keywords support specifying a third parameter that must be
11110 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
11111 */
11112int val_dnfmt(struct arg *arg, char **err_msg)
11113{
11114 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
11115 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
11116 return 0;
11117 }
11118 return 1;
11119}
11120
William Lallemand32af2032016-10-29 18:09:35 +020011121/* register cli keywords */
11122static struct cli_kw_list cli_kws = {{ },{
11123#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11124 { { "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 +020011125 { { "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 +020011126#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010011127 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011128 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
11129 { { "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 +010011130 { { "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 +010011131 { { "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 +020011132 { { NULL }, NULL, NULL, NULL }
11133}};
11134
Willy Tarreau0108d902018-11-25 19:14:37 +010011135INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020011136
Willy Tarreau7875d092012-09-10 08:20:03 +020011137/* Note: must not be declared <const> as its list will be overwritten.
11138 * Please take care of keeping this list alphabetically sorted.
11139 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011140static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020011141 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011142 { "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 +010011143#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010011144 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011145#endif
Emeric Brun645ae792014-04-30 14:21:06 +020011146 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011147#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
11148 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
11149#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010011150 { "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 +020011151 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020011152 { "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 +020011153 { "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 +020011154#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020011155 { "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 -040011156#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011157#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011158 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11159 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040011160 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11161#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011162 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11163 { "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 +010011164 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011165 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011166 { "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 +020011167 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11168 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11169 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11170 { "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 -050011171 { "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 +020011172 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11173 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011174 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011175 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11176 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010011177 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011178 { "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 +020011179 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11180 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11181 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11182 { "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 -050011183 { "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 +020011184 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020011185 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011186 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011187 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011188 { "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 +010011189 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011190 { "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 +020011191 { "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 +010011192 { "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 +020011193 { "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 +010011194#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011195 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020011196#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010011197#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011198 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020011199#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011200 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011201#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020011202 { "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 -040011203#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011204 { "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 +020011205#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011206 { "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 -040011207#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011208#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011209 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11210 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040011211 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11212#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040011213#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011214 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040011215#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011216 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11217 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11218 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11219 { "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 +020011220 { NULL, NULL, 0, 0, 0 },
11221}};
11222
Willy Tarreau0108d902018-11-25 19:14:37 +010011223INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
11224
Willy Tarreau7875d092012-09-10 08:20:03 +020011225/* Note: must not be declared <const> as its list will be overwritten.
11226 * Please take care of keeping this list alphabetically sorted.
11227 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011228static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011229 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
11230 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011231 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020011232}};
11233
Willy Tarreau0108d902018-11-25 19:14:37 +010011234INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
11235
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011236/* Note: must not be declared <const> as its list will be overwritten.
11237 * Please take care of keeping this list alphabetically sorted, doing so helps
11238 * all code contributors.
11239 * Optional keywords are also declared with a NULL ->parse() function so that
11240 * the config parser can report an appropriate error when a known keyword was
11241 * not enabled.
11242 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011243static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011244 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011245 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11246 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11247 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011248#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011249 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11250#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011251 { "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 +010011252 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011253 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011254 { "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 +010011255 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020011256 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
11257 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011258 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
11259 { NULL, NULL, 0 },
11260};
11261
Willy Tarreau0108d902018-11-25 19:14:37 +010011262/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
11263
Willy Tarreau51fb7652012-09-18 18:24:39 +020011264static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011265 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011266 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11267 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11268 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
11269 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
11270 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
11271 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011272#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011273 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11274#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011275 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
11276 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
11277 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
11278 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
11279 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
11280 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
11281 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
11282 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
11283 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
11284 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011285 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011286 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011287 { "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 +020011288 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
11289 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
11290 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
11291 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011292 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011293 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
11294 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011295 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
11296 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011297 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
11298 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
11299 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
11300 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
11301 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011302 { NULL, NULL, 0 },
11303}};
Emeric Brun46591952012-05-18 15:47:34 +020011304
Willy Tarreau0108d902018-11-25 19:14:37 +010011305INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
11306
Willy Tarreau92faadf2012-10-10 23:04:25 +020011307/* Note: must not be declared <const> as its list will be overwritten.
11308 * Please take care of keeping this list alphabetically sorted, doing so helps
11309 * all code contributors.
11310 * Optional keywords are also declared with a NULL ->parse() function so that
11311 * the config parser can report an appropriate error when a known keyword was
11312 * not enabled.
11313 */
11314static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010011315 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010011316 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011317 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010011318 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020011319 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011320 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
11321 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011322#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011323 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
11324#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011325 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
11326 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
11327 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
11328 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
11329 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
11330 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
11331 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
11332 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
11333 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
11334 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
11335 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
11336 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
11337 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
11338 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
11339 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
11340 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
11341 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
11342 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010011343 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011344 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
11345 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
11346 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
11347 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
11348 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
11349 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
11350 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
11351 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
11352 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
11353 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020011354 { NULL, NULL, 0, 0 },
11355}};
11356
Willy Tarreau0108d902018-11-25 19:14:37 +010011357INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
11358
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011359static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010011360 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
11361 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010011362 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011363 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
11364 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010011365#ifndef OPENSSL_NO_DH
11366 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
11367#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011368 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011369#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011370 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011371#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010011372 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
11373#ifndef OPENSSL_NO_DH
11374 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
11375#endif
11376 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
11377 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
11378 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
11379 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011380 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010011381 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
11382 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011383#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011384 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
11385 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
11386#endif
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011387 { 0, NULL, NULL },
11388}};
11389
Willy Tarreau0108d902018-11-25 19:14:37 +010011390INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
11391
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011392/* Note: must not be declared <const> as its list will be overwritten */
11393static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020011394#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011395 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
11396#endif
11397 { NULL, NULL, 0, 0, 0 },
11398}};
11399
11400INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
11401
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020011402/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010011403static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020011404 .snd_buf = ssl_sock_from_buf,
11405 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010011406 .subscribe = ssl_subscribe,
11407 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020011408 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020011409 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020011410 .rcv_pipe = NULL,
11411 .snd_pipe = NULL,
11412 .shutr = NULL,
11413 .shutw = ssl_sock_shutw,
11414 .close = ssl_sock_close,
11415 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010011416 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010011417 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010011418 .prepare_srv = ssl_sock_prepare_srv_ctx,
11419 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010011420 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010011421 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020011422};
11423
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011424enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
11425 struct session *sess, struct stream *s, int flags)
11426{
11427 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011428 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011429
11430 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011431 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011432
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011433 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011434 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011435 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011436 s->req.flags |= CF_READ_NULL;
11437 return ACT_RET_YIELD;
11438 }
11439 }
11440 return (ACT_RET_CONT);
11441}
11442
11443static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
11444{
11445 rule->action_ptr = ssl_action_wait_for_hs;
11446
11447 return ACT_RET_PRS_OK;
11448}
11449
11450static struct action_kw_list http_req_actions = {ILH, {
11451 { "wait-for-handshake", ssl_parse_wait_for_hs },
11452 { /* END */ }
11453}};
11454
Willy Tarreau0108d902018-11-25 19:14:37 +010011455INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
11456
Willy Tarreau5db847a2019-05-09 14:13:35 +020011457#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011458
11459static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11460{
11461 if (ptr) {
11462 chunk_destroy(ptr);
11463 free(ptr);
11464 }
11465}
11466
11467#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011468static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11469{
Willy Tarreaubafbe012017-11-24 17:34:44 +010011470 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011471}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011472
Emeric Brun46591952012-05-18 15:47:34 +020011473__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020011474static void __ssl_sock_init(void)
11475{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011476#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011477 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011478 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011479#endif
Emeric Brun46591952012-05-18 15:47:34 +020011480
Willy Tarreauef934602016-12-22 23:12:01 +010011481 if (global_ssl.listen_default_ciphers)
11482 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
11483 if (global_ssl.connect_default_ciphers)
11484 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011485#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011486 if (global_ssl.listen_default_ciphersuites)
11487 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
11488 if (global_ssl.connect_default_ciphersuites)
11489 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
11490#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010011491
Willy Tarreau13e14102016-12-22 20:25:26 +010011492 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011493#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020011494 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080011495#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011496#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011497 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011498 n = sk_SSL_COMP_num(cm);
11499 while (n--) {
11500 (void) sk_SSL_COMP_pop(cm);
11501 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011502#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011503
Willy Tarreau5db847a2019-05-09 14:13:35 +020011504#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011505 ssl_locking_init();
11506#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020011507#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011508 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
11509#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020011510 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020011511 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 +020011512#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011513 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011514 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011515#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010011516#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11517 hap_register_post_check(tlskeys_finalize_config);
11518#endif
Willy Tarreau80713382018-11-26 10:19:54 +010011519
11520 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
11521 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
11522
11523#ifndef OPENSSL_NO_DH
11524 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
11525 hap_register_post_deinit(ssl_free_dh);
11526#endif
11527#ifndef OPENSSL_NO_ENGINE
11528 hap_register_post_deinit(ssl_free_engines);
11529#endif
11530 /* Load SSL string for the verbose & debug mode. */
11531 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020011532 ha_meth = BIO_meth_new(0x666, "ha methods");
11533 BIO_meth_set_write(ha_meth, ha_ssl_write);
11534 BIO_meth_set_read(ha_meth, ha_ssl_read);
11535 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
11536 BIO_meth_set_create(ha_meth, ha_ssl_new);
11537 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
11538 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
11539 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020011540
11541 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010011542}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010011543
Willy Tarreau80713382018-11-26 10:19:54 +010011544/* Compute and register the version string */
11545static void ssl_register_build_options()
11546{
11547 char *ptr = NULL;
11548 int i;
11549
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011550 memprintf(&ptr, "Built with OpenSSL version : "
11551#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011552 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011553#else /* OPENSSL_IS_BORINGSSL */
11554 OPENSSL_VERSION_TEXT
11555 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080011556 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020011557 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011558#endif
11559 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011560#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011561 "no (library version too old)"
11562#elif defined(OPENSSL_NO_TLSEXT)
11563 "no (disabled via OPENSSL_NO_TLSEXT)"
11564#else
11565 "yes"
11566#endif
11567 "", ptr);
11568
11569 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
11570#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11571 "yes"
11572#else
11573#ifdef OPENSSL_NO_TLSEXT
11574 "no (because of OPENSSL_NO_TLSEXT)"
11575#else
11576 "no (version might be too old, 0.9.8f min needed)"
11577#endif
11578#endif
11579 "", ptr);
11580
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020011581 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
11582 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
11583 if (methodVersions[i].option)
11584 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011585
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011586 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010011587}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011588
Willy Tarreau80713382018-11-26 10:19:54 +010011589INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020011590
Emeric Brun46591952012-05-18 15:47:34 +020011591
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011592#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011593void ssl_free_engines(void) {
11594 struct ssl_engine_list *wl, *wlb;
11595 /* free up engine list */
11596 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
11597 ENGINE_finish(wl->e);
11598 ENGINE_free(wl->e);
11599 LIST_DEL(&wl->list);
11600 free(wl);
11601 }
11602}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011603#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020011604
Remi Gacogned3a23c32015-05-28 16:39:47 +020011605#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000011606void ssl_free_dh(void) {
11607 if (local_dh_1024) {
11608 DH_free(local_dh_1024);
11609 local_dh_1024 = NULL;
11610 }
11611 if (local_dh_2048) {
11612 DH_free(local_dh_2048);
11613 local_dh_2048 = NULL;
11614 }
11615 if (local_dh_4096) {
11616 DH_free(local_dh_4096);
11617 local_dh_4096 = NULL;
11618 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020011619 if (global_dh) {
11620 DH_free(global_dh);
11621 global_dh = NULL;
11622 }
Grant Zhang872f9c22017-01-21 01:10:18 +000011623}
11624#endif
11625
11626__attribute__((destructor))
11627static void __ssl_sock_deinit(void)
11628{
11629#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011630 if (ssl_ctx_lru_tree) {
11631 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010011632 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020011633 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020011634#endif
11635
Willy Tarreau5db847a2019-05-09 14:13:35 +020011636#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011637 ERR_remove_state(0);
11638 ERR_free_strings();
11639
11640 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080011641#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020011642
Willy Tarreau5db847a2019-05-09 14:13:35 +020011643#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011644 CRYPTO_cleanup_all_ex_data();
11645#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020011646 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020011647}
11648
11649
Emeric Brun46591952012-05-18 15:47:34 +020011650/*
11651 * Local variables:
11652 * c-indent-level: 8
11653 * c-basic-offset: 8
11654 * End:
11655 */