blob: 7df952be56eaa93948bbfe55bae366b95f67151a [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;
3276 DH *dh;
3277
3278 if (buf) {
3279 /* reading from a buffer */
3280 in = BIO_new_mem_buf(buf, -1);
3281 if (in == NULL) {
3282 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3283 goto end;
3284 }
yanbzhu488a4d22015-12-01 15:16:07 -05003285
William Lallemand96a9c972019-10-17 11:56:17 +02003286 } else {
3287 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003288 in = BIO_new(BIO_s_file());
3289 if (in == NULL)
3290 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003291
William Lallemandf11365b2019-09-19 14:25:58 +02003292 if (BIO_read_filename(in, path) <= 0)
3293 goto end;
William Lallemandf11365b2019-09-19 14:25:58 +02003294 }
yanbzhu488a4d22015-12-01 15:16:07 -05003295
yanbzhu488a4d22015-12-01 15:16:07 -05003296 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003297 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3298 if (key == NULL) {
yanbzhu488a4d22015-12-01 15:16:07 -05003299 memprintf(err, "%sunable to load private key from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003300 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003301 goto end;
3302 }
3303
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003304#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003305 /* Seek back to beginning of file */
3306 if (BIO_reset(in) == -1) {
3307 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3308 err && *err ? *err : "", path);
3309 goto end;
3310 }
3311
William Lallemand96a9c972019-10-17 11:56:17 +02003312 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3313 /* no need to return an error there, dh is not mandatory */
3314
3315 if (dh) {
3316 if (ckch->dh)
3317 DH_free(ckch->dh);
3318 ckch->dh = dh;
3319 }
3320
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003321#endif
William Lallemandfa892222019-07-23 16:06:08 +02003322
Willy Tarreaubb137a82016-04-06 19:02:38 +02003323 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003324 if (BIO_reset(in) == -1) {
3325 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3326 err && *err ? *err : "", path);
3327 goto end;
3328 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003329
3330 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003331 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3332 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003333 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003334 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003335 goto end;
3336 }
3337
William Lallemand96a9c972019-10-17 11:56:17 +02003338 if (!X509_check_private_key(cert, key)) {
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003339 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003340 err && *err ? *err : "", path);
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003341 goto end;
3342 }
3343
William Lallemand96a9c972019-10-17 11:56:17 +02003344 /* Key and Cert are good, we can use them in the ckch */
3345 if (ckch->key) /* free the previous key */
3346 EVP_PKEY_free(ckch->key);
3347 ckch->key = key;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003348 key = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003349
3350 if (ckch->cert) /* free the previous cert */
3351 X509_free(ckch->cert);
3352 ckch->cert = cert;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003353 cert = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003354
3355 /* Look for a Certificate Chain */
3356 ca = PEM_read_bio_X509(in, NULL, NULL, NULL);
3357 if (ca) {
3358 /* there is a chain a in the PEM, clean the previous one in the CKCH */
3359 if (ckch->chain) /* free the previous chain */
3360 sk_X509_pop_free(ckch->chain, X509_free);
3361 ckch->chain = sk_X509_new_null();
3362 if (!sk_X509_push(ckch->chain, ca)) {
3363 X509_free(ca);
3364 goto end;
3365 }
3366 }
3367 /* look for other crt in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003368 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL)))
3369 if (!sk_X509_push(ckch->chain, ca)) {
3370 X509_free(ca);
3371 goto end;
3372 }
yanbzhu488a4d22015-12-01 15:16:07 -05003373
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003374 /* no chain */
3375 if (ckch->chain == NULL) {
3376 ckch->chain = sk_X509_new_null();
3377 }
3378
yanbzhu488a4d22015-12-01 15:16:07 -05003379 ret = ERR_get_error();
3380 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3381 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003382 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003383 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003384 }
3385
3386 ret = 0;
3387
William Lallemand96a9c972019-10-17 11:56:17 +02003388end:
William Lallemand246c0242019-10-11 08:59:13 +02003389
3390 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003391 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003392 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003393 if (key)
3394 EVP_PKEY_free(key);
3395 if (cert)
3396 X509_free(cert);
William Lallemanda17f4112019-10-10 15:16:44 +02003397
William Lallemand96a9c972019-10-17 11:56:17 +02003398 return ret;
3399}
3400
3401/*
3402 * Try to load in a ckch every files related to a ckch.
3403 * (PEM, sctl, ocsp, issuer etc.)
3404 *
3405 * This function is only used to load files during the configuration parsing,
3406 * it is not used with the CLI.
3407 *
3408 * This allows us to carry the contents of the file without having to read the
3409 * file multiple times. The caller must call
3410 * ssl_sock_free_cert_key_and_chain_contents.
3411 *
3412 * returns:
3413 * 0 on Success
3414 * 1 on SSL Failure
3415 */
3416static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3417{
3418 int ret = 1;
3419
3420 /* try to load the PEM */
3421 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3422 goto end;
3423 }
3424
William Lallemanda17f4112019-10-10 15:16:44 +02003425#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3426 /* try to load the sctl file */
3427 {
3428 char fp[MAXPATHLEN+1];
3429 struct stat st;
3430
3431 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3432 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003433 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003434 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003435 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003436 ret = 1;
3437 goto end;
3438 }
3439 }
3440 }
3441#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003442
William Lallemand246c0242019-10-11 08:59:13 +02003443 /* try to load an ocsp response file */
3444 {
3445 char fp[MAXPATHLEN+1];
3446 struct stat st;
3447
3448 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3449 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003450 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003451 ret = 1;
3452 goto end;
3453 }
3454 }
3455 }
3456
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003457#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand246c0242019-10-11 08:59:13 +02003458 if (ckch->ocsp_response) {
3459 X509 *issuer;
3460 int i;
3461
3462 /* check if one of the certificate of the chain is the issuer */
3463 for (i = 0; i < sk_X509_num(ckch->chain); i++) {
3464 issuer = sk_X509_value(ckch->chain, i);
3465 if (X509_check_issued(issuer, ckch->cert) == X509_V_OK) {
3466 ckch->ocsp_issuer = issuer;
3467 break;
3468 } else
3469 issuer = NULL;
3470 }
3471
3472 /* if no issuer was found, try to load an issuer from the .issuer */
3473 if (!issuer) {
3474 struct stat st;
3475 char fp[MAXPATHLEN+1];
3476
3477 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3478 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003479 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003480 ret = 1;
3481 goto end;
3482 }
3483
3484 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003485 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003486 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003487 ret = 1;
3488 goto end;
3489 }
3490 } else {
3491 memprintf(err, "%sNo issuer found, cannot use the OCSP response'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003492 err && *err ? *err : "");
William Lallemand246c0242019-10-11 08:59:13 +02003493 ret = 1;
3494 goto end;
3495 }
3496 }
3497 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003498#endif
William Lallemand246c0242019-10-11 08:59:13 +02003499
yanbzhu488a4d22015-12-01 15:16:07 -05003500 ret = 0;
3501
3502end:
3503
3504 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003505
3506 /* Something went wrong in one of the reads */
3507 if (ret != 0)
3508 ssl_sock_free_cert_key_and_chain_contents(ckch);
3509
3510 return ret;
3511}
3512
3513/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003514 * Returns a bitfield containing the flags:
3515 * ERR_FATAL in any fatal error case
3516 * ERR_ALERT if the reason of the error is available in err
3517 * ERR_WARN if a warning is available into err
3518 * The value 0 means there is no error nor warning and
3519 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003520 */
3521static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3522{
Emeric Bruna96b5822019-10-17 13:25:14 +02003523 int errcode = 0;
3524
yanbzhu488a4d22015-12-01 15:16:07 -05003525 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3526 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3527 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003528 errcode |= ERR_ALERT | ERR_FATAL;
3529 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003530 }
3531
3532 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3533 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3534 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003535 errcode |= ERR_ALERT | ERR_FATAL;
3536 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003537 }
3538
yanbzhu488a4d22015-12-01 15:16:07 -05003539 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003540#ifdef SSL_CTX_set1_chain
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003541 if (!SSL_CTX_set1_chain(ctx, ckch->chain)) {
3542 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3543 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003544 errcode |= ERR_ALERT | ERR_FATAL;
3545 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003546 }
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003547#else
3548 { /* legacy compat (< openssl 1.0.2) */
3549 X509 *ca;
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003550 STACK_OF(X509) *chain;
3551 chain = X509_chain_up_ref(ckch->chain);
3552 while ((ca = sk_X509_shift(chain)))
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003553 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3554 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3555 err && *err ? *err : "", path);
3556 X509_free(ca);
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003557 sk_X509_pop_free(chain, X509_free);
Emeric Bruna96b5822019-10-17 13:25:14 +02003558 errcode |= ERR_ALERT | ERR_FATAL;
3559 goto end;
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003560 }
3561 }
3562#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003563
William Lallemandfa892222019-07-23 16:06:08 +02003564#ifndef OPENSSL_NO_DH
3565 /* store a NULL pointer to indicate we have not yet loaded
3566 a custom DH param file */
3567 if (ssl_dh_ptr_index >= 0) {
3568 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3569 }
3570
Emeric Brun7a883362019-10-17 13:27:40 +02003571 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3572 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003573 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3574 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003575 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003576 }
3577#endif
3578
William Lallemanda17f4112019-10-10 15:16:44 +02003579#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3580 if (sctl_ex_index >= 0 && ckch->sctl) {
3581 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3582 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003583 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003584 errcode |= ERR_ALERT | ERR_FATAL;
3585 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003586 }
3587 }
3588#endif
3589
William Lallemand4a660132019-10-14 14:51:41 +02003590#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003591 /* Load OCSP Info into context */
3592 if (ckch->ocsp_response) {
3593 if (ssl_sock_load_ocsp(ctx, ckch) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003594 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",
3595 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003596 errcode |= ERR_ALERT | ERR_FATAL;
3597 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003598 }
3599 }
William Lallemand246c0242019-10-11 08:59:13 +02003600#endif
3601
Emeric Bruna96b5822019-10-17 13:25:14 +02003602 end:
3603 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003604}
3605
William Lallemandc4ecddf2019-07-31 16:50:08 +02003606#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003607
William Lallemand28a8fce2019-10-04 17:36:55 +02003608static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003609{
3610 struct sni_keytype *s_kt = NULL;
3611 struct ebmb_node *node;
3612 int i;
3613
3614 for (i = 0; i < trash.size; i++) {
3615 if (!str[i])
3616 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003617 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003618 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003619 trash.area[i] = 0;
3620 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003621 if (!node) {
3622 /* CN not found in tree */
3623 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3624 /* Using memcpy here instead of strncpy.
3625 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3626 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3627 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003628 if (!s_kt)
3629 return -1;
3630
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003631 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003632 s_kt->keytypes = 0;
3633 ebst_insert(sni_keytypes, &s_kt->name);
3634 } else {
3635 /* CN found in tree */
3636 s_kt = container_of(node, struct sni_keytype, name);
3637 }
3638
3639 /* Mark that this CN has the keytype of key_index via keytypes mask */
3640 s_kt->keytypes |= 1<<key_index;
3641
William Lallemand28a8fce2019-10-04 17:36:55 +02003642 return 0;
3643
yanbzhu08ce6ab2015-12-02 13:01:29 -05003644}
3645
William Lallemandc4ecddf2019-07-31 16:50:08 +02003646#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003647/*
3648 * Free a ckch_store and its ckch(s)
3649 * The linked ckch_inst are not free'd
3650 */
3651void ckchs_free(struct ckch_store *ckchs)
3652{
3653 if (!ckchs)
3654 return;
3655
3656#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3657 if (ckchs->multi) {
3658 int n;
3659
3660 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3661 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
3662 } else
3663#endif
3664 {
3665 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
3666 ckchs->ckch = NULL;
3667 }
3668
3669 free(ckchs);
3670}
3671
3672/* allocate and duplicate a ckch_store
3673 * Return a new ckch_store or NULL */
3674static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3675{
3676 struct ckch_store *dst;
3677 int pathlen;
3678
3679 pathlen = strlen(src->path);
3680 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3681 if (!dst)
3682 return NULL;
3683 /* copy previous key */
3684 memcpy(dst->path, src->path, pathlen + 1);
3685 dst->multi = src->multi;
3686 LIST_INIT(&dst->ckch_inst);
3687
3688 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3689 if (!dst->ckch)
3690 goto error;
3691
3692#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3693 if (src->multi) {
3694 int n;
3695
3696 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3697 if (&src->ckch[n]) {
3698 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3699 goto error;
3700 }
3701 }
3702 } else
3703#endif
3704 {
3705 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3706 goto error;
3707 }
3708
3709 return dst;
3710
3711error:
3712 ckchs_free(dst);
3713
3714 return NULL;
3715}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003716
William Lallemand36b84632019-07-18 19:28:17 +02003717/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003718 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003719 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003720static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003721{
3722 struct ebmb_node *eb;
3723
William Lallemande3af8fb2019-10-08 11:36:53 +02003724 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003725 if (!eb)
3726 return NULL;
3727
William Lallemande3af8fb2019-10-08 11:36:53 +02003728 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003729}
3730
3731/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003732 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003733 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003734static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003735{
William Lallemande3af8fb2019-10-08 11:36:53 +02003736 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003737
William Lallemande3af8fb2019-10-08 11:36:53 +02003738 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3739 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003740 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3741 goto end;
3742 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003743 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003744
William Lallemande3af8fb2019-10-08 11:36:53 +02003745 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003746 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3747 goto end;
3748 }
3749
William Lallemand9117de92019-10-04 00:29:42 +02003750 LIST_INIT(&ckchs->ckch_inst);
3751
William Lallemand36b84632019-07-18 19:28:17 +02003752 if (!multi) {
3753
William Lallemand96a9c972019-10-17 11:56:17 +02003754 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003755 goto end;
3756
William Lallemande3af8fb2019-10-08 11:36:53 +02003757 /* insert into the ckchs tree */
3758 memcpy(ckchs->path, path, strlen(path) + 1);
3759 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003760 } else {
3761 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003762#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3763 char fp[MAXPATHLEN+1] = {0};
3764 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003765
3766 /* Load all possible certs and keys */
3767 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3768 struct stat buf;
3769 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3770 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003771 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003772 goto end;
3773 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003774 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003775 }
3776 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003777#endif
William Lallemand36b84632019-07-18 19:28:17 +02003778
3779 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003780 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003781 goto end;
3782 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003783 /* insert into the ckchs tree */
3784 memcpy(ckchs->path, path, strlen(path) + 1);
3785 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003786 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003787 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003788
William Lallemand36b84632019-07-18 19:28:17 +02003789end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003790 if (ckchs) {
3791 free(ckchs->ckch);
3792 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003793 }
3794
William Lallemande3af8fb2019-10-08 11:36:53 +02003795 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003796
3797 return NULL;
3798}
3799
William Lallemandc4ecddf2019-07-31 16:50:08 +02003800#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3801
William Lallemand36b84632019-07-18 19:28:17 +02003802/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003803 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003804 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003805 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3806 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003807 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003808 *
Emeric Brun054563d2019-10-17 13:16:58 +02003809 * Returns a bitfield containing the flags:
3810 * ERR_FATAL in any fatal error case
3811 * ERR_ALERT if the reason of the error is available in err
3812 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003813 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003814 */
Emeric Brun054563d2019-10-17 13:16:58 +02003815static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3816 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3817 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003818{
William Lallemand36b84632019-07-18 19:28:17 +02003819 int i = 0, n = 0;
3820 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003821 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003822 struct ebmb_node *node;
3823 struct ebmb_node *next;
3824 /* Array of SSL_CTX pointers corresponding to each possible combo
3825 * of keytypes
3826 */
3827 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003828 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003829 X509_NAME *xname = NULL;
3830 char *str = NULL;
3831#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3832 STACK_OF(GENERAL_NAME) *names = NULL;
3833#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003834 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003835
Emeric Brun054563d2019-10-17 13:16:58 +02003836 *ckchi = NULL;
3837
William Lallemande3af8fb2019-10-08 11:36:53 +02003838 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003839 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3840 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003841 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003842 }
3843
3844 ckch_inst = ckch_inst_new();
3845 if (!ckch_inst) {
3846 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3847 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003848 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003849 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003850 }
3851
William Lallemande3af8fb2019-10-08 11:36:53 +02003852 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003853
William Lallemand150bfa82019-09-19 17:12:49 +02003854 /* at least one of the instances is using filters during the config
3855 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01003856 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02003857
yanbzhu08ce6ab2015-12-02 13:01:29 -05003858 /* Process each ckch and update keytypes for each CN/SAN
3859 * for example, if CN/SAN www.a.com is associated with
3860 * certs with keytype 0 and 2, then at the end of the loop,
3861 * www.a.com will have:
3862 * keyindex = 0 | 1 | 4 = 5
3863 */
3864 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003865 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003866
3867 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3868 continue;
3869
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003870 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003871 for (i = 0; i < fcount; i++) {
3872 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3873 if (ret < 0) {
3874 memprintf(err, "%sunable to allocate SSL context.\n",
3875 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003876 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003877 goto end;
3878 }
3879 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003880 } else {
3881 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3882 * so the line that contains logic is marked via comments
3883 */
3884 xname = X509_get_subject_name(certs_and_keys[n].cert);
3885 i = -1;
3886 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3887 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003888 ASN1_STRING *value;
3889 value = X509_NAME_ENTRY_get_data(entry);
3890 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003891 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003892 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003893
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003894 OPENSSL_free(str);
3895 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003896 if (ret < 0) {
3897 memprintf(err, "%sunable to allocate SSL context.\n",
3898 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003899 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003900 goto end;
3901 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003902 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003903 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003904
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003905 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003906#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003907 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3908 if (names) {
3909 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3910 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003911
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003912 if (name->type == GEN_DNS) {
3913 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3914 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003915 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003916
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003917 OPENSSL_free(str);
3918 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003919 if (ret < 0) {
3920 memprintf(err, "%sunable to allocate SSL context.\n",
3921 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003922 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003923 goto end;
3924 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003925 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003926 }
3927 }
3928 }
3929 }
3930#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3931 }
3932
3933 /* If no files found, return error */
3934 if (eb_is_empty(&sni_keytypes_map)) {
3935 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3936 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003937 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003938 goto end;
3939 }
3940
3941 /* We now have a map of CN/SAN to keytypes that are loaded in
3942 * Iterate through the map to create the SSL_CTX's (if needed)
3943 * and add each CTX to the SNI tree
3944 *
3945 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003946 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003947 * combination is denoted by the key in the map. Each key
3948 * has a value between 1 and 2^n - 1. Conveniently, the array
3949 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3950 * entry in the array to correspond to the unique combo (key)
3951 * associated with i. This unique key combo (i) will be associated
3952 * with combos[i-1]
3953 */
3954
3955 node = ebmb_first(&sni_keytypes_map);
3956 while (node) {
3957 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003958 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003959 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003960
3961 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3962 i = container_of(node, struct sni_keytype, name)->keytypes;
3963 cur_ctx = key_combos[i-1].ctx;
3964
3965 if (cur_ctx == NULL) {
3966 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003967 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003968 if (cur_ctx == NULL) {
3969 memprintf(err, "%sunable to allocate SSL context.\n",
3970 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003971 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003972 goto end;
3973 }
3974
yanbzhube2774d2015-12-10 15:07:30 -05003975 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003976 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3977 if (i & (1<<n)) {
3978 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003979 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02003980 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3981 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003982 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003983 }
3984 }
3985
yanbzhu08ce6ab2015-12-02 13:01:29 -05003986 /* Update key_combos */
3987 key_combos[i-1].ctx = cur_ctx;
3988 }
3989
3990 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02003991
William Lallemand1d29c742019-10-04 00:53:29 +02003992 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 +02003993 kinfo, str, key_combos[i-1].order);
3994 if (key_combos[i-1].order < 0) {
3995 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003996 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02003997 goto end;
3998 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003999 node = ebmb_next(node);
4000 }
4001
4002
4003 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4004 if (!bind_conf->default_ctx) {
4005 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4006 if (key_combos[i].ctx) {
4007 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004008 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004009 ckch_inst->is_default = 1;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004010 break;
4011 }
4012 }
4013 }
4014
William Lallemand614ca0d2019-10-07 13:52:11 +02004015 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004016 ckch_inst->ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004017end:
4018
4019 if (names)
4020 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4021
yanbzhu08ce6ab2015-12-02 13:01:29 -05004022 node = ebmb_first(&sni_keytypes_map);
4023 while (node) {
4024 next = ebmb_next(node);
4025 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004026 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004027 node = next;
4028 }
4029
Emeric Brun054563d2019-10-17 13:16:58 +02004030 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02004031 struct sni_ctx *sc0, *sc0b;
4032
4033 /* free the SSL_CTX in case of error */
4034 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4035 if (key_combos[i].ctx)
4036 SSL_CTX_free(key_combos[i].ctx);
4037 }
4038
4039 /* free the sni_ctx in case of error */
4040 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4041
4042 ebmb_delete(&sc0->name);
4043 LIST_DEL(&sc0->by_ckch_inst);
4044 free(sc0);
4045 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004046 free(ckch_inst);
4047 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004048 }
4049
Emeric Brun054563d2019-10-17 13:16:58 +02004050 *ckchi = ckch_inst;
4051 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004052}
4053#else
4054/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004055static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4056 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4057 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004058{
4059 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4060 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004061 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004062}
4063
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004064#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004065
William Lallemand614ca0d2019-10-07 13:52:11 +02004066/*
4067 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004068 *
4069 * Returns a bitfield containing the flags:
4070 * ERR_FATAL in any fatal error case
4071 * ERR_ALERT if the reason of the error is available in err
4072 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004073 */
Emeric Brun054563d2019-10-17 13:16:58 +02004074static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4075 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004076{
William Lallemandc9402072019-05-15 15:33:54 +02004077 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004078 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004079 int order = 0;
4080 X509_NAME *xname;
4081 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004082 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004083 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004084#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4085 STACK_OF(GENERAL_NAME) *names;
4086#endif
William Lallemand36b84632019-07-18 19:28:17 +02004087 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004088 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004089 int errcode = 0;
4090
4091 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004092
William Lallemande3af8fb2019-10-08 11:36:53 +02004093 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004094 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004095
William Lallemande3af8fb2019-10-08 11:36:53 +02004096 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004097
William Lallemand150bfa82019-09-19 17:12:49 +02004098 /* at least one of the instances is using filters during the config
4099 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01004100 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02004101
William Lallemandc9402072019-05-15 15:33:54 +02004102 ctx = SSL_CTX_new(SSLv23_server_method());
4103 if (!ctx) {
4104 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4105 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004106 errcode |= ERR_ALERT | ERR_FATAL;
4107 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004108 }
4109
Emeric Bruna96b5822019-10-17 13:25:14 +02004110 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4111 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004112 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004113
4114 ckch_inst = ckch_inst_new();
4115 if (!ckch_inst) {
4116 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4117 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004118 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004119 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004120 }
4121
William Lallemand36b84632019-07-18 19:28:17 +02004122 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004123 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004124 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004125 switch(EVP_PKEY_base_id(pkey)) {
4126 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004127 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004128 break;
4129 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004130 kinfo.sig = TLSEXT_signature_ecdsa;
4131 break;
4132 case EVP_PKEY_DSA:
4133 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004134 break;
4135 }
4136 EVP_PKEY_free(pkey);
4137 }
4138
Emeric Brun50bcecc2013-04-22 13:05:23 +02004139 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004140 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004141 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 +02004142 if (order < 0) {
4143 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004144 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004145 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004146 }
4147 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004148 }
4149 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004150#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004151 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004152 if (names) {
4153 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4154 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4155 if (name->type == GEN_DNS) {
4156 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004157 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004158 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004159 if (order < 0) {
4160 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004161 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004162 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004163 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004164 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004165 }
4166 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004167 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004168 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004169#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004170 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004171 i = -1;
4172 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4173 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004174 ASN1_STRING *value;
4175
4176 value = X509_NAME_ENTRY_get_data(entry);
4177 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004178 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004179 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004180 if (order < 0) {
4181 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004182 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004183 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004184 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004185 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004186 }
4187 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004188 /* we must not free the SSL_CTX anymore below, since it's already in
4189 * the tree, so it will be discovered and cleaned in time.
4190 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004191
Emeric Brunfc0421f2012-09-07 17:30:07 +02004192#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004193 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004194 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4195 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004196 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004197 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004198 }
4199#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004200 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004201 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004202 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004203 ckch_inst->is_default = 1;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004204 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004205
William Lallemand9117de92019-10-04 00:29:42 +02004206 /* everything succeed, the ckch instance can be used */
4207 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004208 ckch_inst->ssl_conf = ssl_conf;
William Lallemand9117de92019-10-04 00:29:42 +02004209
Emeric Brun054563d2019-10-17 13:16:58 +02004210 *ckchi = ckch_inst;
4211 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004212
4213error:
4214 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004215 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004216 struct sni_ctx *sc0, *sc0b;
4217
4218 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4219
4220 ebmb_delete(&sc0->name);
4221 LIST_DEL(&sc0->by_ckch_inst);
4222 free(sc0);
4223 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004224 free(ckch_inst);
4225 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004226 }
4227 /* We only created 1 SSL_CTX so we can free it there */
4228 SSL_CTX_free(ctx);
4229
Emeric Brun054563d2019-10-17 13:16:58 +02004230 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004231}
4232
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004233/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004234static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4235 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4236 char **sni_filter, int fcount, char **err)
4237{
4238 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004239 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004240
4241 /* we found the ckchs in the tree, we can use it directly */
4242 if (ckchs->multi)
Emeric Brun054563d2019-10-17 13:16:58 +02004243 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 +02004244 else
Emeric Brun054563d2019-10-17 13:16:58 +02004245 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 +02004246
Emeric Brun054563d2019-10-17 13:16:58 +02004247 if (errcode & ERR_CODE)
4248 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004249
4250 ssl_sock_load_cert_sni(ckch_inst, bind_conf);
4251
4252 /* succeed, add the instance to the ckch_store's list of instance */
4253 LIST_ADDQ(&ckchs->ckch_inst, &ckch_inst->by_ckchs);
Emeric Brun054563d2019-10-17 13:16:58 +02004254 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004255}
4256
4257
Willy Tarreaubbc91962019-10-16 16:42:19 +02004258/* Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau03209342016-12-22 17:08:28 +01004259int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004260{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004261 struct dirent **de_list;
4262 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004263 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004264 char *end;
4265 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004266 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004267 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004268#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004269 int is_bundle;
4270 int j;
4271#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004272 if ((ckchs = ckchs_lookup(path))) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004273 /* we found the ckchs in the tree, we can use it directly */
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004274 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand6af03992019-07-23 15:00:54 +02004275 }
4276
yanbzhu08ce6ab2015-12-02 13:01:29 -05004277 if (stat(path, &buf) == 0) {
William Dauchy9a8ef7f2020-01-13 17:52:49 +01004278 if (S_ISDIR(buf.st_mode) == 0) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004279 ckchs = ckchs_load_cert_file(path, 0, err);
4280 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004281 return ERR_ALERT | ERR_FATAL;
4282
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004283 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004284 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004285
yanbzhu08ce6ab2015-12-02 13:01:29 -05004286 /* strip trailing slashes, including first one */
4287 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
4288 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004289
yanbzhu08ce6ab2015-12-02 13:01:29 -05004290 n = scandir(path, &de_list, 0, alphasort);
4291 if (n < 0) {
4292 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4293 err && *err ? *err : "", path, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004294 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004295 }
4296 else {
4297 for (i = 0; i < n; i++) {
4298 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004299
yanbzhu08ce6ab2015-12-02 13:01:29 -05004300 end = strrchr(de->d_name, '.');
4301 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
4302 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004303
yanbzhu08ce6ab2015-12-02 13:01:29 -05004304 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4305 if (stat(fp, &buf) != 0) {
4306 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4307 err && *err ? *err : "", fp, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004308 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004309 goto ignore_entry;
4310 }
4311 if (!S_ISREG(buf.st_mode))
4312 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004313
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004314#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004315 is_bundle = 0;
4316 /* Check if current entry in directory is part of a multi-cert bundle */
4317
4318 if (end) {
4319 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4320 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4321 is_bundle = 1;
4322 break;
4323 }
4324 }
4325
4326 if (is_bundle) {
yanbzhu63ea8462015-12-09 13:35:14 -05004327 int dp_len;
4328
4329 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004330
4331 /* increment i and free de until we get to a non-bundle cert
4332 * Note here that we look at de_list[i + 1] before freeing de
Willy Tarreau05800522019-10-29 10:48:50 +01004333 * this is important since ignore_entry will free de. This also
4334 * guarantees that de->d_name continues to hold the same prefix.
yanbzhu63ea8462015-12-09 13:35:14 -05004335 */
Willy Tarreau05800522019-10-29 10:48:50 +01004336 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
yanbzhu63ea8462015-12-09 13:35:14 -05004337 free(de);
4338 i++;
4339 de = de_list[i];
4340 }
4341
Willy Tarreau05800522019-10-29 10:48:50 +01004342 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemande3af8fb2019-10-08 11:36:53 +02004343 if ((ckchs = ckchs_lookup(fp)) == NULL)
4344 ckchs = ckchs_load_cert_file(fp, 1, err);
4345 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004346 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004347 else
4348 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05004349 /* Successfully processed the bundle */
4350 goto ignore_entry;
4351 }
4352 }
4353
4354#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004355 if ((ckchs = ckchs_lookup(fp)) == NULL)
4356 ckchs = ckchs_load_cert_file(fp, 0, err);
4357 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004358 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004359 else
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004360 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004361
yanbzhu08ce6ab2015-12-02 13:01:29 -05004362ignore_entry:
4363 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004364 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004365 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004366 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004367 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004368 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004369
William Lallemande3af8fb2019-10-08 11:36:53 +02004370 ckchs = ckchs_load_cert_file(path, 1, err);
4371 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004372 return ERR_ALERT | ERR_FATAL;
4373
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004374 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004375
Emeric Brunfc0421f2012-09-07 17:30:07 +02004376 return cfgerr;
4377}
4378
Thierry Fournier383085f2013-01-24 14:15:43 +01004379/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4380 * done once. Zero is returned if the operation fails. No error is returned
4381 * if the random is said as not implemented, because we expect that openssl
4382 * will use another method once needed.
4383 */
4384static int ssl_initialize_random()
4385{
4386 unsigned char random;
4387 static int random_initialized = 0;
4388
4389 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4390 random_initialized = 1;
4391
4392 return random_initialized;
4393}
4394
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004395/* release ssl bind conf */
4396void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004397{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004398 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01004399#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004400 free(conf->npn_str);
4401 conf->npn_str = NULL;
4402#endif
4403#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4404 free(conf->alpn_str);
4405 conf->alpn_str = NULL;
4406#endif
4407 free(conf->ca_file);
4408 conf->ca_file = NULL;
4409 free(conf->crl_file);
4410 conf->crl_file = NULL;
4411 free(conf->ciphers);
4412 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004413#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004414 free(conf->ciphersuites);
4415 conf->ciphersuites = NULL;
4416#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004417 free(conf->curves);
4418 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004419 free(conf->ecdhe);
4420 conf->ecdhe = NULL;
4421 }
4422}
4423
Willy Tarreaubbc91962019-10-16 16:42:19 +02004424/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004425int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
4426{
4427 char thisline[CRT_LINESIZE];
4428 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004429 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004430 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004431 int linenum = 0;
4432 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004433 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004434
Willy Tarreauad1731d2013-04-02 17:35:58 +02004435 if ((f = fopen(file, "r")) == NULL) {
4436 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004437 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004438 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004439
4440 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004441 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004442 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004443 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004444 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004445 char *crt_path;
4446 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004447
4448 linenum++;
4449 end = line + strlen(line);
4450 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4451 /* Check if we reached the limit and the last char is not \n.
4452 * Watch out for the last line without the terminating '\n'!
4453 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004454 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4455 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004456 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004457 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004458 }
4459
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004460 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004461 newarg = 1;
4462 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004463 if (*line == '#' || *line == '\n' || *line == '\r') {
4464 /* end of string, end of loop */
4465 *line = 0;
4466 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004467 } else if (isspace(*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02004468 newarg = 1;
4469 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004470 } else if (*line == '[') {
4471 if (ssl_b) {
4472 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004473 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004474 break;
4475 }
4476 if (!arg) {
4477 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004478 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004479 break;
4480 }
4481 ssl_b = arg;
4482 newarg = 1;
4483 *line = 0;
4484 } else if (*line == ']') {
4485 if (ssl_e) {
4486 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004487 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004488 break;
4489 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004490 if (!ssl_b) {
4491 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004492 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004493 break;
4494 }
4495 ssl_e = arg;
4496 newarg = 1;
4497 *line = 0;
4498 } else if (newarg) {
4499 if (arg == MAX_CRT_ARGS) {
4500 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004501 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004502 break;
4503 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004504 newarg = 0;
4505 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004506 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004507 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004508 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004509 if (cfgerr)
4510 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004511 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004512
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004513 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004514 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004515 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004516
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004517 crt_path = args[0];
4518 if (*crt_path != '/' && global_ssl.crt_base) {
4519 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4520 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4521 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004522 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004523 break;
4524 }
4525 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4526 crt_path = path;
4527 }
4528
4529 ssl_conf = calloc(1, sizeof *ssl_conf);
4530 cur_arg = ssl_b ? ssl_b : 1;
4531 while (cur_arg < ssl_e) {
4532 newarg = 0;
4533 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4534 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4535 newarg = 1;
Willy Tarreaubbc91962019-10-16 16:42:19 +02004536 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004537 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4538 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4539 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004540 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004541 }
4542 cur_arg += 1 + ssl_bind_kws[i].skip;
4543 break;
4544 }
4545 }
4546 if (!cfgerr && !newarg) {
4547 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4548 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004549 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004550 break;
4551 }
4552 }
Willy Tarreaubbc91962019-10-16 16:42:19 +02004553
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004554 if (cfgerr) {
4555 ssl_sock_free_ssl_conf(ssl_conf);
4556 free(ssl_conf);
4557 ssl_conf = NULL;
4558 break;
4559 }
4560
William Lallemande3af8fb2019-10-08 11:36:53 +02004561 if ((ckchs = ckchs_lookup(crt_path)) == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004562 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004563 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004564 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004565 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004566 }
4567
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004568 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004569 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004570 else
4571 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 +02004572
Willy Tarreauad1731d2013-04-02 17:35:58 +02004573 if (cfgerr) {
4574 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004575 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004576 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004577 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004578 fclose(f);
4579 return cfgerr;
4580}
4581
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004582/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004583static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004584ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004585{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004586 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004587 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004588 SSL_OP_ALL | /* all known workarounds for bugs */
4589 SSL_OP_NO_SSLv2 |
4590 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004591 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02004592 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004593 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02004594 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004595 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004596 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004597 SSL_MODE_ENABLE_PARTIAL_WRITE |
4598 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004599 SSL_MODE_RELEASE_BUFFERS |
4600 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004601 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004602 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004603 int flags = MC_SSL_O_ALL;
4604 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004605
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004606 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004607 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004608
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004609 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004610 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
4611 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4612 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004613 else
4614 flags = conf_ssl_methods->flags;
4615
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004616 min = conf_ssl_methods->min;
4617 max = conf_ssl_methods->max;
4618 /* start with TLSv10 to remove SSLv3 per default */
4619 if (!min && (!max || max >= CONF_TLSV10))
4620 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004621 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004622 if (min)
4623 flags |= (methodVersions[min].flag - 1);
4624 if (max)
4625 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004626 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004627 min = max = CONF_TLSV_NONE;
4628 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004629 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004630 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004631 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004632 if (min) {
4633 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004634 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
4635 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4636 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
4637 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004638 hole = 0;
4639 }
4640 max = i;
4641 }
4642 else {
4643 min = max = i;
4644 }
4645 }
4646 else {
4647 if (min)
4648 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004649 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004650 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004651 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4652 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004653 cfgerr += 1;
4654 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004655 /* save real min/max in bind_conf */
4656 conf_ssl_methods->min = min;
4657 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004658
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004659#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004660 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004661 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004662 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004663 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004664 else
4665 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4666 if (flags & methodVersions[i].flag)
4667 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004668#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004669 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004670 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4671 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004672#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004673
4674 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4675 options |= SSL_OP_NO_TICKET;
4676 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4677 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004678
4679#ifdef SSL_OP_NO_RENEGOTIATION
4680 options |= SSL_OP_NO_RENEGOTIATION;
4681#endif
4682
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004683 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004684
Willy Tarreau5db847a2019-05-09 14:13:35 +02004685#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004686 if (global_ssl.async)
4687 mode |= SSL_MODE_ASYNC;
4688#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004689 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004690 if (global_ssl.life_time)
4691 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004692
4693#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4694#ifdef OPENSSL_IS_BORINGSSL
4695 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4696 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05004697#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01004698 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004699 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004700 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4701 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004702#else
4703 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004704#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004705 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004706#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004707 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004708}
4709
William Lallemand4f45bb92017-10-30 20:08:51 +01004710
4711static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4712{
4713 if (first == block) {
4714 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4715 if (first->len > 0)
4716 sh_ssl_sess_tree_delete(sh_ssl_sess);
4717 }
4718}
4719
4720/* return first block from sh_ssl_sess */
4721static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4722{
4723 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4724
4725}
4726
4727/* store a session into the cache
4728 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4729 * data: asn1 encoded session
4730 * data_len: asn1 encoded session length
4731 * Returns 1 id session was stored (else 0)
4732 */
4733static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4734{
4735 struct shared_block *first;
4736 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4737
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004738 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004739 if (!first) {
4740 /* Could not retrieve enough free blocks to store that session */
4741 return 0;
4742 }
4743
4744 /* STORE the key in the first elem */
4745 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4746 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4747 first->len = sizeof(struct sh_ssl_sess_hdr);
4748
4749 /* it returns the already existing node
4750 or current node if none, never returns null */
4751 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4752 if (oldsh_ssl_sess != sh_ssl_sess) {
4753 /* NOTE: Row couldn't be in use because we lock read & write function */
4754 /* release the reserved row */
4755 shctx_row_dec_hot(ssl_shctx, first);
4756 /* replace the previous session already in the tree */
4757 sh_ssl_sess = oldsh_ssl_sess;
4758 /* ignore the previous session data, only use the header */
4759 first = sh_ssl_sess_first_block(sh_ssl_sess);
4760 shctx_row_inc_hot(ssl_shctx, first);
4761 first->len = sizeof(struct sh_ssl_sess_hdr);
4762 }
4763
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004764 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004765 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004766 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004767 }
4768
4769 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004770
4771 return 1;
4772}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004773
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004774/* SSL callback used when a new session is created while connecting to a server */
4775static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4776{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004777 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004778 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004779
Willy Tarreau07d94e42018-09-20 10:57:52 +02004780 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004781
Olivier Houcharde6060c52017-11-16 17:42:52 +01004782 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4783 int len;
4784 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004785
Olivier Houcharde6060c52017-11-16 17:42:52 +01004786 len = i2d_SSL_SESSION(sess, NULL);
4787 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4788 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4789 } else {
4790 free(s->ssl_ctx.reused_sess[tid].ptr);
4791 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4792 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4793 }
4794 if (s->ssl_ctx.reused_sess[tid].ptr) {
4795 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4796 &ptr);
4797 }
4798 } else {
4799 free(s->ssl_ctx.reused_sess[tid].ptr);
4800 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4801 }
4802
4803 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004804}
4805
Olivier Houcharde6060c52017-11-16 17:42:52 +01004806
William Lallemanded0b5ad2017-10-30 19:36:36 +01004807/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004808int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004809{
4810 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4811 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4812 unsigned char *p;
4813 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02004814 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004815 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004816
4817 /* Session id is already stored in to key and session id is known
4818 * so we dont store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02004819 * note: SSL_SESSION_set1_id is using
4820 * a memcpy so we need to use a different pointer
4821 * than sid_data or sid_ctx_data to avoid valgrind
4822 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004823 */
4824
4825 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02004826
4827 /* copy value in an other buffer */
4828 memcpy(encid, sid_data, sid_length);
4829
4830 /* pad with 0 */
4831 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4832 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4833
4834 /* force length to zero to avoid ASN1 encoding */
4835 SSL_SESSION_set1_id(sess, encid, 0);
4836
4837 /* force length to zero to avoid ASN1 encoding */
4838 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004839
4840 /* check if buffer is large enough for the ASN1 encoded session */
4841 data_len = i2d_SSL_SESSION(sess, NULL);
4842 if (data_len > SHSESS_MAX_DATA_LEN)
4843 goto err;
4844
4845 p = encsess;
4846
4847 /* process ASN1 session encoding before the lock */
4848 i2d_SSL_SESSION(sess, &p);
4849
William Lallemanded0b5ad2017-10-30 19:36:36 +01004850
William Lallemanda3c77cf2017-10-30 23:44:40 +01004851 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004852 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004853 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004854 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004855err:
4856 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02004857 SSL_SESSION_set1_id(sess, encid, sid_length);
4858 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004859
4860 return 0; /* do not increment session reference count */
4861}
4862
4863/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004864SSL_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 +01004865{
William Lallemand4f45bb92017-10-30 20:08:51 +01004866 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004867 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4868 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004869 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004870 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004871
4872 global.shctx_lookups++;
4873
4874 /* allow the session to be freed automatically by openssl */
4875 *do_copy = 0;
4876
4877 /* tree key is zeros padded sessionid */
4878 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4879 memcpy(tmpkey, key, key_len);
4880 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4881 key = tmpkey;
4882 }
4883
4884 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004885 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004886
4887 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004888 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4889 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004890 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004891 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004892 global.shctx_misses++;
4893 return NULL;
4894 }
4895
William Lallemand4f45bb92017-10-30 20:08:51 +01004896 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4897 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004898
William Lallemand4f45bb92017-10-30 20:08:51 +01004899 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 +01004900
William Lallemanda3c77cf2017-10-30 23:44:40 +01004901 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004902
4903 /* decode ASN1 session */
4904 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004905 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004906 /* Reset session id and session id contenxt */
4907 if (sess) {
4908 SSL_SESSION_set1_id(sess, key, key_len);
4909 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4910 }
4911
4912 return sess;
4913}
4914
William Lallemand4f45bb92017-10-30 20:08:51 +01004915
William Lallemanded0b5ad2017-10-30 19:36:36 +01004916/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004917void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004918{
William Lallemand4f45bb92017-10-30 20:08:51 +01004919 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004920 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4921 unsigned int sid_length;
4922 const unsigned char *sid_data;
4923 (void)ctx;
4924
4925 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4926 /* tree key is zeros padded sessionid */
4927 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4928 memcpy(tmpkey, sid_data, sid_length);
4929 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4930 sid_data = tmpkey;
4931 }
4932
William Lallemanda3c77cf2017-10-30 23:44:40 +01004933 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004934
4935 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004936 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4937 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004938 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004939 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004940 }
4941
4942 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004943 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004944}
4945
4946/* Set session cache mode to server and disable openssl internal cache.
4947 * Set shared cache callbacks on an ssl context.
4948 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004949void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004950{
4951 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4952
4953 if (!ssl_shctx) {
4954 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4955 return;
4956 }
4957
4958 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4959 SSL_SESS_CACHE_NO_INTERNAL |
4960 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4961
4962 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004963 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4964 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4965 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004966}
4967
William Lallemand8b453912019-11-21 15:48:10 +01004968/*
4969 * This function applies the SSL configuration on a SSL_CTX
4970 * It returns an error code and fills the <err> buffer
4971 */
4972int 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 +01004973{
4974 struct proxy *curproxy = bind_conf->frontend;
4975 int cfgerr = 0;
4976 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004977 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004978 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004979#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004980 const char *conf_ciphersuites;
4981#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004982 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004983
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004984 if (ssl_conf) {
4985 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4986 int i, min, max;
4987 int flags = MC_SSL_O_ALL;
4988
4989 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004990 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4991 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004992 if (min)
4993 flags |= (methodVersions[min].flag - 1);
4994 if (max)
4995 flags |= ~((methodVersions[max].flag << 1) - 1);
4996 min = max = CONF_TLSV_NONE;
4997 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4998 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4999 if (min)
5000 max = i;
5001 else
5002 min = max = i;
5003 }
5004 /* save real min/max */
5005 conf_ssl_methods->min = min;
5006 conf_ssl_methods->max = max;
5007 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005008 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5009 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005010 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005011 }
5012 }
5013
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005014 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005015 case SSL_SOCK_VERIFY_NONE:
5016 verify = SSL_VERIFY_NONE;
5017 break;
5018 case SSL_SOCK_VERIFY_OPTIONAL:
5019 verify = SSL_VERIFY_PEER;
5020 break;
5021 case SSL_SOCK_VERIFY_REQUIRED:
5022 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5023 break;
5024 }
5025 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5026 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005027 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
5028 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
5029 if (ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005030 /* set CAfile to verify */
5031 if (!ssl_set_verify_locations_file(ctx, ca_file)) {
5032 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005033 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005034 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005035 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005036 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
5037 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005038 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 +02005039 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005040 }
Emeric Brun850efd52014-01-29 12:24:34 +01005041 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005042 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5043 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005044 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005045 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005046#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005047 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005048 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5049
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005050 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005051 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5052 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005053 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005054 }
Emeric Brun561e5742012-10-02 15:20:55 +02005055 else {
5056 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5057 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005058 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005059#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005060 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005061 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005062#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005063 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005064 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005065 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5066 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005067 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005068 }
5069 }
5070#endif
5071
William Lallemand4f45bb92017-10-30 20:08:51 +01005072 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005073 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5074 if (conf_ciphers &&
5075 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005076 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5077 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005078 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005079 }
5080
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005081#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005082 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5083 if (conf_ciphersuites &&
5084 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005085 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5086 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005087 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005088 }
5089#endif
5090
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005091#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005092 /* If tune.ssl.default-dh-param has not been set,
5093 neither has ssl-default-dh-file and no static DH
5094 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005095 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005096 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005097 (ssl_dh_ptr_index == -1 ||
5098 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005099 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5100 const SSL_CIPHER * cipher = NULL;
5101 char cipher_description[128];
5102 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5103 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5104 which is not ephemeral DH. */
5105 const char dhe_description[] = " Kx=DH ";
5106 const char dhe_export_description[] = " Kx=DH(";
5107 int idx = 0;
5108 int dhe_found = 0;
5109 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005110
Remi Gacogne23d5d372014-10-10 17:04:26 +02005111 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005112
Remi Gacogne23d5d372014-10-10 17:04:26 +02005113 if (ssl) {
5114 ciphers = SSL_get_ciphers(ssl);
5115
5116 if (ciphers) {
5117 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5118 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5119 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5120 if (strstr(cipher_description, dhe_description) != NULL ||
5121 strstr(cipher_description, dhe_export_description) != NULL) {
5122 dhe_found = 1;
5123 break;
5124 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005125 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005126 }
5127 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005128 SSL_free(ssl);
5129 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005130 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005131
Lukas Tribus90132722014-08-18 00:56:33 +02005132 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005133 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",
5134 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005135 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005136 }
5137
Willy Tarreauef934602016-12-22 23:12:01 +01005138 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005139 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005140
Willy Tarreauef934602016-12-22 23:12:01 +01005141 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005142 if (local_dh_1024 == NULL) {
5143 local_dh_1024 = ssl_get_dh_1024();
5144 }
Willy Tarreauef934602016-12-22 23:12:01 +01005145 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005146 if (local_dh_2048 == NULL) {
5147 local_dh_2048 = ssl_get_dh_2048();
5148 }
Willy Tarreauef934602016-12-22 23:12:01 +01005149 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005150 if (local_dh_4096 == NULL) {
5151 local_dh_4096 = ssl_get_dh_4096();
5152 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005153 }
5154 }
5155 }
5156#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005157
Emeric Brunfc0421f2012-09-07 17:30:07 +02005158 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005159#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005160 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005161#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005162
Bernard Spil13c53f82018-02-15 13:34:58 +01005163#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005164 ssl_conf_cur = NULL;
5165 if (ssl_conf && ssl_conf->npn_str)
5166 ssl_conf_cur = ssl_conf;
5167 else if (bind_conf->ssl_conf.npn_str)
5168 ssl_conf_cur = &bind_conf->ssl_conf;
5169 if (ssl_conf_cur)
5170 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005171#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005172#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005173 ssl_conf_cur = NULL;
5174 if (ssl_conf && ssl_conf->alpn_str)
5175 ssl_conf_cur = ssl_conf;
5176 else if (bind_conf->ssl_conf.alpn_str)
5177 ssl_conf_cur = &bind_conf->ssl_conf;
5178 if (ssl_conf_cur)
5179 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005180#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005181#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005182 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5183 if (conf_curves) {
5184 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005185 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5186 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005187 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005188 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005189 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005190 }
5191#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005192#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005193 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005194 int i;
5195 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005196#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005197 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005198 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5199 NULL);
5200
5201 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005202 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005203 return cfgerr;
5204 }
5205#else
5206 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5207 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5208 ECDHE_DEFAULT_CURVE);
5209#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005210
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005211 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005212 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005213 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5214 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005215 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005216 }
5217 else {
5218 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5219 EC_KEY_free(ecdh);
5220 }
5221 }
5222#endif
5223
Emeric Brunfc0421f2012-09-07 17:30:07 +02005224 return cfgerr;
5225}
5226
Evan Broderbe554312013-06-27 00:05:25 -07005227static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5228{
5229 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5230 size_t prefixlen, suffixlen;
5231
5232 /* Trivial case */
5233 if (strcmp(pattern, hostname) == 0)
5234 return 1;
5235
Evan Broderbe554312013-06-27 00:05:25 -07005236 /* The rest of this logic is based on RFC 6125, section 6.4.3
5237 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5238
Emeric Bruna848dae2013-10-08 11:27:28 +02005239 pattern_wildcard = NULL;
5240 pattern_left_label_end = pattern;
5241 while (*pattern_left_label_end != '.') {
5242 switch (*pattern_left_label_end) {
5243 case 0:
5244 /* End of label not found */
5245 return 0;
5246 case '*':
5247 /* If there is more than one wildcards */
5248 if (pattern_wildcard)
5249 return 0;
5250 pattern_wildcard = pattern_left_label_end;
5251 break;
5252 }
5253 pattern_left_label_end++;
5254 }
5255
5256 /* If it's not trivial and there is no wildcard, it can't
5257 * match */
5258 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005259 return 0;
5260
5261 /* Make sure all labels match except the leftmost */
5262 hostname_left_label_end = strchr(hostname, '.');
5263 if (!hostname_left_label_end
5264 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5265 return 0;
5266
5267 /* Make sure the leftmost label of the hostname is long enough
5268 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005269 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005270 return 0;
5271
5272 /* Finally compare the string on either side of the
5273 * wildcard */
5274 prefixlen = pattern_wildcard - pattern;
5275 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005276 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5277 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005278 return 0;
5279
5280 return 1;
5281}
5282
5283static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5284{
5285 SSL *ssl;
5286 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005287 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005288 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005289 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005290
5291 int depth;
5292 X509 *cert;
5293 STACK_OF(GENERAL_NAME) *alt_names;
5294 int i;
5295 X509_NAME *cert_subject;
5296 char *str;
5297
5298 if (ok == 0)
5299 return ok;
5300
5301 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005302 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005303 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005304
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005305 /* We're checking if the provided hostnames match the desired one. The
5306 * desired hostname comes from the SNI we presented if any, or if not
5307 * provided then it may have been explicitly stated using a "verifyhost"
5308 * directive. If neither is set, we don't care about the name so the
5309 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005310 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005311 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005312 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005313 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005314 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005315 if (!servername)
5316 return ok;
5317 }
Evan Broderbe554312013-06-27 00:05:25 -07005318
5319 /* We only need to verify the CN on the actual server cert,
5320 * not the indirect CAs */
5321 depth = X509_STORE_CTX_get_error_depth(ctx);
5322 if (depth != 0)
5323 return ok;
5324
5325 /* At this point, the cert is *not* OK unless we can find a
5326 * hostname match */
5327 ok = 0;
5328
5329 cert = X509_STORE_CTX_get_current_cert(ctx);
5330 /* It seems like this might happen if verify peer isn't set */
5331 if (!cert)
5332 return ok;
5333
5334 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5335 if (alt_names) {
5336 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5337 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5338 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005339#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005340 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5341#else
Evan Broderbe554312013-06-27 00:05:25 -07005342 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005343#endif
Evan Broderbe554312013-06-27 00:05:25 -07005344 ok = ssl_sock_srv_hostcheck(str, servername);
5345 OPENSSL_free(str);
5346 }
5347 }
5348 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005349 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005350 }
5351
5352 cert_subject = X509_get_subject_name(cert);
5353 i = -1;
5354 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5355 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005356 ASN1_STRING *value;
5357 value = X509_NAME_ENTRY_get_data(entry);
5358 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005359 ok = ssl_sock_srv_hostcheck(str, servername);
5360 OPENSSL_free(str);
5361 }
5362 }
5363
Willy Tarreau71d058c2017-07-26 20:09:56 +02005364 /* report the mismatch and indicate if SNI was used or not */
5365 if (!ok && !conn->err_code)
5366 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005367 return ok;
5368}
5369
Emeric Brun94324a42012-10-11 14:00:19 +02005370/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005371int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005372{
Willy Tarreau03209342016-12-22 17:08:28 +01005373 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005374 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005375 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005376 SSL_OP_ALL | /* all known workarounds for bugs */
5377 SSL_OP_NO_SSLv2 |
5378 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005379 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005380 SSL_MODE_ENABLE_PARTIAL_WRITE |
5381 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005382 SSL_MODE_RELEASE_BUFFERS |
5383 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005384 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005385 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005386 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005387 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005388 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005389
Thierry Fournier383085f2013-01-24 14:15:43 +01005390 /* Make sure openssl opens /dev/urandom before the chroot */
5391 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005392 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005393 cfgerr++;
5394 }
5395
Willy Tarreaufce03112015-01-15 21:32:40 +01005396 /* Automatic memory computations need to know we use SSL there */
5397 global.ssl_used_backend = 1;
5398
5399 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005400 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005401 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005402 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5403 curproxy->id, srv->id,
5404 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005405 cfgerr++;
5406 return cfgerr;
5407 }
5408 }
Emeric Brun94324a42012-10-11 14:00:19 +02005409 if (srv->use_ssl)
5410 srv->xprt = &ssl_sock;
5411 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005412 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005413
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005414 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005415 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005416 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5417 proxy_type_str(curproxy), curproxy->id,
5418 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005419 cfgerr++;
5420 return cfgerr;
5421 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005422
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005423 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005424 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5425 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5426 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005427 else
5428 flags = conf_ssl_methods->flags;
5429
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005430 /* Real min and max should be determinate with configuration and openssl's capabilities */
5431 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005432 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005433 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005434 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005435
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005436 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005437 min = max = CONF_TLSV_NONE;
5438 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005439 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005440 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005441 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005442 if (min) {
5443 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005444 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5445 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5446 proxy_type_str(curproxy), curproxy->id, srv->id,
5447 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005448 hole = 0;
5449 }
5450 max = i;
5451 }
5452 else {
5453 min = max = i;
5454 }
5455 }
5456 else {
5457 if (min)
5458 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005459 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005460 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005461 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5462 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005463 cfgerr += 1;
5464 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005465
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005466#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005467 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005468 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005469 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005470 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005471 else
5472 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5473 if (flags & methodVersions[i].flag)
5474 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005475#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005476 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005477 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5478 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005479#endif
5480
5481 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5482 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005483 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005484
Willy Tarreau5db847a2019-05-09 14:13:35 +02005485#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005486 if (global_ssl.async)
5487 mode |= SSL_MODE_ASYNC;
5488#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005489 SSL_CTX_set_mode(ctx, mode);
5490 srv->ssl_ctx.ctx = ctx;
5491
Emeric Bruna7aa3092012-10-26 12:58:00 +02005492 if (srv->ssl_ctx.client_crt) {
5493 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 +01005494 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5495 proxy_type_str(curproxy), curproxy->id,
5496 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005497 cfgerr++;
5498 }
5499 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 +01005500 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5501 proxy_type_str(curproxy), curproxy->id,
5502 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005503 cfgerr++;
5504 }
5505 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005506 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5507 proxy_type_str(curproxy), curproxy->id,
5508 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005509 cfgerr++;
5510 }
5511 }
Emeric Brun94324a42012-10-11 14:00:19 +02005512
Emeric Brun850efd52014-01-29 12:24:34 +01005513 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5514 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005515 switch (srv->ssl_ctx.verify) {
5516 case SSL_SOCK_VERIFY_NONE:
5517 verify = SSL_VERIFY_NONE;
5518 break;
5519 case SSL_SOCK_VERIFY_REQUIRED:
5520 verify = SSL_VERIFY_PEER;
5521 break;
5522 }
Evan Broderbe554312013-06-27 00:05:25 -07005523 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005524 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005525 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005526 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005527 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005528 /* set CAfile to verify */
5529 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
5530 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01005531 curproxy->id, srv->id,
5532 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005533 cfgerr++;
5534 }
5535 }
Emeric Brun850efd52014-01-29 12:24:34 +01005536 else {
5537 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01005538 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",
5539 curproxy->id, srv->id,
5540 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005541 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01005542 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
5543 curproxy->id, srv->id,
5544 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005545 cfgerr++;
5546 }
Emeric Brunef42d922012-10-11 16:11:36 +02005547#ifdef X509_V_FLAG_CRL_CHECK
5548 if (srv->ssl_ctx.crl_file) {
5549 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
5550
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005551 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005552 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
5553 curproxy->id, srv->id,
5554 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005555 cfgerr++;
5556 }
5557 else {
5558 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5559 }
5560 }
5561#endif
5562 }
5563
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005564 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
5565 SSL_SESS_CACHE_NO_INTERNAL_STORE);
5566 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02005567 if (srv->ssl_ctx.ciphers &&
5568 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005569 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
5570 curproxy->id, srv->id,
5571 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02005572 cfgerr++;
5573 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005574
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005575#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005576 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00005577 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005578 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
5579 curproxy->id, srv->id,
5580 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
5581 cfgerr++;
5582 }
5583#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01005584#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
5585 if (srv->ssl_ctx.npn_str)
5586 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
5587#endif
5588#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5589 if (srv->ssl_ctx.alpn_str)
5590 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
5591#endif
5592
Emeric Brun94324a42012-10-11 14:00:19 +02005593
5594 return cfgerr;
5595}
5596
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005597/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005598 * be NULL, in which case nothing is done. Returns the number of errors
5599 * encountered.
5600 */
Willy Tarreau03209342016-12-22 17:08:28 +01005601int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005602{
5603 struct ebmb_node *node;
5604 struct sni_ctx *sni;
5605 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01005606 int errcode = 0;
5607 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005608
Willy Tarreaufce03112015-01-15 21:32:40 +01005609 /* Automatic memory computations need to know we use SSL there */
5610 global.ssl_used_frontend = 1;
5611
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005612 /* Make sure openssl opens /dev/urandom before the chroot */
5613 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005614 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005615 err++;
5616 }
5617 /* Create initial_ctx used to start the ssl connection before do switchctx */
5618 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005619 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005620 /* It should not be necessary to call this function, but it's
5621 necessary first to check and move all initialisation related
5622 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01005623 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005624 }
Emeric Brun0bed9942014-10-30 19:25:24 +01005625 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01005626 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01005627
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005628 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005629 while (node) {
5630 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01005631 if (!sni->order && sni->ctx != bind_conf->default_ctx)
5632 /* only initialize the CTX on its first occurrence and
5633 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005634 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005635 node = ebmb_next(node);
5636 }
5637
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005638 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005639 while (node) {
5640 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01005641 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01005642 /* only initialize the CTX on its first occurrence and
5643 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005644 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
5645 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005646 node = ebmb_next(node);
5647 }
William Lallemand8b453912019-11-21 15:48:10 +01005648
5649 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005650 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005651 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005652 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005653 err++;
5654 }
5655
5656 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005657 return err;
5658}
5659
Willy Tarreau55d37912016-12-21 23:38:39 +01005660/* Prepares all the contexts for a bind_conf and allocates the shared SSL
5661 * context if needed. Returns < 0 on error, 0 on success. The warnings and
5662 * alerts are directly emitted since the rest of the stack does it below.
5663 */
5664int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
5665{
5666 struct proxy *px = bind_conf->frontend;
5667 int alloc_ctx;
5668 int err;
5669
5670 if (!bind_conf->is_ssl) {
5671 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005672 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
5673 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01005674 }
5675 return 0;
5676 }
5677 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005678 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005679 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
5680 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005681 }
5682 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005683 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
5684 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005685 return -1;
5686 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005687 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005688 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005689 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005690 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01005691 sizeof(*sh_ssl_sess_tree),
5692 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005693 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005694 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5695 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");
5696 else
5697 ha_alert("Unable to allocate SSL session cache.\n");
5698 return -1;
5699 }
5700 /* free block callback */
5701 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5702 /* init the root tree within the extra space */
5703 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5704 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005705 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005706 err = 0;
5707 /* initialize all certificate contexts */
5708 err += ssl_sock_prepare_all_ctx(bind_conf);
5709
5710 /* initialize CA variables if the certificates generation is enabled */
5711 err += ssl_sock_load_ca(bind_conf);
5712
5713 return -err;
5714}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005715
5716/* release ssl context allocated for servers. */
5717void ssl_sock_free_srv_ctx(struct server *srv)
5718{
Olivier Houchardc7566002018-11-20 23:33:50 +01005719#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5720 if (srv->ssl_ctx.alpn_str)
5721 free(srv->ssl_ctx.alpn_str);
5722#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005723#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01005724 if (srv->ssl_ctx.npn_str)
5725 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005726#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005727 if (srv->ssl_ctx.ctx)
5728 SSL_CTX_free(srv->ssl_ctx.ctx);
5729}
5730
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005731/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005732 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5733 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005734void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005735{
5736 struct ebmb_node *node, *back;
5737 struct sni_ctx *sni;
5738
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005739 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005740 while (node) {
5741 sni = ebmb_entry(node, struct sni_ctx, name);
5742 back = ebmb_next(node);
5743 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005744 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005745 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005746 ssl_sock_free_ssl_conf(sni->conf);
5747 free(sni->conf);
5748 sni->conf = NULL;
5749 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005750 free(sni);
5751 node = back;
5752 }
5753
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005754 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005755 while (node) {
5756 sni = ebmb_entry(node, struct sni_ctx, name);
5757 back = ebmb_next(node);
5758 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005759 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005760 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005761 ssl_sock_free_ssl_conf(sni->conf);
5762 free(sni->conf);
5763 sni->conf = NULL;
5764 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005765 free(sni);
5766 node = back;
5767 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005768 SSL_CTX_free(bind_conf->initial_ctx);
5769 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005770 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005771 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005772}
5773
Willy Tarreau795cdab2016-12-22 17:30:54 +01005774/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5775void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5776{
5777 ssl_sock_free_ca(bind_conf);
5778 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005779 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005780 free(bind_conf->ca_sign_file);
5781 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005782 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005783 free(bind_conf->keys_ref->filename);
5784 free(bind_conf->keys_ref->tlskeys);
5785 LIST_DEL(&bind_conf->keys_ref->list);
5786 free(bind_conf->keys_ref);
5787 }
5788 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005789 bind_conf->ca_sign_pass = NULL;
5790 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005791}
5792
Christopher Faulet31af49d2015-06-09 17:29:50 +02005793/* Load CA cert file and private key used to generate certificates */
5794int
Willy Tarreau03209342016-12-22 17:08:28 +01005795ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005796{
Willy Tarreau03209342016-12-22 17:08:28 +01005797 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005798 FILE *fp;
5799 X509 *cacert = NULL;
5800 EVP_PKEY *capkey = NULL;
5801 int err = 0;
5802
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005803 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005804 return err;
5805
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005806#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005807 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005808 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005809 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005810 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005811 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005812#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005813
Christopher Faulet31af49d2015-06-09 17:29:50 +02005814 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005815 ha_alert("Proxy '%s': cannot enable certificate generation, "
5816 "no CA certificate File configured at [%s:%d].\n",
5817 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005818 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005819 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005820
5821 /* read in the CA certificate */
5822 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005823 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5824 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005825 goto load_error;
5826 }
5827 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005828 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5829 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005830 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005831 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005832 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005833 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005834 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5835 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005836 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005837 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005838
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005839 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005840 bind_conf->ca_sign_cert = cacert;
5841 bind_conf->ca_sign_pkey = capkey;
5842 return err;
5843
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005844 read_error:
5845 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005846 if (capkey) EVP_PKEY_free(capkey);
5847 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005848 load_error:
5849 bind_conf->generate_certs = 0;
5850 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005851 return err;
5852}
5853
5854/* Release CA cert and private key used to generate certificated */
5855void
5856ssl_sock_free_ca(struct bind_conf *bind_conf)
5857{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005858 if (bind_conf->ca_sign_pkey)
5859 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5860 if (bind_conf->ca_sign_cert)
5861 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005862 bind_conf->ca_sign_pkey = NULL;
5863 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005864}
5865
Emeric Brun46591952012-05-18 15:47:34 +02005866/*
5867 * This function is called if SSL * context is not yet allocated. The function
5868 * is designed to be called before any other data-layer operation and sets the
5869 * handshake flag on the connection. It is safe to call it multiple times.
5870 * It returns 0 on success and -1 in error case.
5871 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005872static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005873{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005874 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005875 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005876 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005877 return 0;
5878
Willy Tarreau3c728722014-01-23 13:50:42 +01005879 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005880 return 0;
5881
Olivier Houchard66ab4982019-02-26 18:37:15 +01005882 ctx = pool_alloc(ssl_sock_ctx_pool);
5883 if (!ctx) {
5884 conn->err_code = CO_ER_SSL_NO_MEM;
5885 return -1;
5886 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005887 ctx->wait_event.tasklet = tasklet_new();
5888 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005889 conn->err_code = CO_ER_SSL_NO_MEM;
5890 pool_free(ssl_sock_ctx_pool, ctx);
5891 return -1;
5892 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005893 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5894 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005895 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005896 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01005897 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005898 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01005899 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02005900 ctx->xprt_st = 0;
5901 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005902
5903 /* Only work with sockets for now, this should be adapted when we'll
5904 * add QUIC support.
5905 */
5906 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005907 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005908 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5909 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005910 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005911
Willy Tarreau20879a02012-12-03 16:32:10 +01005912 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5913 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005914 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005915 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005916
Emeric Brun46591952012-05-18 15:47:34 +02005917 /* If it is in client mode initiate SSL session
5918 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005919 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005920 int may_retry = 1;
5921
5922 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005923 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005924 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5925 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005926 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005927 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005928 goto retry_connect;
5929 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005930 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005931 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005932 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005933 ctx->bio = BIO_new(ha_meth);
5934 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005935 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005936 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005937 goto retry_connect;
5938 }
Emeric Brun55476152014-11-12 17:35:37 +01005939 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005940 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005941 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005942 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005943 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005944
Evan Broderbe554312013-06-27 00:05:25 -07005945 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005946 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5947 SSL_free(ctx->ssl);
5948 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005949 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005950 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005951 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005952 goto retry_connect;
5953 }
Emeric Brun55476152014-11-12 17:35:37 +01005954 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005955 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005956 }
5957
Olivier Houchard66ab4982019-02-26 18:37:15 +01005958 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005959 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5960 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5961 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 +01005962 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005963 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005964 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5965 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005966 } else if (sess) {
5967 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005968 }
5969 }
Evan Broderbe554312013-06-27 00:05:25 -07005970
Emeric Brun46591952012-05-18 15:47:34 +02005971 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005972 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005973
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005974 _HA_ATOMIC_ADD(&sslconns, 1);
5975 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005976 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005977 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005978 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005979 if (conn->flags & CO_FL_ERROR)
5980 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005981 return 0;
5982 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005983 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005984 int may_retry = 1;
5985
5986 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005987 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005988 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5989 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005990 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005991 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005992 goto retry_accept;
5993 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005994 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005995 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005996 }
Olivier Houchard545989f2019-12-17 15:39:54 +01005997#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard54907bb2019-12-19 15:02:39 +01005998 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
5999 b_alloc(&ctx->early_buf);
6000 SSL_set_max_early_data(ctx->ssl,
6001 /* Only allow early data if we managed to allocate
6002 * a buffer.
6003 */
6004 (!b_is_null(&ctx->early_buf)) ?
6005 global.tune.bufsize - global.tune.maxrewrite : 0);
6006 }
Olivier Houchard545989f2019-12-17 15:39:54 +01006007#endif
Emeric Brun46591952012-05-18 15:47:34 +02006008
Olivier Houcharda8955d52019-04-07 22:00:38 +02006009 ctx->bio = BIO_new(ha_meth);
6010 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006011 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006012 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006013 goto retry_accept;
6014 }
Emeric Brun55476152014-11-12 17:35:37 +01006015 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006016 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006017 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006018 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006019 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006020
Emeric Brune1f38db2012-09-03 20:36:47 +02006021 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006022 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6023 SSL_free(ctx->ssl);
6024 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006025 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006026 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006027 goto retry_accept;
6028 }
Emeric Brun55476152014-11-12 17:35:37 +01006029 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006030 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006031 }
6032
Olivier Houchard66ab4982019-02-26 18:37:15 +01006033 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006034
Emeric Brun46591952012-05-18 15:47:34 +02006035 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006036 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006037#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006038 conn->flags |= CO_FL_EARLY_SSL_HS;
6039#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006040
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006041 _HA_ATOMIC_ADD(&sslconns, 1);
6042 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006043 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006044 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006045 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006046 if (conn->flags & CO_FL_ERROR)
6047 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02006048 return 0;
6049 }
6050 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006051 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006052err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006053 if (ctx && ctx->wait_event.tasklet)
6054 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006055 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006056 return -1;
6057}
6058
6059
6060/* This is the callback which is used when an SSL handshake is pending. It
6061 * updates the FD status if it wants some polling before being called again.
6062 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6063 * otherwise it returns non-zero and removes itself from the connection's
6064 * flags (the bit is provided in <flag> by the caller).
6065 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006066static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006067{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006068 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006069 int ret;
6070
Willy Tarreau3c728722014-01-23 13:50:42 +01006071 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006072 return 0;
6073
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006074 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006075 goto out_error;
6076
Willy Tarreau5db847a2019-05-09 14:13:35 +02006077#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006078 /*
6079 * Check if we have early data. If we do, we have to read them
6080 * before SSL_do_handshake() is called, And there's no way to
6081 * detect early data, except to try to read them
6082 */
6083 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006084 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006085
Olivier Houchard54907bb2019-12-19 15:02:39 +01006086 while (1) {
6087 ret = SSL_read_early_data(ctx->ssl,
6088 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6089 &read_data);
6090 if (ret == SSL_READ_EARLY_DATA_ERROR)
6091 goto check_error;
6092 if (read_data > 0) {
6093 conn->flags |= CO_FL_EARLY_DATA;
6094 b_add(&ctx->early_buf, read_data);
6095 }
6096 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6097 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6098 if (!b_data(&ctx->early_buf))
6099 b_free(&ctx->early_buf);
6100 break;
6101 }
6102 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006103 }
6104#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006105 /* If we use SSL_do_handshake to process a reneg initiated by
6106 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6107 * Usually SSL_write and SSL_read are used and process implicitly
6108 * the reneg handshake.
6109 * Here we use SSL_peek as a workaround for reneg.
6110 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006111 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006112 char c;
6113
Olivier Houchard66ab4982019-02-26 18:37:15 +01006114 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006115 if (ret <= 0) {
6116 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006117 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006118
Emeric Brun674b7432012-11-08 19:21:55 +01006119 if (ret == SSL_ERROR_WANT_WRITE) {
6120 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006121 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006122 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006123 return 0;
6124 }
6125 else if (ret == SSL_ERROR_WANT_READ) {
6126 /* handshake may have been completed but we have
6127 * no more data to read.
6128 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006129 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006130 ret = 1;
6131 goto reneg_ok;
6132 }
6133 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006134 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006135 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006136 return 0;
6137 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006138#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006139 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006140 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006141 return 0;
6142 }
6143#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006144 else if (ret == SSL_ERROR_SYSCALL) {
6145 /* if errno is null, then connection was successfully established */
6146 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6147 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006148 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006149#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6150 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006151 conn->err_code = CO_ER_SSL_HANDSHAKE;
6152#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006153 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006154#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006155 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006156 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006157 empty_handshake = state == TLS_ST_BEFORE;
6158#else
Lukas Tribus49799162019-07-08 14:29:15 +02006159 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6160 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006161#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006162 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006163 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006164 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006165 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6166 else
6167 conn->err_code = CO_ER_SSL_EMPTY;
6168 }
6169 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006170 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006171 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6172 else
6173 conn->err_code = CO_ER_SSL_ABORT;
6174 }
6175 }
6176 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006177 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006178 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006179 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006180 conn->err_code = CO_ER_SSL_HANDSHAKE;
6181 }
Lukas Tribus49799162019-07-08 14:29:15 +02006182#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006183 }
Emeric Brun674b7432012-11-08 19:21:55 +01006184 goto out_error;
6185 }
6186 else {
6187 /* Fail on all other handshake errors */
6188 /* Note: OpenSSL may leave unread bytes in the socket's
6189 * buffer, causing an RST to be emitted upon close() on
6190 * TCP sockets. We first try to drain possibly pending
6191 * data to avoid this as much as possible.
6192 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006193 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006194 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006195 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006196 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006197 goto out_error;
6198 }
6199 }
6200 /* read some data: consider handshake completed */
6201 goto reneg_ok;
6202 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006203 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006204check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006205 if (ret != 1) {
6206 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006207 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006208
6209 if (ret == SSL_ERROR_WANT_WRITE) {
6210 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006211 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006212 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006213 return 0;
6214 }
6215 else if (ret == SSL_ERROR_WANT_READ) {
6216 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006217 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006218 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6219 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006220 return 0;
6221 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006222#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006223 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006224 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006225 return 0;
6226 }
6227#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006228 else if (ret == SSL_ERROR_SYSCALL) {
6229 /* if errno is null, then connection was successfully established */
6230 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6231 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006232 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006233#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6234 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006235 conn->err_code = CO_ER_SSL_HANDSHAKE;
6236#else
6237 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006238#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006239 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006240 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006241 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006242#else
Lukas Tribus49799162019-07-08 14:29:15 +02006243 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6244 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006245#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006246 if (empty_handshake) {
6247 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006248 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006249 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6250 else
6251 conn->err_code = CO_ER_SSL_EMPTY;
6252 }
6253 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006254 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006255 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6256 else
6257 conn->err_code = CO_ER_SSL_ABORT;
6258 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006259 }
6260 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006261 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006262 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6263 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006264 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006265 }
Lukas Tribus49799162019-07-08 14:29:15 +02006266#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006267 }
Willy Tarreau89230192012-09-28 20:22:13 +02006268 goto out_error;
6269 }
Emeric Brun46591952012-05-18 15:47:34 +02006270 else {
6271 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006272 /* Note: OpenSSL may leave unread bytes in the socket's
6273 * buffer, causing an RST to be emitted upon close() on
6274 * TCP sockets. We first try to drain possibly pending
6275 * data to avoid this as much as possible.
6276 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006277 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006278 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006279 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006280 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006281 goto out_error;
6282 }
6283 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006284#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006285 else {
6286 /*
6287 * If the server refused the early data, we have to send a
6288 * 425 to the client, as we no longer have the data to sent
6289 * them again.
6290 */
6291 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006292 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006293 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6294 goto out_error;
6295 }
6296 }
6297 }
6298#endif
6299
Emeric Brun46591952012-05-18 15:47:34 +02006300
Emeric Brun674b7432012-11-08 19:21:55 +01006301reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006302
Willy Tarreau5db847a2019-05-09 14:13:35 +02006303#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006304 /* ASYNC engine API doesn't support moving read/write
6305 * buffers. So we disable ASYNC mode right after
6306 * the handshake to avoid buffer oveflows.
6307 */
6308 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006309 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006310#endif
Emeric Brun46591952012-05-18 15:47:34 +02006311 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006312 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006313 if (objt_server(conn->target)) {
6314 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6315 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6316 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006317 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006318 else {
6319 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6320 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6321 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6322 }
Emeric Brun46591952012-05-18 15:47:34 +02006323 }
6324
6325 /* The connection is now established at both layers, it's time to leave */
6326 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6327 return 1;
6328
6329 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006330 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006331 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006332 ERR_clear_error();
6333
Emeric Brun9fa89732012-10-04 17:09:56 +02006334 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006335 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6336 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6337 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006338 }
6339
Emeric Brun46591952012-05-18 15:47:34 +02006340 /* Fail on all other handshake errors */
6341 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006342 if (!conn->err_code)
6343 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006344 return 0;
6345}
6346
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006347/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6348 * event subscriber <es> is not allowed to change from a previous call as long
6349 * as at least one event is still subscribed. The <event_type> must only be a
6350 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6351 * unless the transport layer was already released.
6352 */
6353static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006354{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006355 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006356
Olivier Houchard0ff28652019-06-24 18:57:39 +02006357 if (!ctx)
6358 return -1;
6359
Willy Tarreau113d52b2020-01-10 09:20:26 +01006360 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6361 BUG_ON(ctx->subs && ctx->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006362 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006363
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006364 ctx->subs = es;
6365 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006366
6367 /* we may have to subscribe to lower layers for new events */
6368 event_type &= ~ctx->wait_event.events;
6369 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6370 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006371 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006372}
6373
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006374/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6375 * The <es> pointer is not allowed to differ from the one passed to the
6376 * subscribe() call. It always returns zero.
6377 */
6378static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006379{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006380 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006381
Willy Tarreau113d52b2020-01-10 09:20:26 +01006382 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006383 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006384
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006385 es->events &= ~event_type;
6386 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006387 ctx->subs = NULL;
6388
6389 /* If we subscribed, and we're not doing the handshake,
6390 * then we subscribed because the upper layer asked for it,
6391 * as the upper layer is no longer interested, we can
6392 * unsubscribe too.
6393 */
6394 event_type &= ctx->wait_event.events;
6395 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6396 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006397
6398 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006399}
6400
Olivier Houchard2e055482019-05-27 19:50:12 +02006401/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6402 * Returns 0 on success, and non-zero on failure.
6403 */
6404static 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)
6405{
6406 struct ssl_sock_ctx *ctx = xprt_ctx;
6407
6408 if (oldxprt_ops != NULL)
6409 *oldxprt_ops = ctx->xprt;
6410 if (oldxprt_ctx != NULL)
6411 *oldxprt_ctx = ctx->xprt_ctx;
6412 ctx->xprt = toadd_ops;
6413 ctx->xprt_ctx = toadd_ctx;
6414 return 0;
6415}
6416
Olivier Houchard5149b592019-05-23 17:47:36 +02006417/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6418 * return 0, otherwise just call the remove_xprt method from the underlying
6419 * XPRT.
6420 */
6421static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6422{
6423 struct ssl_sock_ctx *ctx = xprt_ctx;
6424
6425 if (ctx->xprt_ctx == toremove_ctx) {
6426 ctx->xprt_ctx = newctx;
6427 ctx->xprt = newops;
6428 return 0;
6429 }
6430 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6431}
6432
Olivier Houchardea8dd942019-05-20 14:02:16 +02006433static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6434{
6435 struct ssl_sock_ctx *ctx = context;
6436
6437 /* First if we're doing an handshake, try that */
6438 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6439 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6440 /* If we had an error, or the handshake is done and I/O is available,
6441 * let the upper layer know.
6442 * If no mux was set up yet, and nobody subscribed, then call
6443 * xprt_done_cb() ourself if it's set, or destroy the connection,
6444 * we can't be sure conn_fd_handler() will be called again.
6445 */
6446 if ((ctx->conn->flags & CO_FL_ERROR) ||
6447 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6448 int ret = 0;
6449 int woke = 0;
6450
6451 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006452 if (ctx->subs) {
6453 tasklet_wakeup(ctx->subs->tasklet);
6454 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006455 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006456 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006457 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006458
Olivier Houchardea8dd942019-05-20 14:02:16 +02006459 /* If we're the first xprt for the connection, let the
6460 * upper layers know. If xprt_done_cb() is set, call it,
6461 * otherwise, we should have a mux, so call its wake
6462 * method if we didn't woke a tasklet already.
6463 */
6464 if (ctx->conn->xprt_ctx == ctx) {
6465 if (ctx->conn->xprt_done_cb)
6466 ret = ctx->conn->xprt_done_cb(ctx->conn);
6467 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6468 ctx->conn->mux->wake(ctx->conn);
6469 return NULL;
6470 }
6471 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006472#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6473 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006474 else if (b_data(&ctx->early_buf) && ctx->subs &&
6475 ctx->subs->events & SUB_RETRY_RECV) {
6476 tasklet_wakeup(ctx->subs->tasklet);
6477 ctx->subs->events &= ~SUB_RETRY_RECV;
6478 if (!ctx->subs->events)
6479 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006480 }
6481#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006482 return NULL;
6483}
6484
Emeric Brun46591952012-05-18 15:47:34 +02006485/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006486 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006487 * buffer wraps, in which case a second call may be performed. The connection's
6488 * flags are updated with whatever special event is detected (error, read0,
6489 * empty). The caller is responsible for taking care of those events and
6490 * avoiding the call if inappropriate. The function does not call the
6491 * connection's polling update function, so the caller is responsible for this.
6492 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006493static 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 +02006494{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006495 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006496 ssize_t ret;
6497 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006498
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006499 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006500 goto out_error;
6501
Olivier Houchard54907bb2019-12-19 15:02:39 +01006502#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6503 if (b_data(&ctx->early_buf)) {
6504 try = b_contig_space(buf);
6505 if (try > b_data(&ctx->early_buf))
6506 try = b_data(&ctx->early_buf);
6507 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6508 b_add(buf, try);
6509 b_del(&ctx->early_buf, try);
6510 if (b_data(&ctx->early_buf) == 0)
6511 b_free(&ctx->early_buf);
6512 return try;
6513 }
6514#endif
6515
Emeric Brun46591952012-05-18 15:47:34 +02006516 if (conn->flags & CO_FL_HANDSHAKE)
6517 /* a handshake was requested */
6518 return 0;
6519
Emeric Brun46591952012-05-18 15:47:34 +02006520 /* read the largest possible block. For this, we perform only one call
6521 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6522 * in which case we accept to do it once again. A new attempt is made on
6523 * EINTR too.
6524 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006525 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006526
Willy Tarreau591d4452018-06-15 17:21:00 +02006527 try = b_contig_space(buf);
6528 if (!try)
6529 break;
6530
Willy Tarreauabf08d92014-01-14 11:31:27 +01006531 if (try > count)
6532 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006533
Olivier Houchard66ab4982019-02-26 18:37:15 +01006534 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006535
Emeric Brune1f38db2012-09-03 20:36:47 +02006536 if (conn->flags & CO_FL_ERROR) {
6537 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006538 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006539 }
Emeric Brun46591952012-05-18 15:47:34 +02006540 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006541 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006542 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006543 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006544 }
Emeric Brun46591952012-05-18 15:47:34 +02006545 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006546 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006547 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006548 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02006549 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006550 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006551#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006552 /* Async mode can be re-enabled, because we're leaving data state.*/
6553 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006554 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006555#endif
Emeric Brun46591952012-05-18 15:47:34 +02006556 break;
6557 }
6558 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006559 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006560 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6561 SUB_RETRY_RECV,
6562 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01006563 /* handshake is running, and it may need to re-enable read */
6564 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006565#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006566 /* Async mode can be re-enabled, because we're leaving data state.*/
6567 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006568 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006569#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006570 break;
6571 }
Emeric Brun46591952012-05-18 15:47:34 +02006572 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006573 } else if (ret == SSL_ERROR_ZERO_RETURN)
6574 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006575 /* For SSL_ERROR_SYSCALL, make sure to clear the error
6576 * stack before shutting down the connection for
6577 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006578 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
6579 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02006580 /* otherwise it's a real error */
6581 goto out_error;
6582 }
6583 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006584 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006585 return done;
6586
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006587 clear_ssl_error:
6588 /* Clear openssl global errors stack */
6589 ssl_sock_dump_errors(conn);
6590 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02006591 read0:
6592 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006593 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006594
Emeric Brun46591952012-05-18 15:47:34 +02006595 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006596 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01006597 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006598 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006599 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006600 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006601}
6602
6603
Willy Tarreau787db9a2018-06-14 18:31:46 +02006604/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
6605 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
6606 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02006607 * Only one call to send() is performed, unless the buffer wraps, in which case
6608 * a second call may be performed. The connection's flags are updated with
6609 * whatever special event is detected (error, empty). The caller is responsible
6610 * for taking care of those events and avoiding the call if inappropriate. The
6611 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02006612 * is responsible for this. The buffer's output is not adjusted, it's up to the
6613 * caller to take care of this. It's up to the caller to update the buffer's
6614 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02006615 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006616static 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 +02006617{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006618 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006619 ssize_t ret;
6620 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02006621
6622 done = 0;
6623
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006624 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006625 goto out_error;
6626
Olivier Houchard010941f2019-05-03 20:56:19 +02006627 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006628 /* a handshake was requested */
6629 return 0;
6630
6631 /* send the largest possible block. For this we perform only one call
6632 * to send() unless the buffer wraps and we exactly fill the first hunk,
6633 * in which case we accept to do it once again.
6634 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02006635 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02006636#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006637 size_t written_data;
6638#endif
6639
Willy Tarreau787db9a2018-06-14 18:31:46 +02006640 try = b_contig_data(buf, done);
6641 if (try > count)
6642 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01006643
Willy Tarreau7bed9452014-02-02 02:00:24 +01006644 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006645 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01006646 global_ssl.max_record && try > global_ssl.max_record) {
6647 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006648 }
6649 else {
6650 /* we need to keep the information about the fact that
6651 * we're not limiting the upcoming send(), because if it
6652 * fails, we'll have to retry with at least as many data.
6653 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006654 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006655 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01006656
Willy Tarreau5db847a2019-05-09 14:13:35 +02006657#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02006658 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006659 unsigned int max_early;
6660
Olivier Houchard522eea72017-11-03 16:27:47 +01006661 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006662 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006663 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006664 if (SSL_get0_session(ctx->ssl))
6665 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006666 else
6667 max_early = 0;
6668 }
6669
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006670 if (try + ctx->sent_early_data > max_early) {
6671 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006672 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006673 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006674 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006675 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006676 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006677 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006678 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006679 if (ret == 1) {
6680 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006681 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006682 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006683 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006684 /* Initiate the handshake, now */
6685 tasklet_wakeup(ctx->wait_event.tasklet);
6686 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006687
Olivier Houchardc2aae742017-09-22 18:26:28 +02006688 }
6689
6690 } else
6691#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006692 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006693
Emeric Brune1f38db2012-09-03 20:36:47 +02006694 if (conn->flags & CO_FL_ERROR) {
6695 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006696 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006697 }
Emeric Brun46591952012-05-18 15:47:34 +02006698 if (ret > 0) {
Olivier Houchardf24502b2019-01-17 19:09:11 +01006699 /* A send succeeded, so we can consier ourself connected */
6700 conn->flags |= CO_FL_CONNECTED;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006701 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006702 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006703 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006704 }
6705 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006706 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006707
Emeric Brun46591952012-05-18 15:47:34 +02006708 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006709 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006710 /* handshake is running, and it may need to re-enable write */
6711 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006712 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &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
Emeric Brun282a76a2012-11-08 18:02:56 +01006718 break;
6719 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006720
Emeric Brun46591952012-05-18 15:47:34 +02006721 break;
6722 }
6723 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006724 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006725 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006726 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6727 SUB_RETRY_RECV,
6728 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006729#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006730 /* Async mode can be re-enabled, because we're leaving data state.*/
6731 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006732 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006733#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006734 break;
6735 }
Emeric Brun46591952012-05-18 15:47:34 +02006736 goto out_error;
6737 }
6738 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006739 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006740 return done;
6741
6742 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006743 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006744 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006745 ERR_clear_error();
6746
Emeric Brun46591952012-05-18 15:47:34 +02006747 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006748 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006749}
6750
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006751static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006752
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006753 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006754
Olivier Houchardea8dd942019-05-20 14:02:16 +02006755
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006756 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006757 if (ctx->wait_event.events != 0)
6758 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6759 ctx->wait_event.events,
6760 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01006761 if (ctx->subs) {
6762 ctx->subs->events = 0;
6763 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006764 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006765
Olivier Houchard692c1d02019-05-23 18:41:47 +02006766 if (ctx->xprt->close)
6767 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006768#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006769 if (global_ssl.async) {
6770 OSSL_ASYNC_FD all_fd[32], afd;
6771 size_t num_all_fds = 0;
6772 int i;
6773
Olivier Houchard66ab4982019-02-26 18:37:15 +01006774 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006775 if (num_all_fds > 32) {
6776 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6777 return;
6778 }
6779
Olivier Houchard66ab4982019-02-26 18:37:15 +01006780 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006781
6782 /* If an async job is pending, we must try to
6783 to catch the end using polling before calling
6784 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006785 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006786 for (i=0 ; i < num_all_fds ; i++) {
6787 /* switch on an handler designed to
6788 * handle the SSL_free
6789 */
6790 afd = all_fd[i];
6791 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006792 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006793 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006794 /* To ensure that the fd cache won't be used
6795 * and we'll catch a real RD event.
6796 */
6797 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006798 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006799 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006800 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006801 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006802 return;
6803 }
Emeric Brun3854e012017-05-17 20:42:48 +02006804 /* Else we can remove the fds from the fdtab
6805 * and call SSL_free.
6806 * note: we do a fd_remove and not a delete
6807 * because the fd is owned by the engine.
6808 * the engine is responsible to close
6809 */
6810 for (i=0 ; i < num_all_fds ; i++)
6811 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006812 }
6813#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006814 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01006815 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006816 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006817 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006818 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006819 }
Emeric Brun46591952012-05-18 15:47:34 +02006820}
6821
6822/* This function tries to perform a clean shutdown on an SSL connection, and in
6823 * any case, flags the connection as reusable if no handshake was in progress.
6824 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006825static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006826{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006827 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006828
Emeric Brun46591952012-05-18 15:47:34 +02006829 if (conn->flags & CO_FL_HANDSHAKE)
6830 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006831 if (!clean)
6832 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006833 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006834 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006835 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006836 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006837 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006838 ERR_clear_error();
6839 }
Emeric Brun46591952012-05-18 15:47:34 +02006840}
6841
William Lallemandd4f946c2019-12-05 10:26:40 +01006842/* fill a buffer with the algorithm and size of a public key */
6843static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006844{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006845 int bits = 0;
6846 int sig = TLSEXT_signature_anonymous;
6847 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006848 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006849
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006850 pkey = X509_get_pubkey(crt);
6851 if (pkey) {
6852 bits = EVP_PKEY_bits(pkey);
6853 switch(EVP_PKEY_base_id(pkey)) {
6854 case EVP_PKEY_RSA:
6855 sig = TLSEXT_signature_rsa;
6856 break;
6857 case EVP_PKEY_EC:
6858 sig = TLSEXT_signature_ecdsa;
6859 break;
6860 case EVP_PKEY_DSA:
6861 sig = TLSEXT_signature_dsa;
6862 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006863 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006864 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006865 }
6866
6867 switch(sig) {
6868 case TLSEXT_signature_rsa:
6869 len = chunk_printf(out, "RSA%d", bits);
6870 break;
6871 case TLSEXT_signature_ecdsa:
6872 len = chunk_printf(out, "EC%d", bits);
6873 break;
6874 case TLSEXT_signature_dsa:
6875 len = chunk_printf(out, "DSA%d", bits);
6876 break;
6877 default:
6878 return 0;
6879 }
6880 if (len < 0)
6881 return 0;
6882 return 1;
6883}
6884
William Lallemandd4f946c2019-12-05 10:26:40 +01006885/* used for ppv2 pkey alog (can be used for logging) */
6886int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
6887{
6888 struct ssl_sock_ctx *ctx;
6889 X509 *crt;
6890
6891 if (!ssl_sock_is_ssl(conn))
6892 return 0;
6893
6894 ctx = conn->xprt_ctx;
6895
6896 crt = SSL_get_certificate(ctx->ssl);
6897 if (!crt)
6898 return 0;
6899
6900 return cert_get_pkey_algo(crt, out);
6901}
6902
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006903/* used for ppv2 cert signature (can be used for logging) */
6904const char *ssl_sock_get_cert_sig(struct connection *conn)
6905{
Christopher Faulet82004142019-09-10 10:12:03 +02006906 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006907
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006908 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6909 X509 *crt;
6910
6911 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 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006915 if (!crt)
6916 return NULL;
6917 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6918 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6919}
6920
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006921/* used for ppv2 authority */
6922const char *ssl_sock_get_sni(struct connection *conn)
6923{
6924#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02006925 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006926
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006927 if (!ssl_sock_is_ssl(conn))
6928 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006929 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006930 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006931#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006932 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006933#endif
6934}
6935
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006936/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006937const char *ssl_sock_get_cipher_name(struct connection *conn)
6938{
Christopher Faulet82004142019-09-10 10:12:03 +02006939 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006940
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006941 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006942 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006943 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006944 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006945}
6946
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006947/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006948const char *ssl_sock_get_proto_version(struct connection *conn)
6949{
Christopher Faulet82004142019-09-10 10:12:03 +02006950 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006951
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006952 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006953 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006954 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006955 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006956}
6957
Willy Tarreau8d598402012-10-22 17:58:39 +02006958/* Extract a serial from a cert, and copy it to a chunk.
6959 * Returns 1 if serial is found and copied, 0 if no serial found and
6960 * -1 if output is not large enough.
6961 */
6962static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006963ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006964{
6965 ASN1_INTEGER *serial;
6966
6967 serial = X509_get_serialNumber(crt);
6968 if (!serial)
6969 return 0;
6970
6971 if (out->size < serial->length)
6972 return -1;
6973
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006974 memcpy(out->area, serial->data, serial->length);
6975 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006976 return 1;
6977}
6978
Emeric Brun43e79582014-10-29 19:03:26 +01006979/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006980 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6981 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006982 */
6983static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006984ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006985{
6986 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006987 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01006988
6989 len =i2d_X509(crt, NULL);
6990 if (len <= 0)
6991 return 1;
6992
6993 if (out->size < len)
6994 return -1;
6995
6996 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006997 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01006998 return 1;
6999}
7000
Emeric Brunce5ad802012-10-22 14:11:22 +02007001
Willy Tarreau83061a82018-07-13 11:56:34 +02007002/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007003 * Returns 1 if serial is found and copied, 0 if no valid time found
7004 * and -1 if output is not large enough.
7005 */
7006static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007007ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007008{
7009 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7010 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7011
7012 if (gentm->length < 12)
7013 return 0;
7014 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7015 return 0;
7016 if (out->size < gentm->length-2)
7017 return -1;
7018
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007019 memcpy(out->area, gentm->data+2, gentm->length-2);
7020 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007021 return 1;
7022 }
7023 else if (tm->type == V_ASN1_UTCTIME) {
7024 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7025
7026 if (utctm->length < 10)
7027 return 0;
7028 if (utctm->data[0] >= 0x35)
7029 return 0;
7030 if (out->size < utctm->length)
7031 return -1;
7032
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007033 memcpy(out->area, utctm->data, utctm->length);
7034 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007035 return 1;
7036 }
7037
7038 return 0;
7039}
7040
Emeric Brun87855892012-10-17 17:39:35 +02007041/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7042 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7043 */
7044static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007045ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7046 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007047{
7048 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007049 ASN1_OBJECT *obj;
7050 ASN1_STRING *data;
7051 const unsigned char *data_ptr;
7052 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007053 int i, j, n;
7054 int cur = 0;
7055 const char *s;
7056 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007057 int name_count;
7058
7059 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007060
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007061 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007062 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007063 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007064 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007065 else
7066 j = i;
7067
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007068 ne = X509_NAME_get_entry(a, j);
7069 obj = X509_NAME_ENTRY_get_object(ne);
7070 data = X509_NAME_ENTRY_get_data(ne);
7071 data_ptr = ASN1_STRING_get0_data(data);
7072 data_len = ASN1_STRING_length(data);
7073 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007074 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007075 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007076 s = tmp;
7077 }
7078
7079 if (chunk_strcasecmp(entry, s) != 0)
7080 continue;
7081
7082 if (pos < 0)
7083 cur--;
7084 else
7085 cur++;
7086
7087 if (cur != pos)
7088 continue;
7089
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007090 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007091 return -1;
7092
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007093 memcpy(out->area, data_ptr, data_len);
7094 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007095 return 1;
7096 }
7097
7098 return 0;
7099
William Lallemandd4f946c2019-12-05 10:26:40 +01007100}
7101
7102/*
7103 * Extract and format the DNS SAN extensions and copy result into a chuink
7104 * Return 0;
7105 */
7106#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7107static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7108{
7109 int i;
7110 char *str;
7111 STACK_OF(GENERAL_NAME) *names = NULL;
7112
7113 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7114 if (names) {
7115 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7116 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7117 if (i > 0)
7118 chunk_appendf(out, ", ");
7119 if (name->type == GEN_DNS) {
7120 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7121 chunk_appendf(out, "DNS:%s", str);
7122 OPENSSL_free(str);
7123 }
7124 }
7125 }
7126 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7127 }
7128 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007129}
William Lallemandd4f946c2019-12-05 10:26:40 +01007130#endif
Emeric Brun87855892012-10-17 17:39:35 +02007131
Elliot Otchet71f82972020-01-15 08:12:14 -05007132/*
7133 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7134 * Currently supports rfc2253 for returning LDAP V3 DNs.
7135 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7136 */
7137static int
7138ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7139{
7140 BIO *bio = NULL;
7141 int ret = 0;
7142 int data_len = 0;
7143
7144 if (chunk_strcmp(format, "rfc2253") == 0) {
7145 bio = BIO_new(BIO_s_mem());
7146 if (bio == NULL)
7147 goto out;
7148
7149 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7150 goto out;
7151
7152 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7153 goto out;
7154
7155 out->data = data_len;
7156
7157 ret = 1;
7158 }
7159out:
7160 if (bio)
7161 BIO_free(bio);
7162 return ret;
7163}
7164
Emeric Brun87855892012-10-17 17:39:35 +02007165/* Extract and format full DN from a X509_NAME and copy result into a chunk
7166 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7167 */
7168static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007169ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007170{
7171 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007172 ASN1_OBJECT *obj;
7173 ASN1_STRING *data;
7174 const unsigned char *data_ptr;
7175 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007176 int i, n, ln;
7177 int l = 0;
7178 const char *s;
7179 char *p;
7180 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007181 int name_count;
7182
7183
7184 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007185
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007186 out->data = 0;
7187 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007188 for (i = 0; i < name_count; i++) {
7189 ne = X509_NAME_get_entry(a, i);
7190 obj = X509_NAME_ENTRY_get_object(ne);
7191 data = X509_NAME_ENTRY_get_data(ne);
7192 data_ptr = ASN1_STRING_get0_data(data);
7193 data_len = ASN1_STRING_length(data);
7194 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007195 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007196 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007197 s = tmp;
7198 }
7199 ln = strlen(s);
7200
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007201 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007202 if (l > out->size)
7203 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007204 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007205
7206 *(p++)='/';
7207 memcpy(p, s, ln);
7208 p += ln;
7209 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007210 memcpy(p, data_ptr, data_len);
7211 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007212 }
7213
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007214 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007215 return 0;
7216
7217 return 1;
7218}
7219
Olivier Houchardab28a322018-12-21 19:45:40 +01007220void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7221{
7222#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007223 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007224
Olivier Houcharde488ea82019-06-28 14:10:33 +02007225 if (!ssl_sock_is_ssl(conn))
7226 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007227 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007228 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007229#endif
7230}
7231
Willy Tarreau119a4082016-12-22 21:58:38 +01007232/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7233 * to disable SNI.
7234 */
Willy Tarreau63076412015-07-10 11:33:32 +02007235void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7236{
7237#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007238 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007239
Willy Tarreau119a4082016-12-22 21:58:38 +01007240 char *prev_name;
7241
Willy Tarreau63076412015-07-10 11:33:32 +02007242 if (!ssl_sock_is_ssl(conn))
7243 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007244 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007245
Willy Tarreau119a4082016-12-22 21:58:38 +01007246 /* if the SNI changes, we must destroy the reusable context so that a
7247 * new connection will present a new SNI. As an optimization we could
7248 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7249 * server.
7250 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007251 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007252 if ((!prev_name && hostname) ||
7253 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007254 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007255
Olivier Houchard66ab4982019-02-26 18:37:15 +01007256 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007257#endif
7258}
7259
Emeric Brun0abf8362014-06-24 18:26:41 +02007260/* Extract peer certificate's common name into the chunk dest
7261 * Returns
7262 * the len of the extracted common name
7263 * or 0 if no CN found in DN
7264 * or -1 on error case (i.e. no peer certificate)
7265 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007266int ssl_sock_get_remote_common_name(struct connection *conn,
7267 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007268{
Christopher Faulet82004142019-09-10 10:12:03 +02007269 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007270 X509 *crt = NULL;
7271 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007272 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007273 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007274 .area = (char *)&find_cn,
7275 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007276 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007277 int result = -1;
David Safb76832014-05-08 23:42:08 -04007278
7279 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007280 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007281 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007282
7283 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007284 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007285 if (!crt)
7286 goto out;
7287
7288 name = X509_get_subject_name(crt);
7289 if (!name)
7290 goto out;
David Safb76832014-05-08 23:42:08 -04007291
Emeric Brun0abf8362014-06-24 18:26:41 +02007292 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7293out:
David Safb76832014-05-08 23:42:08 -04007294 if (crt)
7295 X509_free(crt);
7296
7297 return result;
7298}
7299
Dave McCowan328fb582014-07-30 10:39:13 -04007300/* returns 1 if client passed a certificate for this session, 0 if not */
7301int ssl_sock_get_cert_used_sess(struct connection *conn)
7302{
Christopher Faulet82004142019-09-10 10:12:03 +02007303 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007304 X509 *crt = NULL;
7305
7306 if (!ssl_sock_is_ssl(conn))
7307 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007308 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007309
7310 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007311 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007312 if (!crt)
7313 return 0;
7314
7315 X509_free(crt);
7316 return 1;
7317}
7318
7319/* returns 1 if client passed a certificate for this connection, 0 if not */
7320int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007321{
Christopher Faulet82004142019-09-10 10:12:03 +02007322 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007323
David Safb76832014-05-08 23:42:08 -04007324 if (!ssl_sock_is_ssl(conn))
7325 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007326 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007327 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007328}
7329
7330/* returns result from SSL verify */
7331unsigned int ssl_sock_get_verify_result(struct connection *conn)
7332{
Christopher Faulet82004142019-09-10 10:12:03 +02007333 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007334
David Safb76832014-05-08 23:42:08 -04007335 if (!ssl_sock_is_ssl(conn))
7336 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007337 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007338 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007339}
7340
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007341/* Returns the application layer protocol name in <str> and <len> when known.
7342 * Zero is returned if the protocol name was not found, otherwise non-zero is
7343 * returned. The string is allocated in the SSL context and doesn't have to be
7344 * freed by the caller. NPN is also checked if available since older versions
7345 * of openssl (1.0.1) which are more common in field only support this one.
7346 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007347static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007348{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007349#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7350 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007351 struct ssl_sock_ctx *ctx = xprt_ctx;
7352 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007353 return 0;
7354
7355 *str = NULL;
7356
7357#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007358 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007359 if (*str)
7360 return 1;
7361#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007362#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007363 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007364 if (*str)
7365 return 1;
7366#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007367#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007368 return 0;
7369}
7370
Willy Tarreau7875d092012-09-10 08:20:03 +02007371/***** Below are some sample fetching functions for ACL/patterns *****/
7372
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007373static int
7374smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7375{
7376 struct connection *conn;
7377
7378 conn = objt_conn(smp->sess->origin);
7379 if (!conn || conn->xprt != &ssl_sock)
7380 return 0;
7381
7382 smp->flags = 0;
7383 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007384#ifdef OPENSSL_IS_BORINGSSL
7385 {
7386 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7387 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7388 SSL_early_data_accepted(ctx->ssl));
7389 }
7390#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007391 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
7392 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007393#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007394 return 1;
7395}
7396
Emeric Brune64aef12012-09-21 13:15:06 +02007397/* boolean, returns true if client cert was present */
7398static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007399smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007400{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007401 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007402 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007403
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007404 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007405 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007406 return 0;
7407
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007408 ctx = conn->xprt_ctx;
7409
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007410 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02007411 smp->flags |= SMP_F_MAY_CHANGE;
7412 return 0;
7413 }
7414
7415 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007416 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007417 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007418
7419 return 1;
7420}
7421
Emeric Brun43e79582014-10-29 19:03:26 +01007422/* binary, returns a certificate in a binary chunk (der/raw).
7423 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7424 * should be use.
7425 */
7426static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007427smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007428{
7429 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7430 X509 *crt = NULL;
7431 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007432 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007433 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007434 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007435
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007436 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007437 if (!conn || conn->xprt != &ssl_sock)
7438 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007439 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007440
7441 if (!(conn->flags & CO_FL_CONNECTED)) {
7442 smp->flags |= SMP_F_MAY_CHANGE;
7443 return 0;
7444 }
7445
7446 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007447 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007448 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007449 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007450
7451 if (!crt)
7452 goto out;
7453
7454 smp_trash = get_trash_chunk();
7455 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7456 goto out;
7457
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007458 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007459 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007460 ret = 1;
7461out:
7462 /* SSL_get_peer_certificate, it increase X509 * ref count */
7463 if (cert_peer && crt)
7464 X509_free(crt);
7465 return ret;
7466}
7467
Emeric Brunba841a12014-04-30 17:05:08 +02007468/* binary, returns serial of certificate in a binary chunk.
7469 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7470 * should be use.
7471 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007472static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007473smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007474{
Emeric Brunba841a12014-04-30 17:05:08 +02007475 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007476 X509 *crt = NULL;
7477 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007478 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007479 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007480 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007481
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007482 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007483 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007484 return 0;
7485
Olivier Houchard66ab4982019-02-26 18:37:15 +01007486 ctx = conn->xprt_ctx;
7487
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007488 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007489 smp->flags |= SMP_F_MAY_CHANGE;
7490 return 0;
7491 }
7492
Emeric Brunba841a12014-04-30 17:05:08 +02007493 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007494 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007495 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007496 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007497
Willy Tarreau8d598402012-10-22 17:58:39 +02007498 if (!crt)
7499 goto out;
7500
Willy Tarreau47ca5452012-12-23 20:22:19 +01007501 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007502 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7503 goto out;
7504
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007505 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007506 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007507 ret = 1;
7508out:
Emeric Brunba841a12014-04-30 17:05:08 +02007509 /* SSL_get_peer_certificate, it increase X509 * ref count */
7510 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007511 X509_free(crt);
7512 return ret;
7513}
Emeric Brune64aef12012-09-21 13:15:06 +02007514
Emeric Brunba841a12014-04-30 17:05:08 +02007515/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7516 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7517 * should be use.
7518 */
James Votha051b4a2013-05-14 20:37:59 +02007519static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007520smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007521{
Emeric Brunba841a12014-04-30 17:05:08 +02007522 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007523 X509 *crt = NULL;
7524 const EVP_MD *digest;
7525 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007526 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007527 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007528 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02007529
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007530 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007531 if (!conn || conn->xprt != &ssl_sock)
7532 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007533 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007534
7535 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02007536 smp->flags |= SMP_F_MAY_CHANGE;
7537 return 0;
7538 }
7539
Emeric Brunba841a12014-04-30 17:05:08 +02007540 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007541 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007542 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007543 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02007544 if (!crt)
7545 goto out;
7546
7547 smp_trash = get_trash_chunk();
7548 digest = EVP_sha1();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007549 X509_digest(crt, digest, (unsigned char *) smp_trash->area,
7550 (unsigned int *)&smp_trash->data);
James Votha051b4a2013-05-14 20:37:59 +02007551
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007552 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007553 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02007554 ret = 1;
7555out:
Emeric Brunba841a12014-04-30 17:05:08 +02007556 /* SSL_get_peer_certificate, it increase X509 * ref count */
7557 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02007558 X509_free(crt);
7559 return ret;
7560}
7561
Emeric Brunba841a12014-04-30 17:05:08 +02007562/* string, returns certificate's notafter date in ASN1_UTCTIME format.
7563 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7564 * should be use.
7565 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007566static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007567smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007568{
Emeric Brunba841a12014-04-30 17:05:08 +02007569 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007570 X509 *crt = NULL;
7571 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007572 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007573 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007574 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007575
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007576 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007577 if (!conn || conn->xprt != &ssl_sock)
7578 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007579 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007580
7581 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007582 smp->flags |= SMP_F_MAY_CHANGE;
7583 return 0;
7584 }
7585
Emeric Brunba841a12014-04-30 17:05:08 +02007586 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007587 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007588 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007589 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007590 if (!crt)
7591 goto out;
7592
Willy Tarreau47ca5452012-12-23 20:22:19 +01007593 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007594 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007595 goto out;
7596
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007597 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007598 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007599 ret = 1;
7600out:
Emeric Brunba841a12014-04-30 17:05:08 +02007601 /* SSL_get_peer_certificate, it increase X509 * ref count */
7602 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007603 X509_free(crt);
7604 return ret;
7605}
7606
Emeric Brunba841a12014-04-30 17:05:08 +02007607/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
7608 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7609 * should be use.
7610 */
Emeric Brun87855892012-10-17 17:39:35 +02007611static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007612smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007613{
Emeric Brunba841a12014-04-30 17:05:08 +02007614 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007615 X509 *crt = NULL;
7616 X509_NAME *name;
7617 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007618 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007619 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007620 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007621
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007622 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007623 if (!conn || conn->xprt != &ssl_sock)
7624 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007625 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007626
7627 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007628 smp->flags |= SMP_F_MAY_CHANGE;
7629 return 0;
7630 }
7631
Emeric Brunba841a12014-04-30 17:05:08 +02007632 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007633 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007634 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007635 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007636 if (!crt)
7637 goto out;
7638
7639 name = X509_get_issuer_name(crt);
7640 if (!name)
7641 goto out;
7642
Willy Tarreau47ca5452012-12-23 20:22:19 +01007643 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007644 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007645 int pos = 1;
7646
7647 if (args[1].type == ARGT_SINT)
7648 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007649
7650 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7651 goto out;
7652 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007653 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7654 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7655 goto out;
7656 }
Emeric Brun87855892012-10-17 17:39:35 +02007657 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7658 goto out;
7659
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007660 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007661 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007662 ret = 1;
7663out:
Emeric Brunba841a12014-04-30 17:05:08 +02007664 /* SSL_get_peer_certificate, it increase X509 * ref count */
7665 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007666 X509_free(crt);
7667 return ret;
7668}
7669
Emeric Brunba841a12014-04-30 17:05:08 +02007670/* string, returns notbefore date in ASN1_UTCTIME format.
7671 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7672 * should be use.
7673 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007674static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007675smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007676{
Emeric Brunba841a12014-04-30 17:05:08 +02007677 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007678 X509 *crt = NULL;
7679 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007680 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007681 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007682 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007683
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007684 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007685 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02007686 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007687 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007688
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007689 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007690 smp->flags |= SMP_F_MAY_CHANGE;
7691 return 0;
7692 }
7693
Emeric Brunba841a12014-04-30 17:05:08 +02007694 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007695 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007696 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007697 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007698 if (!crt)
7699 goto out;
7700
Willy Tarreau47ca5452012-12-23 20:22:19 +01007701 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007702 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007703 goto out;
7704
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007705 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007706 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007707 ret = 1;
7708out:
Emeric Brunba841a12014-04-30 17:05:08 +02007709 /* SSL_get_peer_certificate, it increase X509 * ref count */
7710 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007711 X509_free(crt);
7712 return ret;
7713}
7714
Emeric Brunba841a12014-04-30 17:05:08 +02007715/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
7716 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7717 * should be use.
7718 */
Emeric Brun87855892012-10-17 17:39:35 +02007719static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007720smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007721{
Emeric Brunba841a12014-04-30 17:05:08 +02007722 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007723 X509 *crt = NULL;
7724 X509_NAME *name;
7725 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007726 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007727 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007728 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007729
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007730 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007731 if (!conn || conn->xprt != &ssl_sock)
7732 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007733 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007734
7735 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007736 smp->flags |= SMP_F_MAY_CHANGE;
7737 return 0;
7738 }
7739
Emeric Brunba841a12014-04-30 17:05:08 +02007740 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007741 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007742 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007743 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007744 if (!crt)
7745 goto out;
7746
7747 name = X509_get_subject_name(crt);
7748 if (!name)
7749 goto out;
7750
Willy Tarreau47ca5452012-12-23 20:22:19 +01007751 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007752 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007753 int pos = 1;
7754
7755 if (args[1].type == ARGT_SINT)
7756 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007757
7758 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7759 goto out;
7760 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007761 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7762 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7763 goto out;
7764 }
Emeric Brun87855892012-10-17 17:39:35 +02007765 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7766 goto out;
7767
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007768 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007769 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007770 ret = 1;
7771out:
Emeric Brunba841a12014-04-30 17:05:08 +02007772 /* SSL_get_peer_certificate, it increase X509 * ref count */
7773 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007774 X509_free(crt);
7775 return ret;
7776}
Emeric Brun9143d372012-12-20 15:44:16 +01007777
7778/* integer, returns true if current session use a client certificate */
7779static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007780smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007781{
7782 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007783 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007784 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007785
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007786 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007787 if (!conn || conn->xprt != &ssl_sock)
7788 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007789 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007790
7791 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01007792 smp->flags |= SMP_F_MAY_CHANGE;
7793 return 0;
7794 }
7795
7796 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007797 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007798 if (crt) {
7799 X509_free(crt);
7800 }
7801
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007802 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007803 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007804 return 1;
7805}
7806
Emeric Brunba841a12014-04-30 17:05:08 +02007807/* integer, returns the certificate version
7808 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7809 * should be use.
7810 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007811static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007812smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007813{
Emeric Brunba841a12014-04-30 17:05:08 +02007814 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007815 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007816 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007817 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007818
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007819 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007820 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007821 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007822 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007823
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007824 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007825 smp->flags |= SMP_F_MAY_CHANGE;
7826 return 0;
7827 }
7828
Emeric Brunba841a12014-04-30 17:05:08 +02007829 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007830 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007831 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007832 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007833 if (!crt)
7834 return 0;
7835
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007836 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007837 /* SSL_get_peer_certificate increase X509 * ref count */
7838 if (cert_peer)
7839 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007840 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007841
7842 return 1;
7843}
7844
Emeric Brunba841a12014-04-30 17:05:08 +02007845/* string, returns the certificate's signature algorithm.
7846 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7847 * should be use.
7848 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007849static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007850smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007851{
Emeric Brunba841a12014-04-30 17:05:08 +02007852 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007853 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007854 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007855 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007856 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007857 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007858
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007859 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007860 if (!conn || conn->xprt != &ssl_sock)
7861 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007862 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007863
7864 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007865 smp->flags |= SMP_F_MAY_CHANGE;
7866 return 0;
7867 }
7868
Emeric Brunba841a12014-04-30 17:05:08 +02007869 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007870 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007871 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007872 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007873 if (!crt)
7874 return 0;
7875
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007876 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7877 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007878
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007879 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7880 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007881 /* SSL_get_peer_certificate increase X509 * ref count */
7882 if (cert_peer)
7883 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007884 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007885 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007886
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007887 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007888 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007889 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007890 /* SSL_get_peer_certificate increase X509 * ref count */
7891 if (cert_peer)
7892 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007893
7894 return 1;
7895}
7896
Emeric Brunba841a12014-04-30 17:05:08 +02007897/* string, returns the certificate's key algorithm.
7898 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7899 * should be use.
7900 */
Emeric Brun521a0112012-10-22 12:22:55 +02007901static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007902smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02007903{
Emeric Brunba841a12014-04-30 17:05:08 +02007904 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02007905 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007906 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02007907 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007908 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007909 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02007910
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007911 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007912 if (!conn || conn->xprt != &ssl_sock)
7913 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007914 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007915
7916 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02007917 smp->flags |= SMP_F_MAY_CHANGE;
7918 return 0;
7919 }
7920
Emeric Brunba841a12014-04-30 17:05:08 +02007921 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007922 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007923 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007924 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02007925 if (!crt)
7926 return 0;
7927
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007928 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
7929 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02007930
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007931 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7932 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007933 /* SSL_get_peer_certificate increase X509 * ref count */
7934 if (cert_peer)
7935 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007936 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007937 }
Emeric Brun521a0112012-10-22 12:22:55 +02007938
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007939 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007940 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007941 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007942 if (cert_peer)
7943 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007944
7945 return 1;
7946}
7947
Emeric Brun645ae792014-04-30 14:21:06 +02007948/* boolean, returns true if front conn. transport layer is SSL.
7949 * This function is also usable on backend conn if the fetch keyword 5th
7950 * char is 'b'.
7951 */
Willy Tarreau7875d092012-09-10 08:20:03 +02007952static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007953smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007954{
Emeric Bruneb8def92018-02-19 15:59:48 +01007955 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7956 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007957
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007958 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007959 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007960 return 1;
7961}
7962
Emeric Brun2525b6b2012-10-18 15:59:43 +02007963/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007964static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007965smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007966{
7967#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007968 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007969 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007970
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007971 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007972 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007973 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007974 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007975 return 1;
7976#else
7977 return 0;
7978#endif
7979}
7980
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007981/* boolean, returns true if client session has been resumed.
7982 * This function is also usable on backend conn if the fetch keyword 5th
7983 * char is 'b'.
7984 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007985static int
7986smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
7987{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007988 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7989 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007990 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007991
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007992
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007993 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007994 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007995 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007996 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007997 return 1;
7998}
7999
Emeric Brun645ae792014-04-30 14:21:06 +02008000/* string, returns the used cipher if front conn. transport layer is SSL.
8001 * This function is also usable on backend conn if the fetch keyword 5th
8002 * char is 'b'.
8003 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008004static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008005smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008006{
Emeric Bruneb8def92018-02-19 15:59:48 +01008007 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8008 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008009 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008010
Willy Tarreaube508f12016-03-10 11:47:01 +01008011 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008012 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008013 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008014 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008015
Olivier Houchard66ab4982019-02-26 18:37:15 +01008016 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008017 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008018 return 0;
8019
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008020 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008021 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008022 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008023
8024 return 1;
8025}
8026
Emeric Brun645ae792014-04-30 14:21:06 +02008027/* integer, returns the algoritm's keysize if front conn. transport layer
8028 * is SSL.
8029 * This function is also usable on backend conn if the fetch keyword 5th
8030 * char is 'b'.
8031 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008032static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008033smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008034{
Emeric Bruneb8def92018-02-19 15:59:48 +01008035 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8036 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008037 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008038 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008039
Emeric Brun589fcad2012-10-16 14:13:26 +02008040 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008041 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008042 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008043 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008044
Olivier Houchard66ab4982019-02-26 18:37:15 +01008045 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008046 return 0;
8047
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008048 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008049 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008050
8051 return 1;
8052}
8053
Emeric Brun645ae792014-04-30 14:21:06 +02008054/* integer, returns the used keysize if front conn. transport layer is SSL.
8055 * This function is also usable on backend conn if the fetch keyword 5th
8056 * char is 'b'.
8057 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008058static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008059smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008060{
Emeric Bruneb8def92018-02-19 15:59:48 +01008061 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8062 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008063 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008064
Emeric Brun589fcad2012-10-16 14:13:26 +02008065 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008066 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8067 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008068 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008069
Olivier Houchard66ab4982019-02-26 18:37:15 +01008070 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008071 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008072 return 0;
8073
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008074 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008075
8076 return 1;
8077}
8078
Bernard Spil13c53f82018-02-15 13:34:58 +01008079#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008080static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008081smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008082{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008083 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008084 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008085
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008086 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008087 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008088
Olivier Houchard6b77f492018-11-22 18:18:29 +01008089 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8090 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008091 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8092 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008093 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008094
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008095 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008096 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008097 (const unsigned char **)&smp->data.u.str.area,
8098 (unsigned *)&smp->data.u.str.data);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008099
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008100 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008101 return 0;
8102
8103 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008104}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008105#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008106
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008107#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008108static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008109smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008110{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008111 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008112 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008113
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008114 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008115 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008116
Olivier Houchard6b77f492018-11-22 18:18:29 +01008117 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8118 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8119
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008120 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008121 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008122 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008123
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008124 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008125 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008126 (const unsigned char **)&smp->data.u.str.area,
8127 (unsigned *)&smp->data.u.str.data);
Willy Tarreauab861d32013-04-02 02:30:41 +02008128
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008129 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008130 return 0;
8131
8132 return 1;
8133}
8134#endif
8135
Emeric Brun645ae792014-04-30 14:21:06 +02008136/* string, returns the used protocol if front conn. transport layer is SSL.
8137 * This function is also usable on backend conn if the fetch keyword 5th
8138 * char is 'b'.
8139 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008140static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008141smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008142{
Emeric Bruneb8def92018-02-19 15:59:48 +01008143 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8144 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008145 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008146
Emeric Brun589fcad2012-10-16 14:13:26 +02008147 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008148 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8149 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008150 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008151
Olivier Houchard66ab4982019-02-26 18:37:15 +01008152 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008153 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008154 return 0;
8155
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008156 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008157 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008158 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008159
8160 return 1;
8161}
8162
Willy Tarreau87b09662015-04-03 00:22:06 +02008163/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008164 * This function is also usable on backend conn if the fetch keyword 5th
8165 * char is 'b'.
8166 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008167#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008168static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008169smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008170{
Emeric Bruneb8def92018-02-19 15:59:48 +01008171 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8172 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008173 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008174 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008175
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008176 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008177 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008178
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008179 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8180 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008181 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008182
Olivier Houchard66ab4982019-02-26 18:37:15 +01008183 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008184 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008185 return 0;
8186
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008187 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess,
8188 (unsigned int *)&smp->data.u.str.data);
8189 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008190 return 0;
8191
8192 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008193}
Patrick Hemmer41966772018-04-28 19:15:48 -04008194#endif
8195
Emeric Brunfe68f682012-10-16 14:59:28 +02008196
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008197#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008198static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008199smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8200{
8201 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8202 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8203 struct buffer *data;
8204 struct ssl_sock_ctx *ctx;
8205
8206 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8207 return 0;
8208 ctx = conn->xprt_ctx;
8209
8210 data = get_trash_chunk();
8211 if (kw[7] == 'c')
8212 data->data = SSL_get_client_random(ctx->ssl,
8213 (unsigned char *) data->area,
8214 data->size);
8215 else
8216 data->data = SSL_get_server_random(ctx->ssl,
8217 (unsigned char *) data->area,
8218 data->size);
8219 if (!data->data)
8220 return 0;
8221
8222 smp->flags = 0;
8223 smp->data.type = SMP_T_BIN;
8224 smp->data.u.str = *data;
8225
8226 return 1;
8227}
8228
8229static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008230smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8231{
8232 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8233 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8234 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008235 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008236 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008237
8238 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8239 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008240 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008241
Olivier Houchard66ab4982019-02-26 18:37:15 +01008242 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008243 if (!ssl_sess)
8244 return 0;
8245
8246 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008247 data->data = SSL_SESSION_get_master_key(ssl_sess,
8248 (unsigned char *) data->area,
8249 data->size);
8250 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008251 return 0;
8252
8253 smp->flags = 0;
8254 smp->data.type = SMP_T_BIN;
8255 smp->data.u.str = *data;
8256
8257 return 1;
8258}
8259#endif
8260
Patrick Hemmer41966772018-04-28 19:15:48 -04008261#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008262static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008263smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008264{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008265 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008266 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008267
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008268 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008269 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008270
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008271 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008272 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8273 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008274 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008275
Olivier Houchard66ab4982019-02-26 18:37:15 +01008276 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008277 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008278 return 0;
8279
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008280 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008281 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008282}
Patrick Hemmer41966772018-04-28 19:15:48 -04008283#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008284
David Sc1ad52e2014-04-08 18:48:47 -04008285static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008286smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8287{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008288 struct connection *conn;
8289 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008290 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008291
8292 conn = objt_conn(smp->sess->origin);
8293 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8294 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008295 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008296
Olivier Houchard66ab4982019-02-26 18:37:15 +01008297 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008298 if (!capture)
8299 return 0;
8300
8301 smp->flags = SMP_F_CONST;
8302 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008303 smp->data.u.str.area = capture->ciphersuite;
8304 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008305 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008306}
8307
8308static int
8309smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8310{
Willy Tarreau83061a82018-07-13 11:56:34 +02008311 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008312
8313 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8314 return 0;
8315
8316 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008317 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008318 smp->data.type = SMP_T_BIN;
8319 smp->data.u.str = *data;
8320 return 1;
8321}
8322
8323static int
8324smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8325{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008326 struct connection *conn;
8327 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008328 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008329
8330 conn = objt_conn(smp->sess->origin);
8331 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8332 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008333 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008334
Olivier Houchard66ab4982019-02-26 18:37:15 +01008335 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008336 if (!capture)
8337 return 0;
8338
8339 smp->data.type = SMP_T_SINT;
8340 smp->data.u.sint = capture->xxh64;
8341 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008342}
8343
8344static int
8345smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8346{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008347#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008348 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008349 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008350
8351 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8352 return 0;
8353
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008354 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008355 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008356 const char *str;
8357 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008358 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008359 uint16_t id = (bin[0] << 8) | bin[1];
8360#if defined(OPENSSL_IS_BORINGSSL)
8361 cipher = SSL_get_cipher_by_value(id);
8362#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008363 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008364 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8365 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008366#endif
8367 str = SSL_CIPHER_get_name(cipher);
8368 if (!str || strcmp(str, "(NONE)") == 0)
8369 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008370 else
8371 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8372 }
8373 smp->data.type = SMP_T_STR;
8374 smp->data.u.str = *data;
8375 return 1;
8376#else
8377 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8378#endif
8379}
8380
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008381#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008382static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008383smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008384{
Emeric Bruneb8def92018-02-19 15:59:48 +01008385 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8386 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008387 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008388 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008389 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008390
8391 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008392 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8393 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008394 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008395
8396 if (!(conn->flags & CO_FL_CONNECTED)) {
8397 smp->flags |= SMP_F_MAY_CHANGE;
8398 return 0;
8399 }
8400
8401 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008402 if (!SSL_session_reused(ctx->ssl))
8403 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008404 finished_trash->area,
8405 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008406 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008407 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008408 finished_trash->area,
8409 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008410
8411 if (!finished_len)
8412 return 0;
8413
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008414 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008415 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008416 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008417
8418 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008419}
Patrick Hemmer41966772018-04-28 19:15:48 -04008420#endif
David Sc1ad52e2014-04-08 18:48:47 -04008421
Emeric Brun2525b6b2012-10-18 15:59:43 +02008422/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008423static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008424smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008425{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008426 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008427 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008428
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008429 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008430 if (!conn || conn->xprt != &ssl_sock)
8431 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008432 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008433
8434 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008435 smp->flags = SMP_F_MAY_CHANGE;
8436 return 0;
8437 }
8438
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008439 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008440 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008441 smp->flags = 0;
8442
8443 return 1;
8444}
8445
Emeric Brun2525b6b2012-10-18 15:59:43 +02008446/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008447static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008448smp_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 +02008449{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008450 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008451 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008452
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008453 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008454 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008455 return 0;
8456
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008457 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008458 smp->flags = SMP_F_MAY_CHANGE;
8459 return 0;
8460 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008461 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008462
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008463 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008464 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008465 smp->flags = 0;
8466
8467 return 1;
8468}
8469
Emeric Brun2525b6b2012-10-18 15:59:43 +02008470/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008471static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008472smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008473{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008474 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008475 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008476
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008477 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008478 if (!conn || conn->xprt != &ssl_sock)
8479 return 0;
8480
8481 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008482 smp->flags = SMP_F_MAY_CHANGE;
8483 return 0;
8484 }
8485
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008486 ctx = conn->xprt_ctx;
8487
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008488 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008489 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008490 smp->flags = 0;
8491
8492 return 1;
8493}
8494
Emeric Brun2525b6b2012-10-18 15:59:43 +02008495/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008496static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008497smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008498{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008499 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008500 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008501
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008502 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008503 if (!conn || conn->xprt != &ssl_sock)
8504 return 0;
8505
8506 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008507 smp->flags = SMP_F_MAY_CHANGE;
8508 return 0;
8509 }
8510
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008511 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008512 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008513 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008514
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008515 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008516 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008517 smp->flags = 0;
8518
8519 return 1;
8520}
8521
Emeric Brunfb510ea2012-10-05 12:00:26 +02008522/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008523static 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 +02008524{
8525 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008526 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008527 return ERR_ALERT | ERR_FATAL;
8528 }
8529
Willy Tarreauef934602016-12-22 23:12:01 +01008530 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8531 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008532 else
8533 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008534
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02008535 if (!ssl_store_load_locations_file(conf->ca_file)) {
8536 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->ca_file);
8537 return ERR_ALERT | ERR_FATAL;
8538 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02008539 return 0;
8540}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008541static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8542{
8543 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
8544}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008545
Christopher Faulet31af49d2015-06-09 17:29:50 +02008546/* parse the "ca-sign-file" bind keyword */
8547static int bind_parse_ca_sign_file(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 CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008551 return ERR_ALERT | ERR_FATAL;
8552 }
8553
Willy Tarreauef934602016-12-22 23:12:01 +01008554 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8555 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008556 else
8557 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
8558
8559 return 0;
8560}
8561
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008562/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02008563static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8564{
8565 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008566 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008567 return ERR_ALERT | ERR_FATAL;
8568 }
8569 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
8570 return 0;
8571}
8572
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008573/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008574static 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 +02008575{
8576 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008577 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008578 return ERR_ALERT | ERR_FATAL;
8579 }
8580
Emeric Brun76d88952012-10-05 15:47:31 +02008581 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02008582 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008583 return 0;
8584}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008585static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8586{
8587 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
8588}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008589
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008590#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008591/* parse the "ciphersuites" bind keyword */
8592static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8593{
8594 if (!*args[cur_arg + 1]) {
8595 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
8596 return ERR_ALERT | ERR_FATAL;
8597 }
8598
8599 free(conf->ciphersuites);
8600 conf->ciphersuites = strdup(args[cur_arg + 1]);
8601 return 0;
8602}
8603static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8604{
8605 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
8606}
8607#endif
8608
Willy Tarreaubbc91962019-10-16 16:42:19 +02008609/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008610static 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 +02008611{
Willy Tarreau38011032013-08-13 16:59:39 +02008612 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02008613
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008614 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008615 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008616 return ERR_ALERT | ERR_FATAL;
8617 }
8618
Willy Tarreauef934602016-12-22 23:12:01 +01008619 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
8620 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02008621 memprintf(err, "'%s' : path too long", args[cur_arg]);
8622 return ERR_ALERT | ERR_FATAL;
8623 }
Willy Tarreauef934602016-12-22 23:12:01 +01008624 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02008625 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008626 }
8627
Willy Tarreaubbc91962019-10-16 16:42:19 +02008628 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008629}
8630
Willy Tarreaubbc91962019-10-16 16:42:19 +02008631/* 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 +01008632static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8633{
Willy Tarreaubbc91962019-10-16 16:42:19 +02008634 int err_code;
8635
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008636 if (!*args[cur_arg + 1]) {
8637 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
8638 return ERR_ALERT | ERR_FATAL;
8639 }
8640
Willy Tarreaubbc91962019-10-16 16:42:19 +02008641 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err);
8642 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02008643 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008644
Willy Tarreaubbc91962019-10-16 16:42:19 +02008645 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008646}
8647
Emeric Brunfb510ea2012-10-05 12:00:26 +02008648/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008649static 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 +02008650{
Emeric Brun051cdab2012-10-02 19:25:50 +02008651#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01008652 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02008653 return ERR_ALERT | ERR_FATAL;
8654#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02008655 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008656 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008657 return ERR_ALERT | ERR_FATAL;
8658 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008659
Willy Tarreauef934602016-12-22 23:12:01 +01008660 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8661 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008662 else
8663 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008664
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01008665 if (!ssl_store_load_locations_file(conf->crl_file)) {
8666 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
8667 return ERR_ALERT | ERR_FATAL;
8668 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008669 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02008670#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02008671}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008672static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8673{
8674 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
8675}
Emeric Brun2b58d042012-09-20 17:10:03 +02008676
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008677/* parse the "curves" bind keyword keyword */
8678static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8679{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01008680#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008681 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008682 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008683 return ERR_ALERT | ERR_FATAL;
8684 }
8685 conf->curves = strdup(args[cur_arg + 1]);
8686 return 0;
8687#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008688 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008689 return ERR_ALERT | ERR_FATAL;
8690#endif
8691}
8692static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8693{
8694 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
8695}
8696
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008697/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008698static 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 +02008699{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008700#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01008701 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008702 return ERR_ALERT | ERR_FATAL;
8703#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01008704 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 +02008705 return ERR_ALERT | ERR_FATAL;
8706#else
8707 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008708 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008709 return ERR_ALERT | ERR_FATAL;
8710 }
8711
8712 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008713
8714 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02008715#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008716}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008717static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8718{
8719 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
8720}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008721
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008722/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02008723static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8724{
8725 int code;
8726 char *p = args[cur_arg + 1];
8727 unsigned long long *ignerr = &conf->crt_ignerr;
8728
8729 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008730 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008731 return ERR_ALERT | ERR_FATAL;
8732 }
8733
8734 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
8735 ignerr = &conf->ca_ignerr;
8736
8737 if (strcmp(p, "all") == 0) {
8738 *ignerr = ~0ULL;
8739 return 0;
8740 }
8741
8742 while (p) {
8743 code = atoi(p);
8744 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008745 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
8746 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008747 return ERR_ALERT | ERR_FATAL;
8748 }
8749 *ignerr |= 1ULL << code;
8750 p = strchr(p, ',');
8751 if (p)
8752 p++;
8753 }
8754
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008755 return 0;
8756}
8757
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008758/* parse tls_method_options "no-xxx" and "force-xxx" */
8759static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008760{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008761 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008762 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008763 p = strchr(arg, '-');
8764 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008765 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008766 p++;
8767 if (!strcmp(p, "sslv3"))
8768 v = CONF_SSLV3;
8769 else if (!strcmp(p, "tlsv10"))
8770 v = CONF_TLSV10;
8771 else if (!strcmp(p, "tlsv11"))
8772 v = CONF_TLSV11;
8773 else if (!strcmp(p, "tlsv12"))
8774 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008775 else if (!strcmp(p, "tlsv13"))
8776 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008777 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008778 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008779 if (!strncmp(arg, "no-", 3))
8780 methods->flags |= methodVersions[v].flag;
8781 else if (!strncmp(arg, "force-", 6))
8782 methods->min = methods->max = v;
8783 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008784 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008785 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008786 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01008787 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008788 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008789}
8790
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008791static 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 +02008792{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008793 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008794}
8795
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008796static 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 +02008797{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008798 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8799}
8800
8801/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8802static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8803{
8804 uint16_t i, v = 0;
8805 char *argv = args[cur_arg + 1];
8806 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008807 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008808 return ERR_ALERT | ERR_FATAL;
8809 }
8810 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8811 if (!strcmp(argv, methodVersions[i].name))
8812 v = i;
8813 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008814 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008815 return ERR_ALERT | ERR_FATAL;
8816 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008817 if (!strcmp("ssl-min-ver", args[cur_arg]))
8818 methods->min = v;
8819 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8820 methods->max = v;
8821 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01008822 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008823 return ERR_ALERT | ERR_FATAL;
8824 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008825 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008826}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008827
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008828static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8829{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008830#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008831 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 +02008832#endif
8833 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8834}
8835
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008836static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8837{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008838 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008839}
8840
8841static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8842{
8843 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8844}
8845
Emeric Brun2d0c4822012-10-02 13:45:20 +02008846/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008847static 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 +02008848{
Emeric Brun89675492012-10-05 13:48:26 +02008849 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008850 return 0;
8851}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008852
Olivier Houchardc2aae742017-09-22 18:26:28 +02008853/* parse the "allow-0rtt" bind keyword */
8854static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8855{
8856 conf->early_data = 1;
8857 return 0;
8858}
8859
8860static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8861{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008862 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008863 return 0;
8864}
8865
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008866/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008867static 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 +02008868{
Bernard Spil13c53f82018-02-15 13:34:58 +01008869#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008870 char *p1, *p2;
8871
8872 if (!*args[cur_arg + 1]) {
8873 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8874 return ERR_ALERT | ERR_FATAL;
8875 }
8876
8877 free(conf->npn_str);
8878
Willy Tarreau3724da12016-02-12 17:11:12 +01008879 /* the NPN string is built as a suite of (<len> <name>)*,
8880 * so we reuse each comma to store the next <len> and need
8881 * one more for the end of the string.
8882 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008883 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008884 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008885 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8886
8887 /* replace commas with the name length */
8888 p1 = conf->npn_str;
8889 p2 = p1 + 1;
8890 while (1) {
8891 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8892 if (!p2)
8893 p2 = p1 + 1 + strlen(p1 + 1);
8894
8895 if (p2 - (p1 + 1) > 255) {
8896 *p2 = '\0';
8897 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8898 return ERR_ALERT | ERR_FATAL;
8899 }
8900
8901 *p1 = p2 - (p1 + 1);
8902 p1 = p2;
8903
8904 if (!*p2)
8905 break;
8906
8907 *(p2++) = '\0';
8908 }
8909 return 0;
8910#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008911 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008912 return ERR_ALERT | ERR_FATAL;
8913#endif
8914}
8915
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008916static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8917{
8918 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8919}
8920
Willy Tarreauab861d32013-04-02 02:30:41 +02008921/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008922static 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 +02008923{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008924#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008925 char *p1, *p2;
8926
8927 if (!*args[cur_arg + 1]) {
8928 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
8929 return ERR_ALERT | ERR_FATAL;
8930 }
8931
8932 free(conf->alpn_str);
8933
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008934 /* the ALPN string is built as a suite of (<len> <name>)*,
8935 * so we reuse each comma to store the next <len> and need
8936 * one more for the end of the string.
8937 */
Willy Tarreauab861d32013-04-02 02:30:41 +02008938 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008939 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02008940 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
8941
8942 /* replace commas with the name length */
8943 p1 = conf->alpn_str;
8944 p2 = p1 + 1;
8945 while (1) {
8946 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
8947 if (!p2)
8948 p2 = p1 + 1 + strlen(p1 + 1);
8949
8950 if (p2 - (p1 + 1) > 255) {
8951 *p2 = '\0';
8952 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8953 return ERR_ALERT | ERR_FATAL;
8954 }
8955
8956 *p1 = p2 - (p1 + 1);
8957 p1 = p2;
8958
8959 if (!*p2)
8960 break;
8961
8962 *(p2++) = '\0';
8963 }
8964 return 0;
8965#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008966 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02008967 return ERR_ALERT | ERR_FATAL;
8968#endif
8969}
8970
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008971static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8972{
8973 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8974}
8975
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008976/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008977static 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 +02008978{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01008979 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02008980 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02008981
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008982 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
8983 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008984#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008985 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
8986 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
8987#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008988 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008989 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
8990 if (!conf->ssl_conf.ssl_methods.min)
8991 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
8992 if (!conf->ssl_conf.ssl_methods.max)
8993 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02008994
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008995 return 0;
8996}
8997
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008998/* parse the "prefer-client-ciphers" bind keyword */
8999static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9000{
9001 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9002 return 0;
9003}
9004
Christopher Faulet31af49d2015-06-09 17:29:50 +02009005/* parse the "generate-certificates" bind keyword */
9006static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9007{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009008#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009009 conf->generate_certs = 1;
9010#else
9011 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9012 err && *err ? *err : "");
9013#endif
9014 return 0;
9015}
9016
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009017/* parse the "strict-sni" bind keyword */
9018static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9019{
9020 conf->strict_sni = 1;
9021 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009022}
9023
9024/* parse the "tls-ticket-keys" bind keyword */
9025static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9026{
9027#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009028 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009029 int i = 0;
9030 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009031 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009032
9033 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009034 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009035 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009036 }
9037
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009038 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009039 if (keys_ref) {
9040 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009041 conf->keys_ref = keys_ref;
9042 return 0;
9043 }
9044
Christopher Faulete566f3d2019-10-21 09:55:49 +02009045 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009046 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009047 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009048 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009049 }
9050
Emeric Brun9e754772019-01-10 17:51:55 +01009051 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009052 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009053 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009054 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009055 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009056
9057 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009058 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009059 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009060 }
9061
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009062 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009063 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009064 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009065 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009066 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009067
Emeric Brun9e754772019-01-10 17:51:55 +01009068 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009069 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9070 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009071 int dec_size;
9072
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009073 /* Strip newline characters from the end */
9074 if(thisline[len - 1] == '\n')
9075 thisline[--len] = 0;
9076
9077 if(thisline[len - 1] == '\r')
9078 thisline[--len] = 0;
9079
Emeric Brun9e754772019-01-10 17:51:55 +01009080 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9081 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009082 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009083 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009084 }
Emeric Brun9e754772019-01-10 17:51:55 +01009085 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9086 keys_ref->key_size_bits = 128;
9087 }
9088 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9089 keys_ref->key_size_bits = 256;
9090 }
9091 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9092 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9093 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009094 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009095 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009096 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009097 i++;
9098 }
9099
9100 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009101 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 +02009102 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009103 }
9104
9105 fclose(f);
9106
9107 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009108 i -= 2;
9109 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009110 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009111 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009112 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009113 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009114
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009115 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9116
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009117 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009118
9119 fail:
9120 if (f)
9121 fclose(f);
9122 if (keys_ref) {
9123 free(keys_ref->filename);
9124 free(keys_ref->tlskeys);
9125 free(keys_ref);
9126 }
9127 return ERR_ALERT | ERR_FATAL;
9128
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009129#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009130 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009131 return ERR_ALERT | ERR_FATAL;
9132#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009133}
9134
Emeric Brund94b3fe2012-09-20 18:23:56 +02009135/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009136static 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 +02009137{
9138 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009139 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009140 return ERR_ALERT | ERR_FATAL;
9141 }
9142
9143 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009144 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009145 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009146 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009147 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009148 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009149 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009150 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9151 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009152 return ERR_ALERT | ERR_FATAL;
9153 }
9154
9155 return 0;
9156}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009157static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9158{
9159 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9160}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009161
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009162/* parse the "no-ca-names" bind keyword */
9163static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9164{
9165 conf->no_ca_names = 1;
9166 return 0;
9167}
9168static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9169{
9170 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9171}
9172
Willy Tarreau92faadf2012-10-10 23:04:25 +02009173/************** "server" keywords ****************/
9174
Olivier Houchardc7566002018-11-20 23:33:50 +01009175/* parse the "npn" bind keyword */
9176static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9177{
9178#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9179 char *p1, *p2;
9180
9181 if (!*args[*cur_arg + 1]) {
9182 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9183 return ERR_ALERT | ERR_FATAL;
9184 }
9185
9186 free(newsrv->ssl_ctx.npn_str);
9187
9188 /* the NPN string is built as a suite of (<len> <name>)*,
9189 * so we reuse each comma to store the next <len> and need
9190 * one more for the end of the string.
9191 */
9192 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9193 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9194 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9195 newsrv->ssl_ctx.npn_len);
9196
9197 /* replace commas with the name length */
9198 p1 = newsrv->ssl_ctx.npn_str;
9199 p2 = p1 + 1;
9200 while (1) {
9201 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9202 newsrv->ssl_ctx.npn_len - (p1 + 1));
9203 if (!p2)
9204 p2 = p1 + 1 + strlen(p1 + 1);
9205
9206 if (p2 - (p1 + 1) > 255) {
9207 *p2 = '\0';
9208 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9209 return ERR_ALERT | ERR_FATAL;
9210 }
9211
9212 *p1 = p2 - (p1 + 1);
9213 p1 = p2;
9214
9215 if (!*p2)
9216 break;
9217
9218 *(p2++) = '\0';
9219 }
9220 return 0;
9221#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009222 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009223 return ERR_ALERT | ERR_FATAL;
9224#endif
9225}
9226
Olivier Houchard92150142018-12-21 19:47:01 +01009227/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009228static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9229{
9230#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9231 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009232 char **alpn_str;
9233 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009234
Olivier Houchard92150142018-12-21 19:47:01 +01009235 if (*args[*cur_arg] == 'c') {
9236 alpn_str = &newsrv->check.alpn_str;
9237 alpn_len = &newsrv->check.alpn_len;
9238 } else {
9239 alpn_str = &newsrv->ssl_ctx.alpn_str;
9240 alpn_len = &newsrv->ssl_ctx.alpn_len;
9241
9242 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009243 if (!*args[*cur_arg + 1]) {
9244 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9245 return ERR_ALERT | ERR_FATAL;
9246 }
9247
Olivier Houchard92150142018-12-21 19:47:01 +01009248 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009249
9250 /* the ALPN string is built as a suite of (<len> <name>)*,
9251 * so we reuse each comma to store the next <len> and need
9252 * one more for the end of the string.
9253 */
Olivier Houchard92150142018-12-21 19:47:01 +01009254 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9255 *alpn_str = calloc(1, *alpn_len + 1);
9256 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009257
9258 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009259 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009260 p2 = p1 + 1;
9261 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009262 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009263 if (!p2)
9264 p2 = p1 + 1 + strlen(p1 + 1);
9265
9266 if (p2 - (p1 + 1) > 255) {
9267 *p2 = '\0';
9268 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9269 return ERR_ALERT | ERR_FATAL;
9270 }
9271
9272 *p1 = p2 - (p1 + 1);
9273 p1 = p2;
9274
9275 if (!*p2)
9276 break;
9277
9278 *(p2++) = '\0';
9279 }
9280 return 0;
9281#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009282 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009283 return ERR_ALERT | ERR_FATAL;
9284#endif
9285}
9286
Emeric Brunef42d922012-10-11 16:11:36 +02009287/* parse the "ca-file" server keyword */
9288static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9289{
9290 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009291 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009292 return ERR_ALERT | ERR_FATAL;
9293 }
9294
Willy Tarreauef934602016-12-22 23:12:01 +01009295 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9296 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009297 else
9298 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9299
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009300 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9301 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9302 return ERR_ALERT | ERR_FATAL;
9303 }
Emeric Brunef42d922012-10-11 16:11:36 +02009304 return 0;
9305}
9306
Olivier Houchard9130a962017-10-17 17:33:43 +02009307/* parse the "check-sni" server keyword */
9308static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9309{
9310 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009311 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009312 return ERR_ALERT | ERR_FATAL;
9313 }
9314
9315 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9316 if (!newsrv->check.sni) {
9317 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9318 return ERR_ALERT | ERR_FATAL;
9319 }
9320 return 0;
9321
9322}
9323
Willy Tarreau92faadf2012-10-10 23:04:25 +02009324/* parse the "check-ssl" server keyword */
9325static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9326{
9327 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009328 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9329 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009330#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009331 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9332 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9333#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009334 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009335 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9336 if (!newsrv->ssl_ctx.methods.min)
9337 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9338 if (!newsrv->ssl_ctx.methods.max)
9339 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9340
Willy Tarreau92faadf2012-10-10 23:04:25 +02009341 return 0;
9342}
9343
9344/* parse the "ciphers" server keyword */
9345static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9346{
9347 if (!*args[*cur_arg + 1]) {
9348 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9349 return ERR_ALERT | ERR_FATAL;
9350 }
9351
9352 free(newsrv->ssl_ctx.ciphers);
9353 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9354 return 0;
9355}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009356
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009357#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009358/* parse the "ciphersuites" server keyword */
9359static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9360{
9361 if (!*args[*cur_arg + 1]) {
9362 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9363 return ERR_ALERT | ERR_FATAL;
9364 }
9365
9366 free(newsrv->ssl_ctx.ciphersuites);
9367 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9368 return 0;
9369}
9370#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009371
Emeric Brunef42d922012-10-11 16:11:36 +02009372/* parse the "crl-file" server keyword */
9373static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9374{
9375#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009376 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009377 return ERR_ALERT | ERR_FATAL;
9378#else
9379 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009380 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009381 return ERR_ALERT | ERR_FATAL;
9382 }
9383
Willy Tarreauef934602016-12-22 23:12:01 +01009384 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9385 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009386 else
9387 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9388
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009389 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9390 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9391 return ERR_ALERT | ERR_FATAL;
9392 }
Emeric Brunef42d922012-10-11 16:11:36 +02009393 return 0;
9394#endif
9395}
9396
Emeric Bruna7aa3092012-10-26 12:58:00 +02009397/* parse the "crt" server keyword */
9398static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9399{
9400 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009401 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009402 return ERR_ALERT | ERR_FATAL;
9403 }
9404
Willy Tarreauef934602016-12-22 23:12:01 +01009405 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009406 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009407 else
9408 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9409
9410 return 0;
9411}
Emeric Brunef42d922012-10-11 16:11:36 +02009412
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009413/* parse the "no-check-ssl" server keyword */
9414static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9415{
9416 newsrv->check.use_ssl = 0;
9417 free(newsrv->ssl_ctx.ciphers);
9418 newsrv->ssl_ctx.ciphers = NULL;
9419 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9420 return 0;
9421}
9422
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009423/* parse the "no-send-proxy-v2-ssl" server keyword */
9424static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9425{
9426 newsrv->pp_opts &= ~SRV_PP_V2;
9427 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9428 return 0;
9429}
9430
9431/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9432static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9433{
9434 newsrv->pp_opts &= ~SRV_PP_V2;
9435 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9436 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9437 return 0;
9438}
9439
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009440/* parse the "no-ssl" server keyword */
9441static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9442{
9443 newsrv->use_ssl = 0;
9444 free(newsrv->ssl_ctx.ciphers);
9445 newsrv->ssl_ctx.ciphers = NULL;
9446 return 0;
9447}
9448
Olivier Houchard522eea72017-11-03 16:27:47 +01009449/* parse the "allow-0rtt" server keyword */
9450static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9451{
9452 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9453 return 0;
9454}
9455
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009456/* parse the "no-ssl-reuse" server keyword */
9457static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9458{
9459 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9460 return 0;
9461}
9462
Emeric Brunf9c5c472012-10-11 15:28:34 +02009463/* parse the "no-tls-tickets" server keyword */
9464static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9465{
9466 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9467 return 0;
9468}
David Safb76832014-05-08 23:42:08 -04009469/* parse the "send-proxy-v2-ssl" server keyword */
9470static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9471{
9472 newsrv->pp_opts |= SRV_PP_V2;
9473 newsrv->pp_opts |= SRV_PP_V2_SSL;
9474 return 0;
9475}
9476
9477/* parse the "send-proxy-v2-ssl-cn" server keyword */
9478static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9479{
9480 newsrv->pp_opts |= SRV_PP_V2;
9481 newsrv->pp_opts |= SRV_PP_V2_SSL;
9482 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9483 return 0;
9484}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009485
Willy Tarreau732eac42015-07-09 11:40:25 +02009486/* parse the "sni" server keyword */
9487static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9488{
9489#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9490 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9491 return ERR_ALERT | ERR_FATAL;
9492#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009493 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009494
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009495 arg = args[*cur_arg + 1];
9496 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009497 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9498 return ERR_ALERT | ERR_FATAL;
9499 }
9500
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009501 free(newsrv->sni_expr);
9502 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02009503
Willy Tarreau732eac42015-07-09 11:40:25 +02009504 return 0;
9505#endif
9506}
9507
Willy Tarreau92faadf2012-10-10 23:04:25 +02009508/* parse the "ssl" server keyword */
9509static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9510{
9511 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009512 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9513 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009514#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009515 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9516 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9517#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009518 return 0;
9519}
9520
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009521/* parse the "ssl-reuse" server keyword */
9522static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9523{
9524 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
9525 return 0;
9526}
9527
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009528/* parse the "tls-tickets" server keyword */
9529static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9530{
9531 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
9532 return 0;
9533}
9534
Emeric Brunef42d922012-10-11 16:11:36 +02009535/* parse the "verify" server keyword */
9536static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9537{
9538 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009539 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009540 return ERR_ALERT | ERR_FATAL;
9541 }
9542
9543 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009544 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02009545 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009546 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02009547 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009548 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
9549 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009550 return ERR_ALERT | ERR_FATAL;
9551 }
9552
Evan Broderbe554312013-06-27 00:05:25 -07009553 return 0;
9554}
9555
9556/* parse the "verifyhost" server keyword */
9557static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9558{
9559 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009560 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -07009561 return ERR_ALERT | ERR_FATAL;
9562 }
9563
Frédéric Lécaille273f3212017-03-13 15:52:01 +01009564 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07009565 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
9566
Emeric Brunef42d922012-10-11 16:11:36 +02009567 return 0;
9568}
9569
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009570/* parse the "ssl-default-bind-options" keyword in global section */
9571static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
9572 struct proxy *defpx, const char *file, int line,
9573 char **err) {
9574 int i = 1;
9575
9576 if (*(args[i]) == 0) {
9577 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9578 return -1;
9579 }
9580 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009581 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009582 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009583 else if (!strcmp(args[i], "prefer-client-ciphers"))
9584 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009585 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9586 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
9587 i++;
9588 else {
9589 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9590 return -1;
9591 }
9592 }
9593 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009594 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9595 return -1;
9596 }
9597 i++;
9598 }
9599 return 0;
9600}
9601
9602/* parse the "ssl-default-server-options" keyword in global section */
9603static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
9604 struct proxy *defpx, const char *file, int line,
9605 char **err) {
9606 int i = 1;
9607
9608 if (*(args[i]) == 0) {
9609 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9610 return -1;
9611 }
9612 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009613 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009614 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009615 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9616 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
9617 i++;
9618 else {
9619 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9620 return -1;
9621 }
9622 }
9623 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009624 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9625 return -1;
9626 }
9627 i++;
9628 }
9629 return 0;
9630}
9631
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009632/* parse the "ca-base" / "crt-base" keywords in global section.
9633 * Returns <0 on alert, >0 on warning, 0 on success.
9634 */
9635static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
9636 struct proxy *defpx, const char *file, int line,
9637 char **err)
9638{
9639 char **target;
9640
Willy Tarreauef934602016-12-22 23:12:01 +01009641 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009642
9643 if (too_many_args(1, args, err, NULL))
9644 return -1;
9645
9646 if (*target) {
9647 memprintf(err, "'%s' already specified.", args[0]);
9648 return -1;
9649 }
9650
9651 if (*(args[1]) == 0) {
9652 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
9653 return -1;
9654 }
9655 *target = strdup(args[1]);
9656 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009657}
9658
9659/* parse the "ssl-mode-async" keyword in global section.
9660 * Returns <0 on alert, >0 on warning, 0 on success.
9661 */
9662static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
9663 struct proxy *defpx, const char *file, int line,
9664 char **err)
9665{
Willy Tarreau5db847a2019-05-09 14:13:35 +02009666#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009667 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01009668 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009669 return 0;
9670#else
9671 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
9672 return -1;
9673#endif
9674}
9675
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009676#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009677static int ssl_check_async_engine_count(void) {
9678 int err_code = 0;
9679
Emeric Brun3854e012017-05-17 20:42:48 +02009680 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009681 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009682 err_code = ERR_ABORT;
9683 }
9684 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009685}
9686
Grant Zhang872f9c22017-01-21 01:10:18 +00009687/* parse the "ssl-engine" keyword in global section.
9688 * Returns <0 on alert, >0 on warning, 0 on success.
9689 */
9690static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9691 struct proxy *defpx, const char *file, int line,
9692 char **err)
9693{
9694 char *algo;
9695 int ret = -1;
9696
9697 if (*(args[1]) == 0) {
9698 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9699 return ret;
9700 }
9701
9702 if (*(args[2]) == 0) {
9703 /* if no list of algorithms is given, it defaults to ALL */
9704 algo = strdup("ALL");
9705 goto add_engine;
9706 }
9707
9708 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
9709 if (strcmp(args[2], "algo") != 0) {
9710 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
9711 return ret;
9712 }
9713
9714 if (*(args[3]) == 0) {
9715 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
9716 return ret;
9717 }
9718 algo = strdup(args[3]);
9719
9720add_engine:
9721 if (ssl_init_single_engine(args[1], algo)==0) {
9722 openssl_engines_initialized++;
9723 ret = 0;
9724 }
9725 free(algo);
9726 return ret;
9727}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009728#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00009729
Willy Tarreauf22e9682016-12-21 23:23:19 +01009730/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
9731 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9732 */
9733static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
9734 struct proxy *defpx, const char *file, int line,
9735 char **err)
9736{
9737 char **target;
9738
Willy Tarreauef934602016-12-22 23:12:01 +01009739 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01009740
9741 if (too_many_args(1, args, err, NULL))
9742 return -1;
9743
9744 if (*(args[1]) == 0) {
9745 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9746 return -1;
9747 }
9748
9749 free(*target);
9750 *target = strdup(args[1]);
9751 return 0;
9752}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009753
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009754#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009755/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9756 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9757 */
9758static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9759 struct proxy *defpx, const char *file, int line,
9760 char **err)
9761{
9762 char **target;
9763
9764 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9765
9766 if (too_many_args(1, args, err, NULL))
9767 return -1;
9768
9769 if (*(args[1]) == 0) {
9770 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9771 return -1;
9772 }
9773
9774 free(*target);
9775 *target = strdup(args[1]);
9776 return 0;
9777}
9778#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009779
Willy Tarreau9ceda382016-12-21 23:13:03 +01009780/* parse various global tune.ssl settings consisting in positive integers.
9781 * Returns <0 on alert, >0 on warning, 0 on success.
9782 */
9783static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9784 struct proxy *defpx, const char *file, int line,
9785 char **err)
9786{
9787 int *target;
9788
9789 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9790 target = &global.tune.sslcachesize;
9791 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009792 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009793 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009794 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009795 else if (strcmp(args[0], "maxsslconn") == 0)
9796 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009797 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9798 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009799 else {
9800 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9801 return -1;
9802 }
9803
9804 if (too_many_args(1, args, err, NULL))
9805 return -1;
9806
9807 if (*(args[1]) == 0) {
9808 memprintf(err, "'%s' expects an integer argument.", args[0]);
9809 return -1;
9810 }
9811
9812 *target = atoi(args[1]);
9813 if (*target < 0) {
9814 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9815 return -1;
9816 }
9817 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009818}
9819
9820static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9821 struct proxy *defpx, const char *file, int line,
9822 char **err)
9823{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009824 int ret;
9825
9826 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9827 if (ret != 0)
9828 return ret;
9829
Willy Tarreaubafbe012017-11-24 17:34:44 +01009830 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009831 memprintf(err, "'%s' is already configured.", args[0]);
9832 return -1;
9833 }
9834
Willy Tarreaubafbe012017-11-24 17:34:44 +01009835 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9836 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009837 memprintf(err, "Out of memory error.");
9838 return -1;
9839 }
9840 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009841}
9842
9843/* parse "ssl.force-private-cache".
9844 * Returns <0 on alert, >0 on warning, 0 on success.
9845 */
9846static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9847 struct proxy *defpx, const char *file, int line,
9848 char **err)
9849{
9850 if (too_many_args(0, args, err, NULL))
9851 return -1;
9852
Willy Tarreauef934602016-12-22 23:12:01 +01009853 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009854 return 0;
9855}
9856
9857/* parse "ssl.lifetime".
9858 * Returns <0 on alert, >0 on warning, 0 on success.
9859 */
9860static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9861 struct proxy *defpx, const char *file, int line,
9862 char **err)
9863{
9864 const char *res;
9865
9866 if (too_many_args(1, args, err, NULL))
9867 return -1;
9868
9869 if (*(args[1]) == 0) {
9870 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9871 return -1;
9872 }
9873
Willy Tarreauef934602016-12-22 23:12:01 +01009874 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009875 if (res == PARSE_TIME_OVER) {
9876 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9877 args[1], args[0]);
9878 return -1;
9879 }
9880 else if (res == PARSE_TIME_UNDER) {
9881 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9882 args[1], args[0]);
9883 return -1;
9884 }
9885 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009886 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9887 return -1;
9888 }
9889 return 0;
9890}
9891
9892#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009893/* parse "ssl-dh-param-file".
9894 * Returns <0 on alert, >0 on warning, 0 on success.
9895 */
9896static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9897 struct proxy *defpx, const char *file, int line,
9898 char **err)
9899{
9900 if (too_many_args(1, args, err, NULL))
9901 return -1;
9902
9903 if (*(args[1]) == 0) {
9904 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9905 return -1;
9906 }
9907
9908 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9909 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9910 return -1;
9911 }
9912 return 0;
9913}
9914
Willy Tarreau9ceda382016-12-21 23:13:03 +01009915/* parse "ssl.default-dh-param".
9916 * Returns <0 on alert, >0 on warning, 0 on success.
9917 */
9918static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9919 struct proxy *defpx, const char *file, int line,
9920 char **err)
9921{
9922 if (too_many_args(1, args, err, NULL))
9923 return -1;
9924
9925 if (*(args[1]) == 0) {
9926 memprintf(err, "'%s' expects an integer argument.", args[0]);
9927 return -1;
9928 }
9929
Willy Tarreauef934602016-12-22 23:12:01 +01009930 global_ssl.default_dh_param = atoi(args[1]);
9931 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009932 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9933 return -1;
9934 }
9935 return 0;
9936}
9937#endif
9938
9939
William Lallemand32af2032016-10-29 18:09:35 +02009940/* This function is used with TLS ticket keys management. It permits to browse
9941 * each reference. The variable <getnext> must contain the current node,
9942 * <end> point to the root node.
9943 */
9944#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9945static inline
9946struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
9947{
9948 struct tls_keys_ref *ref = getnext;
9949
9950 while (1) {
9951
9952 /* Get next list entry. */
9953 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
9954
9955 /* If the entry is the last of the list, return NULL. */
9956 if (&ref->list == end)
9957 return NULL;
9958
9959 return ref;
9960 }
9961}
9962
9963static inline
9964struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
9965{
9966 int id;
9967 char *error;
9968
9969 /* If the reference starts by a '#', this is numeric id. */
9970 if (reference[0] == '#') {
9971 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
9972 id = strtol(reference + 1, &error, 10);
9973 if (*error != '\0')
9974 return NULL;
9975
9976 /* Perform the unique id lookup. */
9977 return tlskeys_ref_lookupid(id);
9978 }
9979
9980 /* Perform the string lookup. */
9981 return tlskeys_ref_lookup(reference);
9982}
9983#endif
9984
9985
9986#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9987
9988static int cli_io_handler_tlskeys_files(struct appctx *appctx);
9989
9990static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
9991 return cli_io_handler_tlskeys_files(appctx);
9992}
9993
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009994/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
9995 * (next index to be dumped), and cli.p0 (next key reference).
9996 */
William Lallemand32af2032016-10-29 18:09:35 +02009997static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
9998
9999 struct stream_interface *si = appctx->owner;
10000
10001 switch (appctx->st2) {
10002 case STAT_ST_INIT:
10003 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010004 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010005 * later and restart at the state "STAT_ST_INIT".
10006 */
10007 chunk_reset(&trash);
10008
10009 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10010 chunk_appendf(&trash, "# id secret\n");
10011 else
10012 chunk_appendf(&trash, "# id (file)\n");
10013
Willy Tarreau06d80a92017-10-19 14:32:15 +020010014 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010015 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010016 return 0;
10017 }
10018
William Lallemand32af2032016-10-29 18:09:35 +020010019 /* Now, we start the browsing of the references lists.
10020 * Note that the following call to LIST_ELEM return bad pointer. The only
10021 * available field of this pointer is <list>. It is used with the function
10022 * tlskeys_list_get_next() for retruning the first available entry
10023 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010024 if (appctx->ctx.cli.p0 == NULL) {
10025 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10026 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010027 }
10028
10029 appctx->st2 = STAT_ST_LIST;
10030 /* fall through */
10031
10032 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010033 while (appctx->ctx.cli.p0) {
10034 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010035
10036 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010037 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010038 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010039
10040 if (appctx->ctx.cli.i1 == 0)
10041 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10042
William Lallemand32af2032016-10-29 18:09:35 +020010043 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010044 int head;
10045
10046 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10047 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010048 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010049 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010050
10051 chunk_reset(t2);
10052 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010053 if (ref->key_size_bits == 128) {
10054 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10055 sizeof(struct tls_sess_key_128),
10056 t2->area, t2->size);
10057 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10058 t2->area);
10059 }
10060 else if (ref->key_size_bits == 256) {
10061 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10062 sizeof(struct tls_sess_key_256),
10063 t2->area, t2->size);
10064 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10065 t2->area);
10066 }
10067 else {
10068 /* This case should never happen */
10069 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10070 }
William Lallemand32af2032016-10-29 18:09:35 +020010071
Willy Tarreau06d80a92017-10-19 14:32:15 +020010072 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010073 /* let's try again later from this stream. We add ourselves into
10074 * this stream's users so that it can remove us upon termination.
10075 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010076 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010077 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010078 return 0;
10079 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010080 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010081 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010082 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010083 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010084 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010085 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010086 /* let's try again later from this stream. We add ourselves into
10087 * this stream's users so that it can remove us upon termination.
10088 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010089 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010090 return 0;
10091 }
10092
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010093 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010094 break;
10095
10096 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010097 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010098 }
10099
10100 appctx->st2 = STAT_ST_FIN;
10101 /* fall through */
10102
10103 default:
10104 appctx->st2 = STAT_ST_FIN;
10105 return 1;
10106 }
10107 return 0;
10108}
10109
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010110/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010111static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010112{
William Lallemand32af2032016-10-29 18:09:35 +020010113 /* no parameter, shows only file list */
10114 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010115 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010116 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010117 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010118 }
10119
10120 if (args[2][0] == '*') {
10121 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010122 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010123 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010124 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010125 if (!appctx->ctx.cli.p0)
10126 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010127 }
William Lallemand32af2032016-10-29 18:09:35 +020010128 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010129 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010130}
10131
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010132static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010133{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010134 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010135 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010136
William Lallemand32af2032016-10-29 18:09:35 +020010137 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010138 if (!*args[3] || !*args[4])
10139 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 +020010140
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010141 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010142 if (!ref)
10143 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010144
Willy Tarreau1c913e42018-08-22 05:26:57 +020010145 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010146 if (ret < 0)
10147 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010148
Willy Tarreau1c913e42018-08-22 05:26:57 +020010149 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010150 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10151 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010152
Willy Tarreau9d008692019-08-09 11:21:01 +020010153 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010154}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010155#endif
William Lallemand32af2032016-10-29 18:09:35 +020010156
William Lallemand44b35322019-10-17 16:28:40 +020010157
10158/* Type of SSL payloads that can be updated over the CLI */
10159
10160enum {
10161 CERT_TYPE_PEM = 0,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010162#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010163 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010164#endif
William Lallemand44b35322019-10-17 16:28:40 +020010165 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010166#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,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010168#endif
William Lallemand44b35322019-10-17 16:28:40 +020010169 CERT_TYPE_MAX,
10170};
10171
10172struct {
10173 const char *ext;
10174 int type;
10175 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
10176 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020010177} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020010178 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand541a5342019-10-23 14:11:54 +020010179#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010180 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010181#endif
10182#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010183 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010184#endif
William Lallemand44b35322019-10-17 16:28:40 +020010185 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020010186 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020010187};
10188
William Lallemand430413e2019-10-28 14:30:47 +010010189/* states of the CLI IO handler for 'set ssl cert' */
10190enum {
10191 SETCERT_ST_INIT = 0,
10192 SETCERT_ST_GEN,
10193 SETCERT_ST_INSERT,
10194 SETCERT_ST_FIN,
10195};
William Lallemand8f840d72019-10-23 10:53:05 +020010196
William Lallemandd4f946c2019-12-05 10:26:40 +010010197/* release function of the `show ssl cert' command */
10198static void cli_release_show_cert(struct appctx *appctx)
10199{
10200 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10201}
10202
10203/* IO handler of "show ssl cert <filename>" */
10204static int cli_io_handler_show_cert(struct appctx *appctx)
10205{
10206 struct buffer *trash = alloc_trash_chunk();
10207 struct ebmb_node *node;
10208 struct stream_interface *si = appctx->owner;
10209 struct ckch_store *ckchs;
10210 int n;
10211
10212 if (trash == NULL)
10213 return 1;
10214
10215 if (!appctx->ctx.ssl.old_ckchs) {
10216 if (ckchs_transaction.old_ckchs) {
10217 ckchs = ckchs_transaction.old_ckchs;
10218 chunk_appendf(trash, "# transaction\n");
10219 if (!ckchs->multi) {
10220 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010221#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010222 } else {
10223 chunk_appendf(trash, "*%s:", ckchs->path);
10224 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10225 if (ckchs->ckch[n].cert)
10226 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10227 }
10228 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010229#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010230 }
10231 }
10232 }
10233
10234 if (!appctx->ctx.cli.p0) {
10235 chunk_appendf(trash, "# filename\n");
10236 node = ebmb_first(&ckchs_tree);
10237 } else {
10238 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
10239 }
10240 while (node) {
10241 ckchs = ebmb_entry(node, struct ckch_store, node);
10242 if (!ckchs->multi) {
10243 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010244#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010245 } else {
10246 chunk_appendf(trash, "%s:", ckchs->path);
10247 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10248 if (ckchs->ckch[n].cert)
10249 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10250 }
10251 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010252#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010253 }
10254
10255 node = ebmb_next(node);
10256 if (ci_putchk(si_ic(si), trash) == -1) {
10257 si_rx_room_blk(si);
10258 goto yield;
10259 }
10260 }
10261
10262 appctx->ctx.cli.p0 = NULL;
10263 free_trash_chunk(trash);
10264 return 1;
10265yield:
10266
10267 free_trash_chunk(trash);
10268 appctx->ctx.cli.p0 = ckchs;
10269 return 0; /* should come back */
10270}
10271
10272/* IO handler of the details "show ssl cert <filename>" */
10273static int cli_io_handler_show_cert_detail(struct appctx *appctx)
10274{
10275 struct stream_interface *si = appctx->owner;
10276 struct ckch_store *ckchs = appctx->ctx.cli.p0;
10277 struct buffer *out = alloc_trash_chunk();
10278 struct buffer *tmp = alloc_trash_chunk();
10279 X509_NAME *name = NULL;
10280 int write = -1;
10281 BIO *bio = NULL;
10282
10283 if (!tmp || !out)
10284 goto end;
10285
10286 if (!ckchs->multi) {
10287 chunk_appendf(out, "Filename: ");
10288 if (ckchs == ckchs_transaction.new_ckchs)
10289 chunk_appendf(out, "*");
10290 chunk_appendf(out, "%s\n", ckchs->path);
10291 chunk_appendf(out, "Serial: ");
10292 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
10293 goto end;
10294 dump_binary(out, tmp->area, tmp->data);
10295 chunk_appendf(out, "\n");
10296
10297 chunk_appendf(out, "notBefore: ");
10298 chunk_reset(tmp);
10299 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10300 goto end;
10301 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
10302 goto end;
10303 write = BIO_read(bio, tmp->area, tmp->size-1);
10304 tmp->area[write] = '\0';
10305 BIO_free(bio);
10306 chunk_appendf(out, "%s\n", tmp->area);
10307
10308 chunk_appendf(out, "notAfter: ");
10309 chunk_reset(tmp);
10310 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10311 goto end;
10312 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
10313 goto end;
10314 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
10315 goto end;
10316 tmp->area[write] = '\0';
10317 BIO_free(bio);
10318 chunk_appendf(out, "%s\n", tmp->area);
10319
10320
10321 chunk_appendf(out, "Issuer: ");
10322 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
10323 goto end;
10324 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10325 goto end;
10326 *(tmp->area + tmp->data) = '\0';
10327 chunk_appendf(out, "%s\n", tmp->area);
10328
10329 chunk_appendf(out, "Subject: ");
10330 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
10331 goto end;
10332 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10333 goto end;
10334 *(tmp->area + tmp->data) = '\0';
10335 chunk_appendf(out, "%s\n", tmp->area);
10336
10337
10338#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10339 chunk_appendf(out, "Subject Alternative Name: ");
10340 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
10341 goto end;
10342 *(out->area + out->data) = '\0';
10343 chunk_appendf(out, "\n");
10344#endif
10345 chunk_reset(tmp);
10346 chunk_appendf(out, "Algorithm: ");
10347 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
10348 goto end;
10349 chunk_appendf(out, "%s\n", tmp->area);
10350
10351 chunk_reset(tmp);
10352 chunk_appendf(out, "SHA1 FingerPrint: ");
10353 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area,
10354 (unsigned int *)&tmp->data) == 0)
10355 goto end;
10356 dump_binary(out, tmp->area, tmp->data);
10357 chunk_appendf(out, "\n");
10358 }
10359
10360 if (ci_putchk(si_ic(si), out) == -1) {
10361 si_rx_room_blk(si);
10362 goto yield;
10363 }
10364
10365end:
10366 free_trash_chunk(tmp);
10367 free_trash_chunk(out);
10368 return 1;
10369yield:
10370 free_trash_chunk(tmp);
10371 free_trash_chunk(out);
10372 return 0; /* should come back */
10373}
10374
10375/* parsing function for 'show ssl cert [certfile]' */
10376static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
10377{
10378 struct ckch_store *ckchs;
10379
10380 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
10381 return cli_err(appctx, "Can't allocate memory!\n");
10382
10383 /* The operations on the CKCH architecture are locked so we can
10384 * manipulate ckch_store and ckch_inst */
10385 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10386 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
10387
10388 /* check if there is a certificate to lookup */
10389 if (*args[3]) {
10390 if (*args[3] == '*') {
10391 if (!ckchs_transaction.new_ckchs)
10392 goto error;
10393
10394 ckchs = ckchs_transaction.new_ckchs;
10395
10396 if (strcmp(args[3] + 1, ckchs->path))
10397 goto error;
10398
10399 } else {
10400 if ((ckchs = ckchs_lookup(args[3])) == NULL)
10401 goto error;
10402
10403 }
10404
10405 if (ckchs->multi)
10406 goto error;
10407
10408 appctx->ctx.cli.p0 = ckchs;
10409 /* use the IO handler that shows details */
10410 appctx->io_handler = cli_io_handler_show_cert_detail;
10411 }
10412
10413 return 0;
10414
10415error:
10416 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10417 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
10418}
10419
William Lallemand430413e2019-10-28 14:30:47 +010010420/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010421static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010422{
10423 struct ckch_store *new_ckchs;
10424 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010425
William Lallemand430413e2019-10-28 14:30:47 +010010426 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020010427
William Lallemand430413e2019-10-28 14:30:47 +010010428 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020010429 /* 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 +010010430 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010431
William Lallemandbeea2a42019-10-30 17:45:33 +010010432 if (!new_ckchs)
10433 return;
William Lallemand8f840d72019-10-23 10:53:05 +020010434
William Lallemandbeea2a42019-10-30 17:45:33 +010010435 /* if the allocation failed, we need to free everything from the temporary list */
10436 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10437 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010438
William Lallemandbeea2a42019-10-30 17:45:33 +010010439 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10440 if (sc0->order == 0) /* we only free if it's the first inserted */
10441 SSL_CTX_free(sc0->ctx);
10442 LIST_DEL(&sc0->by_ckch_inst);
10443 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020010444 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010445 LIST_DEL(&ckchi->by_ckchs);
10446 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020010447 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010448 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010449 }
10450}
10451
10452
10453/*
10454 * This function tries to create the new ckch_inst and their SNIs
10455 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010456static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010457{
10458 struct stream_interface *si = appctx->owner;
10459 int y = 0;
10460 char *err = NULL;
10461 int errcode = 0;
10462 struct ckch_store *old_ckchs, *new_ckchs = NULL;
10463 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010464 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010465 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010466
William Lallemand33cc76f2019-10-31 11:43:45 +010010467 if (trash == NULL)
10468 goto error;
10469
William Lallemand8f840d72019-10-23 10:53:05 +020010470 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
10471 goto error;
10472
William Lallemand430413e2019-10-28 14:30:47 +010010473 while (1) {
10474 switch (appctx->st2) {
10475 case SETCERT_ST_INIT:
10476 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010477 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010010478 if (ci_putchk(si_ic(si), trash) == -1) {
10479 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020010480 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010010481 }
10482 appctx->st2 = SETCERT_ST_GEN;
10483 /* fallthrough */
10484 case SETCERT_ST_GEN:
10485 /*
10486 * This state generates the ckch instances with their
10487 * sni_ctxs and SSL_CTX.
10488 *
William Lallemand430413e2019-10-28 14:30:47 +010010489 * Since the SSL_CTX generation can be CPU consumer, we
10490 * yield every 10 instances.
10491 */
William Lallemand8f840d72019-10-23 10:53:05 +020010492
William Lallemandbeea2a42019-10-30 17:45:33 +010010493 old_ckchs = appctx->ctx.ssl.old_ckchs;
10494 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010495
William Lallemandbeea2a42019-10-30 17:45:33 +010010496 if (!new_ckchs)
10497 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020010498
William Lallemandbeea2a42019-10-30 17:45:33 +010010499 /* get the next ckchi to regenerate */
10500 ckchi = appctx->ctx.ssl.next_ckchi;
10501 /* we didn't start yet, set it to the first elem */
10502 if (ckchi == NULL)
10503 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010504
William Lallemandbeea2a42019-10-30 17:45:33 +010010505 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
10506 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
10507 struct ckch_inst *new_inst;
William Lallemand8f840d72019-10-23 10:53:05 +020010508
William Lallemandbeea2a42019-10-30 17:45:33 +010010509 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
10510 if (y >= 10) {
10511 /* save the next ckchi to compute */
10512 appctx->ctx.ssl.next_ckchi = ckchi;
10513 goto yield;
10514 }
William Lallemand8f840d72019-10-23 10:53:05 +020010515
William Lallemandbeea2a42019-10-30 17:45:33 +010010516 if (new_ckchs->multi)
10517 errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
10518 else
10519 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 +020010520
William Lallemandbeea2a42019-10-30 17:45:33 +010010521 if (errcode & ERR_CODE)
10522 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020010523
William Lallemand21724f02019-11-04 17:56:13 +010010524 /* if the previous ckchi was used as the default */
10525 if (ckchi->is_default)
10526 new_inst->is_default = 1;
10527
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010528 /* we need to initialize the SSL_CTX generated */
10529 /* TODO: the prepare_ctx function need to be reworked to be safer there */
10530 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10531 if (!sc0->order) { /* we initiliazed only the first SSL_CTX because it's the same in the other sni_ctx's */
10532 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
10533 if (errcode & ERR_CODE)
10534 goto error;
10535 }
10536 }
10537
10538
William Lallemandbeea2a42019-10-30 17:45:33 +010010539 /* display one dot per new instance */
10540 chunk_appendf(trash, ".");
10541 /* link the new ckch_inst to the duplicate */
10542 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
10543 y++;
10544 }
William Lallemand430413e2019-10-28 14:30:47 +010010545 appctx->st2 = SETCERT_ST_INSERT;
10546 /* fallthrough */
10547 case SETCERT_ST_INSERT:
10548 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020010549
William Lallemandbeea2a42019-10-30 17:45:33 +010010550 old_ckchs = appctx->ctx.ssl.old_ckchs;
10551 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010552
William Lallemandbeea2a42019-10-30 17:45:33 +010010553 if (!new_ckchs)
10554 continue;
William Lallemand430413e2019-10-28 14:30:47 +010010555
William Lallemand21724f02019-11-04 17:56:13 +010010556 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010010557 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10558 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10559 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
10560 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10561 }
William Lallemand8f840d72019-10-23 10:53:05 +020010562
William Lallemandbeea2a42019-10-30 17:45:33 +010010563 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
10564 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemand430413e2019-10-28 14:30:47 +010010565
William Lallemandbeea2a42019-10-30 17:45:33 +010010566 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10567 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10568 ebmb_delete(&sc0->name);
10569 LIST_DEL(&sc0->by_ckch_inst);
10570 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010010571 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010572 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10573 LIST_DEL(&ckchi->by_ckchs);
10574 free(ckchi);
10575 }
William Lallemand8f840d72019-10-23 10:53:05 +020010576
William Lallemandbeea2a42019-10-30 17:45:33 +010010577 /* Replace the old ckchs by the new one */
10578 ebmb_delete(&old_ckchs->node);
10579 ckchs_free(old_ckchs);
10580 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010010581 appctx->st2 = SETCERT_ST_FIN;
10582 /* fallthrough */
10583 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010584 /* we achieved the transaction, we can set everything to NULL */
10585 free(ckchs_transaction.path);
10586 ckchs_transaction.path = NULL;
10587 ckchs_transaction.new_ckchs = NULL;
10588 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010010589 goto end;
10590 }
William Lallemand8f840d72019-10-23 10:53:05 +020010591 }
William Lallemand430413e2019-10-28 14:30:47 +010010592end:
William Lallemand8f840d72019-10-23 10:53:05 +020010593
William Lallemanded442432019-11-21 16:41:07 +010010594 chunk_appendf(trash, "\n");
10595 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010010596 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010010597 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010010598 if (ci_putchk(si_ic(si), trash) == -1)
10599 si_rx_room_blk(si);
10600 free_trash_chunk(trash);
10601 /* success: call the release function and don't come back */
10602 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010603yield:
10604 /* store the state */
10605 if (ci_putchk(si_ic(si), trash) == -1)
10606 si_rx_room_blk(si);
10607 free_trash_chunk(trash);
10608 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020010609 return 0; /* should come back */
10610
10611error:
10612 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010010613 if (trash) {
10614 chunk_appendf(trash, "\n%sFailed!\n", err);
10615 if (ci_putchk(si_ic(si), trash) == -1)
10616 si_rx_room_blk(si);
10617 free_trash_chunk(trash);
10618 }
William Lallemand430413e2019-10-28 14:30:47 +010010619 /* error: call the release function and don't come back */
10620 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010621}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010622
10623/*
10624 * Parsing function of 'commit ssl cert'
10625 */
10626static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
10627{
10628 char *err = NULL;
10629
William Lallemand230662a2019-12-03 13:32:54 +010010630 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10631 return 1;
10632
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010633 if (!*args[3])
10634 return cli_err(appctx, "'commit ssl cert expects a filename\n");
10635
10636 /* The operations on the CKCH architecture are locked so we can
10637 * manipulate ckch_store and ckch_inst */
10638 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10639 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
10640
10641 if (!ckchs_transaction.path) {
10642 memprintf(&err, "No ongoing transaction! !\n");
10643 goto error;
10644 }
10645
10646 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10647 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
10648 goto error;
10649 }
10650
10651 /* init the appctx structure */
10652 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010653 appctx->ctx.ssl.next_ckchi = NULL;
10654 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
10655 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
10656
10657 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
10658 return 0;
10659
10660error:
10661
10662 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10663 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
10664
10665 return cli_dynerr(appctx, err);
10666}
10667
10668
William Lallemand8f840d72019-10-23 10:53:05 +020010669/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010670 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020010671 */
William Lallemand150bfa82019-09-19 17:12:49 +020010672static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
10673{
William Lallemand0c3b7d92019-10-18 11:27:07 +020010674 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020010675 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020010676 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020010677 int i;
William Lallemand849eed62019-10-17 16:23:50 +020010678 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010679 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020010680 char *end;
10681 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010682 struct cert_key_and_chain *ckch;
10683 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020010684
William Lallemand230662a2019-12-03 13:32:54 +010010685 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10686 return 1;
10687
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010688 if ((buf = alloc_trash_chunk()) == NULL)
10689 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020010690
10691 if (!*args[3] || !payload)
10692 return cli_err(appctx, "'set ssl cert expects a filename and a certificat as a payload\n");
10693
10694 /* The operations on the CKCH architecture are locked so we can
10695 * manipulate ckch_store and ckch_inst */
10696 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10697 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
10698
William Lallemand8f840d72019-10-23 10:53:05 +020010699 if (!chunk_strcpy(buf, args[3])) {
10700 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10701 errcode |= ERR_ALERT | ERR_FATAL;
10702 goto end;
10703 }
10704
William Lallemand44b35322019-10-17 16:28:40 +020010705 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020010706 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020010707 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020010708 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
10709 *end = '\0';
10710 type = cert_exts[i].type;
10711 break;
10712 }
10713 }
10714
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010715 appctx->ctx.ssl.old_ckchs = NULL;
10716 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020010717
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010718 /* if there is an ongoing transaction */
10719 if (ckchs_transaction.path) {
10720 /* 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 +020010721#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010722 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010723 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020010724 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020010725
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010726 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020010727 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020010728 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010729 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020010730 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10731 bundle = j; /* keep the type of certificate so we insert it at the right place */
10732 *end = '\0'; /* it's a bundle let's end the string*/
10733 break;
10734 }
William Lallemand150bfa82019-09-19 17:12:49 +020010735 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010736 if (bundle < 0) {
10737 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);
10738 errcode |= ERR_ALERT | ERR_FATAL;
10739 goto end;
10740 }
10741 }
10742#endif
10743
10744 /* if there is an ongoing transaction, check if this is the same file */
10745 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
10746 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
10747 errcode |= ERR_ALERT | ERR_FATAL;
10748 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010749 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010750
10751 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
10752
10753 } else {
10754 struct ckch_store *find_ckchs[2] = { NULL, NULL };
10755
10756 /* lookup for the certificate in the tree:
10757 * check if this is used as a bundle AND as a unique certificate */
10758 for (i = 0; i < 2; i++) {
10759
10760 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
10761 /* only the bundle name is in the tree and you should
10762 * never update a bundle name, only a filename */
10763 if (bundle < 0 && find_ckchs[i]->multi) {
10764 /* we tried to look for a non-bundle and we found a bundle */
10765 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
10766 err ? err : "", args[3], args[3]);
10767 errcode |= ERR_ALERT | ERR_FATAL;
10768 goto end;
10769 }
William Lallemand3246d942019-11-04 14:02:11 +010010770 /* If we want a bundle but this is not a bundle
10771 * example: When you try to update <file>.rsa, but
10772 * <file> is a regular file */
10773 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
10774 find_ckchs[i] = NULL;
10775 break;
10776 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010777 }
10778#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
10779 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010780 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010781 int j;
10782
10783 /* check if it was used in a bundle by removing the
10784 * .dsa/.rsa/.ecdsa at the end of the filename */
10785 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010786 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010787 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10788 bundle = j; /* keep the type of certificate so we insert it at the right place */
10789 *end = '\0'; /* it's a bundle let's end the string*/
10790 break;
10791 }
10792 }
William Lallemand37031b82019-11-04 13:38:53 +010010793 if (bundle < 0) /* we didn't find a bundle extension */
10794 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010795 }
William Lallemand963b2e72019-10-14 11:38:36 +020010796#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010797 /* bundles are not supported here, so we don't need to lookup again */
10798 break;
William Lallemand963b2e72019-10-14 11:38:36 +020010799#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010800 }
10801
10802 if (find_ckchs[0] && find_ckchs[1]) {
10803 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",
10804 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
10805 errcode |= ERR_ALERT | ERR_FATAL;
10806 goto end;
10807 }
10808
10809 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020010810 }
10811
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010812 if (!appctx->ctx.ssl.old_ckchs) {
10813 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020010814 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010815 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020010816 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010817 }
10818
William Lallemand8a7fdf02019-11-04 10:59:32 +010010819 if (!appctx->ctx.ssl.path) {
10820 /* this is a new transaction, set the path of the transaction */
10821 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
10822 if (!appctx->ctx.ssl.path) {
10823 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10824 errcode |= ERR_ALERT | ERR_FATAL;
10825 goto end;
10826 }
10827 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010828
10829 old_ckchs = appctx->ctx.ssl.old_ckchs;
10830
10831 /* TODO: handle filters */
10832 if (old_ckchs->filters) {
10833 memprintf(&err, "%sCertificates used in crt-list with filters are not supported!\n",
10834 err ? err : "");
10835 errcode |= ERR_ALERT | ERR_FATAL;
10836 goto end;
10837 }
10838
10839 /* duplicate the ckch store */
10840 new_ckchs = ckchs_dup(old_ckchs);
10841 if (!new_ckchs) {
10842 memprintf(&err, "%sCannot allocate memory!\n",
10843 err ? err : "");
10844 errcode |= ERR_ALERT | ERR_FATAL;
10845 goto end;
10846 }
10847
10848 if (!new_ckchs->multi)
10849 ckch = new_ckchs->ckch;
10850 else
10851 ckch = &new_ckchs->ckch[bundle];
10852
10853 /* appply the change on the duplicate */
10854 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
10855 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
10856 errcode |= ERR_ALERT | ERR_FATAL;
10857 goto end;
10858 }
10859
10860 appctx->ctx.ssl.new_ckchs = new_ckchs;
10861
10862 /* we succeed, we can save the ckchs in the transaction */
10863
10864 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010010865 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010866 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
10867 ckchs_transaction.path = appctx->ctx.ssl.path;
10868 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
10869 } else {
10870 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
10871
10872 }
10873
10874 /* free the previous ckchs if there was a transaction */
10875 ckchs_free(ckchs_transaction.new_ckchs);
10876
10877 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
10878
10879
William Lallemand8f840d72019-10-23 10:53:05 +020010880 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010881
William Lallemand8f840d72019-10-23 10:53:05 +020010882end:
10883 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020010884
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010885 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010886
10887 ckchs_free(appctx->ctx.ssl.new_ckchs);
10888 appctx->ctx.ssl.new_ckchs = NULL;
10889
10890 appctx->ctx.ssl.old_ckchs = NULL;
10891
10892 free(appctx->ctx.ssl.path);
10893 appctx->ctx.ssl.path = NULL;
10894
10895 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020010896 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010010897 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010898
10899 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10900 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010010901 }
William Lallemand8f840d72019-10-23 10:53:05 +020010902 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010903}
10904
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010905/* parsing function of 'abort ssl cert' */
10906static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
10907{
10908 char *err = NULL;
10909
William Lallemand230662a2019-12-03 13:32:54 +010010910 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10911 return 1;
10912
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010913 if (!*args[3])
10914 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
10915
10916 /* The operations on the CKCH architecture are locked so we can
10917 * manipulate ckch_store and ckch_inst */
10918 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10919 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
10920
10921 if (!ckchs_transaction.path) {
10922 memprintf(&err, "No ongoing transaction!\n");
10923 goto error;
10924 }
10925
10926 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10927 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
10928 goto error;
10929 }
10930
10931 /* Only free the ckchs there, because the SNI and instances were not generated yet */
10932 ckchs_free(ckchs_transaction.new_ckchs);
10933 ckchs_transaction.new_ckchs = NULL;
10934 ckchs_free(ckchs_transaction.old_ckchs);
10935 ckchs_transaction.old_ckchs = NULL;
10936 free(ckchs_transaction.path);
10937 ckchs_transaction.path = NULL;
10938
10939 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10940
10941 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
10942 return cli_dynmsg(appctx, LOG_NOTICE, err);
10943
10944error:
10945 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10946
10947 return cli_dynerr(appctx, err);
10948}
10949
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010950static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010951{
10952#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
10953 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010954 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010955
10956 if (!payload)
10957 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020010958
10959 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010960 if (!*payload)
10961 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010962
10963 /* remove \r and \n from the payload */
10964 for (i = 0, j = 0; payload[i]; i++) {
10965 if (payload[i] == '\r' || payload[i] == '\n')
10966 continue;
10967 payload[j++] = payload[i];
10968 }
10969 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010970
Willy Tarreau1c913e42018-08-22 05:26:57 +020010971 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010972 if (ret < 0)
10973 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010974
Willy Tarreau1c913e42018-08-22 05:26:57 +020010975 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020010976 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020010977 if (err)
10978 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
10979 else
10980 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010981 }
Willy Tarreau9d008692019-08-09 11:21:01 +020010982
10983 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010984#else
Willy Tarreau9d008692019-08-09 11:21:01 +020010985 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 +020010986#endif
10987
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010988}
10989
Willy Tarreau86a394e2019-05-09 14:15:32 +020010990#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010991static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
10992{
10993 switch (arg->type) {
10994 case ARGT_STR:
10995 smp->data.type = SMP_T_STR;
10996 smp->data.u.str = arg->data.str;
10997 return 1;
10998 case ARGT_VAR:
10999 if (!vars_get_by_desc(&arg->data.var, smp))
11000 return 0;
11001 if (!sample_casts[smp->data.type][SMP_T_STR])
11002 return 0;
11003 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
11004 return 0;
11005 return 1;
11006 default:
11007 return 0;
11008 }
11009}
11010
11011static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
11012 const char *file, int line, char **err)
11013{
11014 switch(args[0].data.sint) {
11015 case 128:
11016 case 192:
11017 case 256:
11018 break;
11019 default:
11020 memprintf(err, "key size must be 128, 192 or 256 (bits).");
11021 return 0;
11022 }
11023 /* Try to decode a variable. */
11024 vars_check_arg(&args[1], NULL);
11025 vars_check_arg(&args[2], NULL);
11026 vars_check_arg(&args[3], NULL);
11027 return 1;
11028}
11029
11030/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
11031static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
11032{
11033 struct sample nonce, key, aead_tag;
11034 struct buffer *smp_trash, *smp_trash_alloc;
11035 EVP_CIPHER_CTX *ctx;
11036 int dec_size, ret;
11037
11038 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
11039 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
11040 return 0;
11041
11042 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
11043 if (!sample_conv_var2smp_str(&arg_p[2], &key))
11044 return 0;
11045
11046 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
11047 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
11048 return 0;
11049
11050 smp_trash = get_trash_chunk();
11051 smp_trash_alloc = alloc_trash_chunk();
11052 if (!smp_trash_alloc)
11053 return 0;
11054
11055 ctx = EVP_CIPHER_CTX_new();
11056
11057 if (!ctx)
11058 goto err;
11059
11060 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
11061 if (dec_size < 0)
11062 goto err;
11063 smp_trash->data = dec_size;
11064
11065 /* Set cipher type and mode */
11066 switch(arg_p[0].data.sint) {
11067 case 128:
11068 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
11069 break;
11070 case 192:
11071 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
11072 break;
11073 case 256:
11074 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
11075 break;
11076 }
11077
11078 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
11079
11080 /* Initialise IV */
11081 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
11082 goto err;
11083
11084 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
11085 if (dec_size < 0)
11086 goto err;
11087 smp_trash->data = dec_size;
11088
11089 /* Initialise key */
11090 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
11091 goto err;
11092
11093 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
11094 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
11095 goto err;
11096
11097 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
11098 if (dec_size < 0)
11099 goto err;
11100 smp_trash_alloc->data = dec_size;
11101 dec_size = smp_trash->data;
11102
11103 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
11104 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
11105
11106 if (ret <= 0)
11107 goto err;
11108
11109 smp->data.u.str.data = dec_size + smp_trash->data;
11110 smp->data.u.str.area = smp_trash->area;
11111 smp->data.type = SMP_T_BIN;
11112 smp->flags &= ~SMP_F_CONST;
11113 free_trash_chunk(smp_trash_alloc);
11114 return 1;
11115
11116err:
11117 free_trash_chunk(smp_trash_alloc);
11118 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020011119}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011120# endif
William Lallemand32af2032016-10-29 18:09:35 +020011121
Elliot Otchet71f82972020-01-15 08:12:14 -050011122/* Argument validation functions */
11123
11124/* This function is used to validate the arguments passed to any "x_dn" ssl
11125 * keywords. These keywords support specifying a third parameter that must be
11126 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
11127 */
11128int val_dnfmt(struct arg *arg, char **err_msg)
11129{
11130 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
11131 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
11132 return 0;
11133 }
11134 return 1;
11135}
11136
William Lallemand32af2032016-10-29 18:09:35 +020011137/* register cli keywords */
11138static struct cli_kw_list cli_kws = {{ },{
11139#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11140 { { "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 +020011141 { { "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 +020011142#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010011143 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011144 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
11145 { { "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 +010011146 { { "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 +010011147 { { "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 +020011148 { { NULL }, NULL, NULL, NULL }
11149}};
11150
Willy Tarreau0108d902018-11-25 19:14:37 +010011151INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020011152
Willy Tarreau7875d092012-09-10 08:20:03 +020011153/* Note: must not be declared <const> as its list will be overwritten.
11154 * Please take care of keeping this list alphabetically sorted.
11155 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011156static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020011157 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011158 { "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 +010011159#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010011160 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011161#endif
Emeric Brun645ae792014-04-30 14:21:06 +020011162 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011163#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
11164 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
11165#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010011166 { "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 +020011167 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020011168 { "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 +020011169 { "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 +020011170#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020011171 { "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 -040011172#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011173#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011174 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11175 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040011176 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11177#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011178 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11179 { "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 +010011180 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011181 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011182 { "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 +020011183 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11184 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11185 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11186 { "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 -050011187 { "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 +020011188 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11189 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011190 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011191 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11192 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010011193 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011194 { "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 +020011195 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11196 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11197 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11198 { "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 -050011199 { "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 +020011200 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020011201 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011202 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011203 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011204 { "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 +010011205 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011206 { "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 +020011207 { "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 +010011208 { "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 +020011209 { "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 +010011210#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011211 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020011212#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010011213#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011214 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020011215#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011216 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011217#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020011218 { "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 -040011219#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011220 { "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 +020011221#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011222 { "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 -040011223#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011224#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011225 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11226 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040011227 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11228#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040011229#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011230 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040011231#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011232 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11233 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11234 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11235 { "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 +020011236 { NULL, NULL, 0, 0, 0 },
11237}};
11238
Willy Tarreau0108d902018-11-25 19:14:37 +010011239INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
11240
Willy Tarreau7875d092012-09-10 08:20:03 +020011241/* Note: must not be declared <const> as its list will be overwritten.
11242 * Please take care of keeping this list alphabetically sorted.
11243 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011244static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011245 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
11246 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011247 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020011248}};
11249
Willy Tarreau0108d902018-11-25 19:14:37 +010011250INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
11251
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011252/* Note: must not be declared <const> as its list will be overwritten.
11253 * Please take care of keeping this list alphabetically sorted, doing so helps
11254 * all code contributors.
11255 * Optional keywords are also declared with a NULL ->parse() function so that
11256 * the config parser can report an appropriate error when a known keyword was
11257 * not enabled.
11258 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011259static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011260 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011261 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11262 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11263 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011264#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011265 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11266#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011267 { "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 +010011268 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011269 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011270 { "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 +010011271 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020011272 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
11273 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011274 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
11275 { NULL, NULL, 0 },
11276};
11277
Willy Tarreau0108d902018-11-25 19:14:37 +010011278/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
11279
Willy Tarreau51fb7652012-09-18 18:24:39 +020011280static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011281 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011282 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11283 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11284 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
11285 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
11286 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
11287 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011288#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011289 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11290#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011291 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
11292 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
11293 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
11294 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
11295 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
11296 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
11297 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
11298 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
11299 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
11300 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011301 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011302 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011303 { "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 +020011304 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
11305 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
11306 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
11307 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011308 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011309 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
11310 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011311 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
11312 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011313 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
11314 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
11315 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
11316 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
11317 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011318 { NULL, NULL, 0 },
11319}};
Emeric Brun46591952012-05-18 15:47:34 +020011320
Willy Tarreau0108d902018-11-25 19:14:37 +010011321INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
11322
Willy Tarreau92faadf2012-10-10 23:04:25 +020011323/* Note: must not be declared <const> as its list will be overwritten.
11324 * Please take care of keeping this list alphabetically sorted, doing so helps
11325 * all code contributors.
11326 * Optional keywords are also declared with a NULL ->parse() function so that
11327 * the config parser can report an appropriate error when a known keyword was
11328 * not enabled.
11329 */
11330static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010011331 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010011332 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011333 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010011334 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020011335 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011336 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
11337 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011338#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011339 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
11340#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011341 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
11342 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
11343 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
11344 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
11345 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
11346 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
11347 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
11348 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
11349 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
11350 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
11351 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
11352 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
11353 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
11354 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
11355 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
11356 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
11357 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
11358 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010011359 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011360 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
11361 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
11362 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
11363 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
11364 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
11365 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
11366 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
11367 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
11368 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
11369 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020011370 { NULL, NULL, 0, 0 },
11371}};
11372
Willy Tarreau0108d902018-11-25 19:14:37 +010011373INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
11374
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011375static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010011376 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
11377 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010011378 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011379 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
11380 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010011381#ifndef OPENSSL_NO_DH
11382 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
11383#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011384 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011385#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011386 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011387#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010011388 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
11389#ifndef OPENSSL_NO_DH
11390 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
11391#endif
11392 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
11393 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
11394 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
11395 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011396 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010011397 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
11398 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011399#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011400 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
11401 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
11402#endif
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011403 { 0, NULL, NULL },
11404}};
11405
Willy Tarreau0108d902018-11-25 19:14:37 +010011406INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
11407
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011408/* Note: must not be declared <const> as its list will be overwritten */
11409static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020011410#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011411 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
11412#endif
11413 { NULL, NULL, 0, 0, 0 },
11414}};
11415
11416INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
11417
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020011418/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010011419static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020011420 .snd_buf = ssl_sock_from_buf,
11421 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010011422 .subscribe = ssl_subscribe,
11423 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020011424 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020011425 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020011426 .rcv_pipe = NULL,
11427 .snd_pipe = NULL,
11428 .shutr = NULL,
11429 .shutw = ssl_sock_shutw,
11430 .close = ssl_sock_close,
11431 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010011432 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010011433 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010011434 .prepare_srv = ssl_sock_prepare_srv_ctx,
11435 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010011436 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010011437 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020011438};
11439
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011440enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
11441 struct session *sess, struct stream *s, int flags)
11442{
11443 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011444 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011445
11446 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011447 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011448
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011449 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011450 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011451 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011452 s->req.flags |= CF_READ_NULL;
11453 return ACT_RET_YIELD;
11454 }
11455 }
11456 return (ACT_RET_CONT);
11457}
11458
11459static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
11460{
11461 rule->action_ptr = ssl_action_wait_for_hs;
11462
11463 return ACT_RET_PRS_OK;
11464}
11465
11466static struct action_kw_list http_req_actions = {ILH, {
11467 { "wait-for-handshake", ssl_parse_wait_for_hs },
11468 { /* END */ }
11469}};
11470
Willy Tarreau0108d902018-11-25 19:14:37 +010011471INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
11472
Willy Tarreau5db847a2019-05-09 14:13:35 +020011473#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011474
11475static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11476{
11477 if (ptr) {
11478 chunk_destroy(ptr);
11479 free(ptr);
11480 }
11481}
11482
11483#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011484static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11485{
Willy Tarreaubafbe012017-11-24 17:34:44 +010011486 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011487}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011488
Emeric Brun46591952012-05-18 15:47:34 +020011489__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020011490static void __ssl_sock_init(void)
11491{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011492#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011493 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011494 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011495#endif
Emeric Brun46591952012-05-18 15:47:34 +020011496
Willy Tarreauef934602016-12-22 23:12:01 +010011497 if (global_ssl.listen_default_ciphers)
11498 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
11499 if (global_ssl.connect_default_ciphers)
11500 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011501#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011502 if (global_ssl.listen_default_ciphersuites)
11503 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
11504 if (global_ssl.connect_default_ciphersuites)
11505 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
11506#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010011507
Willy Tarreau13e14102016-12-22 20:25:26 +010011508 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011509#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020011510 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080011511#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011512#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011513 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011514 n = sk_SSL_COMP_num(cm);
11515 while (n--) {
11516 (void) sk_SSL_COMP_pop(cm);
11517 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011518#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011519
Willy Tarreau5db847a2019-05-09 14:13:35 +020011520#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011521 ssl_locking_init();
11522#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020011523#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011524 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
11525#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020011526 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020011527 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 +020011528#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011529 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011530 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011531#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010011532#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11533 hap_register_post_check(tlskeys_finalize_config);
11534#endif
Willy Tarreau80713382018-11-26 10:19:54 +010011535
11536 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
11537 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
11538
11539#ifndef OPENSSL_NO_DH
11540 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
11541 hap_register_post_deinit(ssl_free_dh);
11542#endif
11543#ifndef OPENSSL_NO_ENGINE
11544 hap_register_post_deinit(ssl_free_engines);
11545#endif
11546 /* Load SSL string for the verbose & debug mode. */
11547 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020011548 ha_meth = BIO_meth_new(0x666, "ha methods");
11549 BIO_meth_set_write(ha_meth, ha_ssl_write);
11550 BIO_meth_set_read(ha_meth, ha_ssl_read);
11551 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
11552 BIO_meth_set_create(ha_meth, ha_ssl_new);
11553 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
11554 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
11555 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020011556
11557 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010011558}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010011559
Willy Tarreau80713382018-11-26 10:19:54 +010011560/* Compute and register the version string */
11561static void ssl_register_build_options()
11562{
11563 char *ptr = NULL;
11564 int i;
11565
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011566 memprintf(&ptr, "Built with OpenSSL version : "
11567#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011568 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011569#else /* OPENSSL_IS_BORINGSSL */
11570 OPENSSL_VERSION_TEXT
11571 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080011572 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020011573 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011574#endif
11575 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011576#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011577 "no (library version too old)"
11578#elif defined(OPENSSL_NO_TLSEXT)
11579 "no (disabled via OPENSSL_NO_TLSEXT)"
11580#else
11581 "yes"
11582#endif
11583 "", ptr);
11584
11585 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
11586#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11587 "yes"
11588#else
11589#ifdef OPENSSL_NO_TLSEXT
11590 "no (because of OPENSSL_NO_TLSEXT)"
11591#else
11592 "no (version might be too old, 0.9.8f min needed)"
11593#endif
11594#endif
11595 "", ptr);
11596
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020011597 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
11598 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
11599 if (methodVersions[i].option)
11600 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011601
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011602 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010011603}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011604
Willy Tarreau80713382018-11-26 10:19:54 +010011605INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020011606
Emeric Brun46591952012-05-18 15:47:34 +020011607
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011608#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011609void ssl_free_engines(void) {
11610 struct ssl_engine_list *wl, *wlb;
11611 /* free up engine list */
11612 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
11613 ENGINE_finish(wl->e);
11614 ENGINE_free(wl->e);
11615 LIST_DEL(&wl->list);
11616 free(wl);
11617 }
11618}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011619#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020011620
Remi Gacogned3a23c32015-05-28 16:39:47 +020011621#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000011622void ssl_free_dh(void) {
11623 if (local_dh_1024) {
11624 DH_free(local_dh_1024);
11625 local_dh_1024 = NULL;
11626 }
11627 if (local_dh_2048) {
11628 DH_free(local_dh_2048);
11629 local_dh_2048 = NULL;
11630 }
11631 if (local_dh_4096) {
11632 DH_free(local_dh_4096);
11633 local_dh_4096 = NULL;
11634 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020011635 if (global_dh) {
11636 DH_free(global_dh);
11637 global_dh = NULL;
11638 }
Grant Zhang872f9c22017-01-21 01:10:18 +000011639}
11640#endif
11641
11642__attribute__((destructor))
11643static void __ssl_sock_deinit(void)
11644{
11645#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011646 if (ssl_ctx_lru_tree) {
11647 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010011648 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020011649 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020011650#endif
11651
Willy Tarreau5db847a2019-05-09 14:13:35 +020011652#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011653 ERR_remove_state(0);
11654 ERR_free_strings();
11655
11656 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080011657#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020011658
Willy Tarreau5db847a2019-05-09 14:13:35 +020011659#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011660 CRYPTO_cleanup_all_ex_data();
11661#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020011662 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020011663}
11664
11665
Emeric Brun46591952012-05-18 15:47:34 +020011666/*
11667 * Local variables:
11668 * c-indent-level: 8
11669 * c-basic-offset: 8
11670 * End:
11671 */