blob: f2bce219126819b801872980d5b8f174f9d3a8d1 [file] [log] [blame]
yanbzhu08ce6ab2015-12-02 13:01:29 -05001
Emeric Brun46591952012-05-18 15:47:34 +02002/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003 * SSL/TLS transport layer over SOCK_STREAM sockets
Emeric Brun46591952012-05-18 15:47:34 +02004 *
5 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Willy Tarreau69845df2012-09-10 09:43:09 +020012 * Acknowledgement:
13 * We'd like to specially thank the Stud project authors for a very clean
14 * and well documented code which helped us understand how the OpenSSL API
15 * ought to be used in non-blocking mode. This is one difficult part which
16 * is not easy to get from the OpenSSL doc, and reading the Stud code made
17 * it much more obvious than the examples in the OpenSSL package. Keep up
18 * the good works, guys !
19 *
20 * Stud is an extremely efficient and scalable SSL/TLS proxy which combines
21 * particularly well with haproxy. For more info about this project, visit :
22 * https://github.com/bumptech/stud
23 *
Emeric Brun46591952012-05-18 15:47:34 +020024 */
25
Willy Tarreau8d164dc2019-05-10 09:35:00 +020026/* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */
Emeric Brun46591952012-05-18 15:47:34 +020027#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020028#include <ctype.h>
29#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020030#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020034#include <string.h>
35#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020036
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020040#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020041#include <netinet/tcp.h>
42
Christopher Faulet31af49d2015-06-09 17:29:50 +020043#include <import/lru.h>
44#include <import/xxhash.h>
45
Emeric Brun46591952012-05-18 15:47:34 +020046#include <common/buffer.h>
Willy Tarreau843b7cb2018-07-13 10:54:26 +020047#include <common/chunk.h>
Emeric Brun46591952012-05-18 15:47:34 +020048#include <common/compat.h>
49#include <common/config.h>
50#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020051#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010052#include <common/initcall.h>
Willy Tarreau55994562019-05-09 14:52:44 +020053#include <common/openssl-compat.h>
Emeric Brun46591952012-05-18 15:47:34 +020054#include <common/standard.h>
55#include <common/ticks.h>
56#include <common/time.h>
Emeric Brun2c86cbf2014-10-30 15:56:50 +010057#include <common/cfgparse.h>
Nenad Merdanovic05552d42015-02-27 19:56:49 +010058#include <common/base64.h>
Emeric Brun46591952012-05-18 15:47:34 +020059
Emeric Brunfc0421f2012-09-07 17:30:07 +020060#include <ebsttree.h>
61
William Lallemand32af2032016-10-29 18:09:35 +020062#include <types/applet.h>
63#include <types/cli.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020064#include <types/global.h>
65#include <types/ssl_sock.h>
William Lallemand32af2032016-10-29 18:09:35 +020066#include <types/stats.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020067
Willy Tarreau7875d092012-09-10 08:20:03 +020068#include <proto/acl.h>
69#include <proto/arg.h>
William Lallemand32af2032016-10-29 18:09:35 +020070#include <proto/channel.h>
Emeric Brun46591952012-05-18 15:47:34 +020071#include <proto/connection.h>
William Lallemand32af2032016-10-29 18:09:35 +020072#include <proto/cli.h>
Emeric Brun46591952012-05-18 15:47:34 +020073#include <proto/fd.h>
74#include <proto/freq_ctr.h>
75#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020076#include <proto/http_rules.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020077#include <proto/listener.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010078#include <proto/pattern.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020079#include <proto/proto_tcp.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020080#include <proto/http_ana.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020081#include <proto/server.h>
William Lallemand32af2032016-10-29 18:09:35 +020082#include <proto/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020083#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020084#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020085#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +020086#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020087#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020088#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010089#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020090
Willy Tarreau9356dac2019-05-10 09:22:53 +020091/* ***** READ THIS before adding code here! *****
92 *
93 * Due to API incompatibilities between multiple OpenSSL versions and their
94 * derivatives, it's often tempting to add macros to (re-)define certain
95 * symbols. Please do not do this here, and do it in common/openssl-compat.h
96 * exclusively so that the whole code consistently uses the same macros.
97 *
98 * Whenever possible if a macro is missing in certain versions, it's better
99 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
100 */
101
Willy Tarreau518cedd2014-02-17 15:43:01 +0100102/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +0200103#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +0100104#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +0100105#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +0200106#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
107
Emeric Brunf282a812012-09-21 15:27:54 +0200108/* bits 0xFFFF0000 are reserved to store verify errors */
109
110/* Verify errors macros */
111#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
112#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
113#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
114
115#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
116#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
117#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +0200118
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200119/* ssl_methods flags for ssl options */
120#define MC_SSL_O_ALL 0x0000
121#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
122#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
123#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
124#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200125#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200126
127/* ssl_methods versions */
128enum {
129 CONF_TLSV_NONE = 0,
130 CONF_TLSV_MIN = 1,
131 CONF_SSLV3 = 1,
132 CONF_TLSV10 = 2,
133 CONF_TLSV11 = 3,
134 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200135 CONF_TLSV13 = 5,
136 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200137};
138
Emeric Brun850efd52014-01-29 12:24:34 +0100139/* server and bind verify method, it uses a global value as default */
140enum {
141 SSL_SOCK_VERIFY_DEFAULT = 0,
142 SSL_SOCK_VERIFY_REQUIRED = 1,
143 SSL_SOCK_VERIFY_OPTIONAL = 2,
144 SSL_SOCK_VERIFY_NONE = 3,
145};
146
William Lallemand3f85c9a2017-10-09 16:30:50 +0200147
Willy Tarreau71b734c2014-01-28 15:19:44 +0100148int sslconns = 0;
149int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100150static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100151int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200152
Willy Tarreauef934602016-12-22 23:12:01 +0100153static struct {
154 char *crt_base; /* base directory path for certificates */
155 char *ca_base; /* base directory path for CAs and CRLs */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000156 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100157
158 char *listen_default_ciphers;
159 char *connect_default_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200160#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200161 char *listen_default_ciphersuites;
162 char *connect_default_ciphersuites;
163#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100164 int listen_default_ssloptions;
165 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200166 struct tls_version_filter listen_default_sslmethods;
167 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100168
169 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
170 unsigned int life_time; /* SSL session lifetime in seconds */
171 unsigned int max_record; /* SSL max record size */
172 unsigned int default_dh_param; /* SSL maximum DH parameter size */
173 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100174 int capture_cipherlist; /* Size of the cipherlist buffer. */
Willy Tarreauef934602016-12-22 23:12:01 +0100175} global_ssl = {
176#ifdef LISTEN_DEFAULT_CIPHERS
177 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
178#endif
179#ifdef CONNECT_DEFAULT_CIPHERS
180 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
181#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200182#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200183#ifdef LISTEN_DEFAULT_CIPHERSUITES
184 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
185#endif
186#ifdef CONNECT_DEFAULT_CIPHERSUITES
187 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
188#endif
189#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100190 .listen_default_ssloptions = BC_SSL_O_NONE,
191 .connect_default_ssloptions = SRV_SSL_O_NONE,
192
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200193 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
194 .listen_default_sslmethods.min = CONF_TLSV_NONE,
195 .listen_default_sslmethods.max = CONF_TLSV_NONE,
196 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
197 .connect_default_sslmethods.min = CONF_TLSV_NONE,
198 .connect_default_sslmethods.max = CONF_TLSV_NONE,
199
Willy Tarreauef934602016-12-22 23:12:01 +0100200#ifdef DEFAULT_SSL_MAX_RECORD
201 .max_record = DEFAULT_SSL_MAX_RECORD,
202#endif
203 .default_dh_param = SSL_DEFAULT_DH_PARAM,
204 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100205 .capture_cipherlist = 0,
Willy Tarreauef934602016-12-22 23:12:01 +0100206};
207
Olivier Houcharda8955d52019-04-07 22:00:38 +0200208static BIO_METHOD *ha_meth;
209
Olivier Houchard66ab4982019-02-26 18:37:15 +0100210struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200211 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100212 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200213 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200214 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100215 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200216 struct wait_event wait_event;
217 struct wait_event *recv_wait;
218 struct wait_event *send_wait;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100219 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100220 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100221 int sent_early_data; /* Amount of early data we sent so far */
222
Olivier Houchard66ab4982019-02-26 18:37:15 +0100223};
224
225DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
226
Olivier Houchardea8dd942019-05-20 14:02:16 +0200227static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200228static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200229
Olivier Houcharda8955d52019-04-07 22:00:38 +0200230/* Methods to implement OpenSSL BIO */
231static int ha_ssl_write(BIO *h, const char *buf, int num)
232{
233 struct buffer tmpbuf;
234 struct ssl_sock_ctx *ctx;
235 int ret;
236
237 ctx = BIO_get_data(h);
238 tmpbuf.size = num;
239 tmpbuf.area = (void *)(uintptr_t)buf;
240 tmpbuf.data = num;
241 tmpbuf.head = 0;
242 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200243 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200244 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200245 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200246 } else if (ret == 0)
247 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200248 return ret;
249}
250
251static int ha_ssl_gets(BIO *h, char *buf, int size)
252{
253
254 return 0;
255}
256
257static int ha_ssl_puts(BIO *h, const char *str)
258{
259
260 return ha_ssl_write(h, str, strlen(str));
261}
262
263static int ha_ssl_read(BIO *h, char *buf, int size)
264{
265 struct buffer tmpbuf;
266 struct ssl_sock_ctx *ctx;
267 int ret;
268
269 ctx = BIO_get_data(h);
270 tmpbuf.size = size;
271 tmpbuf.area = buf;
272 tmpbuf.data = 0;
273 tmpbuf.head = 0;
274 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200275 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200276 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200277 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200278 } else if (ret == 0)
279 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200280
281 return ret;
282}
283
284static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
285{
286 int ret = 0;
287 switch (cmd) {
288 case BIO_CTRL_DUP:
289 case BIO_CTRL_FLUSH:
290 ret = 1;
291 break;
292 }
293 return ret;
294}
295
296static int ha_ssl_new(BIO *h)
297{
298 BIO_set_init(h, 1);
299 BIO_set_data(h, NULL);
300 BIO_clear_flags(h, ~0);
301 return 1;
302}
303
304static int ha_ssl_free(BIO *data)
305{
306
307 return 1;
308}
309
310
Willy Tarreau5db847a2019-05-09 14:13:35 +0200311#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100312
Emeric Brun821bb9b2017-06-15 16:37:39 +0200313static HA_RWLOCK_T *ssl_rwlocks;
314
315
316unsigned long ssl_id_function(void)
317{
318 return (unsigned long)tid;
319}
320
321void ssl_locking_function(int mode, int n, const char * file, int line)
322{
323 if (mode & CRYPTO_LOCK) {
324 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100325 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200326 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100327 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200328 }
329 else {
330 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100331 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200332 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100333 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200334 }
335}
336
337static int ssl_locking_init(void)
338{
339 int i;
340
341 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
342 if (!ssl_rwlocks)
343 return -1;
344
345 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100346 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200347
348 CRYPTO_set_id_callback(ssl_id_function);
349 CRYPTO_set_locking_callback(ssl_locking_function);
350
351 return 0;
352}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100353
Emeric Brun821bb9b2017-06-15 16:37:39 +0200354#endif
355
William Lallemand150bfa82019-09-19 17:12:49 +0200356__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200357
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100358/* Uncommitted CKCH transaction */
359
360static struct {
361 struct ckch_store *new_ckchs;
362 struct ckch_store *old_ckchs;
363 char *path;
364} ckchs_transaction;
365
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200366/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100367 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200368 */
369struct cafile_entry {
370 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200371 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200372 struct ebmb_node node;
373 char path[0];
374};
375
376static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
377
378static X509_STORE* ssl_store_get0_locations_file(char *path)
379{
380 struct ebmb_node *eb;
381
382 eb = ebst_lookup(&cafile_tree, path);
383 if (eb) {
384 struct cafile_entry *ca_e;
385 ca_e = ebmb_entry(eb, struct cafile_entry, node);
386 return ca_e->ca_store;
387 }
388 return NULL;
389}
390
391static int ssl_store_load_locations_file(char *path)
392{
393 if (ssl_store_get0_locations_file(path) == NULL) {
394 struct cafile_entry *ca_e;
395 X509_STORE *store = X509_STORE_new();
396 if (X509_STORE_load_locations(store, path, NULL)) {
397 int pathlen;
398 pathlen = strlen(path);
399 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
400 if (ca_e) {
401 memcpy(ca_e->path, path, pathlen + 1);
402 ca_e->ca_store = store;
403 ebst_insert(&cafile_tree, &ca_e->node);
404 return 1;
405 }
406 }
407 X509_STORE_free(store);
408 return 0;
409 }
410 return 1;
411}
412
413/* mimic what X509_STORE_load_locations do with store_ctx */
414static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
415{
416 X509_STORE *store;
417 store = ssl_store_get0_locations_file(path);
418 if (store_ctx && store) {
419 int i;
420 X509_OBJECT *obj;
421 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
422 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
423 obj = sk_X509_OBJECT_value(objs, i);
424 switch (X509_OBJECT_get_type(obj)) {
425 case X509_LU_X509:
426 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
427 break;
428 case X509_LU_CRL:
429 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
430 break;
431 default:
432 break;
433 }
434 }
435 return 1;
436 }
437 return 0;
438}
439
440/* SSL_CTX_load_verify_locations substitute, internaly call X509_STORE_load_locations */
441static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
442{
443 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
444 return ssl_set_cert_crl_file(store_ctx, path);
445}
446
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200447/*
448 Extract CA_list from CA_file already in tree.
449 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
450 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
451*/
452static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
453{
454 struct ebmb_node *eb;
455 struct cafile_entry *ca_e;
456
457 eb = ebst_lookup(&cafile_tree, path);
458 if (!eb)
459 return NULL;
460 ca_e = ebmb_entry(eb, struct cafile_entry, node);
461
462 if (ca_e->ca_list == NULL) {
463 int i;
464 unsigned long key;
465 struct eb_root ca_name_tree = EB_ROOT;
466 struct eb64_node *node, *back;
467 struct {
468 struct eb64_node node;
469 X509_NAME *xname;
470 } *ca_name;
471 STACK_OF(X509_OBJECT) *objs;
472 STACK_OF(X509_NAME) *skn;
473 X509 *x;
474 X509_NAME *xn;
475
476 skn = sk_X509_NAME_new_null();
477 /* take x509 from cafile_tree */
478 objs = X509_STORE_get0_objects(ca_e->ca_store);
479 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
480 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
481 if (!x)
482 continue;
483 xn = X509_get_subject_name(x);
484 if (!xn)
485 continue;
486 /* Check for duplicates. */
487 key = X509_NAME_hash(xn);
488 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
489 node && ca_name == NULL;
490 node = eb64_next(node)) {
491 ca_name = container_of(node, typeof(*ca_name), node);
492 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
493 ca_name = NULL;
494 }
495 /* find a duplicate */
496 if (ca_name)
497 continue;
498 ca_name = calloc(1, sizeof *ca_name);
499 xn = X509_NAME_dup(xn);
500 if (!ca_name ||
501 !xn ||
502 !sk_X509_NAME_push(skn, xn)) {
503 free(ca_name);
504 X509_NAME_free(xn);
505 sk_X509_NAME_pop_free(skn, X509_NAME_free);
506 sk_X509_NAME_free(skn);
507 skn = NULL;
508 break;
509 }
510 ca_name->node.key = key;
511 ca_name->xname = xn;
512 eb64_insert(&ca_name_tree, &ca_name->node);
513 }
514 ca_e->ca_list = skn;
515 /* remove temporary ca_name tree */
516 node = eb64_first(&ca_name_tree);
517 while (node) {
518 ca_name = container_of(node, typeof(*ca_name), node);
519 back = eb64_next(node);
520 eb64_delete(node);
521 free(ca_name);
522 node = back;
523 }
524 }
525 return ca_e->ca_list;
526}
527
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100528/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100529struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100530 unsigned long long int xxh64;
531 unsigned char ciphersuite_len;
532 char ciphersuite[0];
533};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100534struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100535static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200536static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100537
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200538#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
539struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
540#endif
541
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200542#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000543static unsigned int openssl_engines_initialized;
544struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
545struct ssl_engine_list {
546 struct list list;
547 ENGINE *e;
548};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200549#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000550
Remi Gacogne8de54152014-07-15 11:36:40 +0200551#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200552static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200553static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200554static DH *local_dh_1024 = NULL;
555static DH *local_dh_2048 = NULL;
556static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100557static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200558#endif /* OPENSSL_NO_DH */
559
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100560#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200561/* X509V3 Extensions that will be added on generated certificates */
562#define X509V3_EXT_SIZE 5
563static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
564 "basicConstraints",
565 "nsComment",
566 "subjectKeyIdentifier",
567 "authorityKeyIdentifier",
568 "keyUsage",
569};
570static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
571 "CA:FALSE",
572 "\"OpenSSL Generated Certificate\"",
573 "hash",
574 "keyid,issuer:always",
575 "nonRepudiation,digitalSignature,keyEncipherment"
576};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200577/* LRU cache to store generated certificate */
578static struct lru64_head *ssl_ctx_lru_tree = NULL;
579static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200580static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100581__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200582
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200583#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
584
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100585static struct ssl_bind_kw ssl_bind_kws[];
586
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200587#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500588/* The order here matters for picking a default context,
589 * keep the most common keytype at the bottom of the list
590 */
591const char *SSL_SOCK_KEYTYPE_NAMES[] = {
592 "dsa",
593 "ecdsa",
594 "rsa"
595};
596#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100597#else
598#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500599#endif
600
William Lallemandc3cd35f2017-11-28 11:04:43 +0100601static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100602static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
603
604#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
605
606#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
607 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
608
609#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
610 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200611
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100612/*
613 * This function gives the detail of the SSL error. It is used only
614 * if the debug mode and the verbose mode are activated. It dump all
615 * the SSL error until the stack was empty.
616 */
617static forceinline void ssl_sock_dump_errors(struct connection *conn)
618{
619 unsigned long ret;
620
621 if (unlikely(global.mode & MODE_DEBUG)) {
622 while(1) {
623 ret = ERR_get_error();
624 if (ret == 0)
625 return;
626 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200627 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100628 ERR_func_error_string(ret), ERR_reason_error_string(ret));
629 }
630 }
631}
632
yanbzhube2774d2015-12-10 15:07:30 -0500633
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200634#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000635static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
636{
637 int err_code = ERR_ABORT;
638 ENGINE *engine;
639 struct ssl_engine_list *el;
640
641 /* grab the structural reference to the engine */
642 engine = ENGINE_by_id(engine_id);
643 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100644 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000645 goto fail_get;
646 }
647
648 if (!ENGINE_init(engine)) {
649 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100650 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000651 goto fail_init;
652 }
653
654 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100655 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000656 goto fail_set_method;
657 }
658
659 el = calloc(1, sizeof(*el));
660 el->e = engine;
661 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100662 nb_engines++;
663 if (global_ssl.async)
664 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000665 return 0;
666
667fail_set_method:
668 /* release the functional reference from ENGINE_init() */
669 ENGINE_finish(engine);
670
671fail_init:
672 /* release the structural reference from ENGINE_by_id() */
673 ENGINE_free(engine);
674
675fail_get:
676 return err_code;
677}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200678#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000679
Willy Tarreau5db847a2019-05-09 14:13:35 +0200680#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200681/*
682 * openssl async fd handler
683 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200684void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000685{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200686 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000687
Emeric Brun3854e012017-05-17 20:42:48 +0200688 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000689 * to poll this fd until it is requested
690 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000691 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000692 fd_cant_recv(fd);
693
694 /* crypto engine is available, let's notify the associated
695 * connection that it can pursue its processing.
696 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200697 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000698}
699
Emeric Brun3854e012017-05-17 20:42:48 +0200700/*
701 * openssl async delayed SSL_free handler
702 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200703void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000704{
705 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200706 OSSL_ASYNC_FD all_fd[32];
707 size_t num_all_fds = 0;
708 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000709
Emeric Brun3854e012017-05-17 20:42:48 +0200710 /* We suppose that the async job for a same SSL *
711 * are serialized. So if we are awake it is
712 * because the running job has just finished
713 * and we can remove all async fds safely
714 */
715 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
716 if (num_all_fds > 32) {
717 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
718 return;
719 }
720
721 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
722 for (i=0 ; i < num_all_fds ; i++)
723 fd_remove(all_fd[i]);
724
725 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000726 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100727 _HA_ATOMIC_SUB(&sslconns, 1);
728 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000729}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000730/*
Emeric Brun3854e012017-05-17 20:42:48 +0200731 * function used to manage a returned SSL_ERROR_WANT_ASYNC
732 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000733 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200734static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000735{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100736 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200737 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200738 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000739 size_t num_add_fds = 0;
740 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200741 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000742
743 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
744 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200745 if (num_add_fds > 32 || num_del_fds > 32) {
746 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 +0000747 return;
748 }
749
Emeric Brun3854e012017-05-17 20:42:48 +0200750 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000751
Emeric Brun3854e012017-05-17 20:42:48 +0200752 /* We remove unused fds from the fdtab */
753 for (i=0 ; i < num_del_fds ; i++)
754 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000755
Emeric Brun3854e012017-05-17 20:42:48 +0200756 /* We add new fds to the fdtab */
757 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200758 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000759 }
760
Emeric Brun3854e012017-05-17 20:42:48 +0200761 num_add_fds = 0;
762 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
763 if (num_add_fds > 32) {
764 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
765 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000766 }
Emeric Brun3854e012017-05-17 20:42:48 +0200767
768 /* We activate the polling for all known async fds */
769 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000770 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200771 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000772 /* To ensure that the fd cache won't be used
773 * We'll prefer to catch a real RD event
774 * because handling an EAGAIN on this fd will
775 * result in a context switch and also
776 * some engines uses a fd in blocking mode.
777 */
778 fd_cant_recv(add_fd[i]);
779 }
Emeric Brun3854e012017-05-17 20:42:48 +0200780
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000781}
782#endif
783
William Lallemand104a7a62019-10-14 14:14:59 +0200784#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200785/*
786 * This function returns the number of seconds elapsed
787 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
788 * date presented un ASN1_GENERALIZEDTIME.
789 *
790 * In parsing error case, it returns -1.
791 */
792static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
793{
794 long epoch;
795 char *p, *end;
796 const unsigned short month_offset[12] = {
797 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
798 };
799 int year, month;
800
801 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
802
803 p = (char *)d->data;
804 end = p + d->length;
805
806 if (end - p < 4) return -1;
807 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
808 p += 4;
809 if (end - p < 2) return -1;
810 month = 10 * (p[0] - '0') + p[1] - '0';
811 if (month < 1 || month > 12) return -1;
812 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
813 We consider leap years and the current month (<marsh or not) */
814 epoch = ( ((year - 1970) * 365)
815 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
816 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
817 + month_offset[month-1]
818 ) * 24 * 60 * 60;
819 p += 2;
820 if (end - p < 2) return -1;
821 /* Add the number of seconds of completed days of current month */
822 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
823 p += 2;
824 if (end - p < 2) return -1;
825 /* Add the completed hours of the current day */
826 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
827 p += 2;
828 if (end - p < 2) return -1;
829 /* Add the completed minutes of the current hour */
830 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
831 p += 2;
832 if (p == end) return -1;
833 /* Test if there is available seconds */
834 if (p[0] < '0' || p[0] > '9')
835 goto nosec;
836 if (end - p < 2) return -1;
837 /* Add the seconds of the current minute */
838 epoch += 10 * (p[0] - '0') + p[1] - '0';
839 p += 2;
840 if (p == end) return -1;
841 /* Ignore seconds float part if present */
842 if (p[0] == '.') {
843 do {
844 if (++p == end) return -1;
845 } while (p[0] >= '0' && p[0] <= '9');
846 }
847
848nosec:
849 if (p[0] == 'Z') {
850 if (end - p != 1) return -1;
851 return epoch;
852 }
853 else if (p[0] == '+') {
854 if (end - p != 5) return -1;
855 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700856 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 +0200857 }
858 else if (p[0] == '-') {
859 if (end - p != 5) return -1;
860 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700861 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 +0200862 }
863
864 return -1;
865}
866
William Lallemand104a7a62019-10-14 14:14:59 +0200867/*
868 * struct alignment works here such that the key.key is the same as key_data
869 * Do not change the placement of key_data
870 */
871struct certificate_ocsp {
872 struct ebmb_node key;
873 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
874 struct buffer response;
875 long expire;
876};
877
878struct ocsp_cbk_arg {
879 int is_single;
880 int single_kt;
881 union {
882 struct certificate_ocsp *s_ocsp;
883 /*
884 * m_ocsp will have multiple entries dependent on key type
885 * Entry 0 - DSA
886 * Entry 1 - ECDSA
887 * Entry 2 - RSA
888 */
889 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
890 };
891};
892
Emeric Brun1d3865b2014-06-20 15:37:32 +0200893static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200894
895/* This function starts to check if the OCSP response (in DER format) contained
896 * in chunk 'ocsp_response' is valid (else exits on error).
897 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
898 * contained in the OCSP Response and exits on error if no match.
899 * If it's a valid OCSP Response:
900 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
901 * pointed by 'ocsp'.
902 * If 'ocsp' is NULL, the function looks up into the OCSP response's
903 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
904 * from the response) and exits on error if not found. Finally, If an OCSP response is
905 * already present in the container, it will be overwritten.
906 *
907 * Note: OCSP response containing more than one OCSP Single response is not
908 * considered valid.
909 *
910 * Returns 0 on success, 1 in error case.
911 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200912static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
913 struct certificate_ocsp *ocsp,
914 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200915{
916 OCSP_RESPONSE *resp;
917 OCSP_BASICRESP *bs = NULL;
918 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200919 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200920 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200921 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200922 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200923 int reason;
924 int ret = 1;
925
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200926 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
927 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200928 if (!resp) {
929 memprintf(err, "Unable to parse OCSP response");
930 goto out;
931 }
932
933 rc = OCSP_response_status(resp);
934 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
935 memprintf(err, "OCSP response status not successful");
936 goto out;
937 }
938
939 bs = OCSP_response_get1_basic(resp);
940 if (!bs) {
941 memprintf(err, "Failed to get basic response from OCSP Response");
942 goto out;
943 }
944
945 count_sr = OCSP_resp_count(bs);
946 if (count_sr > 1) {
947 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
948 goto out;
949 }
950
951 sr = OCSP_resp_get0(bs, 0);
952 if (!sr) {
953 memprintf(err, "Failed to get OCSP single response");
954 goto out;
955 }
956
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200957 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
958
Emeric Brun4147b2e2014-06-16 18:36:30 +0200959 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200960 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200961 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200962 goto out;
963 }
964
Emeric Brun13a6b482014-06-20 15:44:34 +0200965 if (!nextupd) {
966 memprintf(err, "OCSP single response: missing nextupdate");
967 goto out;
968 }
969
Emeric Brunc8b27b62014-06-19 14:16:17 +0200970 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200971 if (!rc) {
972 memprintf(err, "OCSP single response: no longer valid.");
973 goto out;
974 }
975
976 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200977 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200978 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
979 goto out;
980 }
981 }
982
983 if (!ocsp) {
984 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
985 unsigned char *p;
986
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200987 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200988 if (!rc) {
989 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
990 goto out;
991 }
992
993 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
994 memprintf(err, "OCSP single response: Certificate ID too long");
995 goto out;
996 }
997
998 p = key;
999 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001000 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001001 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
1002 if (!ocsp) {
1003 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
1004 goto out;
1005 }
1006 }
1007
1008 /* According to comments on "chunk_dup", the
1009 previous chunk buffer will be freed */
1010 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1011 memprintf(err, "OCSP response: Memory allocation error");
1012 goto out;
1013 }
1014
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001015 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1016
Emeric Brun4147b2e2014-06-16 18:36:30 +02001017 ret = 0;
1018out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001019 ERR_clear_error();
1020
Emeric Brun4147b2e2014-06-16 18:36:30 +02001021 if (bs)
1022 OCSP_BASICRESP_free(bs);
1023
1024 if (resp)
1025 OCSP_RESPONSE_free(resp);
1026
1027 return ret;
1028}
1029/*
1030 * External function use to update the OCSP response in the OCSP response's
1031 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1032 * to update in DER format.
1033 *
1034 * Returns 0 on success, 1 in error case.
1035 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001036int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001037{
1038 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1039}
1040
William Lallemand4a660132019-10-14 14:51:41 +02001041#endif
1042
1043#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001044/*
1045 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +02001046 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +02001047 *
1048 * Returns 0 on success, 1 in error case.
1049 */
William Lallemand3b5f3602019-10-16 18:05:05 +02001050static 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 +02001051{
1052 int fd = -1;
1053 int r = 0;
1054 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +02001055 struct buffer *ocsp_response;
1056 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001057
William Lallemand3b5f3602019-10-16 18:05:05 +02001058 if (buf) {
1059 int i, j;
1060 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001061
William Lallemand3b5f3602019-10-16 18:05:05 +02001062 /* remove \r and \n from the payload */
1063 for (i = 0, j = 0; buf[i]; i++) {
1064 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +02001065 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +02001066 buf[j++] = buf[i];
1067 }
1068 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001069
William Lallemand3b5f3602019-10-16 18:05:05 +02001070 ret = base64dec(buf, j, trash.area, trash.size);
1071 if (ret < 0) {
1072 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +02001073 goto end;
1074 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001075 trash.data = ret;
1076 src = &trash;
1077 } else {
1078 fd = open(ocsp_path, O_RDONLY);
1079 if (fd == -1) {
1080 memprintf(err, "Error opening OCSP response file");
1081 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001082 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001083
1084 trash.data = 0;
1085 while (trash.data < trash.size) {
1086 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1087 if (r < 0) {
1088 if (errno == EINTR)
1089 continue;
1090
1091 memprintf(err, "Error reading OCSP response from file");
1092 goto end;
1093 }
1094 else if (r == 0) {
1095 break;
1096 }
1097 trash.data += r;
1098 }
1099 close(fd);
1100 fd = -1;
1101 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001102 }
1103
William Lallemand3b5f3602019-10-16 18:05:05 +02001104 ocsp_response = calloc(1, sizeof(*ocsp_response));
1105 if (!chunk_dup(ocsp_response, src)) {
1106 free(ocsp_response);
1107 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001108 goto end;
1109 }
1110
William Lallemand3b5f3602019-10-16 18:05:05 +02001111 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001112 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001113end:
1114 if (fd != -1)
1115 close(fd);
1116
1117 return ret;
1118}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001119#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001120
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001121#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1122static 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)
1123{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001124 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001125 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001126 struct connection *conn;
1127 int head;
1128 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001129 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001130
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001131 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001132 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001133 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1134
1135 keys = ref->tlskeys;
1136 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001137
1138 if (enc) {
1139 memcpy(key_name, keys[head].name, 16);
1140
1141 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001142 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001143
Emeric Brun9e754772019-01-10 17:51:55 +01001144 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001145
Emeric Brun9e754772019-01-10 17:51:55 +01001146 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1147 goto end;
1148
Willy Tarreau9356dac2019-05-10 09:22:53 +02001149 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001150 ret = 1;
1151 }
1152 else if (ref->key_size_bits == 256 ) {
1153
1154 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1155 goto end;
1156
Willy Tarreau9356dac2019-05-10 09:22:53 +02001157 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001158 ret = 1;
1159 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001160 } else {
1161 for (i = 0; i < TLS_TICKETS_NO; i++) {
1162 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1163 goto found;
1164 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001165 ret = 0;
1166 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001167
Christopher Faulet16f45c82018-02-16 11:23:49 +01001168 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001169 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001170 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 +01001171 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1172 goto end;
1173 /* 2 for key renewal, 1 if current key is still valid */
1174 ret = i ? 2 : 1;
1175 }
1176 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001177 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 +01001178 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1179 goto end;
1180 /* 2 for key renewal, 1 if current key is still valid */
1181 ret = i ? 2 : 1;
1182 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001183 }
Emeric Brun9e754772019-01-10 17:51:55 +01001184
Christopher Faulet16f45c82018-02-16 11:23:49 +01001185 end:
1186 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1187 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001188}
1189
1190struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1191{
1192 struct tls_keys_ref *ref;
1193
1194 list_for_each_entry(ref, &tlskeys_reference, list)
1195 if (ref->filename && strcmp(filename, ref->filename) == 0)
1196 return ref;
1197 return NULL;
1198}
1199
1200struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1201{
1202 struct tls_keys_ref *ref;
1203
1204 list_for_each_entry(ref, &tlskeys_reference, list)
1205 if (ref->unique_id == unique_id)
1206 return ref;
1207 return NULL;
1208}
1209
Emeric Brun9e754772019-01-10 17:51:55 +01001210/* Update the key into ref: if keysize doesnt
1211 * match existing ones, this function returns -1
1212 * else it returns 0 on success.
1213 */
1214int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001215 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001216{
Emeric Brun9e754772019-01-10 17:51:55 +01001217 if (ref->key_size_bits == 128) {
1218 if (tlskey->data != sizeof(struct tls_sess_key_128))
1219 return -1;
1220 }
1221 else if (ref->key_size_bits == 256) {
1222 if (tlskey->data != sizeof(struct tls_sess_key_256))
1223 return -1;
1224 }
1225 else
1226 return -1;
1227
Christopher Faulet16f45c82018-02-16 11:23:49 +01001228 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001229 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1230 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001231 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1232 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001233
1234 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001235}
1236
Willy Tarreau83061a82018-07-13 11:56:34 +02001237int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001238{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001239 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1240
1241 if(!ref) {
1242 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1243 return 1;
1244 }
Emeric Brun9e754772019-01-10 17:51:55 +01001245 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1246 memprintf(err, "Invalid key size");
1247 return 1;
1248 }
1249
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001250 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001251}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001252
1253/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001254 * automatic ids. It's called just after the basic checks. It returns
1255 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001256 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001257static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001258{
1259 int i = 0;
1260 struct tls_keys_ref *ref, *ref2, *ref3;
1261 struct list tkr = LIST_HEAD_INIT(tkr);
1262
1263 list_for_each_entry(ref, &tlskeys_reference, list) {
1264 if (ref->unique_id == -1) {
1265 /* Look for the first free id. */
1266 while (1) {
1267 list_for_each_entry(ref2, &tlskeys_reference, list) {
1268 if (ref2->unique_id == i) {
1269 i++;
1270 break;
1271 }
1272 }
1273 if (&ref2->list == &tlskeys_reference)
1274 break;
1275 }
1276
1277 /* Uses the unique id and increment it for the next entry. */
1278 ref->unique_id = i;
1279 i++;
1280 }
1281 }
1282
1283 /* This sort the reference list by id. */
1284 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1285 LIST_DEL(&ref->list);
1286 list_for_each_entry(ref3, &tkr, list) {
1287 if (ref->unique_id < ref3->unique_id) {
1288 LIST_ADDQ(&ref3->list, &ref->list);
1289 break;
1290 }
1291 }
1292 if (&ref3->list == &tkr)
1293 LIST_ADDQ(&tkr, &ref->list);
1294 }
1295
1296 /* swap root */
1297 LIST_ADD(&tkr, &tlskeys_reference);
1298 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001299 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001300}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001301#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1302
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001303#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001304int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1305{
1306 switch (evp_keytype) {
1307 case EVP_PKEY_RSA:
1308 return 2;
1309 case EVP_PKEY_DSA:
1310 return 0;
1311 case EVP_PKEY_EC:
1312 return 1;
1313 }
1314
1315 return -1;
1316}
1317
Emeric Brun4147b2e2014-06-16 18:36:30 +02001318/*
1319 * Callback used to set OCSP status extension content in server hello.
1320 */
1321int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1322{
yanbzhube2774d2015-12-10 15:07:30 -05001323 struct certificate_ocsp *ocsp;
1324 struct ocsp_cbk_arg *ocsp_arg;
1325 char *ssl_buf;
1326 EVP_PKEY *ssl_pkey;
1327 int key_type;
1328 int index;
1329
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001330 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001331
1332 ssl_pkey = SSL_get_privatekey(ssl);
1333 if (!ssl_pkey)
1334 return SSL_TLSEXT_ERR_NOACK;
1335
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001336 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001337
1338 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1339 ocsp = ocsp_arg->s_ocsp;
1340 else {
1341 /* For multiple certs per context, we have to find the correct OCSP response based on
1342 * the certificate type
1343 */
1344 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1345
1346 if (index < 0)
1347 return SSL_TLSEXT_ERR_NOACK;
1348
1349 ocsp = ocsp_arg->m_ocsp[index];
1350
1351 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001352
1353 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001354 !ocsp->response.area ||
1355 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001356 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001357 return SSL_TLSEXT_ERR_NOACK;
1358
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001359 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001360 if (!ssl_buf)
1361 return SSL_TLSEXT_ERR_NOACK;
1362
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001363 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1364 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001365
1366 return SSL_TLSEXT_ERR_OK;
1367}
1368
William Lallemand4a660132019-10-14 14:51:41 +02001369#endif
1370
1371#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001372/*
1373 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001374 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1375 * status extension, the issuer's certificate is mandatory. It should be
1376 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001377 *
William Lallemand246c0242019-10-11 08:59:13 +02001378 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1379 * OCSP response. If file is empty or content is not a valid OCSP response,
1380 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1381 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001382 *
1383 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001384 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001385 */
William Lallemand4a660132019-10-14 14:51:41 +02001386#ifndef OPENSSL_IS_BORINGSSL
William Lallemand246c0242019-10-11 08:59:13 +02001387static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001388{
William Lallemand246c0242019-10-11 08:59:13 +02001389 X509 *x = NULL, *issuer = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001390 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001391 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001392 struct certificate_ocsp *ocsp = NULL, *iocsp;
1393 char *warn = NULL;
1394 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001395 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001396
Emeric Brun4147b2e2014-06-16 18:36:30 +02001397
William Lallemand246c0242019-10-11 08:59:13 +02001398 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001399 if (!x)
1400 goto out;
1401
William Lallemand246c0242019-10-11 08:59:13 +02001402 issuer = ckch->ocsp_issuer;
1403 if (!issuer)
1404 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001405
1406 cid = OCSP_cert_to_id(0, x, issuer);
1407 if (!cid)
1408 goto out;
1409
1410 i = i2d_OCSP_CERTID(cid, NULL);
1411 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1412 goto out;
1413
Vincent Bernat02779b62016-04-03 13:48:43 +02001414 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001415 if (!ocsp)
1416 goto out;
1417
1418 p = ocsp->key_data;
1419 i2d_OCSP_CERTID(cid, &p);
1420
1421 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1422 if (iocsp == ocsp)
1423 ocsp = NULL;
1424
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001425#ifndef SSL_CTX_get_tlsext_status_cb
1426# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1427 *cb = (void (*) (void))ctx->tlsext_status_cb;
1428#endif
1429 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1430
1431 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001432 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001433 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001434
1435 cb_arg->is_single = 1;
1436 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001437
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001438 pkey = X509_get_pubkey(x);
1439 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1440 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001441
1442 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1443 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1444 } else {
1445 /*
1446 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1447 * Update that cb_arg with the new cert's staple
1448 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001449 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001450 struct certificate_ocsp *tmp_ocsp;
1451 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001452 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001453 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001454
1455#ifdef SSL_CTX_get_tlsext_status_arg
1456 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1457#else
1458 cb_arg = ctx->tlsext_status_arg;
1459#endif
yanbzhube2774d2015-12-10 15:07:30 -05001460
1461 /*
1462 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1463 * the order of operations below matter, take care when changing it
1464 */
1465 tmp_ocsp = cb_arg->s_ocsp;
1466 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1467 cb_arg->s_ocsp = NULL;
1468 cb_arg->m_ocsp[index] = tmp_ocsp;
1469 cb_arg->is_single = 0;
1470 cb_arg->single_kt = 0;
1471
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001472 pkey = X509_get_pubkey(x);
1473 key_type = EVP_PKEY_base_id(pkey);
1474 EVP_PKEY_free(pkey);
1475
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001476 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001477 if (index >= 0 && !cb_arg->m_ocsp[index])
1478 cb_arg->m_ocsp[index] = iocsp;
1479
1480 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001481
1482 ret = 0;
1483
1484 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001485 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001486 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001487 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001488 }
1489
1490out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001491 if (cid)
1492 OCSP_CERTID_free(cid);
1493
1494 if (ocsp)
1495 free(ocsp);
1496
1497 if (warn)
1498 free(warn);
1499
Emeric Brun4147b2e2014-06-16 18:36:30 +02001500 return ret;
1501}
William Lallemand4a660132019-10-14 14:51:41 +02001502#else /* OPENSSL_IS_BORINGSSL */
1503static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001504{
William Lallemand4a660132019-10-14 14:51:41 +02001505 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 +02001506}
1507#endif
1508
William Lallemand4a660132019-10-14 14:51:41 +02001509#endif
1510
1511
Willy Tarreau5db847a2019-05-09 14:13:35 +02001512#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001513
1514#define CT_EXTENSION_TYPE 18
1515
1516static int sctl_ex_index = -1;
1517
1518/*
1519 * Try to parse Signed Certificate Timestamp List structure. This function
1520 * makes only basic test if the data seems like SCTL. No signature validation
1521 * is performed.
1522 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001523static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001524{
1525 int ret = 1;
1526 int len, pos, sct_len;
1527 unsigned char *data;
1528
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001529 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001530 goto out;
1531
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001532 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001533 len = (data[0] << 8) | data[1];
1534
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001535 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001536 goto out;
1537
1538 data = data + 2;
1539 pos = 0;
1540 while (pos < len) {
1541 if (len - pos < 2)
1542 goto out;
1543
1544 sct_len = (data[pos] << 8) | data[pos + 1];
1545 if (pos + sct_len + 2 > len)
1546 goto out;
1547
1548 pos += sct_len + 2;
1549 }
1550
1551 ret = 0;
1552
1553out:
1554 return ret;
1555}
1556
William Lallemand0dfae6c2019-10-16 18:06:58 +02001557/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1558 * It fills the ckch->sctl buffer
1559 * return 0 on success or != 0 on failure */
1560static 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 +01001561{
1562 int fd = -1;
1563 int r = 0;
1564 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001565 struct buffer tmp;
1566 struct buffer *src;
1567 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001568
William Lallemand0dfae6c2019-10-16 18:06:58 +02001569 if (buf) {
1570 tmp.area = buf;
1571 tmp.data = strlen(buf);
1572 tmp.size = tmp.data + 1;
1573 src = &tmp;
1574 } else {
1575 fd = open(sctl_path, O_RDONLY);
1576 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001577 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001578
1579 trash.data = 0;
1580 while (trash.data < trash.size) {
1581 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1582 if (r < 0) {
1583 if (errno == EINTR)
1584 continue;
1585 goto end;
1586 }
1587 else if (r == 0) {
1588 break;
1589 }
1590 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001591 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001592 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001593 }
1594
William Lallemand0dfae6c2019-10-16 18:06:58 +02001595 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001596 if (ret)
1597 goto end;
1598
William Lallemand0dfae6c2019-10-16 18:06:58 +02001599 sctl = calloc(1, sizeof(*sctl));
1600 if (!chunk_dup(sctl, src)) {
1601 free(sctl);
1602 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001603 goto end;
1604 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001605 ret = 0;
1606 /* TODO: free the previous SCTL in the ckch */
1607 ckch->sctl = sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001608
1609end:
1610 if (fd != -1)
1611 close(fd);
1612
1613 return ret;
1614}
1615
1616int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1617{
Willy Tarreau83061a82018-07-13 11:56:34 +02001618 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001619
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001620 *out = (unsigned char *) sctl->area;
1621 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001622
1623 return 1;
1624}
1625
1626int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1627{
1628 return 1;
1629}
1630
William Lallemanda17f4112019-10-10 15:16:44 +02001631static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001632{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001633 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001634
William Lallemanda17f4112019-10-10 15:16:44 +02001635 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 +01001636 goto out;
1637
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001638 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1639
1640 ret = 0;
1641
1642out:
1643 return ret;
1644}
1645
1646#endif
1647
Emeric Brune1f38db2012-09-03 20:36:47 +02001648void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1649{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001650 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001651 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001652 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001653 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001654
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001655#ifndef SSL_OP_NO_RENEGOTIATION
1656 /* Please note that BoringSSL defines this macro to zero so don't
1657 * change this to #if and do not assign a default value to this macro!
1658 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001659 if (where & SSL_CB_HANDSHAKE_START) {
1660 /* Disable renegotiation (CVE-2009-3555) */
Olivier Houchard90084a12017-11-23 18:21:29 +01001661 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 +02001662 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001663 conn->err_code = CO_ER_SSL_RENEG;
1664 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001665 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001666#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001667
1668 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001669 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001670 /* Long certificate chains optimz
1671 If write and read bios are differents, we
1672 consider that the buffering was activated,
1673 so we rise the output buffer size from 4k
1674 to 16k */
1675 write_bio = SSL_get_wbio(ssl);
1676 if (write_bio != SSL_get_rbio(ssl)) {
1677 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001678 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001679 }
1680 }
1681 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001682}
1683
Emeric Brune64aef12012-09-21 13:15:06 +02001684/* Callback is called for each certificate of the chain during a verify
1685 ok is set to 1 if preverify detect no error on current certificate.
1686 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001687int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001688{
1689 SSL *ssl;
1690 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001691 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001692 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001693
1694 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001695 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001696
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001697 ctx = conn->xprt_ctx;
1698
1699 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001700
Emeric Brun81c00f02012-09-21 14:31:21 +02001701 if (ok) /* no errors */
1702 return ok;
1703
1704 depth = X509_STORE_CTX_get_error_depth(x_store);
1705 err = X509_STORE_CTX_get_error(x_store);
1706
1707 /* check if CA error needs to be ignored */
1708 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001709 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1710 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1711 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001712 }
1713
Willy Tarreau07d94e42018-09-20 10:57:52 +02001714 if (__objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001715 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001716 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001717 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001718 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001719
Willy Tarreau20879a02012-12-03 16:32:10 +01001720 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001721 return 0;
1722 }
1723
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001724 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1725 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001726
Emeric Brun81c00f02012-09-21 14:31:21 +02001727 /* check if certificate error needs to be ignored */
Willy Tarreau07d94e42018-09-20 10:57:52 +02001728 if (__objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001729 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001730 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001731 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001732 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001733
Willy Tarreau20879a02012-12-03 16:32:10 +01001734 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001735 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001736}
1737
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001738static inline
1739void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001740 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001741{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001742 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001743 unsigned char *msg;
1744 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001745 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001746
1747 /* This function is called for "from client" and "to server"
1748 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001749 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001750 */
1751
1752 /* "write_p" is set to 0 is the bytes are received messages,
1753 * otherwise it is set to 1.
1754 */
1755 if (write_p != 0)
1756 return;
1757
1758 /* content_type contains the type of message received or sent
1759 * according with the SSL/TLS protocol spec. This message is
1760 * encoded with one byte. The value 256 (two bytes) is used
1761 * for designing the SSL/TLS record layer. According with the
1762 * rfc6101, the expected message (other than 256) are:
1763 * - change_cipher_spec(20)
1764 * - alert(21)
1765 * - handshake(22)
1766 * - application_data(23)
1767 * - (255)
1768 * We are interessed by the handshake and specially the client
1769 * hello.
1770 */
1771 if (content_type != 22)
1772 return;
1773
1774 /* The message length is at least 4 bytes, containing the
1775 * message type and the message length.
1776 */
1777 if (len < 4)
1778 return;
1779
1780 /* First byte of the handshake message id the type of
1781 * message. The konwn types are:
1782 * - hello_request(0)
1783 * - client_hello(1)
1784 * - server_hello(2)
1785 * - certificate(11)
1786 * - server_key_exchange (12)
1787 * - certificate_request(13)
1788 * - server_hello_done(14)
1789 * We are interested by the client hello.
1790 */
1791 msg = (unsigned char *)buf;
1792 if (msg[0] != 1)
1793 return;
1794
1795 /* Next three bytes are the length of the message. The total length
1796 * must be this decoded length + 4. If the length given as argument
1797 * is not the same, we abort the protocol dissector.
1798 */
1799 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1800 if (len < rec_len + 4)
1801 return;
1802 msg += 4;
1803 end = msg + rec_len;
1804 if (end < msg)
1805 return;
1806
1807 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1808 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001809 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1810 */
1811 msg += 1 + 1 + 4 + 28;
1812 if (msg > end)
1813 return;
1814
1815 /* Next, is session id:
1816 * if present, we have to jump by length + 1 for the size information
1817 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001818 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001819 if (msg[0] > 0)
1820 msg += msg[0];
1821 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001822 if (msg > end)
1823 return;
1824
1825 /* Next two bytes are the ciphersuite length. */
1826 if (msg + 2 > end)
1827 return;
1828 rec_len = (msg[0] << 8) + msg[1];
1829 msg += 2;
1830 if (msg + rec_len > end || msg + rec_len < msg)
1831 return;
1832
Willy Tarreaubafbe012017-11-24 17:34:44 +01001833 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001834 if (!capture)
1835 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001836 /* Compute the xxh64 of the ciphersuite. */
1837 capture->xxh64 = XXH64(msg, rec_len, 0);
1838
1839 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001840 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1841 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001842 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001843
1844 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001845}
1846
Emeric Brun29f037d2014-04-25 19:05:36 +02001847/* Callback is called for ssl protocol analyse */
1848void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1849{
Emeric Brun29f037d2014-04-25 19:05:36 +02001850#ifdef TLS1_RT_HEARTBEAT
1851 /* test heartbeat received (write_p is set to 0
1852 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001853 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001854 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001855 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001856 const unsigned char *p = buf;
1857 unsigned int payload;
1858
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001859 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001860
1861 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1862 if (*p != TLS1_HB_REQUEST)
1863 return;
1864
Willy Tarreauaeed6722014-04-25 23:59:58 +02001865 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001866 goto kill_it;
1867
1868 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001869 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001870 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001871 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001872 /* We have a clear heartbleed attack (CVE-2014-0160), the
1873 * advertised payload is larger than the advertised packet
1874 * length, so we have garbage in the buffer between the
1875 * payload and the end of the buffer (p+len). We can't know
1876 * if the SSL stack is patched, and we don't know if we can
1877 * safely wipe out the area between p+3+len and payload.
1878 * So instead, we prevent the response from being sent by
1879 * setting the max_send_fragment to 0 and we report an SSL
1880 * error, which will kill this connection. It will be reported
1881 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001882 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1883 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001884 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001885 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1886 return;
1887 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001888#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001889 if (global_ssl.capture_cipherlist > 0)
1890 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001891}
1892
Bernard Spil13c53f82018-02-15 13:34:58 +01001893#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001894static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1895 const unsigned char *in, unsigned int inlen,
1896 void *arg)
1897{
1898 struct server *srv = arg;
1899
1900 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1901 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1902 return SSL_TLSEXT_ERR_OK;
1903 return SSL_TLSEXT_ERR_NOACK;
1904}
1905#endif
1906
1907#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001908/* This callback is used so that the server advertises the list of
1909 * negociable protocols for NPN.
1910 */
1911static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1912 unsigned int *len, void *arg)
1913{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001914 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001915
1916 *data = (const unsigned char *)conf->npn_str;
1917 *len = conf->npn_len;
1918 return SSL_TLSEXT_ERR_OK;
1919}
1920#endif
1921
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001922#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001923/* This callback is used so that the server advertises the list of
1924 * negociable protocols for ALPN.
1925 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001926static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1927 unsigned char *outlen,
1928 const unsigned char *server,
1929 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001930{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001931 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001932
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001933 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1934 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1935 return SSL_TLSEXT_ERR_NOACK;
1936 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001937 return SSL_TLSEXT_ERR_OK;
1938}
1939#endif
1940
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001941#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001942#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001943
Christopher Faulet30548802015-06-11 13:39:32 +02001944/* Create a X509 certificate with the specified servername and serial. This
1945 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001946static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001947ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001948{
Christopher Faulet7969a332015-10-09 11:15:03 +02001949 X509 *cacert = bind_conf->ca_sign_cert;
1950 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001951 SSL_CTX *ssl_ctx = NULL;
1952 X509 *newcrt = NULL;
1953 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001954 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001955 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001956 X509_NAME *name;
1957 const EVP_MD *digest;
1958 X509V3_CTX ctx;
1959 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001960 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001961
Christopher Faulet48a83322017-07-28 16:56:09 +02001962 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001963#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001964 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1965#else
1966 tmp_ssl = SSL_new(bind_conf->default_ctx);
1967 if (tmp_ssl)
1968 pkey = SSL_get_privatekey(tmp_ssl);
1969#endif
1970 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001971 goto mkcert_error;
1972
1973 /* Create the certificate */
1974 if (!(newcrt = X509_new()))
1975 goto mkcert_error;
1976
1977 /* Set version number for the certificate (X509v3) and the serial
1978 * number */
1979 if (X509_set_version(newcrt, 2L) != 1)
1980 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001981 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001982
1983 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001984 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1985 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001986 goto mkcert_error;
1987
1988 /* set public key in the certificate */
1989 if (X509_set_pubkey(newcrt, pkey) != 1)
1990 goto mkcert_error;
1991
1992 /* Set issuer name from the CA */
1993 if (!(name = X509_get_subject_name(cacert)))
1994 goto mkcert_error;
1995 if (X509_set_issuer_name(newcrt, name) != 1)
1996 goto mkcert_error;
1997
1998 /* Set the subject name using the same, but the CN */
1999 name = X509_NAME_dup(name);
2000 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2001 (const unsigned char *)servername,
2002 -1, -1, 0) != 1) {
2003 X509_NAME_free(name);
2004 goto mkcert_error;
2005 }
2006 if (X509_set_subject_name(newcrt, name) != 1) {
2007 X509_NAME_free(name);
2008 goto mkcert_error;
2009 }
2010 X509_NAME_free(name);
2011
2012 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002013 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002014 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2015 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2016 X509_EXTENSION *ext;
2017
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002018 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002019 goto mkcert_error;
2020 if (!X509_add_ext(newcrt, ext, -1)) {
2021 X509_EXTENSION_free(ext);
2022 goto mkcert_error;
2023 }
2024 X509_EXTENSION_free(ext);
2025 }
2026
2027 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002028
2029 key_type = EVP_PKEY_base_id(capkey);
2030
2031 if (key_type == EVP_PKEY_DSA)
2032 digest = EVP_sha1();
2033 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002034 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002035 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002036 digest = EVP_sha256();
2037 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002038#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002039 int nid;
2040
2041 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2042 goto mkcert_error;
2043 if (!(digest = EVP_get_digestbynid(nid)))
2044 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002045#else
2046 goto mkcert_error;
2047#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002048 }
2049
Christopher Faulet31af49d2015-06-09 17:29:50 +02002050 if (!(X509_sign(newcrt, capkey, digest)))
2051 goto mkcert_error;
2052
2053 /* Create and set the new SSL_CTX */
2054 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2055 goto mkcert_error;
2056 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2057 goto mkcert_error;
2058 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2059 goto mkcert_error;
2060 if (!SSL_CTX_check_private_key(ssl_ctx))
2061 goto mkcert_error;
2062
2063 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002064
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002065#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002066 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002067#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002068#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2069 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002070 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002071 EC_KEY *ecc;
2072 int nid;
2073
2074 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2075 goto end;
2076 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2077 goto end;
2078 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2079 EC_KEY_free(ecc);
2080 }
2081#endif
2082 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002083 return ssl_ctx;
2084
2085 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002086 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002087 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002088 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2089 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002090 return NULL;
2091}
2092
Christopher Faulet7969a332015-10-09 11:15:03 +02002093SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002094ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002095{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002096 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002097 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002098
Olivier Houchard66ab4982019-02-26 18:37:15 +01002099 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002100}
2101
Christopher Faulet30548802015-06-11 13:39:32 +02002102/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002103 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002104SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002105ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002106{
2107 struct lru64 *lru = NULL;
2108
2109 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002110 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002111 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002112 if (lru && lru->domain) {
2113 if (ssl)
2114 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002115 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002116 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002117 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002118 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002119 }
2120 return NULL;
2121}
2122
Emeric Brun821bb9b2017-06-15 16:37:39 +02002123/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2124 * function is not thread-safe, it should only be used to check if a certificate
2125 * exists in the lru cache (with no warranty it will not be removed by another
2126 * thread). It is kept for backward compatibility. */
2127SSL_CTX *
2128ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2129{
2130 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2131}
2132
Christopher Fauletd2cab922015-07-28 16:03:47 +02002133/* Set a certificate int the LRU cache used to store generated
2134 * certificate. Return 0 on success, otherwise -1 */
2135int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002136ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002137{
2138 struct lru64 *lru = NULL;
2139
2140 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002141 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002142 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002143 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002144 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002145 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002146 }
Christopher Faulet30548802015-06-11 13:39:32 +02002147 if (lru->domain && lru->data)
2148 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002149 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002150 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002151 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002152 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002153 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002154}
2155
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002156/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002157unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002158ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002159{
2160 return XXH32(data, len, ssl_ctx_lru_seed);
2161}
2162
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002163/* Generate a cert and immediately assign it to the SSL session so that the cert's
2164 * refcount is maintained regardless of the cert's presence in the LRU cache.
2165 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002166static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002167ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002168{
2169 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002170 SSL_CTX *ssl_ctx = NULL;
2171 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002172 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002173
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002174 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002175 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002176 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002177 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002178 if (lru && lru->domain)
2179 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002180 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002181 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002182 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002183 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002184 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002185 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002186 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002187 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002188 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002189 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002190 SSL_set_SSL_CTX(ssl, ssl_ctx);
2191 /* No LRU cache, this CTX will be released as soon as the session dies */
2192 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002193 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002194 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002195 return 0;
2196}
2197static int
2198ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2199{
2200 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002201 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002202
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002203 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002204 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002205 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002206 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002207 }
2208 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002209}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002210#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002211
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002212#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002213typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2214
2215static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002216{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002217#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002218 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002219 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2220#endif
2221}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002222static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2223 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002224 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2225}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002226static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002227#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002228 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002229 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2230#endif
2231}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002232static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002233#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002234 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002235 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2236#endif
2237}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002238/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002239static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2240/* Unusable in this context. */
2241static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2242static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2243static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2244static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2245static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002246#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002247typedef enum { SET_MIN, SET_MAX } set_context_func;
2248
2249static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2250 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002251 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2252}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002253static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2254 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2255 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2256}
2257static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2258 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002259 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2260}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002261static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2262 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2263 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2264}
2265static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2266 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002267 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2268}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002269static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2270 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2271 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2272}
2273static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2274 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002275 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2276}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002277static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2278 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2279 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2280}
2281static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002282#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002283 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002284 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2285#endif
2286}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002287static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2288#if SSL_OP_NO_TLSv1_3
2289 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2290 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002291#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002292}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002293#endif
2294static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2295static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002296
2297static struct {
2298 int option;
2299 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002300 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2301 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002302 const char *name;
2303} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002304 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2305 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2306 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2307 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2308 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2309 {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 +02002310};
2311
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002312static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2313{
2314 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2315 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2316 SSL_set_SSL_CTX(ssl, ctx);
2317}
2318
Willy Tarreau5db847a2019-05-09 14:13:35 +02002319#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002320
2321static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2322{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002323 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002324 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002325
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002326 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2327 return SSL_TLSEXT_ERR_OK;
2328 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002329}
2330
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002331#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002332static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2333{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002334 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002335#else
2336static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2337{
2338#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002339 struct connection *conn;
2340 struct bind_conf *s;
2341 const uint8_t *extension_data;
2342 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002343 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002344
2345 char *wildp = NULL;
2346 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002347 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002348 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002349 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002350 int i;
2351
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002352 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002353 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002354
Olivier Houchard9679ac92017-10-27 14:58:08 +02002355 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002356 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002357#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002358 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2359 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002360#else
2361 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2362#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002363 /*
2364 * The server_name extension was given too much extensibility when it
2365 * was written, so parsing the normal case is a bit complex.
2366 */
2367 size_t len;
2368 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002369 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002370 /* Extract the length of the supplied list of names. */
2371 len = (*extension_data++) << 8;
2372 len |= *extension_data++;
2373 if (len + 2 != extension_len)
2374 goto abort;
2375 /*
2376 * The list in practice only has a single element, so we only consider
2377 * the first one.
2378 */
2379 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2380 goto abort;
2381 extension_len = len - 1;
2382 /* Now we can finally pull out the byte array with the actual hostname. */
2383 if (extension_len <= 2)
2384 goto abort;
2385 len = (*extension_data++) << 8;
2386 len |= *extension_data++;
2387 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2388 || memchr(extension_data, 0, len) != NULL)
2389 goto abort;
2390 servername = extension_data;
2391 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002392 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002393#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2394 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002395 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002396 }
2397#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002398 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002399 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002400 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002401 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002402 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002403 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002404 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002405 goto abort;
2406 }
2407
2408 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002409#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002410 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002411#else
2412 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2413#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002414 uint8_t sign;
2415 size_t len;
2416 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002417 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002418 len = (*extension_data++) << 8;
2419 len |= *extension_data++;
2420 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002421 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002422 if (len % 2 != 0)
2423 goto abort;
2424 for (; len > 0; len -= 2) {
2425 extension_data++; /* hash */
2426 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002427 switch (sign) {
2428 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002429 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002430 break;
2431 case TLSEXT_signature_ecdsa:
2432 has_ecdsa_sig = 1;
2433 break;
2434 default:
2435 continue;
2436 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002437 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002438 break;
2439 }
2440 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002441 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002442 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002443 }
2444 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002445 const SSL_CIPHER *cipher;
2446 size_t len;
2447 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002448 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002449#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002450 len = ctx->cipher_suites_len;
2451 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002452#else
2453 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2454#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002455 if (len % 2 != 0)
2456 goto abort;
2457 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002458#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002459 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002460 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002461#else
2462 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2463#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002464 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002465 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002466 break;
2467 }
2468 }
2469 }
2470
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002471 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002472 trash.area[i] = tolower(servername[i]);
2473 if (!wildp && (trash.area[i] == '.'))
2474 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002475 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002476 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002477
William Lallemand150bfa82019-09-19 17:12:49 +02002478 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002479
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002480 for (i = 0; i < 2; i++) {
2481 if (i == 0) /* lookup in full qualified names */
2482 node = ebst_lookup(&s->sni_ctx, trash.area);
2483 else if (i == 1 && wildp) /* lookup in wildcards names */
2484 node = ebst_lookup(&s->sni_w_ctx, wildp);
2485 else
2486 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002487 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002488 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002489 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002490 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002491 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002492 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002493 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002494 break;
2495 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002496 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002497 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002498 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002499 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002500 if (!node_anonymous)
2501 node_anonymous = n;
2502 break;
2503 }
2504 }
2505 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002506 /* select by key_signature priority order */
2507 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2508 : ((has_rsa_sig && node_rsa) ? node_rsa
2509 : (node_anonymous ? node_anonymous
2510 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2511 : node_rsa /* no rsa signature case (far far away) */
2512 )));
2513 if (node) {
2514 /* switch ctx */
2515 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2516 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002517 if (conf) {
2518 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2519 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2520 if (conf->early_data)
2521 allow_early = 1;
2522 }
William Lallemand02010472019-10-18 11:02:19 +02002523 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002524 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002525 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002526 }
William Lallemand150bfa82019-09-19 17:12:49 +02002527
William Lallemand02010472019-10-18 11:02:19 +02002528 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002529#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002530 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002531 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002532 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002533 }
2534#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002535 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002536 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002537 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002538 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002539 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002540 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002541allow_early:
2542#ifdef OPENSSL_IS_BORINGSSL
2543 if (allow_early)
2544 SSL_set_early_data_enabled(ssl, 1);
2545#else
2546 if (!allow_early)
2547 SSL_set_max_early_data(ssl, 0);
2548#endif
2549 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002550 abort:
2551 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2552 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002553#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002554 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002555#else
2556 *al = SSL_AD_UNRECOGNIZED_NAME;
2557 return 0;
2558#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002559}
2560
2561#else /* OPENSSL_IS_BORINGSSL */
2562
Emeric Brunfc0421f2012-09-07 17:30:07 +02002563/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2564 * warning when no match is found, which implies the default (first) cert
2565 * will keep being used.
2566 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002567static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002568{
2569 const char *servername;
2570 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002571 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002572 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002573 int i;
2574 (void)al; /* shut gcc stupid warning */
2575
2576 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002577 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002578#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002579 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2580 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002581#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002582 if (s->strict_sni)
2583 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002584 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002585 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002586 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002587 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002588 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002589
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002590 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002591 if (!servername[i])
2592 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002593 trash.area[i] = tolower(servername[i]);
2594 if (!wildp && (trash.area[i] == '.'))
2595 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002596 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002597 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002598
William Lallemand150bfa82019-09-19 17:12:49 +02002599 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002600 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002601 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002602 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2603 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002604 if (!container_of(n, struct sni_ctx, name)->neg) {
2605 node = n;
2606 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002607 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002608 }
2609 if (!node && wildp) {
2610 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002611 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2612 /* lookup a not neg filter */
2613 if (!container_of(n, struct sni_ctx, name)->neg) {
2614 node = n;
2615 break;
2616 }
2617 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002618 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002619 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002620#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002621 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2622 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002623 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002624 return SSL_TLSEXT_ERR_OK;
2625 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002626#endif
William Lallemand21724f02019-11-04 17:56:13 +01002627 if (s->strict_sni) {
2628 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002629 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002630 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002631 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002632 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002633 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002634 }
2635
2636 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002637 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002638 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002639 return SSL_TLSEXT_ERR_OK;
2640}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002641#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002642#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2643
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002644#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002645
2646static DH * ssl_get_dh_1024(void)
2647{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002648 static unsigned char dh1024_p[]={
2649 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2650 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2651 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2652 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2653 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2654 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2655 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2656 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2657 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2658 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2659 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2660 };
2661 static unsigned char dh1024_g[]={
2662 0x02,
2663 };
2664
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002665 BIGNUM *p;
2666 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002667 DH *dh = DH_new();
2668 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002669 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2670 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002671
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002672 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002673 DH_free(dh);
2674 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002675 } else {
2676 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002677 }
2678 }
2679 return dh;
2680}
2681
2682static DH *ssl_get_dh_2048(void)
2683{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002684 static unsigned char dh2048_p[]={
2685 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2686 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2687 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2688 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2689 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2690 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2691 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2692 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2693 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2694 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2695 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2696 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2697 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2698 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2699 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2700 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2701 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2702 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2703 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2704 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2705 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2706 0xB7,0x1F,0x77,0xF3,
2707 };
2708 static unsigned char dh2048_g[]={
2709 0x02,
2710 };
2711
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002712 BIGNUM *p;
2713 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002714 DH *dh = DH_new();
2715 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002716 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2717 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002718
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002719 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002720 DH_free(dh);
2721 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002722 } else {
2723 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002724 }
2725 }
2726 return dh;
2727}
2728
2729static DH *ssl_get_dh_4096(void)
2730{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002731 static unsigned char dh4096_p[]={
2732 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2733 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2734 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2735 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2736 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2737 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2738 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2739 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2740 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2741 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2742 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2743 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2744 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2745 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2746 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2747 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2748 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2749 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2750 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2751 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2752 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2753 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2754 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2755 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2756 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2757 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2758 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2759 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2760 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2761 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2762 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2763 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2764 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2765 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2766 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2767 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2768 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2769 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2770 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2771 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2772 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2773 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2774 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002775 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002776 static unsigned char dh4096_g[]={
2777 0x02,
2778 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002779
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002780 BIGNUM *p;
2781 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002782 DH *dh = DH_new();
2783 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002784 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2785 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002786
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002787 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002788 DH_free(dh);
2789 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002790 } else {
2791 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002792 }
2793 }
2794 return dh;
2795}
2796
2797/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002798 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002799static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2800{
2801 DH *dh = NULL;
2802 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002803 int type;
2804
2805 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002806
2807 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2808 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2809 */
2810 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2811 keylen = EVP_PKEY_bits(pkey);
2812 }
2813
Willy Tarreauef934602016-12-22 23:12:01 +01002814 if (keylen > global_ssl.default_dh_param) {
2815 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002816 }
2817
Remi Gacogned3a341a2015-05-29 16:26:17 +02002818 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002819 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002820 }
2821 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002822 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002823 }
2824 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002825 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002826 }
2827
2828 return dh;
2829}
2830
Remi Gacogne47783ef2015-05-29 15:53:22 +02002831static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002832{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002833 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002834 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002835
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002836 if (in == NULL)
2837 goto end;
2838
Remi Gacogne47783ef2015-05-29 15:53:22 +02002839 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002840 goto end;
2841
Remi Gacogne47783ef2015-05-29 15:53:22 +02002842 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2843
2844end:
2845 if (in)
2846 BIO_free(in);
2847
Emeric Brune1b4ed42018-08-16 15:14:12 +02002848 ERR_clear_error();
2849
Remi Gacogne47783ef2015-05-29 15:53:22 +02002850 return dh;
2851}
2852
2853int ssl_sock_load_global_dh_param_from_file(const char *filename)
2854{
2855 global_dh = ssl_sock_get_dh_from_file(filename);
2856
2857 if (global_dh) {
2858 return 0;
2859 }
2860
2861 return -1;
2862}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002863#endif
2864
William Lallemand9117de92019-10-04 00:29:42 +02002865/* Alloc and init a ckch_inst */
2866static struct ckch_inst *ckch_inst_new()
2867{
2868 struct ckch_inst *ckch_inst;
2869
2870 ckch_inst = calloc(1, sizeof *ckch_inst);
2871 if (ckch_inst)
2872 LIST_INIT(&ckch_inst->sni_ctx);
2873
2874 return ckch_inst;
2875}
2876
2877
2878/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002879static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002880 struct bind_conf *s, struct ssl_bind_conf *conf,
2881 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002882{
2883 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002884 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002885
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002886 if (*name == '!') {
2887 neg = 1;
2888 name++;
2889 }
2890 if (*name == '*') {
2891 wild = 1;
2892 name++;
2893 }
2894 /* !* filter is a nop */
2895 if (neg && wild)
2896 return order;
2897 if (*name) {
2898 int j, len;
2899 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002900 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002901 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002902 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002903 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002904 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002905
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002906 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002907 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002908 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002909 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002910 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002911 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002912 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002913 sc->order = order++;
2914 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002915 sc->wild = wild;
2916 sc->name.node.leaf_p = NULL;
William Lallemand1d29c742019-10-04 00:53:29 +02002917 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002918 }
2919 return order;
2920}
2921
William Lallemand6af03992019-07-23 15:00:54 +02002922/*
William Lallemand1d29c742019-10-04 00:53:29 +02002923 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2924 * This function can't return an error.
2925 *
2926 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2927 */
2928static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2929{
2930
2931 struct sni_ctx *sc0, *sc0b, *sc1;
2932 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002933 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002934
2935 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2936
2937 /* ignore if sc0 was already inserted in a tree */
2938 if (sc0->name.node.leaf_p)
2939 continue;
2940
2941 /* Check for duplicates. */
2942 if (sc0->wild)
2943 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2944 else
2945 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2946
2947 for (; node; node = ebmb_next_dup(node)) {
2948 sc1 = ebmb_entry(node, struct sni_ctx, name);
2949 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2950 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2951 /* it's a duplicate, we should remove and free it */
2952 LIST_DEL(&sc0->by_ckch_inst);
2953 free(sc0);
2954 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002955 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002956 }
2957 }
2958
2959 /* if duplicate, ignore the insertion */
2960 if (!sc0)
2961 continue;
2962
2963 if (sc0->wild)
2964 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
2965 else
2966 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01002967
2968 /* replace the default_ctx if required with the first ctx */
2969 if (ckch_inst->is_default && !def) {
2970 /* we don't need to free the default_ctx because the refcount was not incremented */
2971 bind_conf->default_ctx = sc0->ctx;
2972 def = 1;
2973 }
William Lallemand1d29c742019-10-04 00:53:29 +02002974 }
2975}
2976
2977/*
William Lallemande3af8fb2019-10-08 11:36:53 +02002978 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02002979 */
William Lallemande3af8fb2019-10-08 11:36:53 +02002980struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02002981
William Lallemandfa892222019-07-23 16:06:08 +02002982
Emeric Brun7a883362019-10-17 13:27:40 +02002983/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
2984 * If there is no DH paramater availaible in the ckchs, the global
2985 * DH parameter is loaded into the SSL_CTX and if there is no
2986 * DH parameter available in ckchs nor in global, the default
2987 * DH parameters are applied on the SSL_CTX.
2988 * Returns a bitfield containing the flags:
2989 * ERR_FATAL in any fatal error case
2990 * ERR_ALERT if a reason of the error is availabine in err
2991 * ERR_WARN if a warning is available into err
2992 * The value 0 means there is no error nor warning and
2993 * the operation succeed.
2994 */
William Lallemandfa892222019-07-23 16:06:08 +02002995#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02002996static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
2997 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02002998{
Emeric Brun7a883362019-10-17 13:27:40 +02002999 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003000 DH *dh = NULL;
3001
William Lallemanda8c73742019-07-31 18:31:34 +02003002 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003003 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003004 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3005 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3006 err && *err ? *err : "", path);
3007#if defined(SSL_CTX_set_dh_auto)
3008 SSL_CTX_set_dh_auto(ctx, 1);
3009 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3010 err && *err ? *err : "");
3011#else
3012 memprintf(err, "%s, DH ciphers won't be available.\n",
3013 err && *err ? *err : "");
3014#endif
3015 ret |= ERR_WARN;
3016 goto end;
3017 }
William Lallemandfa892222019-07-23 16:06:08 +02003018
3019 if (ssl_dh_ptr_index >= 0) {
3020 /* store a pointer to the DH params to avoid complaining about
3021 ssl-default-dh-param not being set for this SSL_CTX */
3022 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3023 }
3024 }
3025 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003026 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3027 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3028 err && *err ? *err : "", path);
3029#if defined(SSL_CTX_set_dh_auto)
3030 SSL_CTX_set_dh_auto(ctx, 1);
3031 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3032 err && *err ? *err : "");
3033#else
3034 memprintf(err, "%s, DH ciphers won't be available.\n",
3035 err && *err ? *err : "");
3036#endif
3037 ret |= ERR_WARN;
3038 goto end;
3039 }
William Lallemandfa892222019-07-23 16:06:08 +02003040 }
3041 else {
3042 /* Clear openssl global errors stack */
3043 ERR_clear_error();
3044
3045 if (global_ssl.default_dh_param <= 1024) {
3046 /* we are limited to DH parameter of 1024 bits anyway */
3047 if (local_dh_1024 == NULL)
3048 local_dh_1024 = ssl_get_dh_1024();
3049
Emeric Brun7a883362019-10-17 13:27:40 +02003050 if (local_dh_1024 == NULL) {
3051 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3052 err && *err ? *err : "", path);
3053 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003054 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003055 }
William Lallemandfa892222019-07-23 16:06:08 +02003056
Emeric Bruna9363eb2019-10-17 14:53:03 +02003057 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3058 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3059 err && *err ? *err : "", path);
3060#if defined(SSL_CTX_set_dh_auto)
3061 SSL_CTX_set_dh_auto(ctx, 1);
3062 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3063 err && *err ? *err : "");
3064#else
3065 memprintf(err, "%s, DH ciphers won't be available.\n",
3066 err && *err ? *err : "");
3067#endif
3068 ret |= ERR_WARN;
3069 goto end;
3070 }
William Lallemandfa892222019-07-23 16:06:08 +02003071 }
3072 else {
3073 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3074 }
William Lallemandfa892222019-07-23 16:06:08 +02003075 }
3076
3077end:
William Lallemandfa892222019-07-23 16:06:08 +02003078 return ret;
3079}
3080#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003081
yanbzhu488a4d22015-12-01 15:16:07 -05003082/* Frees the contents of a cert_key_and_chain
3083 */
3084static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3085{
yanbzhu488a4d22015-12-01 15:16:07 -05003086 if (!ckch)
3087 return;
3088
3089 /* Free the certificate and set pointer to NULL */
3090 if (ckch->cert)
3091 X509_free(ckch->cert);
3092 ckch->cert = NULL;
3093
3094 /* Free the key and set pointer to NULL */
3095 if (ckch->key)
3096 EVP_PKEY_free(ckch->key);
3097 ckch->key = NULL;
3098
3099 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003100 if (ckch->chain)
3101 sk_X509_pop_free(ckch->chain, X509_free);
3102 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003103
William Lallemand455af502019-10-17 18:04:45 +02003104 if (ckch->dh)
3105 DH_free(ckch->dh);
3106 ckch->dh = NULL;
3107
3108 if (ckch->sctl) {
3109 free(ckch->sctl->area);
3110 ckch->sctl->area = NULL;
3111 free(ckch->sctl);
3112 ckch->sctl = NULL;
3113 }
3114
3115 if (ckch->ocsp_response) {
3116 free(ckch->ocsp_response->area);
3117 ckch->ocsp_response->area = NULL;
3118 free(ckch->ocsp_response);
3119 ckch->ocsp_response = NULL;
3120 }
yanbzhu488a4d22015-12-01 15:16:07 -05003121}
3122
William Lallemand8d0f8932019-10-17 18:03:58 +02003123/*
3124 *
3125 * This function copy a cert_key_and_chain in memory
3126 *
3127 * It's used to try to apply changes on a ckch before committing them, because
3128 * most of the time it's not possible to revert those changes
3129 *
3130 * Return a the dst or NULL
3131 */
3132static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3133 struct cert_key_and_chain *dst)
3134{
3135 if (src->cert) {
3136 dst->cert = src->cert;
3137 X509_up_ref(src->cert);
3138 }
3139
3140 if (src->key) {
3141 dst->key = src->key;
3142 EVP_PKEY_up_ref(src->key);
3143 }
3144
3145 if (src->chain) {
3146 dst->chain = X509_chain_up_ref(src->chain);
3147 }
3148
3149 if (src->dh) {
3150 DH_up_ref(src->dh);
3151 dst->dh = src->dh;
3152 }
3153
3154 if (src->sctl) {
3155 struct buffer *sctl;
3156
3157 sctl = calloc(1, sizeof(*sctl));
3158 if (!chunk_dup(sctl, src->sctl)) {
3159 free(sctl);
3160 sctl = NULL;
3161 goto error;
3162 }
3163 dst->sctl = sctl;
3164 }
3165
3166 if (src->ocsp_response) {
3167 struct buffer *ocsp_response;
3168
3169 ocsp_response = calloc(1, sizeof(*ocsp_response));
3170 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3171 free(ocsp_response);
3172 ocsp_response = NULL;
3173 goto error;
3174 }
3175 dst->ocsp_response = ocsp_response;
3176 }
3177
3178 if (src->ocsp_issuer) {
3179 X509_up_ref(src->ocsp_issuer);
3180 dst->ocsp_issuer = src->ocsp_issuer;
3181 }
3182
3183 return dst;
3184
3185error:
3186
3187 /* free everything */
3188 ssl_sock_free_cert_key_and_chain_contents(dst);
3189
3190 return NULL;
3191}
3192
3193
yanbzhu488a4d22015-12-01 15:16:07 -05003194/* checks if a key and cert exists in the ckch
3195 */
William Lallemand1633e392019-09-30 12:58:13 +02003196#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003197static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3198{
3199 return (ckch->cert != NULL && ckch->key != NULL);
3200}
William Lallemand1633e392019-09-30 12:58:13 +02003201#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003202
William Lallemandf9568fc2019-10-16 18:27:58 +02003203/*
3204 * return 0 on success or != 0 on failure
3205 */
3206static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3207{
3208 int ret = 1;
3209 BIO *in = NULL;
3210 X509 *issuer;
3211
3212 if (buf) {
3213 /* reading from a buffer */
3214 in = BIO_new_mem_buf(buf, -1);
3215 if (in == NULL) {
3216 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3217 goto end;
3218 }
3219
3220 } else {
3221 /* reading from a file */
3222 in = BIO_new(BIO_s_file());
3223 if (in == NULL)
3224 goto end;
3225
3226 if (BIO_read_filename(in, path) <= 0)
3227 goto end;
3228 }
3229
3230 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3231 if (!issuer) {
3232 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003233 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003234 goto end;
3235 }
3236 ret = 0;
3237 ckch->ocsp_issuer = issuer;
3238
3239end:
3240
3241 ERR_clear_error();
3242 if (in)
3243 BIO_free(in);
3244
3245 return ret;
3246}
3247
William Lallemand96a9c972019-10-17 11:56:17 +02003248
3249/*
3250 * Try to load a PEM file from a <path> or a buffer <buf>
3251 * The PEM must contain at least a Private Key and a Certificate,
3252 * It could contain a DH and a certificate chain.
yanbzhu488a4d22015-12-01 15:16:07 -05003253 *
William Lallemand96a9c972019-10-17 11:56:17 +02003254 * If it failed you should not attempt to use the ckch but free it.
3255 *
3256 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003257 */
William Lallemand96a9c972019-10-17 11:56:17 +02003258static 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 -05003259{
William Lallemandf11365b2019-09-19 14:25:58 +02003260 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003261 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003262 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003263 X509 *cert = NULL;
3264 EVP_PKEY *key = NULL;
3265 DH *dh;
3266
3267 if (buf) {
3268 /* reading from a buffer */
3269 in = BIO_new_mem_buf(buf, -1);
3270 if (in == NULL) {
3271 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3272 goto end;
3273 }
yanbzhu488a4d22015-12-01 15:16:07 -05003274
William Lallemand96a9c972019-10-17 11:56:17 +02003275 } else {
3276 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003277 in = BIO_new(BIO_s_file());
3278 if (in == NULL)
3279 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003280
William Lallemandf11365b2019-09-19 14:25:58 +02003281 if (BIO_read_filename(in, path) <= 0)
3282 goto end;
William Lallemandf11365b2019-09-19 14:25:58 +02003283 }
yanbzhu488a4d22015-12-01 15:16:07 -05003284
yanbzhu488a4d22015-12-01 15:16:07 -05003285 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003286 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3287 if (key == NULL) {
yanbzhu488a4d22015-12-01 15:16:07 -05003288 memprintf(err, "%sunable to load private key from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003289 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003290 goto end;
3291 }
3292
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003293#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003294 /* Seek back to beginning of file */
3295 if (BIO_reset(in) == -1) {
3296 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3297 err && *err ? *err : "", path);
3298 goto end;
3299 }
3300
William Lallemand96a9c972019-10-17 11:56:17 +02003301 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3302 /* no need to return an error there, dh is not mandatory */
3303
3304 if (dh) {
3305 if (ckch->dh)
3306 DH_free(ckch->dh);
3307 ckch->dh = dh;
3308 }
3309
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003310#endif
William Lallemandfa892222019-07-23 16:06:08 +02003311
Willy Tarreaubb137a82016-04-06 19:02:38 +02003312 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003313 if (BIO_reset(in) == -1) {
3314 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3315 err && *err ? *err : "", path);
3316 goto end;
3317 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003318
3319 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003320 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3321 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003322 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003323 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003324 goto end;
3325 }
3326
William Lallemand96a9c972019-10-17 11:56:17 +02003327 if (!X509_check_private_key(cert, key)) {
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003328 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003329 err && *err ? *err : "", path);
Emmanuel Hocdet03e09f32019-07-30 14:21:25 +02003330 goto end;
3331 }
3332
William Lallemand96a9c972019-10-17 11:56:17 +02003333 /* Key and Cert are good, we can use them in the ckch */
3334 if (ckch->key) /* free the previous key */
3335 EVP_PKEY_free(ckch->key);
3336 ckch->key = key;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003337 key = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003338
3339 if (ckch->cert) /* free the previous cert */
3340 X509_free(ckch->cert);
3341 ckch->cert = cert;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003342 cert = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003343
3344 /* Look for a Certificate Chain */
3345 ca = PEM_read_bio_X509(in, NULL, NULL, NULL);
3346 if (ca) {
3347 /* there is a chain a in the PEM, clean the previous one in the CKCH */
3348 if (ckch->chain) /* free the previous chain */
3349 sk_X509_pop_free(ckch->chain, X509_free);
3350 ckch->chain = sk_X509_new_null();
3351 if (!sk_X509_push(ckch->chain, ca)) {
3352 X509_free(ca);
3353 goto end;
3354 }
3355 }
3356 /* look for other crt in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003357 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL)))
3358 if (!sk_X509_push(ckch->chain, ca)) {
3359 X509_free(ca);
3360 goto end;
3361 }
yanbzhu488a4d22015-12-01 15:16:07 -05003362
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003363 /* no chain */
3364 if (ckch->chain == NULL) {
3365 ckch->chain = sk_X509_new_null();
3366 }
3367
yanbzhu488a4d22015-12-01 15:16:07 -05003368 ret = ERR_get_error();
3369 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3370 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003371 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003372 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003373 }
3374
3375 ret = 0;
3376
William Lallemand96a9c972019-10-17 11:56:17 +02003377end:
William Lallemand246c0242019-10-11 08:59:13 +02003378
3379 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003380 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003381 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003382 if (key)
3383 EVP_PKEY_free(key);
3384 if (cert)
3385 X509_free(cert);
William Lallemanda17f4112019-10-10 15:16:44 +02003386
William Lallemand96a9c972019-10-17 11:56:17 +02003387 return ret;
3388}
3389
3390/*
3391 * Try to load in a ckch every files related to a ckch.
3392 * (PEM, sctl, ocsp, issuer etc.)
3393 *
3394 * This function is only used to load files during the configuration parsing,
3395 * it is not used with the CLI.
3396 *
3397 * This allows us to carry the contents of the file without having to read the
3398 * file multiple times. The caller must call
3399 * ssl_sock_free_cert_key_and_chain_contents.
3400 *
3401 * returns:
3402 * 0 on Success
3403 * 1 on SSL Failure
3404 */
3405static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3406{
3407 int ret = 1;
3408
3409 /* try to load the PEM */
3410 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3411 goto end;
3412 }
3413
William Lallemanda17f4112019-10-10 15:16:44 +02003414#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3415 /* try to load the sctl file */
3416 {
3417 char fp[MAXPATHLEN+1];
3418 struct stat st;
3419
3420 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3421 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003422 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003423 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003424 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003425 ret = 1;
3426 goto end;
3427 }
3428 }
3429 }
3430#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003431
William Lallemand246c0242019-10-11 08:59:13 +02003432 /* try to load an ocsp response file */
3433 {
3434 char fp[MAXPATHLEN+1];
3435 struct stat st;
3436
3437 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3438 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003439 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003440 ret = 1;
3441 goto end;
3442 }
3443 }
3444 }
3445
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003446#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand246c0242019-10-11 08:59:13 +02003447 if (ckch->ocsp_response) {
3448 X509 *issuer;
3449 int i;
3450
3451 /* check if one of the certificate of the chain is the issuer */
3452 for (i = 0; i < sk_X509_num(ckch->chain); i++) {
3453 issuer = sk_X509_value(ckch->chain, i);
3454 if (X509_check_issued(issuer, ckch->cert) == X509_V_OK) {
3455 ckch->ocsp_issuer = issuer;
3456 break;
3457 } else
3458 issuer = NULL;
3459 }
3460
3461 /* if no issuer was found, try to load an issuer from the .issuer */
3462 if (!issuer) {
3463 struct stat st;
3464 char fp[MAXPATHLEN+1];
3465
3466 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3467 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003468 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003469 ret = 1;
3470 goto end;
3471 }
3472
3473 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003474 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003475 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003476 ret = 1;
3477 goto end;
3478 }
3479 } else {
3480 memprintf(err, "%sNo issuer found, cannot use the OCSP response'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003481 err && *err ? *err : "");
William Lallemand246c0242019-10-11 08:59:13 +02003482 ret = 1;
3483 goto end;
3484 }
3485 }
3486 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003487#endif
William Lallemand246c0242019-10-11 08:59:13 +02003488
yanbzhu488a4d22015-12-01 15:16:07 -05003489 ret = 0;
3490
3491end:
3492
3493 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003494
3495 /* Something went wrong in one of the reads */
3496 if (ret != 0)
3497 ssl_sock_free_cert_key_and_chain_contents(ckch);
3498
3499 return ret;
3500}
3501
3502/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003503 * Returns a bitfield containing the flags:
3504 * ERR_FATAL in any fatal error case
3505 * ERR_ALERT if the reason of the error is available in err
3506 * ERR_WARN if a warning is available into err
3507 * The value 0 means there is no error nor warning and
3508 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003509 */
3510static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3511{
Emeric Bruna96b5822019-10-17 13:25:14 +02003512 int errcode = 0;
3513
yanbzhu488a4d22015-12-01 15:16:07 -05003514 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3515 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3516 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003517 errcode |= ERR_ALERT | ERR_FATAL;
3518 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003519 }
3520
3521 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3522 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3523 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003524 errcode |= ERR_ALERT | ERR_FATAL;
3525 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003526 }
3527
yanbzhu488a4d22015-12-01 15:16:07 -05003528 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003529#ifdef SSL_CTX_set1_chain
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003530 if (!SSL_CTX_set1_chain(ctx, ckch->chain)) {
3531 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3532 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003533 errcode |= ERR_ALERT | ERR_FATAL;
3534 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003535 }
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003536#else
3537 { /* legacy compat (< openssl 1.0.2) */
3538 X509 *ca;
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003539 STACK_OF(X509) *chain;
3540 chain = X509_chain_up_ref(ckch->chain);
3541 while ((ca = sk_X509_shift(chain)))
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003542 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3543 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3544 err && *err ? *err : "", path);
3545 X509_free(ca);
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003546 sk_X509_pop_free(chain, X509_free);
Emeric Bruna96b5822019-10-17 13:25:14 +02003547 errcode |= ERR_ALERT | ERR_FATAL;
3548 goto end;
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003549 }
3550 }
3551#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003552
William Lallemandfa892222019-07-23 16:06:08 +02003553#ifndef OPENSSL_NO_DH
3554 /* store a NULL pointer to indicate we have not yet loaded
3555 a custom DH param file */
3556 if (ssl_dh_ptr_index >= 0) {
3557 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3558 }
3559
Emeric Brun7a883362019-10-17 13:27:40 +02003560 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3561 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003562 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3563 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003564 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003565 }
3566#endif
3567
William Lallemanda17f4112019-10-10 15:16:44 +02003568#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3569 if (sctl_ex_index >= 0 && ckch->sctl) {
3570 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3571 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003572 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003573 errcode |= ERR_ALERT | ERR_FATAL;
3574 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003575 }
3576 }
3577#endif
3578
William Lallemand4a660132019-10-14 14:51:41 +02003579#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003580 /* Load OCSP Info into context */
3581 if (ckch->ocsp_response) {
3582 if (ssl_sock_load_ocsp(ctx, ckch) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003583 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",
3584 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003585 errcode |= ERR_ALERT | ERR_FATAL;
3586 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003587 }
3588 }
William Lallemand246c0242019-10-11 08:59:13 +02003589#endif
3590
Emeric Bruna96b5822019-10-17 13:25:14 +02003591 end:
3592 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003593}
3594
William Lallemandc4ecddf2019-07-31 16:50:08 +02003595#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003596
William Lallemand28a8fce2019-10-04 17:36:55 +02003597static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003598{
3599 struct sni_keytype *s_kt = NULL;
3600 struct ebmb_node *node;
3601 int i;
3602
3603 for (i = 0; i < trash.size; i++) {
3604 if (!str[i])
3605 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003606 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003607 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003608 trash.area[i] = 0;
3609 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003610 if (!node) {
3611 /* CN not found in tree */
3612 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3613 /* Using memcpy here instead of strncpy.
3614 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3615 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3616 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003617 if (!s_kt)
3618 return -1;
3619
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003620 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003621 s_kt->keytypes = 0;
3622 ebst_insert(sni_keytypes, &s_kt->name);
3623 } else {
3624 /* CN found in tree */
3625 s_kt = container_of(node, struct sni_keytype, name);
3626 }
3627
3628 /* Mark that this CN has the keytype of key_index via keytypes mask */
3629 s_kt->keytypes |= 1<<key_index;
3630
William Lallemand28a8fce2019-10-04 17:36:55 +02003631 return 0;
3632
yanbzhu08ce6ab2015-12-02 13:01:29 -05003633}
3634
William Lallemandc4ecddf2019-07-31 16:50:08 +02003635#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003636/*
3637 * Free a ckch_store and its ckch(s)
3638 * The linked ckch_inst are not free'd
3639 */
3640void ckchs_free(struct ckch_store *ckchs)
3641{
3642 if (!ckchs)
3643 return;
3644
3645#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3646 if (ckchs->multi) {
3647 int n;
3648
3649 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3650 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
3651 } else
3652#endif
3653 {
3654 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
3655 ckchs->ckch = NULL;
3656 }
3657
3658 free(ckchs);
3659}
3660
3661/* allocate and duplicate a ckch_store
3662 * Return a new ckch_store or NULL */
3663static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3664{
3665 struct ckch_store *dst;
3666 int pathlen;
3667
3668 pathlen = strlen(src->path);
3669 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3670 if (!dst)
3671 return NULL;
3672 /* copy previous key */
3673 memcpy(dst->path, src->path, pathlen + 1);
3674 dst->multi = src->multi;
3675 LIST_INIT(&dst->ckch_inst);
3676
3677 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3678 if (!dst->ckch)
3679 goto error;
3680
3681#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3682 if (src->multi) {
3683 int n;
3684
3685 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3686 if (&src->ckch[n]) {
3687 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3688 goto error;
3689 }
3690 }
3691 } else
3692#endif
3693 {
3694 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3695 goto error;
3696 }
3697
3698 return dst;
3699
3700error:
3701 ckchs_free(dst);
3702
3703 return NULL;
3704}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003705
William Lallemand36b84632019-07-18 19:28:17 +02003706/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003707 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003708 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003709static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003710{
3711 struct ebmb_node *eb;
3712
William Lallemande3af8fb2019-10-08 11:36:53 +02003713 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003714 if (!eb)
3715 return NULL;
3716
William Lallemande3af8fb2019-10-08 11:36:53 +02003717 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003718}
3719
3720/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003721 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003722 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003723static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003724{
William Lallemande3af8fb2019-10-08 11:36:53 +02003725 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003726
William Lallemande3af8fb2019-10-08 11:36:53 +02003727 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3728 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003729 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3730 goto end;
3731 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003732 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003733
William Lallemande3af8fb2019-10-08 11:36:53 +02003734 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003735 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3736 goto end;
3737 }
3738
William Lallemand9117de92019-10-04 00:29:42 +02003739 LIST_INIT(&ckchs->ckch_inst);
3740
William Lallemand36b84632019-07-18 19:28:17 +02003741 if (!multi) {
3742
William Lallemand96a9c972019-10-17 11:56:17 +02003743 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003744 goto end;
3745
William Lallemande3af8fb2019-10-08 11:36:53 +02003746 /* insert into the ckchs tree */
3747 memcpy(ckchs->path, path, strlen(path) + 1);
3748 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003749 } else {
3750 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003751#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3752 char fp[MAXPATHLEN+1] = {0};
3753 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003754
3755 /* Load all possible certs and keys */
3756 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3757 struct stat buf;
3758 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3759 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003760 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003761 goto end;
3762 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003763 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003764 }
3765 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003766#endif
William Lallemand36b84632019-07-18 19:28:17 +02003767
3768 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003769 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003770 goto end;
3771 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003772 /* insert into the ckchs tree */
3773 memcpy(ckchs->path, path, strlen(path) + 1);
3774 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003775 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003776 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003777
William Lallemand36b84632019-07-18 19:28:17 +02003778end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003779 if (ckchs) {
3780 free(ckchs->ckch);
3781 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003782 }
3783
William Lallemande3af8fb2019-10-08 11:36:53 +02003784 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003785
3786 return NULL;
3787}
3788
William Lallemandc4ecddf2019-07-31 16:50:08 +02003789#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3790
William Lallemand36b84632019-07-18 19:28:17 +02003791/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003792 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003793 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003794 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3795 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003796 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003797 *
Emeric Brun054563d2019-10-17 13:16:58 +02003798 * Returns a bitfield containing the flags:
3799 * ERR_FATAL in any fatal error case
3800 * ERR_ALERT if the reason of the error is available in err
3801 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003802 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003803 */
Emeric Brun054563d2019-10-17 13:16:58 +02003804static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3805 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3806 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003807{
William Lallemand36b84632019-07-18 19:28:17 +02003808 int i = 0, n = 0;
3809 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003810 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003811 struct ebmb_node *node;
3812 struct ebmb_node *next;
3813 /* Array of SSL_CTX pointers corresponding to each possible combo
3814 * of keytypes
3815 */
3816 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003817 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003818 X509_NAME *xname = NULL;
3819 char *str = NULL;
3820#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3821 STACK_OF(GENERAL_NAME) *names = NULL;
3822#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003823 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003824
Emeric Brun054563d2019-10-17 13:16:58 +02003825 *ckchi = NULL;
3826
William Lallemande3af8fb2019-10-08 11:36:53 +02003827 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003828 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3829 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003830 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003831 }
3832
3833 ckch_inst = ckch_inst_new();
3834 if (!ckch_inst) {
3835 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3836 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003837 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003838 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003839 }
3840
William Lallemande3af8fb2019-10-08 11:36:53 +02003841 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003842
William Lallemand150bfa82019-09-19 17:12:49 +02003843 /* at least one of the instances is using filters during the config
3844 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01003845 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02003846
yanbzhu08ce6ab2015-12-02 13:01:29 -05003847 /* Process each ckch and update keytypes for each CN/SAN
3848 * for example, if CN/SAN www.a.com is associated with
3849 * certs with keytype 0 and 2, then at the end of the loop,
3850 * www.a.com will have:
3851 * keyindex = 0 | 1 | 4 = 5
3852 */
3853 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003854 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003855
3856 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3857 continue;
3858
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003859 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003860 for (i = 0; i < fcount; i++) {
3861 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3862 if (ret < 0) {
3863 memprintf(err, "%sunable to allocate SSL context.\n",
3864 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003865 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003866 goto end;
3867 }
3868 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003869 } else {
3870 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3871 * so the line that contains logic is marked via comments
3872 */
3873 xname = X509_get_subject_name(certs_and_keys[n].cert);
3874 i = -1;
3875 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3876 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003877 ASN1_STRING *value;
3878 value = X509_NAME_ENTRY_get_data(entry);
3879 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003880 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003881 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003882
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003883 OPENSSL_free(str);
3884 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003885 if (ret < 0) {
3886 memprintf(err, "%sunable to allocate SSL context.\n",
3887 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003888 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003889 goto end;
3890 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003891 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003892 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003893
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003894 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003895#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003896 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3897 if (names) {
3898 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3899 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003900
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003901 if (name->type == GEN_DNS) {
3902 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3903 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003904 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003905
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003906 OPENSSL_free(str);
3907 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003908 if (ret < 0) {
3909 memprintf(err, "%sunable to allocate SSL context.\n",
3910 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003911 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003912 goto end;
3913 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003914 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003915 }
3916 }
3917 }
3918 }
3919#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3920 }
3921
3922 /* If no files found, return error */
3923 if (eb_is_empty(&sni_keytypes_map)) {
3924 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3925 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003926 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003927 goto end;
3928 }
3929
3930 /* We now have a map of CN/SAN to keytypes that are loaded in
3931 * Iterate through the map to create the SSL_CTX's (if needed)
3932 * and add each CTX to the SNI tree
3933 *
3934 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003935 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003936 * combination is denoted by the key in the map. Each key
3937 * has a value between 1 and 2^n - 1. Conveniently, the array
3938 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3939 * entry in the array to correspond to the unique combo (key)
3940 * associated with i. This unique key combo (i) will be associated
3941 * with combos[i-1]
3942 */
3943
3944 node = ebmb_first(&sni_keytypes_map);
3945 while (node) {
3946 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003947 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003948 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003949
3950 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3951 i = container_of(node, struct sni_keytype, name)->keytypes;
3952 cur_ctx = key_combos[i-1].ctx;
3953
3954 if (cur_ctx == NULL) {
3955 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003956 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003957 if (cur_ctx == NULL) {
3958 memprintf(err, "%sunable to allocate SSL context.\n",
3959 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003960 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003961 goto end;
3962 }
3963
yanbzhube2774d2015-12-10 15:07:30 -05003964 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003965 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3966 if (i & (1<<n)) {
3967 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003968 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02003969 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3970 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003971 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003972 }
3973 }
3974
yanbzhu08ce6ab2015-12-02 13:01:29 -05003975 /* Update key_combos */
3976 key_combos[i-1].ctx = cur_ctx;
3977 }
3978
3979 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02003980
William Lallemand1d29c742019-10-04 00:53:29 +02003981 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 +02003982 kinfo, str, key_combos[i-1].order);
3983 if (key_combos[i-1].order < 0) {
3984 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003985 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02003986 goto end;
3987 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003988 node = ebmb_next(node);
3989 }
3990
3991
3992 /* Mark a default context if none exists, using the ctx that has the most shared keys */
3993 if (!bind_conf->default_ctx) {
3994 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
3995 if (key_combos[i].ctx) {
3996 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003997 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01003998 ckch_inst->is_default = 1;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003999 break;
4000 }
4001 }
4002 }
4003
William Lallemand614ca0d2019-10-07 13:52:11 +02004004 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004005 ckch_inst->ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004006end:
4007
4008 if (names)
4009 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4010
yanbzhu08ce6ab2015-12-02 13:01:29 -05004011 node = ebmb_first(&sni_keytypes_map);
4012 while (node) {
4013 next = ebmb_next(node);
4014 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004015 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004016 node = next;
4017 }
4018
Emeric Brun054563d2019-10-17 13:16:58 +02004019 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02004020 struct sni_ctx *sc0, *sc0b;
4021
4022 /* free the SSL_CTX in case of error */
4023 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4024 if (key_combos[i].ctx)
4025 SSL_CTX_free(key_combos[i].ctx);
4026 }
4027
4028 /* free the sni_ctx in case of error */
4029 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4030
4031 ebmb_delete(&sc0->name);
4032 LIST_DEL(&sc0->by_ckch_inst);
4033 free(sc0);
4034 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004035 free(ckch_inst);
4036 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004037 }
4038
Emeric Brun054563d2019-10-17 13:16:58 +02004039 *ckchi = ckch_inst;
4040 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004041}
4042#else
4043/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004044static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4045 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4046 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004047{
4048 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4049 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004050 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004051}
4052
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004053#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004054
William Lallemand614ca0d2019-10-07 13:52:11 +02004055/*
4056 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004057 *
4058 * Returns a bitfield containing the flags:
4059 * ERR_FATAL in any fatal error case
4060 * ERR_ALERT if the reason of the error is available in err
4061 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004062 */
Emeric Brun054563d2019-10-17 13:16:58 +02004063static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4064 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004065{
William Lallemandc9402072019-05-15 15:33:54 +02004066 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004067 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004068 int order = 0;
4069 X509_NAME *xname;
4070 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004071 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004072 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004073#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4074 STACK_OF(GENERAL_NAME) *names;
4075#endif
William Lallemand36b84632019-07-18 19:28:17 +02004076 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004077 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004078 int errcode = 0;
4079
4080 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004081
William Lallemande3af8fb2019-10-08 11:36:53 +02004082 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004083 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004084
William Lallemande3af8fb2019-10-08 11:36:53 +02004085 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004086
William Lallemand150bfa82019-09-19 17:12:49 +02004087 /* at least one of the instances is using filters during the config
4088 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01004089 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02004090
William Lallemandc9402072019-05-15 15:33:54 +02004091 ctx = SSL_CTX_new(SSLv23_server_method());
4092 if (!ctx) {
4093 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4094 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004095 errcode |= ERR_ALERT | ERR_FATAL;
4096 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004097 }
4098
Emeric Bruna96b5822019-10-17 13:25:14 +02004099 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4100 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004101 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004102
4103 ckch_inst = ckch_inst_new();
4104 if (!ckch_inst) {
4105 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4106 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004107 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004108 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004109 }
4110
William Lallemand36b84632019-07-18 19:28:17 +02004111 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004112 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004113 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004114 switch(EVP_PKEY_base_id(pkey)) {
4115 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004116 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004117 break;
4118 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004119 kinfo.sig = TLSEXT_signature_ecdsa;
4120 break;
4121 case EVP_PKEY_DSA:
4122 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004123 break;
4124 }
4125 EVP_PKEY_free(pkey);
4126 }
4127
Emeric Brun50bcecc2013-04-22 13:05:23 +02004128 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004129 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004130 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 +02004131 if (order < 0) {
4132 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004133 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004134 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004135 }
4136 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004137 }
4138 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004139#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004140 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004141 if (names) {
4142 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4143 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4144 if (name->type == GEN_DNS) {
4145 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004146 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004147 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004148 if (order < 0) {
4149 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004150 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004151 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004152 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004153 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004154 }
4155 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004156 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004157 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004158#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004159 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004160 i = -1;
4161 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4162 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004163 ASN1_STRING *value;
4164
4165 value = X509_NAME_ENTRY_get_data(entry);
4166 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004167 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004168 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004169 if (order < 0) {
4170 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004171 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004172 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004173 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004174 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004175 }
4176 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004177 /* we must not free the SSL_CTX anymore below, since it's already in
4178 * the tree, so it will be discovered and cleaned in time.
4179 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004180
Emeric Brunfc0421f2012-09-07 17:30:07 +02004181#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004182 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004183 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4184 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004185 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004186 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004187 }
4188#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004189 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004190 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004191 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004192 ckch_inst->is_default = 1;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004193 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004194
William Lallemand9117de92019-10-04 00:29:42 +02004195 /* everything succeed, the ckch instance can be used */
4196 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004197 ckch_inst->ssl_conf = ssl_conf;
William Lallemand9117de92019-10-04 00:29:42 +02004198
Emeric Brun054563d2019-10-17 13:16:58 +02004199 *ckchi = ckch_inst;
4200 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004201
4202error:
4203 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004204 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004205 struct sni_ctx *sc0, *sc0b;
4206
4207 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4208
4209 ebmb_delete(&sc0->name);
4210 LIST_DEL(&sc0->by_ckch_inst);
4211 free(sc0);
4212 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004213 free(ckch_inst);
4214 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004215 }
4216 /* We only created 1 SSL_CTX so we can free it there */
4217 SSL_CTX_free(ctx);
4218
Emeric Brun054563d2019-10-17 13:16:58 +02004219 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004220}
4221
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004222/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004223static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4224 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4225 char **sni_filter, int fcount, char **err)
4226{
4227 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004228 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004229
4230 /* we found the ckchs in the tree, we can use it directly */
4231 if (ckchs->multi)
Emeric Brun054563d2019-10-17 13:16:58 +02004232 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 +02004233 else
Emeric Brun054563d2019-10-17 13:16:58 +02004234 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 +02004235
Emeric Brun054563d2019-10-17 13:16:58 +02004236 if (errcode & ERR_CODE)
4237 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004238
4239 ssl_sock_load_cert_sni(ckch_inst, bind_conf);
4240
4241 /* succeed, add the instance to the ckch_store's list of instance */
4242 LIST_ADDQ(&ckchs->ckch_inst, &ckch_inst->by_ckchs);
Emeric Brun054563d2019-10-17 13:16:58 +02004243 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004244}
4245
4246
Willy Tarreaubbc91962019-10-16 16:42:19 +02004247/* Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau03209342016-12-22 17:08:28 +01004248int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004249{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004250 struct dirent **de_list;
4251 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004252 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004253 char *end;
4254 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004255 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004256 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004257#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004258 int is_bundle;
4259 int j;
4260#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004261 if ((ckchs = ckchs_lookup(path))) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004262 /* we found the ckchs in the tree, we can use it directly */
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004263 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand6af03992019-07-23 15:00:54 +02004264 }
4265
yanbzhu08ce6ab2015-12-02 13:01:29 -05004266 if (stat(path, &buf) == 0) {
William Dauchy9a8ef7f2020-01-13 17:52:49 +01004267 if (S_ISDIR(buf.st_mode) == 0) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004268 ckchs = ckchs_load_cert_file(path, 0, err);
4269 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004270 return ERR_ALERT | ERR_FATAL;
4271
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004272 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004273 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004274
yanbzhu08ce6ab2015-12-02 13:01:29 -05004275 /* strip trailing slashes, including first one */
4276 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
4277 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004278
yanbzhu08ce6ab2015-12-02 13:01:29 -05004279 n = scandir(path, &de_list, 0, alphasort);
4280 if (n < 0) {
4281 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4282 err && *err ? *err : "", path, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004283 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004284 }
4285 else {
4286 for (i = 0; i < n; i++) {
4287 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004288
yanbzhu08ce6ab2015-12-02 13:01:29 -05004289 end = strrchr(de->d_name, '.');
4290 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
4291 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004292
yanbzhu08ce6ab2015-12-02 13:01:29 -05004293 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4294 if (stat(fp, &buf) != 0) {
4295 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4296 err && *err ? *err : "", fp, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004297 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004298 goto ignore_entry;
4299 }
4300 if (!S_ISREG(buf.st_mode))
4301 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004302
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004303#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004304 is_bundle = 0;
4305 /* Check if current entry in directory is part of a multi-cert bundle */
4306
4307 if (end) {
4308 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4309 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4310 is_bundle = 1;
4311 break;
4312 }
4313 }
4314
4315 if (is_bundle) {
yanbzhu63ea8462015-12-09 13:35:14 -05004316 int dp_len;
4317
4318 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004319
4320 /* increment i and free de until we get to a non-bundle cert
4321 * Note here that we look at de_list[i + 1] before freeing de
Willy Tarreau05800522019-10-29 10:48:50 +01004322 * this is important since ignore_entry will free de. This also
4323 * guarantees that de->d_name continues to hold the same prefix.
yanbzhu63ea8462015-12-09 13:35:14 -05004324 */
Willy Tarreau05800522019-10-29 10:48:50 +01004325 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
yanbzhu63ea8462015-12-09 13:35:14 -05004326 free(de);
4327 i++;
4328 de = de_list[i];
4329 }
4330
Willy Tarreau05800522019-10-29 10:48:50 +01004331 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemande3af8fb2019-10-08 11:36:53 +02004332 if ((ckchs = ckchs_lookup(fp)) == NULL)
4333 ckchs = ckchs_load_cert_file(fp, 1, err);
4334 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004335 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004336 else
4337 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05004338 /* Successfully processed the bundle */
4339 goto ignore_entry;
4340 }
4341 }
4342
4343#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004344 if ((ckchs = ckchs_lookup(fp)) == NULL)
4345 ckchs = ckchs_load_cert_file(fp, 0, err);
4346 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004347 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004348 else
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004349 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004350
yanbzhu08ce6ab2015-12-02 13:01:29 -05004351ignore_entry:
4352 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004353 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004354 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004355 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004356 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004357 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004358
William Lallemande3af8fb2019-10-08 11:36:53 +02004359 ckchs = ckchs_load_cert_file(path, 1, err);
4360 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004361 return ERR_ALERT | ERR_FATAL;
4362
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004363 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004364
Emeric Brunfc0421f2012-09-07 17:30:07 +02004365 return cfgerr;
4366}
4367
Thierry Fournier383085f2013-01-24 14:15:43 +01004368/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4369 * done once. Zero is returned if the operation fails. No error is returned
4370 * if the random is said as not implemented, because we expect that openssl
4371 * will use another method once needed.
4372 */
4373static int ssl_initialize_random()
4374{
4375 unsigned char random;
4376 static int random_initialized = 0;
4377
4378 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4379 random_initialized = 1;
4380
4381 return random_initialized;
4382}
4383
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004384/* release ssl bind conf */
4385void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004386{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004387 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01004388#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004389 free(conf->npn_str);
4390 conf->npn_str = NULL;
4391#endif
4392#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4393 free(conf->alpn_str);
4394 conf->alpn_str = NULL;
4395#endif
4396 free(conf->ca_file);
4397 conf->ca_file = NULL;
4398 free(conf->crl_file);
4399 conf->crl_file = NULL;
4400 free(conf->ciphers);
4401 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004402#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004403 free(conf->ciphersuites);
4404 conf->ciphersuites = NULL;
4405#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004406 free(conf->curves);
4407 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004408 free(conf->ecdhe);
4409 conf->ecdhe = NULL;
4410 }
4411}
4412
Willy Tarreaubbc91962019-10-16 16:42:19 +02004413/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004414int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
4415{
4416 char thisline[CRT_LINESIZE];
4417 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004418 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004419 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004420 int linenum = 0;
4421 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004422 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004423
Willy Tarreauad1731d2013-04-02 17:35:58 +02004424 if ((f = fopen(file, "r")) == NULL) {
4425 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004426 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004427 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004428
4429 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004430 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004431 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004432 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004433 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004434 char *crt_path;
4435 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004436
4437 linenum++;
4438 end = line + strlen(line);
4439 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4440 /* Check if we reached the limit and the last char is not \n.
4441 * Watch out for the last line without the terminating '\n'!
4442 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004443 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4444 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004445 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004446 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004447 }
4448
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004449 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004450 newarg = 1;
4451 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004452 if (*line == '#' || *line == '\n' || *line == '\r') {
4453 /* end of string, end of loop */
4454 *line = 0;
4455 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004456 } else if (isspace(*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02004457 newarg = 1;
4458 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004459 } else if (*line == '[') {
4460 if (ssl_b) {
4461 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004462 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004463 break;
4464 }
4465 if (!arg) {
4466 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004467 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004468 break;
4469 }
4470 ssl_b = arg;
4471 newarg = 1;
4472 *line = 0;
4473 } else if (*line == ']') {
4474 if (ssl_e) {
4475 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004476 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004477 break;
4478 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004479 if (!ssl_b) {
4480 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004481 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004482 break;
4483 }
4484 ssl_e = arg;
4485 newarg = 1;
4486 *line = 0;
4487 } else if (newarg) {
4488 if (arg == MAX_CRT_ARGS) {
4489 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004490 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004491 break;
4492 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004493 newarg = 0;
4494 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004495 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004496 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004497 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004498 if (cfgerr)
4499 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004500 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004501
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004502 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004503 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004504 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004505
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004506 crt_path = args[0];
4507 if (*crt_path != '/' && global_ssl.crt_base) {
4508 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4509 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4510 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004511 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004512 break;
4513 }
4514 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4515 crt_path = path;
4516 }
4517
4518 ssl_conf = calloc(1, sizeof *ssl_conf);
4519 cur_arg = ssl_b ? ssl_b : 1;
4520 while (cur_arg < ssl_e) {
4521 newarg = 0;
4522 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4523 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4524 newarg = 1;
Willy Tarreaubbc91962019-10-16 16:42:19 +02004525 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004526 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4527 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4528 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004529 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004530 }
4531 cur_arg += 1 + ssl_bind_kws[i].skip;
4532 break;
4533 }
4534 }
4535 if (!cfgerr && !newarg) {
4536 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4537 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004538 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004539 break;
4540 }
4541 }
Willy Tarreaubbc91962019-10-16 16:42:19 +02004542
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004543 if (cfgerr) {
4544 ssl_sock_free_ssl_conf(ssl_conf);
4545 free(ssl_conf);
4546 ssl_conf = NULL;
4547 break;
4548 }
4549
William Lallemande3af8fb2019-10-08 11:36:53 +02004550 if ((ckchs = ckchs_lookup(crt_path)) == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004551 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004552 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004553 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004554 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004555 }
4556
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004557 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004558 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004559 else
4560 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 +02004561
Willy Tarreauad1731d2013-04-02 17:35:58 +02004562 if (cfgerr) {
4563 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004564 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004565 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004566 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004567 fclose(f);
4568 return cfgerr;
4569}
4570
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004571/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004572static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004573ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004574{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004575 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004576 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004577 SSL_OP_ALL | /* all known workarounds for bugs */
4578 SSL_OP_NO_SSLv2 |
4579 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004580 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02004581 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004582 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02004583 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004584 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004585 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004586 SSL_MODE_ENABLE_PARTIAL_WRITE |
4587 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004588 SSL_MODE_RELEASE_BUFFERS |
4589 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004590 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004591 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004592 int flags = MC_SSL_O_ALL;
4593 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004594
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004595 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004596 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004597
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004598 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004599 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
4600 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4601 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004602 else
4603 flags = conf_ssl_methods->flags;
4604
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004605 min = conf_ssl_methods->min;
4606 max = conf_ssl_methods->max;
4607 /* start with TLSv10 to remove SSLv3 per default */
4608 if (!min && (!max || max >= CONF_TLSV10))
4609 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004610 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004611 if (min)
4612 flags |= (methodVersions[min].flag - 1);
4613 if (max)
4614 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004615 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004616 min = max = CONF_TLSV_NONE;
4617 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004618 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004619 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004620 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004621 if (min) {
4622 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004623 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
4624 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4625 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
4626 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004627 hole = 0;
4628 }
4629 max = i;
4630 }
4631 else {
4632 min = max = i;
4633 }
4634 }
4635 else {
4636 if (min)
4637 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004638 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004639 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004640 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4641 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004642 cfgerr += 1;
4643 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004644 /* save real min/max in bind_conf */
4645 conf_ssl_methods->min = min;
4646 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004647
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004648#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004649 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004650 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004651 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004652 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004653 else
4654 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4655 if (flags & methodVersions[i].flag)
4656 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004657#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004658 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004659 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4660 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004661#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004662
4663 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4664 options |= SSL_OP_NO_TICKET;
4665 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4666 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004667
4668#ifdef SSL_OP_NO_RENEGOTIATION
4669 options |= SSL_OP_NO_RENEGOTIATION;
4670#endif
4671
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004672 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004673
Willy Tarreau5db847a2019-05-09 14:13:35 +02004674#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004675 if (global_ssl.async)
4676 mode |= SSL_MODE_ASYNC;
4677#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004678 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004679 if (global_ssl.life_time)
4680 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004681
4682#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4683#ifdef OPENSSL_IS_BORINGSSL
4684 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4685 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Willy Tarreau5db847a2019-05-09 14:13:35 +02004686#elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard545989f2019-12-17 15:39:54 +01004687 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004688 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004689 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4690 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004691#else
4692 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004693#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004694 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004695#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004696 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004697}
4698
William Lallemand4f45bb92017-10-30 20:08:51 +01004699
4700static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4701{
4702 if (first == block) {
4703 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4704 if (first->len > 0)
4705 sh_ssl_sess_tree_delete(sh_ssl_sess);
4706 }
4707}
4708
4709/* return first block from sh_ssl_sess */
4710static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4711{
4712 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4713
4714}
4715
4716/* store a session into the cache
4717 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4718 * data: asn1 encoded session
4719 * data_len: asn1 encoded session length
4720 * Returns 1 id session was stored (else 0)
4721 */
4722static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4723{
4724 struct shared_block *first;
4725 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4726
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004727 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004728 if (!first) {
4729 /* Could not retrieve enough free blocks to store that session */
4730 return 0;
4731 }
4732
4733 /* STORE the key in the first elem */
4734 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4735 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4736 first->len = sizeof(struct sh_ssl_sess_hdr);
4737
4738 /* it returns the already existing node
4739 or current node if none, never returns null */
4740 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4741 if (oldsh_ssl_sess != sh_ssl_sess) {
4742 /* NOTE: Row couldn't be in use because we lock read & write function */
4743 /* release the reserved row */
4744 shctx_row_dec_hot(ssl_shctx, first);
4745 /* replace the previous session already in the tree */
4746 sh_ssl_sess = oldsh_ssl_sess;
4747 /* ignore the previous session data, only use the header */
4748 first = sh_ssl_sess_first_block(sh_ssl_sess);
4749 shctx_row_inc_hot(ssl_shctx, first);
4750 first->len = sizeof(struct sh_ssl_sess_hdr);
4751 }
4752
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004753 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004754 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004755 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004756 }
4757
4758 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004759
4760 return 1;
4761}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004762
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004763/* SSL callback used when a new session is created while connecting to a server */
4764static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4765{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004766 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004767 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004768
Willy Tarreau07d94e42018-09-20 10:57:52 +02004769 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004770
Olivier Houcharde6060c52017-11-16 17:42:52 +01004771 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4772 int len;
4773 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004774
Olivier Houcharde6060c52017-11-16 17:42:52 +01004775 len = i2d_SSL_SESSION(sess, NULL);
4776 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4777 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4778 } else {
4779 free(s->ssl_ctx.reused_sess[tid].ptr);
4780 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4781 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4782 }
4783 if (s->ssl_ctx.reused_sess[tid].ptr) {
4784 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4785 &ptr);
4786 }
4787 } else {
4788 free(s->ssl_ctx.reused_sess[tid].ptr);
4789 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4790 }
4791
4792 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004793}
4794
Olivier Houcharde6060c52017-11-16 17:42:52 +01004795
William Lallemanded0b5ad2017-10-30 19:36:36 +01004796/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004797int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004798{
4799 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4800 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4801 unsigned char *p;
4802 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02004803 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004804 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004805
4806 /* Session id is already stored in to key and session id is known
4807 * so we dont store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02004808 * note: SSL_SESSION_set1_id is using
4809 * a memcpy so we need to use a different pointer
4810 * than sid_data or sid_ctx_data to avoid valgrind
4811 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004812 */
4813
4814 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02004815
4816 /* copy value in an other buffer */
4817 memcpy(encid, sid_data, sid_length);
4818
4819 /* pad with 0 */
4820 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4821 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4822
4823 /* force length to zero to avoid ASN1 encoding */
4824 SSL_SESSION_set1_id(sess, encid, 0);
4825
4826 /* force length to zero to avoid ASN1 encoding */
4827 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004828
4829 /* check if buffer is large enough for the ASN1 encoded session */
4830 data_len = i2d_SSL_SESSION(sess, NULL);
4831 if (data_len > SHSESS_MAX_DATA_LEN)
4832 goto err;
4833
4834 p = encsess;
4835
4836 /* process ASN1 session encoding before the lock */
4837 i2d_SSL_SESSION(sess, &p);
4838
William Lallemanded0b5ad2017-10-30 19:36:36 +01004839
William Lallemanda3c77cf2017-10-30 23:44:40 +01004840 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004841 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004842 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004843 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004844err:
4845 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02004846 SSL_SESSION_set1_id(sess, encid, sid_length);
4847 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004848
4849 return 0; /* do not increment session reference count */
4850}
4851
4852/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004853SSL_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 +01004854{
William Lallemand4f45bb92017-10-30 20:08:51 +01004855 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004856 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4857 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004858 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004859 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004860
4861 global.shctx_lookups++;
4862
4863 /* allow the session to be freed automatically by openssl */
4864 *do_copy = 0;
4865
4866 /* tree key is zeros padded sessionid */
4867 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4868 memcpy(tmpkey, key, key_len);
4869 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4870 key = tmpkey;
4871 }
4872
4873 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004874 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004875
4876 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004877 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4878 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004879 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004880 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004881 global.shctx_misses++;
4882 return NULL;
4883 }
4884
William Lallemand4f45bb92017-10-30 20:08:51 +01004885 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4886 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004887
William Lallemand4f45bb92017-10-30 20:08:51 +01004888 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 +01004889
William Lallemanda3c77cf2017-10-30 23:44:40 +01004890 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004891
4892 /* decode ASN1 session */
4893 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004894 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004895 /* Reset session id and session id contenxt */
4896 if (sess) {
4897 SSL_SESSION_set1_id(sess, key, key_len);
4898 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4899 }
4900
4901 return sess;
4902}
4903
William Lallemand4f45bb92017-10-30 20:08:51 +01004904
William Lallemanded0b5ad2017-10-30 19:36:36 +01004905/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004906void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004907{
William Lallemand4f45bb92017-10-30 20:08:51 +01004908 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004909 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4910 unsigned int sid_length;
4911 const unsigned char *sid_data;
4912 (void)ctx;
4913
4914 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4915 /* tree key is zeros padded sessionid */
4916 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4917 memcpy(tmpkey, sid_data, sid_length);
4918 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4919 sid_data = tmpkey;
4920 }
4921
William Lallemanda3c77cf2017-10-30 23:44:40 +01004922 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004923
4924 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004925 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4926 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004927 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004928 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004929 }
4930
4931 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004932 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004933}
4934
4935/* Set session cache mode to server and disable openssl internal cache.
4936 * Set shared cache callbacks on an ssl context.
4937 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004938void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004939{
4940 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4941
4942 if (!ssl_shctx) {
4943 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4944 return;
4945 }
4946
4947 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4948 SSL_SESS_CACHE_NO_INTERNAL |
4949 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4950
4951 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004952 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4953 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4954 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004955}
4956
William Lallemand8b453912019-11-21 15:48:10 +01004957/*
4958 * This function applies the SSL configuration on a SSL_CTX
4959 * It returns an error code and fills the <err> buffer
4960 */
4961int 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 +01004962{
4963 struct proxy *curproxy = bind_conf->frontend;
4964 int cfgerr = 0;
4965 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004966 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004967 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004968#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004969 const char *conf_ciphersuites;
4970#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004971 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004972
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004973 if (ssl_conf) {
4974 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4975 int i, min, max;
4976 int flags = MC_SSL_O_ALL;
4977
4978 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004979 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4980 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004981 if (min)
4982 flags |= (methodVersions[min].flag - 1);
4983 if (max)
4984 flags |= ~((methodVersions[max].flag << 1) - 1);
4985 min = max = CONF_TLSV_NONE;
4986 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4987 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4988 if (min)
4989 max = i;
4990 else
4991 min = max = i;
4992 }
4993 /* save real min/max */
4994 conf_ssl_methods->min = min;
4995 conf_ssl_methods->max = max;
4996 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004997 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4998 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004999 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005000 }
5001 }
5002
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005003 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005004 case SSL_SOCK_VERIFY_NONE:
5005 verify = SSL_VERIFY_NONE;
5006 break;
5007 case SSL_SOCK_VERIFY_OPTIONAL:
5008 verify = SSL_VERIFY_PEER;
5009 break;
5010 case SSL_SOCK_VERIFY_REQUIRED:
5011 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5012 break;
5013 }
5014 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5015 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005016 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
5017 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
5018 if (ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005019 /* set CAfile to verify */
5020 if (!ssl_set_verify_locations_file(ctx, ca_file)) {
5021 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005022 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005023 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005024 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005025 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
5026 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005027 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 +02005028 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005029 }
Emeric Brun850efd52014-01-29 12:24:34 +01005030 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005031 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5032 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005033 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005034 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005035#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005036 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005037 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5038
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005039 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005040 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5041 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005042 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005043 }
Emeric Brun561e5742012-10-02 15:20:55 +02005044 else {
5045 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5046 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005047 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005048#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005049 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005050 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005051#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005052 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005053 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005054 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5055 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005056 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005057 }
5058 }
5059#endif
5060
William Lallemand4f45bb92017-10-30 20:08:51 +01005061 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005062 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5063 if (conf_ciphers &&
5064 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005065 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5066 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005067 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005068 }
5069
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005070#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005071 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5072 if (conf_ciphersuites &&
5073 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005074 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5075 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005076 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005077 }
5078#endif
5079
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005080#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005081 /* If tune.ssl.default-dh-param has not been set,
5082 neither has ssl-default-dh-file and no static DH
5083 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005084 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005085 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005086 (ssl_dh_ptr_index == -1 ||
5087 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005088 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5089 const SSL_CIPHER * cipher = NULL;
5090 char cipher_description[128];
5091 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5092 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5093 which is not ephemeral DH. */
5094 const char dhe_description[] = " Kx=DH ";
5095 const char dhe_export_description[] = " Kx=DH(";
5096 int idx = 0;
5097 int dhe_found = 0;
5098 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005099
Remi Gacogne23d5d372014-10-10 17:04:26 +02005100 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005101
Remi Gacogne23d5d372014-10-10 17:04:26 +02005102 if (ssl) {
5103 ciphers = SSL_get_ciphers(ssl);
5104
5105 if (ciphers) {
5106 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5107 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5108 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5109 if (strstr(cipher_description, dhe_description) != NULL ||
5110 strstr(cipher_description, dhe_export_description) != NULL) {
5111 dhe_found = 1;
5112 break;
5113 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005114 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005115 }
5116 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005117 SSL_free(ssl);
5118 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005119 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005120
Lukas Tribus90132722014-08-18 00:56:33 +02005121 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005122 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",
5123 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005124 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005125 }
5126
Willy Tarreauef934602016-12-22 23:12:01 +01005127 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005128 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005129
Willy Tarreauef934602016-12-22 23:12:01 +01005130 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005131 if (local_dh_1024 == NULL) {
5132 local_dh_1024 = ssl_get_dh_1024();
5133 }
Willy Tarreauef934602016-12-22 23:12:01 +01005134 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005135 if (local_dh_2048 == NULL) {
5136 local_dh_2048 = ssl_get_dh_2048();
5137 }
Willy Tarreauef934602016-12-22 23:12:01 +01005138 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005139 if (local_dh_4096 == NULL) {
5140 local_dh_4096 = ssl_get_dh_4096();
5141 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005142 }
5143 }
5144 }
5145#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005146
Emeric Brunfc0421f2012-09-07 17:30:07 +02005147 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005148#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005149 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005150#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005151
Bernard Spil13c53f82018-02-15 13:34:58 +01005152#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005153 ssl_conf_cur = NULL;
5154 if (ssl_conf && ssl_conf->npn_str)
5155 ssl_conf_cur = ssl_conf;
5156 else if (bind_conf->ssl_conf.npn_str)
5157 ssl_conf_cur = &bind_conf->ssl_conf;
5158 if (ssl_conf_cur)
5159 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005160#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005161#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005162 ssl_conf_cur = NULL;
5163 if (ssl_conf && ssl_conf->alpn_str)
5164 ssl_conf_cur = ssl_conf;
5165 else if (bind_conf->ssl_conf.alpn_str)
5166 ssl_conf_cur = &bind_conf->ssl_conf;
5167 if (ssl_conf_cur)
5168 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005169#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005170#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005171 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5172 if (conf_curves) {
5173 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005174 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5175 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005176 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005177 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005178 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005179 }
5180#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005181#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005182 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005183 int i;
5184 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005185#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005186 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005187 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5188 NULL);
5189
5190 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005191 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005192 return cfgerr;
5193 }
5194#else
5195 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5196 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5197 ECDHE_DEFAULT_CURVE);
5198#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005199
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005200 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005201 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005202 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5203 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005204 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005205 }
5206 else {
5207 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5208 EC_KEY_free(ecdh);
5209 }
5210 }
5211#endif
5212
Emeric Brunfc0421f2012-09-07 17:30:07 +02005213 return cfgerr;
5214}
5215
Evan Broderbe554312013-06-27 00:05:25 -07005216static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5217{
5218 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5219 size_t prefixlen, suffixlen;
5220
5221 /* Trivial case */
5222 if (strcmp(pattern, hostname) == 0)
5223 return 1;
5224
Evan Broderbe554312013-06-27 00:05:25 -07005225 /* The rest of this logic is based on RFC 6125, section 6.4.3
5226 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5227
Emeric Bruna848dae2013-10-08 11:27:28 +02005228 pattern_wildcard = NULL;
5229 pattern_left_label_end = pattern;
5230 while (*pattern_left_label_end != '.') {
5231 switch (*pattern_left_label_end) {
5232 case 0:
5233 /* End of label not found */
5234 return 0;
5235 case '*':
5236 /* If there is more than one wildcards */
5237 if (pattern_wildcard)
5238 return 0;
5239 pattern_wildcard = pattern_left_label_end;
5240 break;
5241 }
5242 pattern_left_label_end++;
5243 }
5244
5245 /* If it's not trivial and there is no wildcard, it can't
5246 * match */
5247 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005248 return 0;
5249
5250 /* Make sure all labels match except the leftmost */
5251 hostname_left_label_end = strchr(hostname, '.');
5252 if (!hostname_left_label_end
5253 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5254 return 0;
5255
5256 /* Make sure the leftmost label of the hostname is long enough
5257 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005258 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005259 return 0;
5260
5261 /* Finally compare the string on either side of the
5262 * wildcard */
5263 prefixlen = pattern_wildcard - pattern;
5264 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005265 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5266 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005267 return 0;
5268
5269 return 1;
5270}
5271
5272static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5273{
5274 SSL *ssl;
5275 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005276 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005277 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005278 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005279
5280 int depth;
5281 X509 *cert;
5282 STACK_OF(GENERAL_NAME) *alt_names;
5283 int i;
5284 X509_NAME *cert_subject;
5285 char *str;
5286
5287 if (ok == 0)
5288 return ok;
5289
5290 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005291 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005292 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005293
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005294 /* We're checking if the provided hostnames match the desired one. The
5295 * desired hostname comes from the SNI we presented if any, or if not
5296 * provided then it may have been explicitly stated using a "verifyhost"
5297 * directive. If neither is set, we don't care about the name so the
5298 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005299 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005300 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005301 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005302 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005303 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005304 if (!servername)
5305 return ok;
5306 }
Evan Broderbe554312013-06-27 00:05:25 -07005307
5308 /* We only need to verify the CN on the actual server cert,
5309 * not the indirect CAs */
5310 depth = X509_STORE_CTX_get_error_depth(ctx);
5311 if (depth != 0)
5312 return ok;
5313
5314 /* At this point, the cert is *not* OK unless we can find a
5315 * hostname match */
5316 ok = 0;
5317
5318 cert = X509_STORE_CTX_get_current_cert(ctx);
5319 /* It seems like this might happen if verify peer isn't set */
5320 if (!cert)
5321 return ok;
5322
5323 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5324 if (alt_names) {
5325 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5326 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5327 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005328#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005329 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5330#else
Evan Broderbe554312013-06-27 00:05:25 -07005331 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005332#endif
Evan Broderbe554312013-06-27 00:05:25 -07005333 ok = ssl_sock_srv_hostcheck(str, servername);
5334 OPENSSL_free(str);
5335 }
5336 }
5337 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005338 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005339 }
5340
5341 cert_subject = X509_get_subject_name(cert);
5342 i = -1;
5343 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5344 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005345 ASN1_STRING *value;
5346 value = X509_NAME_ENTRY_get_data(entry);
5347 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005348 ok = ssl_sock_srv_hostcheck(str, servername);
5349 OPENSSL_free(str);
5350 }
5351 }
5352
Willy Tarreau71d058c2017-07-26 20:09:56 +02005353 /* report the mismatch and indicate if SNI was used or not */
5354 if (!ok && !conn->err_code)
5355 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005356 return ok;
5357}
5358
Emeric Brun94324a42012-10-11 14:00:19 +02005359/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005360int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005361{
Willy Tarreau03209342016-12-22 17:08:28 +01005362 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005363 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005364 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005365 SSL_OP_ALL | /* all known workarounds for bugs */
5366 SSL_OP_NO_SSLv2 |
5367 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005368 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005369 SSL_MODE_ENABLE_PARTIAL_WRITE |
5370 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005371 SSL_MODE_RELEASE_BUFFERS |
5372 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005373 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005374 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005375 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005376 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005377 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005378
Thierry Fournier383085f2013-01-24 14:15:43 +01005379 /* Make sure openssl opens /dev/urandom before the chroot */
5380 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005381 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005382 cfgerr++;
5383 }
5384
Willy Tarreaufce03112015-01-15 21:32:40 +01005385 /* Automatic memory computations need to know we use SSL there */
5386 global.ssl_used_backend = 1;
5387
5388 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005389 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005390 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005391 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5392 curproxy->id, srv->id,
5393 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005394 cfgerr++;
5395 return cfgerr;
5396 }
5397 }
Emeric Brun94324a42012-10-11 14:00:19 +02005398 if (srv->use_ssl)
5399 srv->xprt = &ssl_sock;
5400 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005401 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005402
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005403 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005404 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005405 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5406 proxy_type_str(curproxy), curproxy->id,
5407 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005408 cfgerr++;
5409 return cfgerr;
5410 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005411
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005412 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005413 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5414 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5415 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005416 else
5417 flags = conf_ssl_methods->flags;
5418
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005419 /* Real min and max should be determinate with configuration and openssl's capabilities */
5420 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005421 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005422 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005423 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005424
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005425 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005426 min = max = CONF_TLSV_NONE;
5427 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005428 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005429 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005430 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005431 if (min) {
5432 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005433 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5434 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5435 proxy_type_str(curproxy), curproxy->id, srv->id,
5436 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005437 hole = 0;
5438 }
5439 max = i;
5440 }
5441 else {
5442 min = max = i;
5443 }
5444 }
5445 else {
5446 if (min)
5447 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005448 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005449 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005450 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5451 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005452 cfgerr += 1;
5453 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005454
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005455#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005456 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005457 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005458 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005459 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005460 else
5461 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5462 if (flags & methodVersions[i].flag)
5463 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005464#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005465 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005466 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5467 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005468#endif
5469
5470 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5471 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005472 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005473
Willy Tarreau5db847a2019-05-09 14:13:35 +02005474#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005475 if (global_ssl.async)
5476 mode |= SSL_MODE_ASYNC;
5477#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005478 SSL_CTX_set_mode(ctx, mode);
5479 srv->ssl_ctx.ctx = ctx;
5480
Emeric Bruna7aa3092012-10-26 12:58:00 +02005481 if (srv->ssl_ctx.client_crt) {
5482 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 +01005483 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5484 proxy_type_str(curproxy), curproxy->id,
5485 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005486 cfgerr++;
5487 }
5488 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 +01005489 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5490 proxy_type_str(curproxy), curproxy->id,
5491 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005492 cfgerr++;
5493 }
5494 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005495 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5496 proxy_type_str(curproxy), curproxy->id,
5497 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005498 cfgerr++;
5499 }
5500 }
Emeric Brun94324a42012-10-11 14:00:19 +02005501
Emeric Brun850efd52014-01-29 12:24:34 +01005502 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5503 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005504 switch (srv->ssl_ctx.verify) {
5505 case SSL_SOCK_VERIFY_NONE:
5506 verify = SSL_VERIFY_NONE;
5507 break;
5508 case SSL_SOCK_VERIFY_REQUIRED:
5509 verify = SSL_VERIFY_PEER;
5510 break;
5511 }
Evan Broderbe554312013-06-27 00:05:25 -07005512 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005513 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005514 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005515 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005516 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005517 /* set CAfile to verify */
5518 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
5519 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01005520 curproxy->id, srv->id,
5521 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005522 cfgerr++;
5523 }
5524 }
Emeric Brun850efd52014-01-29 12:24:34 +01005525 else {
5526 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01005527 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",
5528 curproxy->id, srv->id,
5529 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005530 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01005531 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
5532 curproxy->id, srv->id,
5533 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005534 cfgerr++;
5535 }
Emeric Brunef42d922012-10-11 16:11:36 +02005536#ifdef X509_V_FLAG_CRL_CHECK
5537 if (srv->ssl_ctx.crl_file) {
5538 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
5539
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005540 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005541 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
5542 curproxy->id, srv->id,
5543 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005544 cfgerr++;
5545 }
5546 else {
5547 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5548 }
5549 }
5550#endif
5551 }
5552
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005553 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
5554 SSL_SESS_CACHE_NO_INTERNAL_STORE);
5555 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02005556 if (srv->ssl_ctx.ciphers &&
5557 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005558 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
5559 curproxy->id, srv->id,
5560 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02005561 cfgerr++;
5562 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005563
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005564#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005565 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00005566 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005567 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
5568 curproxy->id, srv->id,
5569 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
5570 cfgerr++;
5571 }
5572#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01005573#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
5574 if (srv->ssl_ctx.npn_str)
5575 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
5576#endif
5577#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5578 if (srv->ssl_ctx.alpn_str)
5579 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
5580#endif
5581
Emeric Brun94324a42012-10-11 14:00:19 +02005582
5583 return cfgerr;
5584}
5585
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005586/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005587 * be NULL, in which case nothing is done. Returns the number of errors
5588 * encountered.
5589 */
Willy Tarreau03209342016-12-22 17:08:28 +01005590int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005591{
5592 struct ebmb_node *node;
5593 struct sni_ctx *sni;
5594 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01005595 int errcode = 0;
5596 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005597
Willy Tarreaufce03112015-01-15 21:32:40 +01005598 /* Automatic memory computations need to know we use SSL there */
5599 global.ssl_used_frontend = 1;
5600
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005601 /* Make sure openssl opens /dev/urandom before the chroot */
5602 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005603 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005604 err++;
5605 }
5606 /* Create initial_ctx used to start the ssl connection before do switchctx */
5607 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005608 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005609 /* It should not be necessary to call this function, but it's
5610 necessary first to check and move all initialisation related
5611 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01005612 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005613 }
Emeric Brun0bed9942014-10-30 19:25:24 +01005614 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01005615 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01005616
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005617 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005618 while (node) {
5619 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01005620 if (!sni->order && sni->ctx != bind_conf->default_ctx)
5621 /* only initialize the CTX on its first occurrence and
5622 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005623 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005624 node = ebmb_next(node);
5625 }
5626
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005627 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005628 while (node) {
5629 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01005630 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01005631 /* only initialize the CTX on its first occurrence and
5632 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005633 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
5634 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005635 node = ebmb_next(node);
5636 }
William Lallemand8b453912019-11-21 15:48:10 +01005637
5638 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005639 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005640 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005641 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005642 err++;
5643 }
5644
5645 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005646 return err;
5647}
5648
Willy Tarreau55d37912016-12-21 23:38:39 +01005649/* Prepares all the contexts for a bind_conf and allocates the shared SSL
5650 * context if needed. Returns < 0 on error, 0 on success. The warnings and
5651 * alerts are directly emitted since the rest of the stack does it below.
5652 */
5653int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
5654{
5655 struct proxy *px = bind_conf->frontend;
5656 int alloc_ctx;
5657 int err;
5658
5659 if (!bind_conf->is_ssl) {
5660 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005661 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
5662 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01005663 }
5664 return 0;
5665 }
5666 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005667 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005668 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
5669 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005670 }
5671 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005672 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
5673 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005674 return -1;
5675 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005676 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005677 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005678 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005679 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01005680 sizeof(*sh_ssl_sess_tree),
5681 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005682 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005683 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5684 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");
5685 else
5686 ha_alert("Unable to allocate SSL session cache.\n");
5687 return -1;
5688 }
5689 /* free block callback */
5690 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5691 /* init the root tree within the extra space */
5692 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5693 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005694 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005695 err = 0;
5696 /* initialize all certificate contexts */
5697 err += ssl_sock_prepare_all_ctx(bind_conf);
5698
5699 /* initialize CA variables if the certificates generation is enabled */
5700 err += ssl_sock_load_ca(bind_conf);
5701
5702 return -err;
5703}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005704
5705/* release ssl context allocated for servers. */
5706void ssl_sock_free_srv_ctx(struct server *srv)
5707{
Olivier Houchardc7566002018-11-20 23:33:50 +01005708#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5709 if (srv->ssl_ctx.alpn_str)
5710 free(srv->ssl_ctx.alpn_str);
5711#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005712#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01005713 if (srv->ssl_ctx.npn_str)
5714 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005715#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005716 if (srv->ssl_ctx.ctx)
5717 SSL_CTX_free(srv->ssl_ctx.ctx);
5718}
5719
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005720/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005721 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5722 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005723void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005724{
5725 struct ebmb_node *node, *back;
5726 struct sni_ctx *sni;
5727
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005728 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005729 while (node) {
5730 sni = ebmb_entry(node, struct sni_ctx, name);
5731 back = ebmb_next(node);
5732 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005733 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005734 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005735 ssl_sock_free_ssl_conf(sni->conf);
5736 free(sni->conf);
5737 sni->conf = NULL;
5738 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005739 free(sni);
5740 node = back;
5741 }
5742
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005743 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005744 while (node) {
5745 sni = ebmb_entry(node, struct sni_ctx, name);
5746 back = ebmb_next(node);
5747 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005748 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005749 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005750 ssl_sock_free_ssl_conf(sni->conf);
5751 free(sni->conf);
5752 sni->conf = NULL;
5753 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005754 free(sni);
5755 node = back;
5756 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005757 SSL_CTX_free(bind_conf->initial_ctx);
5758 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005759 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005760 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005761}
5762
Willy Tarreau795cdab2016-12-22 17:30:54 +01005763/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5764void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5765{
5766 ssl_sock_free_ca(bind_conf);
5767 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005768 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005769 free(bind_conf->ca_sign_file);
5770 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005771 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005772 free(bind_conf->keys_ref->filename);
5773 free(bind_conf->keys_ref->tlskeys);
5774 LIST_DEL(&bind_conf->keys_ref->list);
5775 free(bind_conf->keys_ref);
5776 }
5777 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005778 bind_conf->ca_sign_pass = NULL;
5779 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005780}
5781
Christopher Faulet31af49d2015-06-09 17:29:50 +02005782/* Load CA cert file and private key used to generate certificates */
5783int
Willy Tarreau03209342016-12-22 17:08:28 +01005784ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005785{
Willy Tarreau03209342016-12-22 17:08:28 +01005786 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005787 FILE *fp;
5788 X509 *cacert = NULL;
5789 EVP_PKEY *capkey = NULL;
5790 int err = 0;
5791
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005792 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005793 return err;
5794
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005795#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005796 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005797 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005798 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005799 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005800 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005801#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005802
Christopher Faulet31af49d2015-06-09 17:29:50 +02005803 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005804 ha_alert("Proxy '%s': cannot enable certificate generation, "
5805 "no CA certificate File configured at [%s:%d].\n",
5806 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005807 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005808 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005809
5810 /* read in the CA certificate */
5811 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005812 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5813 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005814 goto load_error;
5815 }
5816 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005817 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5818 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005819 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005820 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005821 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005822 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005823 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5824 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005825 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005826 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005827
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005828 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005829 bind_conf->ca_sign_cert = cacert;
5830 bind_conf->ca_sign_pkey = capkey;
5831 return err;
5832
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005833 read_error:
5834 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005835 if (capkey) EVP_PKEY_free(capkey);
5836 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005837 load_error:
5838 bind_conf->generate_certs = 0;
5839 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005840 return err;
5841}
5842
5843/* Release CA cert and private key used to generate certificated */
5844void
5845ssl_sock_free_ca(struct bind_conf *bind_conf)
5846{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005847 if (bind_conf->ca_sign_pkey)
5848 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5849 if (bind_conf->ca_sign_cert)
5850 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005851 bind_conf->ca_sign_pkey = NULL;
5852 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005853}
5854
Emeric Brun46591952012-05-18 15:47:34 +02005855/*
5856 * This function is called if SSL * context is not yet allocated. The function
5857 * is designed to be called before any other data-layer operation and sets the
5858 * handshake flag on the connection. It is safe to call it multiple times.
5859 * It returns 0 on success and -1 in error case.
5860 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005861static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005862{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005863 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005864 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005865 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005866 return 0;
5867
Willy Tarreau3c728722014-01-23 13:50:42 +01005868 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005869 return 0;
5870
Olivier Houchard66ab4982019-02-26 18:37:15 +01005871 ctx = pool_alloc(ssl_sock_ctx_pool);
5872 if (!ctx) {
5873 conn->err_code = CO_ER_SSL_NO_MEM;
5874 return -1;
5875 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005876 ctx->wait_event.tasklet = tasklet_new();
5877 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005878 conn->err_code = CO_ER_SSL_NO_MEM;
5879 pool_free(ssl_sock_ctx_pool, ctx);
5880 return -1;
5881 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005882 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5883 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005884 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005885 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01005886 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005887 ctx->conn = conn;
Olivier Houchard81284e62019-06-06 13:21:23 +02005888 ctx->send_wait = NULL;
5889 ctx->recv_wait = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02005890 ctx->xprt_st = 0;
5891 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005892
5893 /* Only work with sockets for now, this should be adapted when we'll
5894 * add QUIC support.
5895 */
5896 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005897 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005898 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5899 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005900 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005901
Willy Tarreau20879a02012-12-03 16:32:10 +01005902 if (global.maxsslconn && sslconns >= global.maxsslconn) {
5903 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005904 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005905 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005906
Emeric Brun46591952012-05-18 15:47:34 +02005907 /* If it is in client mode initiate SSL session
5908 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005909 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005910 int may_retry = 1;
5911
5912 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02005913 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005914 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
5915 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005916 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005917 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005918 goto retry_connect;
5919 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005920 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005921 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005922 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005923 ctx->bio = BIO_new(ha_meth);
5924 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005925 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005926 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005927 goto retry_connect;
5928 }
Emeric Brun55476152014-11-12 17:35:37 +01005929 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005930 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005931 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005932 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005933 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005934
Evan Broderbe554312013-06-27 00:05:25 -07005935 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005936 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5937 SSL_free(ctx->ssl);
5938 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01005939 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005940 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005941 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005942 goto retry_connect;
5943 }
Emeric Brun55476152014-11-12 17:35:37 +01005944 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005945 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005946 }
5947
Olivier Houchard66ab4982019-02-26 18:37:15 +01005948 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005949 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5950 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5951 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 +01005952 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005953 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005954 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5955 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005956 } else if (sess) {
5957 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005958 }
5959 }
Evan Broderbe554312013-06-27 00:05:25 -07005960
Emeric Brun46591952012-05-18 15:47:34 +02005961 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005962 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005963
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005964 _HA_ATOMIC_ADD(&sslconns, 1);
5965 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005966 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005967 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005968 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005969 if (conn->flags & CO_FL_ERROR)
5970 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02005971 return 0;
5972 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005973 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005974 int may_retry = 1;
5975
5976 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005977 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005978 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5979 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005980 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005981 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005982 goto retry_accept;
5983 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005984 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005985 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005986 }
Olivier Houchard545989f2019-12-17 15:39:54 +01005987#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard54907bb2019-12-19 15:02:39 +01005988 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
5989 b_alloc(&ctx->early_buf);
5990 SSL_set_max_early_data(ctx->ssl,
5991 /* Only allow early data if we managed to allocate
5992 * a buffer.
5993 */
5994 (!b_is_null(&ctx->early_buf)) ?
5995 global.tune.bufsize - global.tune.maxrewrite : 0);
5996 }
Olivier Houchard545989f2019-12-17 15:39:54 +01005997#endif
Emeric Brun46591952012-05-18 15:47:34 +02005998
Olivier Houcharda8955d52019-04-07 22:00:38 +02005999 ctx->bio = BIO_new(ha_meth);
6000 if (!ctx->bio) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006001 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006002 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006003 goto retry_accept;
6004 }
Emeric Brun55476152014-11-12 17:35:37 +01006005 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006006 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006007 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006008 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006009 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006010
Emeric Brune1f38db2012-09-03 20:36:47 +02006011 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006012 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6013 SSL_free(ctx->ssl);
6014 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006015 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006016 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006017 goto retry_accept;
6018 }
Emeric Brun55476152014-11-12 17:35:37 +01006019 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006020 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006021 }
6022
Olivier Houchard66ab4982019-02-26 18:37:15 +01006023 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006024
Emeric Brun46591952012-05-18 15:47:34 +02006025 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006026 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006027#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006028 conn->flags |= CO_FL_EARLY_SSL_HS;
6029#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006030
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006031 _HA_ATOMIC_ADD(&sslconns, 1);
6032 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006033 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006034 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006035 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006036 if (conn->flags & CO_FL_ERROR)
6037 goto err;
Emeric Brun46591952012-05-18 15:47:34 +02006038 return 0;
6039 }
6040 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006041 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006042err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006043 if (ctx && ctx->wait_event.tasklet)
6044 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006045 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006046 return -1;
6047}
6048
6049
6050/* This is the callback which is used when an SSL handshake is pending. It
6051 * updates the FD status if it wants some polling before being called again.
6052 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6053 * otherwise it returns non-zero and removes itself from the connection's
6054 * flags (the bit is provided in <flag> by the caller).
6055 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006056static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006057{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006058 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006059 int ret;
6060
Willy Tarreau3c728722014-01-23 13:50:42 +01006061 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006062 return 0;
6063
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006064 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006065 goto out_error;
6066
Willy Tarreau5db847a2019-05-09 14:13:35 +02006067#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006068 /*
6069 * Check if we have early data. If we do, we have to read them
6070 * before SSL_do_handshake() is called, And there's no way to
6071 * detect early data, except to try to read them
6072 */
6073 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006074 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006075
Olivier Houchard54907bb2019-12-19 15:02:39 +01006076 while (1) {
6077 ret = SSL_read_early_data(ctx->ssl,
6078 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6079 &read_data);
6080 if (ret == SSL_READ_EARLY_DATA_ERROR)
6081 goto check_error;
6082 if (read_data > 0) {
6083 conn->flags |= CO_FL_EARLY_DATA;
6084 b_add(&ctx->early_buf, read_data);
6085 }
6086 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6087 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6088 if (!b_data(&ctx->early_buf))
6089 b_free(&ctx->early_buf);
6090 break;
6091 }
6092 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006093 }
6094#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006095 /* If we use SSL_do_handshake to process a reneg initiated by
6096 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6097 * Usually SSL_write and SSL_read are used and process implicitly
6098 * the reneg handshake.
6099 * Here we use SSL_peek as a workaround for reneg.
6100 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006101 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006102 char c;
6103
Olivier Houchard66ab4982019-02-26 18:37:15 +01006104 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006105 if (ret <= 0) {
6106 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006107 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006108
Emeric Brun674b7432012-11-08 19:21:55 +01006109 if (ret == SSL_ERROR_WANT_WRITE) {
6110 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006111 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006112 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006113 return 0;
6114 }
6115 else if (ret == SSL_ERROR_WANT_READ) {
6116 /* handshake may have been completed but we have
6117 * no more data to read.
6118 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006119 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006120 ret = 1;
6121 goto reneg_ok;
6122 }
6123 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006124 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006125 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006126 return 0;
6127 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006128#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006129 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006130 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006131 return 0;
6132 }
6133#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006134 else if (ret == SSL_ERROR_SYSCALL) {
6135 /* if errno is null, then connection was successfully established */
6136 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6137 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006138 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006139#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6140 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006141 conn->err_code = CO_ER_SSL_HANDSHAKE;
6142#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006143 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006144#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006145 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006146 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006147 empty_handshake = state == TLS_ST_BEFORE;
6148#else
Lukas Tribus49799162019-07-08 14:29:15 +02006149 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6150 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006151#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006152 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006153 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006154 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006155 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6156 else
6157 conn->err_code = CO_ER_SSL_EMPTY;
6158 }
6159 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006160 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006161 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6162 else
6163 conn->err_code = CO_ER_SSL_ABORT;
6164 }
6165 }
6166 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006167 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006168 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006169 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006170 conn->err_code = CO_ER_SSL_HANDSHAKE;
6171 }
Lukas Tribus49799162019-07-08 14:29:15 +02006172#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006173 }
Emeric Brun674b7432012-11-08 19:21:55 +01006174 goto out_error;
6175 }
6176 else {
6177 /* Fail on all other handshake errors */
6178 /* Note: OpenSSL may leave unread bytes in the socket's
6179 * buffer, causing an RST to be emitted upon close() on
6180 * TCP sockets. We first try to drain possibly pending
6181 * data to avoid this as much as possible.
6182 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006183 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006184 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006185 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006186 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006187 goto out_error;
6188 }
6189 }
6190 /* read some data: consider handshake completed */
6191 goto reneg_ok;
6192 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006193 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006194check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006195 if (ret != 1) {
6196 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006197 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006198
6199 if (ret == SSL_ERROR_WANT_WRITE) {
6200 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006201 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006202 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006203 return 0;
6204 }
6205 else if (ret == SSL_ERROR_WANT_READ) {
6206 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006207 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006208 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6209 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006210 return 0;
6211 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006212#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006213 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006214 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006215 return 0;
6216 }
6217#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006218 else if (ret == SSL_ERROR_SYSCALL) {
6219 /* if errno is null, then connection was successfully established */
6220 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6221 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006222 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006223#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6224 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006225 conn->err_code = CO_ER_SSL_HANDSHAKE;
6226#else
6227 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006228#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006229 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006230 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006231 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006232#else
Lukas Tribus49799162019-07-08 14:29:15 +02006233 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6234 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006235#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006236 if (empty_handshake) {
6237 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006238 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006239 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6240 else
6241 conn->err_code = CO_ER_SSL_EMPTY;
6242 }
6243 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006244 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006245 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6246 else
6247 conn->err_code = CO_ER_SSL_ABORT;
6248 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006249 }
6250 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006251 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006252 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6253 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006254 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006255 }
Lukas Tribus49799162019-07-08 14:29:15 +02006256#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006257 }
Willy Tarreau89230192012-09-28 20:22:13 +02006258 goto out_error;
6259 }
Emeric Brun46591952012-05-18 15:47:34 +02006260 else {
6261 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006262 /* Note: OpenSSL may leave unread bytes in the socket's
6263 * buffer, causing an RST to be emitted upon close() on
6264 * TCP sockets. We first try to drain possibly pending
6265 * data to avoid this as much as possible.
6266 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006267 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006268 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006269 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006270 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006271 goto out_error;
6272 }
6273 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006274#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006275 else {
6276 /*
6277 * If the server refused the early data, we have to send a
6278 * 425 to the client, as we no longer have the data to sent
6279 * them again.
6280 */
6281 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006282 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006283 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6284 goto out_error;
6285 }
6286 }
6287 }
6288#endif
6289
Emeric Brun46591952012-05-18 15:47:34 +02006290
Emeric Brun674b7432012-11-08 19:21:55 +01006291reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006292
Willy Tarreau5db847a2019-05-09 14:13:35 +02006293#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006294 /* ASYNC engine API doesn't support moving read/write
6295 * buffers. So we disable ASYNC mode right after
6296 * the handshake to avoid buffer oveflows.
6297 */
6298 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006299 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006300#endif
Emeric Brun46591952012-05-18 15:47:34 +02006301 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006302 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006303 if (objt_server(conn->target)) {
6304 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6305 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6306 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006307 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006308 else {
6309 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6310 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6311 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6312 }
Emeric Brun46591952012-05-18 15:47:34 +02006313 }
6314
6315 /* The connection is now established at both layers, it's time to leave */
6316 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6317 return 1;
6318
6319 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006320 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006321 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006322 ERR_clear_error();
6323
Emeric Brun9fa89732012-10-04 17:09:56 +02006324 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006325 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6326 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6327 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006328 }
6329
Emeric Brun46591952012-05-18 15:47:34 +02006330 /* Fail on all other handshake errors */
6331 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006332 if (!conn->err_code)
6333 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006334 return 0;
6335}
6336
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006337static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01006338{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006339 struct wait_event *sw;
6340 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006341
Olivier Houchard0ff28652019-06-24 18:57:39 +02006342 if (!ctx)
6343 return -1;
6344
Olivier Houchardea8dd942019-05-20 14:02:16 +02006345 if (event_type & SUB_RETRY_RECV) {
6346 sw = param;
6347 BUG_ON(ctx->recv_wait != NULL || (sw->events & SUB_RETRY_RECV));
6348 sw->events |= SUB_RETRY_RECV;
6349 ctx->recv_wait = sw;
6350 if (!(conn->flags & CO_FL_SSL_WAIT_HS) &&
6351 !(ctx->wait_event.events & SUB_RETRY_RECV))
6352 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
6353 event_type &= ~SUB_RETRY_RECV;
6354 }
6355 if (event_type & SUB_RETRY_SEND) {
6356sw = param;
6357 BUG_ON(ctx->send_wait != NULL || (sw->events & SUB_RETRY_SEND));
6358 sw->events |= SUB_RETRY_SEND;
6359 ctx->send_wait = sw;
6360 if (!(conn->flags & CO_FL_SSL_WAIT_HS) &&
6361 !(ctx->wait_event.events & SUB_RETRY_SEND))
6362 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
6363 event_type &= ~SUB_RETRY_SEND;
6364
6365 }
6366 if (event_type != 0)
6367 return -1;
6368 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006369}
6370
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006371static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param)
Olivier Houcharddf357842019-03-21 16:30:07 +01006372{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006373 struct wait_event *sw;
6374 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006375
Olivier Houchardea8dd942019-05-20 14:02:16 +02006376 if (event_type & SUB_RETRY_RECV) {
6377 sw = param;
6378 BUG_ON(ctx->recv_wait != sw);
6379 ctx->recv_wait = NULL;
6380 sw->events &= ~SUB_RETRY_RECV;
6381 /* If we subscribed, and we're not doing the handshake,
6382 * then we subscribed because the upper layer asked for it,
6383 * as the upper layer is no longer interested, we can
6384 * unsubscribe too.
6385 */
6386 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) &&
6387 (ctx->wait_event.events & SUB_RETRY_RECV))
6388 conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV,
6389 &ctx->wait_event);
6390 }
6391 if (event_type & SUB_RETRY_SEND) {
6392 sw = param;
6393 BUG_ON(ctx->send_wait != sw);
6394 ctx->send_wait = NULL;
6395 sw->events &= ~SUB_RETRY_SEND;
6396 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) &&
6397 (ctx->wait_event.events & SUB_RETRY_SEND))
6398 conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND,
6399 &ctx->wait_event);
6400
6401 }
6402
6403 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006404}
6405
Olivier Houchard2e055482019-05-27 19:50:12 +02006406/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6407 * Returns 0 on success, and non-zero on failure.
6408 */
6409static 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)
6410{
6411 struct ssl_sock_ctx *ctx = xprt_ctx;
6412
6413 if (oldxprt_ops != NULL)
6414 *oldxprt_ops = ctx->xprt;
6415 if (oldxprt_ctx != NULL)
6416 *oldxprt_ctx = ctx->xprt_ctx;
6417 ctx->xprt = toadd_ops;
6418 ctx->xprt_ctx = toadd_ctx;
6419 return 0;
6420}
6421
Olivier Houchard5149b592019-05-23 17:47:36 +02006422/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6423 * return 0, otherwise just call the remove_xprt method from the underlying
6424 * XPRT.
6425 */
6426static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6427{
6428 struct ssl_sock_ctx *ctx = xprt_ctx;
6429
6430 if (ctx->xprt_ctx == toremove_ctx) {
6431 ctx->xprt_ctx = newctx;
6432 ctx->xprt = newops;
6433 return 0;
6434 }
6435 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6436}
6437
Olivier Houchardea8dd942019-05-20 14:02:16 +02006438static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6439{
6440 struct ssl_sock_ctx *ctx = context;
6441
6442 /* First if we're doing an handshake, try that */
6443 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6444 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6445 /* If we had an error, or the handshake is done and I/O is available,
6446 * let the upper layer know.
6447 * If no mux was set up yet, and nobody subscribed, then call
6448 * xprt_done_cb() ourself if it's set, or destroy the connection,
6449 * we can't be sure conn_fd_handler() will be called again.
6450 */
6451 if ((ctx->conn->flags & CO_FL_ERROR) ||
6452 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6453 int ret = 0;
6454 int woke = 0;
6455
6456 /* On error, wake any waiter */
6457 if (ctx->recv_wait) {
6458 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006459 tasklet_wakeup(ctx->recv_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006460 ctx->recv_wait = NULL;
6461 woke = 1;
6462 }
6463 if (ctx->send_wait) {
6464 ctx->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006465 tasklet_wakeup(ctx->send_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006466 ctx->send_wait = NULL;
6467 woke = 1;
6468 }
6469 /* If we're the first xprt for the connection, let the
6470 * upper layers know. If xprt_done_cb() is set, call it,
6471 * otherwise, we should have a mux, so call its wake
6472 * method if we didn't woke a tasklet already.
6473 */
6474 if (ctx->conn->xprt_ctx == ctx) {
6475 if (ctx->conn->xprt_done_cb)
6476 ret = ctx->conn->xprt_done_cb(ctx->conn);
6477 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6478 ctx->conn->mux->wake(ctx->conn);
6479 return NULL;
6480 }
6481 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006482#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6483 /* If we have early data and somebody wants to receive, let them */
6484 else if (b_data(&ctx->early_buf) && ctx->recv_wait) {
6485 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
6486 tasklet_wakeup(ctx->recv_wait->tasklet);
6487 ctx->recv_wait = NULL;
6488
6489 }
6490#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006491 return NULL;
6492}
6493
Emeric Brun46591952012-05-18 15:47:34 +02006494/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006495 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006496 * buffer wraps, in which case a second call may be performed. The connection's
6497 * flags are updated with whatever special event is detected (error, read0,
6498 * empty). The caller is responsible for taking care of those events and
6499 * avoiding the call if inappropriate. The function does not call the
6500 * connection's polling update function, so the caller is responsible for this.
6501 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006502static 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 +02006503{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006504 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006505 ssize_t ret;
6506 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006507
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006508 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006509 goto out_error;
6510
Olivier Houchard54907bb2019-12-19 15:02:39 +01006511#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6512 if (b_data(&ctx->early_buf)) {
6513 try = b_contig_space(buf);
6514 if (try > b_data(&ctx->early_buf))
6515 try = b_data(&ctx->early_buf);
6516 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6517 b_add(buf, try);
6518 b_del(&ctx->early_buf, try);
6519 if (b_data(&ctx->early_buf) == 0)
6520 b_free(&ctx->early_buf);
6521 return try;
6522 }
6523#endif
6524
Emeric Brun46591952012-05-18 15:47:34 +02006525 if (conn->flags & CO_FL_HANDSHAKE)
6526 /* a handshake was requested */
6527 return 0;
6528
Emeric Brun46591952012-05-18 15:47:34 +02006529 /* read the largest possible block. For this, we perform only one call
6530 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6531 * in which case we accept to do it once again. A new attempt is made on
6532 * EINTR too.
6533 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006534 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006535
Willy Tarreau591d4452018-06-15 17:21:00 +02006536 try = b_contig_space(buf);
6537 if (!try)
6538 break;
6539
Willy Tarreauabf08d92014-01-14 11:31:27 +01006540 if (try > count)
6541 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006542
Olivier Houchard66ab4982019-02-26 18:37:15 +01006543 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006544
Emeric Brune1f38db2012-09-03 20:36:47 +02006545 if (conn->flags & CO_FL_ERROR) {
6546 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006547 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006548 }
Emeric Brun46591952012-05-18 15:47:34 +02006549 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006550 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006551 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006552 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006553 }
Emeric Brun46591952012-05-18 15:47:34 +02006554 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006555 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006556 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006557 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02006558 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006559 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006560#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006561 /* Async mode can be re-enabled, because we're leaving data state.*/
6562 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006563 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006564#endif
Emeric Brun46591952012-05-18 15:47:34 +02006565 break;
6566 }
6567 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006568 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006569 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6570 SUB_RETRY_RECV,
6571 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01006572 /* handshake is running, and it may need to re-enable read */
6573 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006574#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006575 /* Async mode can be re-enabled, because we're leaving data state.*/
6576 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006577 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006578#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006579 break;
6580 }
Emeric Brun46591952012-05-18 15:47:34 +02006581 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006582 } else if (ret == SSL_ERROR_ZERO_RETURN)
6583 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006584 /* For SSL_ERROR_SYSCALL, make sure to clear the error
6585 * stack before shutting down the connection for
6586 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006587 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
6588 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02006589 /* otherwise it's a real error */
6590 goto out_error;
6591 }
6592 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006593 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006594 return done;
6595
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006596 clear_ssl_error:
6597 /* Clear openssl global errors stack */
6598 ssl_sock_dump_errors(conn);
6599 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02006600 read0:
6601 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006602 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006603
Emeric Brun46591952012-05-18 15:47:34 +02006604 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006605 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01006606 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006607 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006608 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006609 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006610}
6611
6612
Willy Tarreau787db9a2018-06-14 18:31:46 +02006613/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
6614 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
6615 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02006616 * Only one call to send() is performed, unless the buffer wraps, in which case
6617 * a second call may be performed. The connection's flags are updated with
6618 * whatever special event is detected (error, empty). The caller is responsible
6619 * for taking care of those events and avoiding the call if inappropriate. The
6620 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02006621 * is responsible for this. The buffer's output is not adjusted, it's up to the
6622 * caller to take care of this. It's up to the caller to update the buffer's
6623 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02006624 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006625static 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 +02006626{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006627 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006628 ssize_t ret;
6629 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02006630
6631 done = 0;
6632
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006633 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006634 goto out_error;
6635
Olivier Houchard010941f2019-05-03 20:56:19 +02006636 if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006637 /* a handshake was requested */
6638 return 0;
6639
6640 /* send the largest possible block. For this we perform only one call
6641 * to send() unless the buffer wraps and we exactly fill the first hunk,
6642 * in which case we accept to do it once again.
6643 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02006644 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02006645#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006646 size_t written_data;
6647#endif
6648
Willy Tarreau787db9a2018-06-14 18:31:46 +02006649 try = b_contig_data(buf, done);
6650 if (try > count)
6651 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01006652
Willy Tarreau7bed9452014-02-02 02:00:24 +01006653 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006654 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01006655 global_ssl.max_record && try > global_ssl.max_record) {
6656 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006657 }
6658 else {
6659 /* we need to keep the information about the fact that
6660 * we're not limiting the upcoming send(), because if it
6661 * fails, we'll have to retry with at least as many data.
6662 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006663 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006664 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01006665
Willy Tarreau5db847a2019-05-09 14:13:35 +02006666#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02006667 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006668 unsigned int max_early;
6669
Olivier Houchard522eea72017-11-03 16:27:47 +01006670 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006671 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006672 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006673 if (SSL_get0_session(ctx->ssl))
6674 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006675 else
6676 max_early = 0;
6677 }
6678
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006679 if (try + ctx->sent_early_data > max_early) {
6680 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006681 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006682 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006683 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006684 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006685 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006686 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006687 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006688 if (ret == 1) {
6689 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006690 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006691 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006692 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006693 /* Initiate the handshake, now */
6694 tasklet_wakeup(ctx->wait_event.tasklet);
6695 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006696
Olivier Houchardc2aae742017-09-22 18:26:28 +02006697 }
6698
6699 } else
6700#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006701 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006702
Emeric Brune1f38db2012-09-03 20:36:47 +02006703 if (conn->flags & CO_FL_ERROR) {
6704 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006705 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006706 }
Emeric Brun46591952012-05-18 15:47:34 +02006707 if (ret > 0) {
Olivier Houchardf24502b2019-01-17 19:09:11 +01006708 /* A send succeeded, so we can consier ourself connected */
6709 conn->flags |= CO_FL_CONNECTED;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006710 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006711 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006712 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006713 }
6714 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006715 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006716
Emeric Brun46591952012-05-18 15:47:34 +02006717 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006718 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006719 /* handshake is running, and it may need to re-enable write */
6720 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006721 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006722#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006723 /* Async mode can be re-enabled, because we're leaving data state.*/
6724 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006725 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006726#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006727 break;
6728 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006729
Emeric Brun46591952012-05-18 15:47:34 +02006730 break;
6731 }
6732 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006733 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006734 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006735 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6736 SUB_RETRY_RECV,
6737 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006738#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006739 /* Async mode can be re-enabled, because we're leaving data state.*/
6740 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006741 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006742#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006743 break;
6744 }
Emeric Brun46591952012-05-18 15:47:34 +02006745 goto out_error;
6746 }
6747 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006748 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006749 return done;
6750
6751 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006752 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006753 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006754 ERR_clear_error();
6755
Emeric Brun46591952012-05-18 15:47:34 +02006756 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006757 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006758}
6759
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006760static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006761
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006762 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006763
Olivier Houchardea8dd942019-05-20 14:02:16 +02006764
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006765 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006766 if (ctx->wait_event.events != 0)
6767 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6768 ctx->wait_event.events,
6769 &ctx->wait_event);
6770 if (ctx->send_wait) {
6771 ctx->send_wait->events &= ~SUB_RETRY_SEND;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006772 tasklet_wakeup(ctx->send_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006773 }
6774 if (ctx->recv_wait) {
6775 ctx->recv_wait->events &= ~SUB_RETRY_RECV;
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006776 tasklet_wakeup(ctx->recv_wait->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006777 }
Olivier Houchard692c1d02019-05-23 18:41:47 +02006778 if (ctx->xprt->close)
6779 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006780#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006781 if (global_ssl.async) {
6782 OSSL_ASYNC_FD all_fd[32], afd;
6783 size_t num_all_fds = 0;
6784 int i;
6785
Olivier Houchard66ab4982019-02-26 18:37:15 +01006786 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006787 if (num_all_fds > 32) {
6788 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6789 return;
6790 }
6791
Olivier Houchard66ab4982019-02-26 18:37:15 +01006792 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006793
6794 /* If an async job is pending, we must try to
6795 to catch the end using polling before calling
6796 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006797 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006798 for (i=0 ; i < num_all_fds ; i++) {
6799 /* switch on an handler designed to
6800 * handle the SSL_free
6801 */
6802 afd = all_fd[i];
6803 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006804 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006805 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006806 /* To ensure that the fd cache won't be used
6807 * and we'll catch a real RD event.
6808 */
6809 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006810 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006811 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006812 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006813 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006814 return;
6815 }
Emeric Brun3854e012017-05-17 20:42:48 +02006816 /* Else we can remove the fds from the fdtab
6817 * and call SSL_free.
6818 * note: we do a fd_remove and not a delete
6819 * because the fd is owned by the engine.
6820 * the engine is responsible to close
6821 */
6822 for (i=0 ; i < num_all_fds ; i++)
6823 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006824 }
6825#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006826 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01006827 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006828 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006829 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006830 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006831 }
Emeric Brun46591952012-05-18 15:47:34 +02006832}
6833
6834/* This function tries to perform a clean shutdown on an SSL connection, and in
6835 * any case, flags the connection as reusable if no handshake was in progress.
6836 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006837static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006838{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006839 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006840
Emeric Brun46591952012-05-18 15:47:34 +02006841 if (conn->flags & CO_FL_HANDSHAKE)
6842 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006843 if (!clean)
6844 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006845 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006846 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006847 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006848 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006849 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006850 ERR_clear_error();
6851 }
Emeric Brun46591952012-05-18 15:47:34 +02006852}
6853
William Lallemandd4f946c2019-12-05 10:26:40 +01006854/* fill a buffer with the algorithm and size of a public key */
6855static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006856{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006857 int bits = 0;
6858 int sig = TLSEXT_signature_anonymous;
6859 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006860 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006861
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006862 pkey = X509_get_pubkey(crt);
6863 if (pkey) {
6864 bits = EVP_PKEY_bits(pkey);
6865 switch(EVP_PKEY_base_id(pkey)) {
6866 case EVP_PKEY_RSA:
6867 sig = TLSEXT_signature_rsa;
6868 break;
6869 case EVP_PKEY_EC:
6870 sig = TLSEXT_signature_ecdsa;
6871 break;
6872 case EVP_PKEY_DSA:
6873 sig = TLSEXT_signature_dsa;
6874 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006875 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006876 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006877 }
6878
6879 switch(sig) {
6880 case TLSEXT_signature_rsa:
6881 len = chunk_printf(out, "RSA%d", bits);
6882 break;
6883 case TLSEXT_signature_ecdsa:
6884 len = chunk_printf(out, "EC%d", bits);
6885 break;
6886 case TLSEXT_signature_dsa:
6887 len = chunk_printf(out, "DSA%d", bits);
6888 break;
6889 default:
6890 return 0;
6891 }
6892 if (len < 0)
6893 return 0;
6894 return 1;
6895}
6896
William Lallemandd4f946c2019-12-05 10:26:40 +01006897/* used for ppv2 pkey alog (can be used for logging) */
6898int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
6899{
6900 struct ssl_sock_ctx *ctx;
6901 X509 *crt;
6902
6903 if (!ssl_sock_is_ssl(conn))
6904 return 0;
6905
6906 ctx = conn->xprt_ctx;
6907
6908 crt = SSL_get_certificate(ctx->ssl);
6909 if (!crt)
6910 return 0;
6911
6912 return cert_get_pkey_algo(crt, out);
6913}
6914
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006915/* used for ppv2 cert signature (can be used for logging) */
6916const char *ssl_sock_get_cert_sig(struct connection *conn)
6917{
Christopher Faulet82004142019-09-10 10:12:03 +02006918 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006919
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006920 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6921 X509 *crt;
6922
6923 if (!ssl_sock_is_ssl(conn))
6924 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006925 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006926 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006927 if (!crt)
6928 return NULL;
6929 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6930 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6931}
6932
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006933/* used for ppv2 authority */
6934const char *ssl_sock_get_sni(struct connection *conn)
6935{
6936#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02006937 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006938
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006939 if (!ssl_sock_is_ssl(conn))
6940 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006941 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006942 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006943#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006944 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006945#endif
6946}
6947
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006948/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006949const char *ssl_sock_get_cipher_name(struct connection *conn)
6950{
Christopher Faulet82004142019-09-10 10:12:03 +02006951 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006952
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006953 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006954 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006955 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006956 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006957}
6958
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006959/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006960const char *ssl_sock_get_proto_version(struct connection *conn)
6961{
Christopher Faulet82004142019-09-10 10:12:03 +02006962 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006963
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006964 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006965 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02006966 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006967 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006968}
6969
Willy Tarreau8d598402012-10-22 17:58:39 +02006970/* Extract a serial from a cert, and copy it to a chunk.
6971 * Returns 1 if serial is found and copied, 0 if no serial found and
6972 * -1 if output is not large enough.
6973 */
6974static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006975ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006976{
6977 ASN1_INTEGER *serial;
6978
6979 serial = X509_get_serialNumber(crt);
6980 if (!serial)
6981 return 0;
6982
6983 if (out->size < serial->length)
6984 return -1;
6985
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006986 memcpy(out->area, serial->data, serial->length);
6987 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006988 return 1;
6989}
6990
Emeric Brun43e79582014-10-29 19:03:26 +01006991/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006992 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6993 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006994 */
6995static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006996ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006997{
6998 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006999 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007000
7001 len =i2d_X509(crt, NULL);
7002 if (len <= 0)
7003 return 1;
7004
7005 if (out->size < len)
7006 return -1;
7007
7008 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007009 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007010 return 1;
7011}
7012
Emeric Brunce5ad802012-10-22 14:11:22 +02007013
Willy Tarreau83061a82018-07-13 11:56:34 +02007014/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007015 * Returns 1 if serial is found and copied, 0 if no valid time found
7016 * and -1 if output is not large enough.
7017 */
7018static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007019ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007020{
7021 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7022 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7023
7024 if (gentm->length < 12)
7025 return 0;
7026 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7027 return 0;
7028 if (out->size < gentm->length-2)
7029 return -1;
7030
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007031 memcpy(out->area, gentm->data+2, gentm->length-2);
7032 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007033 return 1;
7034 }
7035 else if (tm->type == V_ASN1_UTCTIME) {
7036 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7037
7038 if (utctm->length < 10)
7039 return 0;
7040 if (utctm->data[0] >= 0x35)
7041 return 0;
7042 if (out->size < utctm->length)
7043 return -1;
7044
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007045 memcpy(out->area, utctm->data, utctm->length);
7046 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007047 return 1;
7048 }
7049
7050 return 0;
7051}
7052
Emeric Brun87855892012-10-17 17:39:35 +02007053/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7054 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7055 */
7056static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007057ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7058 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007059{
7060 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007061 ASN1_OBJECT *obj;
7062 ASN1_STRING *data;
7063 const unsigned char *data_ptr;
7064 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007065 int i, j, n;
7066 int cur = 0;
7067 const char *s;
7068 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007069 int name_count;
7070
7071 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007072
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007073 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007074 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007075 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007076 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007077 else
7078 j = i;
7079
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007080 ne = X509_NAME_get_entry(a, j);
7081 obj = X509_NAME_ENTRY_get_object(ne);
7082 data = X509_NAME_ENTRY_get_data(ne);
7083 data_ptr = ASN1_STRING_get0_data(data);
7084 data_len = ASN1_STRING_length(data);
7085 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007086 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007087 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007088 s = tmp;
7089 }
7090
7091 if (chunk_strcasecmp(entry, s) != 0)
7092 continue;
7093
7094 if (pos < 0)
7095 cur--;
7096 else
7097 cur++;
7098
7099 if (cur != pos)
7100 continue;
7101
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007102 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007103 return -1;
7104
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007105 memcpy(out->area, data_ptr, data_len);
7106 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007107 return 1;
7108 }
7109
7110 return 0;
7111
William Lallemandd4f946c2019-12-05 10:26:40 +01007112}
7113
7114/*
7115 * Extract and format the DNS SAN extensions and copy result into a chuink
7116 * Return 0;
7117 */
7118#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7119static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7120{
7121 int i;
7122 char *str;
7123 STACK_OF(GENERAL_NAME) *names = NULL;
7124
7125 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7126 if (names) {
7127 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7128 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7129 if (i > 0)
7130 chunk_appendf(out, ", ");
7131 if (name->type == GEN_DNS) {
7132 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7133 chunk_appendf(out, "DNS:%s", str);
7134 OPENSSL_free(str);
7135 }
7136 }
7137 }
7138 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7139 }
7140 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007141}
William Lallemandd4f946c2019-12-05 10:26:40 +01007142#endif
Emeric Brun87855892012-10-17 17:39:35 +02007143
7144/* Extract and format full DN from a X509_NAME and copy result into a chunk
7145 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7146 */
7147static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007148ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007149{
7150 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007151 ASN1_OBJECT *obj;
7152 ASN1_STRING *data;
7153 const unsigned char *data_ptr;
7154 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007155 int i, n, ln;
7156 int l = 0;
7157 const char *s;
7158 char *p;
7159 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007160 int name_count;
7161
7162
7163 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007164
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007165 out->data = 0;
7166 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007167 for (i = 0; i < name_count; i++) {
7168 ne = X509_NAME_get_entry(a, i);
7169 obj = X509_NAME_ENTRY_get_object(ne);
7170 data = X509_NAME_ENTRY_get_data(ne);
7171 data_ptr = ASN1_STRING_get0_data(data);
7172 data_len = ASN1_STRING_length(data);
7173 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007174 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007175 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007176 s = tmp;
7177 }
7178 ln = strlen(s);
7179
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007180 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007181 if (l > out->size)
7182 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007183 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007184
7185 *(p++)='/';
7186 memcpy(p, s, ln);
7187 p += ln;
7188 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007189 memcpy(p, data_ptr, data_len);
7190 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007191 }
7192
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007193 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007194 return 0;
7195
7196 return 1;
7197}
7198
Olivier Houchardab28a322018-12-21 19:45:40 +01007199void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7200{
7201#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007202 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007203
Olivier Houcharde488ea82019-06-28 14:10:33 +02007204 if (!ssl_sock_is_ssl(conn))
7205 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007206 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007207 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007208#endif
7209}
7210
Willy Tarreau119a4082016-12-22 21:58:38 +01007211/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7212 * to disable SNI.
7213 */
Willy Tarreau63076412015-07-10 11:33:32 +02007214void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7215{
7216#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007217 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007218
Willy Tarreau119a4082016-12-22 21:58:38 +01007219 char *prev_name;
7220
Willy Tarreau63076412015-07-10 11:33:32 +02007221 if (!ssl_sock_is_ssl(conn))
7222 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007223 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007224
Willy Tarreau119a4082016-12-22 21:58:38 +01007225 /* if the SNI changes, we must destroy the reusable context so that a
7226 * new connection will present a new SNI. As an optimization we could
7227 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7228 * server.
7229 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007230 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007231 if ((!prev_name && hostname) ||
7232 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007233 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007234
Olivier Houchard66ab4982019-02-26 18:37:15 +01007235 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007236#endif
7237}
7238
Emeric Brun0abf8362014-06-24 18:26:41 +02007239/* Extract peer certificate's common name into the chunk dest
7240 * Returns
7241 * the len of the extracted common name
7242 * or 0 if no CN found in DN
7243 * or -1 on error case (i.e. no peer certificate)
7244 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007245int ssl_sock_get_remote_common_name(struct connection *conn,
7246 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007247{
Christopher Faulet82004142019-09-10 10:12:03 +02007248 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007249 X509 *crt = NULL;
7250 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007251 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007252 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007253 .area = (char *)&find_cn,
7254 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007255 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007256 int result = -1;
David Safb76832014-05-08 23:42:08 -04007257
7258 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007259 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007260 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007261
7262 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007263 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007264 if (!crt)
7265 goto out;
7266
7267 name = X509_get_subject_name(crt);
7268 if (!name)
7269 goto out;
David Safb76832014-05-08 23:42:08 -04007270
Emeric Brun0abf8362014-06-24 18:26:41 +02007271 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7272out:
David Safb76832014-05-08 23:42:08 -04007273 if (crt)
7274 X509_free(crt);
7275
7276 return result;
7277}
7278
Dave McCowan328fb582014-07-30 10:39:13 -04007279/* returns 1 if client passed a certificate for this session, 0 if not */
7280int ssl_sock_get_cert_used_sess(struct connection *conn)
7281{
Christopher Faulet82004142019-09-10 10:12:03 +02007282 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007283 X509 *crt = NULL;
7284
7285 if (!ssl_sock_is_ssl(conn))
7286 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007287 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007288
7289 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007290 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007291 if (!crt)
7292 return 0;
7293
7294 X509_free(crt);
7295 return 1;
7296}
7297
7298/* returns 1 if client passed a certificate for this connection, 0 if not */
7299int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007300{
Christopher Faulet82004142019-09-10 10:12:03 +02007301 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007302
David Safb76832014-05-08 23:42:08 -04007303 if (!ssl_sock_is_ssl(conn))
7304 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007305 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007306 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007307}
7308
7309/* returns result from SSL verify */
7310unsigned int ssl_sock_get_verify_result(struct connection *conn)
7311{
Christopher Faulet82004142019-09-10 10:12:03 +02007312 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007313
David Safb76832014-05-08 23:42:08 -04007314 if (!ssl_sock_is_ssl(conn))
7315 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007316 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007317 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007318}
7319
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007320/* Returns the application layer protocol name in <str> and <len> when known.
7321 * Zero is returned if the protocol name was not found, otherwise non-zero is
7322 * returned. The string is allocated in the SSL context and doesn't have to be
7323 * freed by the caller. NPN is also checked if available since older versions
7324 * of openssl (1.0.1) which are more common in field only support this one.
7325 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007326static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007327{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007328#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7329 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007330 struct ssl_sock_ctx *ctx = xprt_ctx;
7331 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007332 return 0;
7333
7334 *str = NULL;
7335
7336#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007337 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007338 if (*str)
7339 return 1;
7340#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007341#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007342 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007343 if (*str)
7344 return 1;
7345#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007346#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007347 return 0;
7348}
7349
Willy Tarreau7875d092012-09-10 08:20:03 +02007350/***** Below are some sample fetching functions for ACL/patterns *****/
7351
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007352static int
7353smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7354{
7355 struct connection *conn;
7356
7357 conn = objt_conn(smp->sess->origin);
7358 if (!conn || conn->xprt != &ssl_sock)
7359 return 0;
7360
7361 smp->flags = 0;
7362 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007363#ifdef OPENSSL_IS_BORINGSSL
7364 {
7365 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7366 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7367 SSL_early_data_accepted(ctx->ssl));
7368 }
7369#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007370 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
7371 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007372#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007373 return 1;
7374}
7375
Emeric Brune64aef12012-09-21 13:15:06 +02007376/* boolean, returns true if client cert was present */
7377static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007378smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007379{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007380 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007381 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007382
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007383 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007384 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007385 return 0;
7386
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007387 ctx = conn->xprt_ctx;
7388
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007389 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02007390 smp->flags |= SMP_F_MAY_CHANGE;
7391 return 0;
7392 }
7393
7394 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007395 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007396 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007397
7398 return 1;
7399}
7400
Emeric Brun43e79582014-10-29 19:03:26 +01007401/* binary, returns a certificate in a binary chunk (der/raw).
7402 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7403 * should be use.
7404 */
7405static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007406smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007407{
7408 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7409 X509 *crt = NULL;
7410 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007411 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007412 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007413 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007414
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007415 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007416 if (!conn || conn->xprt != &ssl_sock)
7417 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007418 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007419
7420 if (!(conn->flags & CO_FL_CONNECTED)) {
7421 smp->flags |= SMP_F_MAY_CHANGE;
7422 return 0;
7423 }
7424
7425 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007426 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007427 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007428 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007429
7430 if (!crt)
7431 goto out;
7432
7433 smp_trash = get_trash_chunk();
7434 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7435 goto out;
7436
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007437 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007438 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007439 ret = 1;
7440out:
7441 /* SSL_get_peer_certificate, it increase X509 * ref count */
7442 if (cert_peer && crt)
7443 X509_free(crt);
7444 return ret;
7445}
7446
Emeric Brunba841a12014-04-30 17:05:08 +02007447/* binary, returns serial of certificate in a binary chunk.
7448 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7449 * should be use.
7450 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007451static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007452smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007453{
Emeric Brunba841a12014-04-30 17:05:08 +02007454 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007455 X509 *crt = NULL;
7456 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007457 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007458 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007459 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007460
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007461 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007462 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007463 return 0;
7464
Olivier Houchard66ab4982019-02-26 18:37:15 +01007465 ctx = conn->xprt_ctx;
7466
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007467 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007468 smp->flags |= SMP_F_MAY_CHANGE;
7469 return 0;
7470 }
7471
Emeric Brunba841a12014-04-30 17:05:08 +02007472 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007473 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007474 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007475 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007476
Willy Tarreau8d598402012-10-22 17:58:39 +02007477 if (!crt)
7478 goto out;
7479
Willy Tarreau47ca5452012-12-23 20:22:19 +01007480 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007481 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7482 goto out;
7483
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007484 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007485 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007486 ret = 1;
7487out:
Emeric Brunba841a12014-04-30 17:05:08 +02007488 /* SSL_get_peer_certificate, it increase X509 * ref count */
7489 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007490 X509_free(crt);
7491 return ret;
7492}
Emeric Brune64aef12012-09-21 13:15:06 +02007493
Emeric Brunba841a12014-04-30 17:05:08 +02007494/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7495 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7496 * should be use.
7497 */
James Votha051b4a2013-05-14 20:37:59 +02007498static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007499smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007500{
Emeric Brunba841a12014-04-30 17:05:08 +02007501 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007502 X509 *crt = NULL;
7503 const EVP_MD *digest;
7504 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007505 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007506 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007507 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02007508
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007509 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007510 if (!conn || conn->xprt != &ssl_sock)
7511 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007512 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007513
7514 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02007515 smp->flags |= SMP_F_MAY_CHANGE;
7516 return 0;
7517 }
7518
Emeric Brunba841a12014-04-30 17:05:08 +02007519 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007520 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007521 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007522 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02007523 if (!crt)
7524 goto out;
7525
7526 smp_trash = get_trash_chunk();
7527 digest = EVP_sha1();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007528 X509_digest(crt, digest, (unsigned char *) smp_trash->area,
7529 (unsigned int *)&smp_trash->data);
James Votha051b4a2013-05-14 20:37:59 +02007530
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007531 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007532 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02007533 ret = 1;
7534out:
Emeric Brunba841a12014-04-30 17:05:08 +02007535 /* SSL_get_peer_certificate, it increase X509 * ref count */
7536 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02007537 X509_free(crt);
7538 return ret;
7539}
7540
Emeric Brunba841a12014-04-30 17:05:08 +02007541/* string, returns certificate's notafter date in ASN1_UTCTIME format.
7542 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7543 * should be use.
7544 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007545static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007546smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007547{
Emeric Brunba841a12014-04-30 17:05:08 +02007548 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007549 X509 *crt = NULL;
7550 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007551 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007552 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007553 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007554
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007555 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007556 if (!conn || conn->xprt != &ssl_sock)
7557 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007558 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007559
7560 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007561 smp->flags |= SMP_F_MAY_CHANGE;
7562 return 0;
7563 }
7564
Emeric Brunba841a12014-04-30 17:05:08 +02007565 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007566 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007567 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007568 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007569 if (!crt)
7570 goto out;
7571
Willy Tarreau47ca5452012-12-23 20:22:19 +01007572 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007573 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007574 goto out;
7575
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007576 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007577 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007578 ret = 1;
7579out:
Emeric Brunba841a12014-04-30 17:05:08 +02007580 /* SSL_get_peer_certificate, it increase X509 * ref count */
7581 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007582 X509_free(crt);
7583 return ret;
7584}
7585
Emeric Brunba841a12014-04-30 17:05:08 +02007586/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
7587 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7588 * should be use.
7589 */
Emeric Brun87855892012-10-17 17:39:35 +02007590static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007591smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007592{
Emeric Brunba841a12014-04-30 17:05:08 +02007593 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007594 X509 *crt = NULL;
7595 X509_NAME *name;
7596 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007597 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007598 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007599 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007600
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007601 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007602 if (!conn || conn->xprt != &ssl_sock)
7603 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007604 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007605
7606 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007607 smp->flags |= SMP_F_MAY_CHANGE;
7608 return 0;
7609 }
7610
Emeric Brunba841a12014-04-30 17:05:08 +02007611 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007612 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007613 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007614 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007615 if (!crt)
7616 goto out;
7617
7618 name = X509_get_issuer_name(crt);
7619 if (!name)
7620 goto out;
7621
Willy Tarreau47ca5452012-12-23 20:22:19 +01007622 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02007623 if (args && args[0].type == ARGT_STR) {
7624 int pos = 1;
7625
7626 if (args[1].type == ARGT_SINT)
7627 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007628
7629 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7630 goto out;
7631 }
7632 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7633 goto out;
7634
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007635 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007636 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007637 ret = 1;
7638out:
Emeric Brunba841a12014-04-30 17:05:08 +02007639 /* SSL_get_peer_certificate, it increase X509 * ref count */
7640 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007641 X509_free(crt);
7642 return ret;
7643}
7644
Emeric Brunba841a12014-04-30 17:05:08 +02007645/* string, returns notbefore date in ASN1_UTCTIME format.
7646 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7647 * should be use.
7648 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007649static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007650smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007651{
Emeric Brunba841a12014-04-30 17:05:08 +02007652 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007653 X509 *crt = NULL;
7654 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007655 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007656 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007657 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007658
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007659 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007660 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02007661 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007662 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007663
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007664 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007665 smp->flags |= SMP_F_MAY_CHANGE;
7666 return 0;
7667 }
7668
Emeric Brunba841a12014-04-30 17:05:08 +02007669 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007670 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007671 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007672 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007673 if (!crt)
7674 goto out;
7675
Willy Tarreau47ca5452012-12-23 20:22:19 +01007676 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007677 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007678 goto out;
7679
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007680 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007681 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007682 ret = 1;
7683out:
Emeric Brunba841a12014-04-30 17:05:08 +02007684 /* SSL_get_peer_certificate, it increase X509 * ref count */
7685 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007686 X509_free(crt);
7687 return ret;
7688}
7689
Emeric Brunba841a12014-04-30 17:05:08 +02007690/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
7691 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7692 * should be use.
7693 */
Emeric Brun87855892012-10-17 17:39:35 +02007694static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007695smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007696{
Emeric Brunba841a12014-04-30 17:05:08 +02007697 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007698 X509 *crt = NULL;
7699 X509_NAME *name;
7700 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007701 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007702 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007703 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007704
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007705 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007706 if (!conn || conn->xprt != &ssl_sock)
7707 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007708 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007709
7710 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02007711 smp->flags |= SMP_F_MAY_CHANGE;
7712 return 0;
7713 }
7714
Emeric Brunba841a12014-04-30 17:05:08 +02007715 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007716 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007717 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007718 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007719 if (!crt)
7720 goto out;
7721
7722 name = X509_get_subject_name(crt);
7723 if (!name)
7724 goto out;
7725
Willy Tarreau47ca5452012-12-23 20:22:19 +01007726 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02007727 if (args && args[0].type == ARGT_STR) {
7728 int pos = 1;
7729
7730 if (args[1].type == ARGT_SINT)
7731 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007732
7733 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7734 goto out;
7735 }
7736 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7737 goto out;
7738
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007739 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007740 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007741 ret = 1;
7742out:
Emeric Brunba841a12014-04-30 17:05:08 +02007743 /* SSL_get_peer_certificate, it increase X509 * ref count */
7744 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007745 X509_free(crt);
7746 return ret;
7747}
Emeric Brun9143d372012-12-20 15:44:16 +01007748
7749/* integer, returns true if current session use a client certificate */
7750static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007751smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007752{
7753 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007754 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007755 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007756
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007757 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007758 if (!conn || conn->xprt != &ssl_sock)
7759 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007760 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007761
7762 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01007763 smp->flags |= SMP_F_MAY_CHANGE;
7764 return 0;
7765 }
7766
7767 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007768 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007769 if (crt) {
7770 X509_free(crt);
7771 }
7772
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007773 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007774 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007775 return 1;
7776}
7777
Emeric Brunba841a12014-04-30 17:05:08 +02007778/* integer, returns the certificate version
7779 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7780 * should be use.
7781 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007782static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007783smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007784{
Emeric Brunba841a12014-04-30 17:05:08 +02007785 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007786 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007787 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007788 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007789
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007790 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007791 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007792 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007793 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007794
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007795 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007796 smp->flags |= SMP_F_MAY_CHANGE;
7797 return 0;
7798 }
7799
Emeric Brunba841a12014-04-30 17:05:08 +02007800 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007801 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007802 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007803 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007804 if (!crt)
7805 return 0;
7806
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007807 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007808 /* SSL_get_peer_certificate increase X509 * ref count */
7809 if (cert_peer)
7810 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007811 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007812
7813 return 1;
7814}
7815
Emeric Brunba841a12014-04-30 17:05:08 +02007816/* string, returns the certificate's signature algorithm.
7817 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7818 * should be use.
7819 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007820static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007821smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007822{
Emeric Brunba841a12014-04-30 17:05:08 +02007823 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007824 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007825 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007826 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007827 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007828 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007829
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007830 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007831 if (!conn || conn->xprt != &ssl_sock)
7832 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007833 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007834
7835 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007836 smp->flags |= SMP_F_MAY_CHANGE;
7837 return 0;
7838 }
7839
Emeric Brunba841a12014-04-30 17:05:08 +02007840 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007841 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007842 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007843 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007844 if (!crt)
7845 return 0;
7846
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007847 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7848 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007849
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007850 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7851 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007852 /* SSL_get_peer_certificate increase X509 * ref count */
7853 if (cert_peer)
7854 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007855 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007856 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007857
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007858 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007859 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007860 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007861 /* SSL_get_peer_certificate increase X509 * ref count */
7862 if (cert_peer)
7863 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007864
7865 return 1;
7866}
7867
Emeric Brunba841a12014-04-30 17:05:08 +02007868/* string, returns the certificate's key algorithm.
7869 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7870 * should be use.
7871 */
Emeric Brun521a0112012-10-22 12:22:55 +02007872static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007873smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02007874{
Emeric Brunba841a12014-04-30 17:05:08 +02007875 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02007876 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007877 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02007878 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007879 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007880 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02007881
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007882 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007883 if (!conn || conn->xprt != &ssl_sock)
7884 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007885 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007886
7887 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02007888 smp->flags |= SMP_F_MAY_CHANGE;
7889 return 0;
7890 }
7891
Emeric Brunba841a12014-04-30 17:05:08 +02007892 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007893 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007894 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007895 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02007896 if (!crt)
7897 return 0;
7898
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007899 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
7900 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02007901
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007902 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7903 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007904 /* SSL_get_peer_certificate increase X509 * ref count */
7905 if (cert_peer)
7906 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007907 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007908 }
Emeric Brun521a0112012-10-22 12:22:55 +02007909
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007910 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007911 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007912 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007913 if (cert_peer)
7914 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02007915
7916 return 1;
7917}
7918
Emeric Brun645ae792014-04-30 14:21:06 +02007919/* boolean, returns true if front conn. transport layer is SSL.
7920 * This function is also usable on backend conn if the fetch keyword 5th
7921 * char is 'b'.
7922 */
Willy Tarreau7875d092012-09-10 08:20:03 +02007923static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007924smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007925{
Emeric Bruneb8def92018-02-19 15:59:48 +01007926 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7927 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007928
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007929 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007930 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007931 return 1;
7932}
7933
Emeric Brun2525b6b2012-10-18 15:59:43 +02007934/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007935static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007936smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007937{
7938#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007939 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007940 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007941
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007942 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007943 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007944 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007945 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007946 return 1;
7947#else
7948 return 0;
7949#endif
7950}
7951
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007952/* boolean, returns true if client session has been resumed.
7953 * This function is also usable on backend conn if the fetch keyword 5th
7954 * char is 'b'.
7955 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007956static int
7957smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
7958{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007959 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7960 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007961 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007962
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007963
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007964 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007965 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007966 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01007967 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007968 return 1;
7969}
7970
Emeric Brun645ae792014-04-30 14:21:06 +02007971/* string, returns the used cipher if front conn. transport layer is SSL.
7972 * This function is also usable on backend conn if the fetch keyword 5th
7973 * char is 'b'.
7974 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007975static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007976smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007977{
Emeric Bruneb8def92018-02-19 15:59:48 +01007978 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7979 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007980 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007981
Willy Tarreaube508f12016-03-10 11:47:01 +01007982 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007983 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02007984 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007985 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02007986
Olivier Houchard66ab4982019-02-26 18:37:15 +01007987 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007988 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007989 return 0;
7990
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007991 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007992 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007993 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007994
7995 return 1;
7996}
7997
Emeric Brun645ae792014-04-30 14:21:06 +02007998/* integer, returns the algoritm's keysize if front conn. transport layer
7999 * is SSL.
8000 * This function is also usable on backend conn if the fetch keyword 5th
8001 * char is 'b'.
8002 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008003static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008004smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008005{
Emeric Bruneb8def92018-02-19 15:59:48 +01008006 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8007 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008008 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008009 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008010
Emeric Brun589fcad2012-10-16 14:13:26 +02008011 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 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008017 return 0;
8018
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008019 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008020 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008021
8022 return 1;
8023}
8024
Emeric Brun645ae792014-04-30 14:21:06 +02008025/* integer, returns the used keysize if front conn. transport layer is SSL.
8026 * This function is also usable on backend conn if the fetch keyword 5th
8027 * char is 'b'.
8028 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008029static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008030smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008031{
Emeric Bruneb8def92018-02-19 15:59:48 +01008032 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8033 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008034 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008035
Emeric Brun589fcad2012-10-16 14:13:26 +02008036 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008037 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8038 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008039 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008040
Olivier Houchard66ab4982019-02-26 18:37:15 +01008041 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008042 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008043 return 0;
8044
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008045 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008046
8047 return 1;
8048}
8049
Bernard Spil13c53f82018-02-15 13:34:58 +01008050#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008051static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008052smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008053{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008054 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008055 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008056
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008057 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008058 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008059
Olivier Houchard6b77f492018-11-22 18:18:29 +01008060 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8061 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008062 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8063 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008064 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008065
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008066 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008067 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008068 (const unsigned char **)&smp->data.u.str.area,
8069 (unsigned *)&smp->data.u.str.data);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008070
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008071 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008072 return 0;
8073
8074 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008075}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008076#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008077
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008078#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008079static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008080smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008081{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008082 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008083 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008084
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008085 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008086 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008087
Olivier Houchard6b77f492018-11-22 18:18:29 +01008088 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8089 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8090
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008091 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008092 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008093 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +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_alpn_selected(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 Tarreauab861d32013-04-02 02:30:41 +02008099
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008100 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008101 return 0;
8102
8103 return 1;
8104}
8105#endif
8106
Emeric Brun645ae792014-04-30 14:21:06 +02008107/* string, returns the used protocol if front conn. transport layer is SSL.
8108 * This function is also usable on backend conn if the fetch keyword 5th
8109 * char is 'b'.
8110 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008111static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008112smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008113{
Emeric Bruneb8def92018-02-19 15:59:48 +01008114 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8115 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008116 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008117
Emeric Brun589fcad2012-10-16 14:13:26 +02008118 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008119 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8120 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008121 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008122
Olivier Houchard66ab4982019-02-26 18:37:15 +01008123 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008124 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008125 return 0;
8126
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008127 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008128 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008129 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008130
8131 return 1;
8132}
8133
Willy Tarreau87b09662015-04-03 00:22:06 +02008134/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008135 * This function is also usable on backend conn if the fetch keyword 5th
8136 * char is 'b'.
8137 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008138#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008139static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008140smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008141{
Emeric Bruneb8def92018-02-19 15:59:48 +01008142 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8143 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008144 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008145 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008146
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008147 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008148 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008149
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008150 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8151 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008152 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008153
Olivier Houchard66ab4982019-02-26 18:37:15 +01008154 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008155 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008156 return 0;
8157
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008158 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess,
8159 (unsigned int *)&smp->data.u.str.data);
8160 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008161 return 0;
8162
8163 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008164}
Patrick Hemmer41966772018-04-28 19:15:48 -04008165#endif
8166
Emeric Brunfe68f682012-10-16 14:59:28 +02008167
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008168#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008169static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008170smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8171{
8172 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8173 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8174 struct buffer *data;
8175 struct ssl_sock_ctx *ctx;
8176
8177 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8178 return 0;
8179 ctx = conn->xprt_ctx;
8180
8181 data = get_trash_chunk();
8182 if (kw[7] == 'c')
8183 data->data = SSL_get_client_random(ctx->ssl,
8184 (unsigned char *) data->area,
8185 data->size);
8186 else
8187 data->data = SSL_get_server_random(ctx->ssl,
8188 (unsigned char *) data->area,
8189 data->size);
8190 if (!data->data)
8191 return 0;
8192
8193 smp->flags = 0;
8194 smp->data.type = SMP_T_BIN;
8195 smp->data.u.str = *data;
8196
8197 return 1;
8198}
8199
8200static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008201smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8202{
8203 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8204 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8205 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008206 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008207 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008208
8209 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8210 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008211 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008212
Olivier Houchard66ab4982019-02-26 18:37:15 +01008213 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008214 if (!ssl_sess)
8215 return 0;
8216
8217 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008218 data->data = SSL_SESSION_get_master_key(ssl_sess,
8219 (unsigned char *) data->area,
8220 data->size);
8221 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008222 return 0;
8223
8224 smp->flags = 0;
8225 smp->data.type = SMP_T_BIN;
8226 smp->data.u.str = *data;
8227
8228 return 1;
8229}
8230#endif
8231
Patrick Hemmer41966772018-04-28 19:15:48 -04008232#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008233static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008234smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008235{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008236 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008237 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008238
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008239 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008240 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008241
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008242 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008243 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8244 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008245 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008246
Olivier Houchard66ab4982019-02-26 18:37:15 +01008247 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008248 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008249 return 0;
8250
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008251 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008252 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008253}
Patrick Hemmer41966772018-04-28 19:15:48 -04008254#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008255
David Sc1ad52e2014-04-08 18:48:47 -04008256static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008257smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8258{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008259 struct connection *conn;
8260 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008261 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008262
8263 conn = objt_conn(smp->sess->origin);
8264 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8265 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008266 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008267
Olivier Houchard66ab4982019-02-26 18:37:15 +01008268 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008269 if (!capture)
8270 return 0;
8271
8272 smp->flags = SMP_F_CONST;
8273 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008274 smp->data.u.str.area = capture->ciphersuite;
8275 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008276 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008277}
8278
8279static int
8280smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8281{
Willy Tarreau83061a82018-07-13 11:56:34 +02008282 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008283
8284 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8285 return 0;
8286
8287 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008288 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008289 smp->data.type = SMP_T_BIN;
8290 smp->data.u.str = *data;
8291 return 1;
8292}
8293
8294static int
8295smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8296{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008297 struct connection *conn;
8298 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008299 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008300
8301 conn = objt_conn(smp->sess->origin);
8302 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8303 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008304 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008305
Olivier Houchard66ab4982019-02-26 18:37:15 +01008306 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008307 if (!capture)
8308 return 0;
8309
8310 smp->data.type = SMP_T_SINT;
8311 smp->data.u.sint = capture->xxh64;
8312 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008313}
8314
8315static int
8316smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8317{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008318#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008319 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008320 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008321
8322 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8323 return 0;
8324
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008325 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008326 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008327 const char *str;
8328 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008329 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008330 uint16_t id = (bin[0] << 8) | bin[1];
8331#if defined(OPENSSL_IS_BORINGSSL)
8332 cipher = SSL_get_cipher_by_value(id);
8333#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008334 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008335 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8336 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008337#endif
8338 str = SSL_CIPHER_get_name(cipher);
8339 if (!str || strcmp(str, "(NONE)") == 0)
8340 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008341 else
8342 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8343 }
8344 smp->data.type = SMP_T_STR;
8345 smp->data.u.str = *data;
8346 return 1;
8347#else
8348 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8349#endif
8350}
8351
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008352#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008353static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008354smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008355{
Emeric Bruneb8def92018-02-19 15:59:48 +01008356 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8357 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008358 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008359 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008360 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008361
8362 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008363 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8364 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008365 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008366
8367 if (!(conn->flags & CO_FL_CONNECTED)) {
8368 smp->flags |= SMP_F_MAY_CHANGE;
8369 return 0;
8370 }
8371
8372 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008373 if (!SSL_session_reused(ctx->ssl))
8374 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008375 finished_trash->area,
8376 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008377 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008378 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008379 finished_trash->area,
8380 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008381
8382 if (!finished_len)
8383 return 0;
8384
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008385 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008386 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008387 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008388
8389 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008390}
Patrick Hemmer41966772018-04-28 19:15:48 -04008391#endif
David Sc1ad52e2014-04-08 18:48:47 -04008392
Emeric Brun2525b6b2012-10-18 15:59:43 +02008393/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008394static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008395smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008396{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008397 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008398 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008399
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008400 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008401 if (!conn || conn->xprt != &ssl_sock)
8402 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008403 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008404
8405 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008406 smp->flags = SMP_F_MAY_CHANGE;
8407 return 0;
8408 }
8409
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008410 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008411 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008412 smp->flags = 0;
8413
8414 return 1;
8415}
8416
Emeric Brun2525b6b2012-10-18 15:59:43 +02008417/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008418static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008419smp_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 +02008420{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008421 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008422 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008423
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008424 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008425 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008426 return 0;
8427
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008428 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008429 smp->flags = SMP_F_MAY_CHANGE;
8430 return 0;
8431 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008432 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008433
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008434 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008435 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008436 smp->flags = 0;
8437
8438 return 1;
8439}
8440
Emeric Brun2525b6b2012-10-18 15:59:43 +02008441/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008442static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008443smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008444{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008445 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008446 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008447
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008448 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008449 if (!conn || conn->xprt != &ssl_sock)
8450 return 0;
8451
8452 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02008453 smp->flags = SMP_F_MAY_CHANGE;
8454 return 0;
8455 }
8456
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008457 ctx = conn->xprt_ctx;
8458
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008459 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008460 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008461 smp->flags = 0;
8462
8463 return 1;
8464}
8465
Emeric Brun2525b6b2012-10-18 15:59:43 +02008466/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008467static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008468smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008469{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008470 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008471 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008472
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008473 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008474 if (!conn || conn->xprt != &ssl_sock)
8475 return 0;
8476
8477 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008478 smp->flags = SMP_F_MAY_CHANGE;
8479 return 0;
8480 }
8481
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008482 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008483 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008484 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008485
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008486 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008487 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008488 smp->flags = 0;
8489
8490 return 1;
8491}
8492
Emeric Brunfb510ea2012-10-05 12:00:26 +02008493/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008494static 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 +02008495{
8496 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008497 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008498 return ERR_ALERT | ERR_FATAL;
8499 }
8500
Willy Tarreauef934602016-12-22 23:12:01 +01008501 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8502 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008503 else
8504 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008505
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02008506 if (!ssl_store_load_locations_file(conf->ca_file)) {
8507 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->ca_file);
8508 return ERR_ALERT | ERR_FATAL;
8509 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02008510 return 0;
8511}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008512static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8513{
8514 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
8515}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008516
Christopher Faulet31af49d2015-06-09 17:29:50 +02008517/* parse the "ca-sign-file" bind keyword */
8518static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8519{
8520 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008521 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008522 return ERR_ALERT | ERR_FATAL;
8523 }
8524
Willy Tarreauef934602016-12-22 23:12:01 +01008525 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8526 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008527 else
8528 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
8529
8530 return 0;
8531}
8532
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008533/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02008534static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8535{
8536 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008537 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008538 return ERR_ALERT | ERR_FATAL;
8539 }
8540 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
8541 return 0;
8542}
8543
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008544/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008545static 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 +02008546{
8547 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008548 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008549 return ERR_ALERT | ERR_FATAL;
8550 }
8551
Emeric Brun76d88952012-10-05 15:47:31 +02008552 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02008553 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008554 return 0;
8555}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008556static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8557{
8558 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
8559}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008560
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008561#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008562/* parse the "ciphersuites" bind keyword */
8563static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8564{
8565 if (!*args[cur_arg + 1]) {
8566 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
8567 return ERR_ALERT | ERR_FATAL;
8568 }
8569
8570 free(conf->ciphersuites);
8571 conf->ciphersuites = strdup(args[cur_arg + 1]);
8572 return 0;
8573}
8574static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8575{
8576 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
8577}
8578#endif
8579
Willy Tarreaubbc91962019-10-16 16:42:19 +02008580/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008581static 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 +02008582{
Willy Tarreau38011032013-08-13 16:59:39 +02008583 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02008584
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008585 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008586 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008587 return ERR_ALERT | ERR_FATAL;
8588 }
8589
Willy Tarreauef934602016-12-22 23:12:01 +01008590 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
8591 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02008592 memprintf(err, "'%s' : path too long", args[cur_arg]);
8593 return ERR_ALERT | ERR_FATAL;
8594 }
Willy Tarreauef934602016-12-22 23:12:01 +01008595 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02008596 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008597 }
8598
Willy Tarreaubbc91962019-10-16 16:42:19 +02008599 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008600}
8601
Willy Tarreaubbc91962019-10-16 16:42:19 +02008602/* 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 +01008603static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8604{
Willy Tarreaubbc91962019-10-16 16:42:19 +02008605 int err_code;
8606
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008607 if (!*args[cur_arg + 1]) {
8608 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
8609 return ERR_ALERT | ERR_FATAL;
8610 }
8611
Willy Tarreaubbc91962019-10-16 16:42:19 +02008612 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err);
8613 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02008614 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008615
Willy Tarreaubbc91962019-10-16 16:42:19 +02008616 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008617}
8618
Emeric Brunfb510ea2012-10-05 12:00:26 +02008619/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008620static 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 +02008621{
Emeric Brun051cdab2012-10-02 19:25:50 +02008622#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01008623 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02008624 return ERR_ALERT | ERR_FATAL;
8625#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02008626 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008627 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008628 return ERR_ALERT | ERR_FATAL;
8629 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008630
Willy Tarreauef934602016-12-22 23:12:01 +01008631 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8632 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008633 else
8634 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008635
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01008636 if (!ssl_store_load_locations_file(conf->crl_file)) {
8637 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
8638 return ERR_ALERT | ERR_FATAL;
8639 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008640 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02008641#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02008642}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008643static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8644{
8645 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
8646}
Emeric Brun2b58d042012-09-20 17:10:03 +02008647
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008648/* parse the "curves" bind keyword keyword */
8649static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8650{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01008651#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008652 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008653 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008654 return ERR_ALERT | ERR_FATAL;
8655 }
8656 conf->curves = strdup(args[cur_arg + 1]);
8657 return 0;
8658#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008659 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008660 return ERR_ALERT | ERR_FATAL;
8661#endif
8662}
8663static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8664{
8665 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
8666}
8667
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008668/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008669static 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 +02008670{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008671#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01008672 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008673 return ERR_ALERT | ERR_FATAL;
8674#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01008675 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 +02008676 return ERR_ALERT | ERR_FATAL;
8677#else
8678 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008679 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008680 return ERR_ALERT | ERR_FATAL;
8681 }
8682
8683 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008684
8685 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02008686#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008687}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008688static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8689{
8690 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
8691}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008692
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008693/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02008694static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8695{
8696 int code;
8697 char *p = args[cur_arg + 1];
8698 unsigned long long *ignerr = &conf->crt_ignerr;
8699
8700 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008701 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008702 return ERR_ALERT | ERR_FATAL;
8703 }
8704
8705 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
8706 ignerr = &conf->ca_ignerr;
8707
8708 if (strcmp(p, "all") == 0) {
8709 *ignerr = ~0ULL;
8710 return 0;
8711 }
8712
8713 while (p) {
8714 code = atoi(p);
8715 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008716 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
8717 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008718 return ERR_ALERT | ERR_FATAL;
8719 }
8720 *ignerr |= 1ULL << code;
8721 p = strchr(p, ',');
8722 if (p)
8723 p++;
8724 }
8725
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008726 return 0;
8727}
8728
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008729/* parse tls_method_options "no-xxx" and "force-xxx" */
8730static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008731{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008732 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008733 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008734 p = strchr(arg, '-');
8735 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008736 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008737 p++;
8738 if (!strcmp(p, "sslv3"))
8739 v = CONF_SSLV3;
8740 else if (!strcmp(p, "tlsv10"))
8741 v = CONF_TLSV10;
8742 else if (!strcmp(p, "tlsv11"))
8743 v = CONF_TLSV11;
8744 else if (!strcmp(p, "tlsv12"))
8745 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008746 else if (!strcmp(p, "tlsv13"))
8747 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008748 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008749 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008750 if (!strncmp(arg, "no-", 3))
8751 methods->flags |= methodVersions[v].flag;
8752 else if (!strncmp(arg, "force-", 6))
8753 methods->min = methods->max = v;
8754 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008755 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008756 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008757 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01008758 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008759 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008760}
8761
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008762static 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 +02008763{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008764 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008765}
8766
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008767static 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 +02008768{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008769 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8770}
8771
8772/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8773static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8774{
8775 uint16_t i, v = 0;
8776 char *argv = args[cur_arg + 1];
8777 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008778 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008779 return ERR_ALERT | ERR_FATAL;
8780 }
8781 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8782 if (!strcmp(argv, methodVersions[i].name))
8783 v = i;
8784 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008785 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008786 return ERR_ALERT | ERR_FATAL;
8787 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008788 if (!strcmp("ssl-min-ver", args[cur_arg]))
8789 methods->min = v;
8790 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8791 methods->max = v;
8792 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01008793 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008794 return ERR_ALERT | ERR_FATAL;
8795 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008796 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008797}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008798
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008799static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8800{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008801#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008802 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 +02008803#endif
8804 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8805}
8806
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008807static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8808{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008809 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008810}
8811
8812static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8813{
8814 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8815}
8816
Emeric Brun2d0c4822012-10-02 13:45:20 +02008817/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008818static 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 +02008819{
Emeric Brun89675492012-10-05 13:48:26 +02008820 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008821 return 0;
8822}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008823
Olivier Houchardc2aae742017-09-22 18:26:28 +02008824/* parse the "allow-0rtt" bind keyword */
8825static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8826{
8827 conf->early_data = 1;
8828 return 0;
8829}
8830
8831static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8832{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008833 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008834 return 0;
8835}
8836
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008837/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008838static 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 +02008839{
Bernard Spil13c53f82018-02-15 13:34:58 +01008840#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008841 char *p1, *p2;
8842
8843 if (!*args[cur_arg + 1]) {
8844 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8845 return ERR_ALERT | ERR_FATAL;
8846 }
8847
8848 free(conf->npn_str);
8849
Willy Tarreau3724da12016-02-12 17:11:12 +01008850 /* the NPN string is built as a suite of (<len> <name>)*,
8851 * so we reuse each comma to store the next <len> and need
8852 * one more for the end of the string.
8853 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008854 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008855 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008856 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8857
8858 /* replace commas with the name length */
8859 p1 = conf->npn_str;
8860 p2 = p1 + 1;
8861 while (1) {
8862 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8863 if (!p2)
8864 p2 = p1 + 1 + strlen(p1 + 1);
8865
8866 if (p2 - (p1 + 1) > 255) {
8867 *p2 = '\0';
8868 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8869 return ERR_ALERT | ERR_FATAL;
8870 }
8871
8872 *p1 = p2 - (p1 + 1);
8873 p1 = p2;
8874
8875 if (!*p2)
8876 break;
8877
8878 *(p2++) = '\0';
8879 }
8880 return 0;
8881#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008882 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008883 return ERR_ALERT | ERR_FATAL;
8884#endif
8885}
8886
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008887static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8888{
8889 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8890}
8891
Willy Tarreauab861d32013-04-02 02:30:41 +02008892/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008893static 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 +02008894{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008895#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008896 char *p1, *p2;
8897
8898 if (!*args[cur_arg + 1]) {
8899 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
8900 return ERR_ALERT | ERR_FATAL;
8901 }
8902
8903 free(conf->alpn_str);
8904
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008905 /* the ALPN string is built as a suite of (<len> <name>)*,
8906 * so we reuse each comma to store the next <len> and need
8907 * one more for the end of the string.
8908 */
Willy Tarreauab861d32013-04-02 02:30:41 +02008909 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008910 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02008911 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
8912
8913 /* replace commas with the name length */
8914 p1 = conf->alpn_str;
8915 p2 = p1 + 1;
8916 while (1) {
8917 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
8918 if (!p2)
8919 p2 = p1 + 1 + strlen(p1 + 1);
8920
8921 if (p2 - (p1 + 1) > 255) {
8922 *p2 = '\0';
8923 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8924 return ERR_ALERT | ERR_FATAL;
8925 }
8926
8927 *p1 = p2 - (p1 + 1);
8928 p1 = p2;
8929
8930 if (!*p2)
8931 break;
8932
8933 *(p2++) = '\0';
8934 }
8935 return 0;
8936#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008937 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02008938 return ERR_ALERT | ERR_FATAL;
8939#endif
8940}
8941
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008942static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8943{
8944 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8945}
8946
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008947/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008948static 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 +02008949{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01008950 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02008951 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02008952
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008953 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
8954 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008955#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008956 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
8957 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
8958#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008959 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008960 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
8961 if (!conf->ssl_conf.ssl_methods.min)
8962 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
8963 if (!conf->ssl_conf.ssl_methods.max)
8964 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02008965
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008966 return 0;
8967}
8968
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008969/* parse the "prefer-client-ciphers" bind keyword */
8970static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8971{
8972 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
8973 return 0;
8974}
8975
Christopher Faulet31af49d2015-06-09 17:29:50 +02008976/* parse the "generate-certificates" bind keyword */
8977static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8978{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01008979#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02008980 conf->generate_certs = 1;
8981#else
8982 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
8983 err && *err ? *err : "");
8984#endif
8985 return 0;
8986}
8987
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008988/* parse the "strict-sni" bind keyword */
8989static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8990{
8991 conf->strict_sni = 1;
8992 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008993}
8994
8995/* parse the "tls-ticket-keys" bind keyword */
8996static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8997{
8998#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02008999 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009000 int i = 0;
9001 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009002 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009003
9004 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009005 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009006 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009007 }
9008
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009009 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009010 if (keys_ref) {
9011 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009012 conf->keys_ref = keys_ref;
9013 return 0;
9014 }
9015
Christopher Faulete566f3d2019-10-21 09:55:49 +02009016 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009017 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009018 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009019 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009020 }
9021
Emeric Brun9e754772019-01-10 17:51:55 +01009022 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009023 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009024 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009025 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009026 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009027
9028 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009029 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009030 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009031 }
9032
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009033 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009034 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009035 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009036 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009037 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009038
Emeric Brun9e754772019-01-10 17:51:55 +01009039 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009040 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9041 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009042 int dec_size;
9043
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009044 /* Strip newline characters from the end */
9045 if(thisline[len - 1] == '\n')
9046 thisline[--len] = 0;
9047
9048 if(thisline[len - 1] == '\r')
9049 thisline[--len] = 0;
9050
Emeric Brun9e754772019-01-10 17:51:55 +01009051 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9052 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009053 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009054 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009055 }
Emeric Brun9e754772019-01-10 17:51:55 +01009056 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9057 keys_ref->key_size_bits = 128;
9058 }
9059 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9060 keys_ref->key_size_bits = 256;
9061 }
9062 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9063 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9064 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009065 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009066 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009067 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009068 i++;
9069 }
9070
9071 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009072 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 +02009073 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009074 }
9075
9076 fclose(f);
9077
9078 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009079 i -= 2;
9080 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009081 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009082 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009083 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009084 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009085
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009086 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9087
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009088 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009089
9090 fail:
9091 if (f)
9092 fclose(f);
9093 if (keys_ref) {
9094 free(keys_ref->filename);
9095 free(keys_ref->tlskeys);
9096 free(keys_ref);
9097 }
9098 return ERR_ALERT | ERR_FATAL;
9099
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009100#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009101 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009102 return ERR_ALERT | ERR_FATAL;
9103#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009104}
9105
Emeric Brund94b3fe2012-09-20 18:23:56 +02009106/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009107static 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 +02009108{
9109 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009110 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009111 return ERR_ALERT | ERR_FATAL;
9112 }
9113
9114 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009115 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009116 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009117 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009118 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009119 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009120 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009121 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9122 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009123 return ERR_ALERT | ERR_FATAL;
9124 }
9125
9126 return 0;
9127}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009128static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9129{
9130 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9131}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009132
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009133/* parse the "no-ca-names" bind keyword */
9134static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9135{
9136 conf->no_ca_names = 1;
9137 return 0;
9138}
9139static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9140{
9141 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9142}
9143
Willy Tarreau92faadf2012-10-10 23:04:25 +02009144/************** "server" keywords ****************/
9145
Olivier Houchardc7566002018-11-20 23:33:50 +01009146/* parse the "npn" bind keyword */
9147static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9148{
9149#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9150 char *p1, *p2;
9151
9152 if (!*args[*cur_arg + 1]) {
9153 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9154 return ERR_ALERT | ERR_FATAL;
9155 }
9156
9157 free(newsrv->ssl_ctx.npn_str);
9158
9159 /* the NPN string is built as a suite of (<len> <name>)*,
9160 * so we reuse each comma to store the next <len> and need
9161 * one more for the end of the string.
9162 */
9163 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9164 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9165 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9166 newsrv->ssl_ctx.npn_len);
9167
9168 /* replace commas with the name length */
9169 p1 = newsrv->ssl_ctx.npn_str;
9170 p2 = p1 + 1;
9171 while (1) {
9172 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9173 newsrv->ssl_ctx.npn_len - (p1 + 1));
9174 if (!p2)
9175 p2 = p1 + 1 + strlen(p1 + 1);
9176
9177 if (p2 - (p1 + 1) > 255) {
9178 *p2 = '\0';
9179 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9180 return ERR_ALERT | ERR_FATAL;
9181 }
9182
9183 *p1 = p2 - (p1 + 1);
9184 p1 = p2;
9185
9186 if (!*p2)
9187 break;
9188
9189 *(p2++) = '\0';
9190 }
9191 return 0;
9192#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009193 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009194 return ERR_ALERT | ERR_FATAL;
9195#endif
9196}
9197
Olivier Houchard92150142018-12-21 19:47:01 +01009198/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009199static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9200{
9201#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9202 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009203 char **alpn_str;
9204 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009205
Olivier Houchard92150142018-12-21 19:47:01 +01009206 if (*args[*cur_arg] == 'c') {
9207 alpn_str = &newsrv->check.alpn_str;
9208 alpn_len = &newsrv->check.alpn_len;
9209 } else {
9210 alpn_str = &newsrv->ssl_ctx.alpn_str;
9211 alpn_len = &newsrv->ssl_ctx.alpn_len;
9212
9213 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009214 if (!*args[*cur_arg + 1]) {
9215 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9216 return ERR_ALERT | ERR_FATAL;
9217 }
9218
Olivier Houchard92150142018-12-21 19:47:01 +01009219 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009220
9221 /* the ALPN string is built as a suite of (<len> <name>)*,
9222 * so we reuse each comma to store the next <len> and need
9223 * one more for the end of the string.
9224 */
Olivier Houchard92150142018-12-21 19:47:01 +01009225 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9226 *alpn_str = calloc(1, *alpn_len + 1);
9227 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009228
9229 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009230 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009231 p2 = p1 + 1;
9232 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009233 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009234 if (!p2)
9235 p2 = p1 + 1 + strlen(p1 + 1);
9236
9237 if (p2 - (p1 + 1) > 255) {
9238 *p2 = '\0';
9239 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9240 return ERR_ALERT | ERR_FATAL;
9241 }
9242
9243 *p1 = p2 - (p1 + 1);
9244 p1 = p2;
9245
9246 if (!*p2)
9247 break;
9248
9249 *(p2++) = '\0';
9250 }
9251 return 0;
9252#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009253 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009254 return ERR_ALERT | ERR_FATAL;
9255#endif
9256}
9257
Emeric Brunef42d922012-10-11 16:11:36 +02009258/* parse the "ca-file" server keyword */
9259static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9260{
9261 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009262 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009263 return ERR_ALERT | ERR_FATAL;
9264 }
9265
Willy Tarreauef934602016-12-22 23:12:01 +01009266 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9267 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009268 else
9269 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9270
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009271 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9272 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9273 return ERR_ALERT | ERR_FATAL;
9274 }
Emeric Brunef42d922012-10-11 16:11:36 +02009275 return 0;
9276}
9277
Olivier Houchard9130a962017-10-17 17:33:43 +02009278/* parse the "check-sni" server keyword */
9279static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9280{
9281 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009282 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009283 return ERR_ALERT | ERR_FATAL;
9284 }
9285
9286 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9287 if (!newsrv->check.sni) {
9288 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9289 return ERR_ALERT | ERR_FATAL;
9290 }
9291 return 0;
9292
9293}
9294
Willy Tarreau92faadf2012-10-10 23:04:25 +02009295/* parse the "check-ssl" server keyword */
9296static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9297{
9298 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009299 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9300 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009301#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009302 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9303 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9304#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009305 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009306 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9307 if (!newsrv->ssl_ctx.methods.min)
9308 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9309 if (!newsrv->ssl_ctx.methods.max)
9310 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9311
Willy Tarreau92faadf2012-10-10 23:04:25 +02009312 return 0;
9313}
9314
9315/* parse the "ciphers" server keyword */
9316static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9317{
9318 if (!*args[*cur_arg + 1]) {
9319 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9320 return ERR_ALERT | ERR_FATAL;
9321 }
9322
9323 free(newsrv->ssl_ctx.ciphers);
9324 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9325 return 0;
9326}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009327
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009328#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009329/* parse the "ciphersuites" server keyword */
9330static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9331{
9332 if (!*args[*cur_arg + 1]) {
9333 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9334 return ERR_ALERT | ERR_FATAL;
9335 }
9336
9337 free(newsrv->ssl_ctx.ciphersuites);
9338 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9339 return 0;
9340}
9341#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009342
Emeric Brunef42d922012-10-11 16:11:36 +02009343/* parse the "crl-file" server keyword */
9344static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9345{
9346#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009347 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009348 return ERR_ALERT | ERR_FATAL;
9349#else
9350 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009351 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009352 return ERR_ALERT | ERR_FATAL;
9353 }
9354
Willy Tarreauef934602016-12-22 23:12:01 +01009355 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9356 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009357 else
9358 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9359
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009360 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9361 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9362 return ERR_ALERT | ERR_FATAL;
9363 }
Emeric Brunef42d922012-10-11 16:11:36 +02009364 return 0;
9365#endif
9366}
9367
Emeric Bruna7aa3092012-10-26 12:58:00 +02009368/* parse the "crt" server keyword */
9369static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9370{
9371 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009372 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009373 return ERR_ALERT | ERR_FATAL;
9374 }
9375
Willy Tarreauef934602016-12-22 23:12:01 +01009376 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009377 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009378 else
9379 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9380
9381 return 0;
9382}
Emeric Brunef42d922012-10-11 16:11:36 +02009383
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009384/* parse the "no-check-ssl" server keyword */
9385static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9386{
9387 newsrv->check.use_ssl = 0;
9388 free(newsrv->ssl_ctx.ciphers);
9389 newsrv->ssl_ctx.ciphers = NULL;
9390 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9391 return 0;
9392}
9393
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009394/* parse the "no-send-proxy-v2-ssl" server keyword */
9395static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9396{
9397 newsrv->pp_opts &= ~SRV_PP_V2;
9398 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9399 return 0;
9400}
9401
9402/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9403static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9404{
9405 newsrv->pp_opts &= ~SRV_PP_V2;
9406 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9407 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9408 return 0;
9409}
9410
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009411/* parse the "no-ssl" server keyword */
9412static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9413{
9414 newsrv->use_ssl = 0;
9415 free(newsrv->ssl_ctx.ciphers);
9416 newsrv->ssl_ctx.ciphers = NULL;
9417 return 0;
9418}
9419
Olivier Houchard522eea72017-11-03 16:27:47 +01009420/* parse the "allow-0rtt" server keyword */
9421static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9422{
9423 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9424 return 0;
9425}
9426
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009427/* parse the "no-ssl-reuse" server keyword */
9428static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9429{
9430 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9431 return 0;
9432}
9433
Emeric Brunf9c5c472012-10-11 15:28:34 +02009434/* parse the "no-tls-tickets" server keyword */
9435static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9436{
9437 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9438 return 0;
9439}
David Safb76832014-05-08 23:42:08 -04009440/* parse the "send-proxy-v2-ssl" server keyword */
9441static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9442{
9443 newsrv->pp_opts |= SRV_PP_V2;
9444 newsrv->pp_opts |= SRV_PP_V2_SSL;
9445 return 0;
9446}
9447
9448/* parse the "send-proxy-v2-ssl-cn" server keyword */
9449static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9450{
9451 newsrv->pp_opts |= SRV_PP_V2;
9452 newsrv->pp_opts |= SRV_PP_V2_SSL;
9453 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9454 return 0;
9455}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009456
Willy Tarreau732eac42015-07-09 11:40:25 +02009457/* parse the "sni" server keyword */
9458static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9459{
9460#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9461 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9462 return ERR_ALERT | ERR_FATAL;
9463#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009464 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009465
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009466 arg = args[*cur_arg + 1];
9467 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009468 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9469 return ERR_ALERT | ERR_FATAL;
9470 }
9471
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009472 free(newsrv->sni_expr);
9473 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02009474
Willy Tarreau732eac42015-07-09 11:40:25 +02009475 return 0;
9476#endif
9477}
9478
Willy Tarreau92faadf2012-10-10 23:04:25 +02009479/* parse the "ssl" server keyword */
9480static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9481{
9482 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009483 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9484 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009485#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009486 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9487 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9488#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009489 return 0;
9490}
9491
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009492/* parse the "ssl-reuse" server keyword */
9493static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9494{
9495 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
9496 return 0;
9497}
9498
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009499/* parse the "tls-tickets" server keyword */
9500static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9501{
9502 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
9503 return 0;
9504}
9505
Emeric Brunef42d922012-10-11 16:11:36 +02009506/* parse the "verify" server keyword */
9507static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9508{
9509 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009510 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009511 return ERR_ALERT | ERR_FATAL;
9512 }
9513
9514 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009515 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02009516 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009517 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02009518 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009519 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
9520 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009521 return ERR_ALERT | ERR_FATAL;
9522 }
9523
Evan Broderbe554312013-06-27 00:05:25 -07009524 return 0;
9525}
9526
9527/* parse the "verifyhost" server keyword */
9528static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9529{
9530 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009531 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -07009532 return ERR_ALERT | ERR_FATAL;
9533 }
9534
Frédéric Lécaille273f3212017-03-13 15:52:01 +01009535 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07009536 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
9537
Emeric Brunef42d922012-10-11 16:11:36 +02009538 return 0;
9539}
9540
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009541/* parse the "ssl-default-bind-options" keyword in global section */
9542static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
9543 struct proxy *defpx, const char *file, int line,
9544 char **err) {
9545 int i = 1;
9546
9547 if (*(args[i]) == 0) {
9548 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9549 return -1;
9550 }
9551 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009552 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009553 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009554 else if (!strcmp(args[i], "prefer-client-ciphers"))
9555 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009556 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9557 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
9558 i++;
9559 else {
9560 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9561 return -1;
9562 }
9563 }
9564 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009565 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9566 return -1;
9567 }
9568 i++;
9569 }
9570 return 0;
9571}
9572
9573/* parse the "ssl-default-server-options" keyword in global section */
9574static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
9575 struct proxy *defpx, const char *file, int line,
9576 char **err) {
9577 int i = 1;
9578
9579 if (*(args[i]) == 0) {
9580 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9581 return -1;
9582 }
9583 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009584 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009585 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009586 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9587 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
9588 i++;
9589 else {
9590 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9591 return -1;
9592 }
9593 }
9594 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009595 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9596 return -1;
9597 }
9598 i++;
9599 }
9600 return 0;
9601}
9602
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009603/* parse the "ca-base" / "crt-base" keywords in global section.
9604 * Returns <0 on alert, >0 on warning, 0 on success.
9605 */
9606static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
9607 struct proxy *defpx, const char *file, int line,
9608 char **err)
9609{
9610 char **target;
9611
Willy Tarreauef934602016-12-22 23:12:01 +01009612 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009613
9614 if (too_many_args(1, args, err, NULL))
9615 return -1;
9616
9617 if (*target) {
9618 memprintf(err, "'%s' already specified.", args[0]);
9619 return -1;
9620 }
9621
9622 if (*(args[1]) == 0) {
9623 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
9624 return -1;
9625 }
9626 *target = strdup(args[1]);
9627 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009628}
9629
9630/* parse the "ssl-mode-async" keyword in global section.
9631 * Returns <0 on alert, >0 on warning, 0 on success.
9632 */
9633static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
9634 struct proxy *defpx, const char *file, int line,
9635 char **err)
9636{
Willy Tarreau5db847a2019-05-09 14:13:35 +02009637#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009638 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01009639 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009640 return 0;
9641#else
9642 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
9643 return -1;
9644#endif
9645}
9646
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009647#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009648static int ssl_check_async_engine_count(void) {
9649 int err_code = 0;
9650
Emeric Brun3854e012017-05-17 20:42:48 +02009651 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009652 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009653 err_code = ERR_ABORT;
9654 }
9655 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009656}
9657
Grant Zhang872f9c22017-01-21 01:10:18 +00009658/* parse the "ssl-engine" keyword in global section.
9659 * Returns <0 on alert, >0 on warning, 0 on success.
9660 */
9661static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9662 struct proxy *defpx, const char *file, int line,
9663 char **err)
9664{
9665 char *algo;
9666 int ret = -1;
9667
9668 if (*(args[1]) == 0) {
9669 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9670 return ret;
9671 }
9672
9673 if (*(args[2]) == 0) {
9674 /* if no list of algorithms is given, it defaults to ALL */
9675 algo = strdup("ALL");
9676 goto add_engine;
9677 }
9678
9679 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
9680 if (strcmp(args[2], "algo") != 0) {
9681 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
9682 return ret;
9683 }
9684
9685 if (*(args[3]) == 0) {
9686 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
9687 return ret;
9688 }
9689 algo = strdup(args[3]);
9690
9691add_engine:
9692 if (ssl_init_single_engine(args[1], algo)==0) {
9693 openssl_engines_initialized++;
9694 ret = 0;
9695 }
9696 free(algo);
9697 return ret;
9698}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009699#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00009700
Willy Tarreauf22e9682016-12-21 23:23:19 +01009701/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
9702 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9703 */
9704static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
9705 struct proxy *defpx, const char *file, int line,
9706 char **err)
9707{
9708 char **target;
9709
Willy Tarreauef934602016-12-22 23:12:01 +01009710 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01009711
9712 if (too_many_args(1, args, err, NULL))
9713 return -1;
9714
9715 if (*(args[1]) == 0) {
9716 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9717 return -1;
9718 }
9719
9720 free(*target);
9721 *target = strdup(args[1]);
9722 return 0;
9723}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009724
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009725#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009726/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9727 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9728 */
9729static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9730 struct proxy *defpx, const char *file, int line,
9731 char **err)
9732{
9733 char **target;
9734
9735 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9736
9737 if (too_many_args(1, args, err, NULL))
9738 return -1;
9739
9740 if (*(args[1]) == 0) {
9741 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9742 return -1;
9743 }
9744
9745 free(*target);
9746 *target = strdup(args[1]);
9747 return 0;
9748}
9749#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009750
Willy Tarreau9ceda382016-12-21 23:13:03 +01009751/* parse various global tune.ssl settings consisting in positive integers.
9752 * Returns <0 on alert, >0 on warning, 0 on success.
9753 */
9754static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9755 struct proxy *defpx, const char *file, int line,
9756 char **err)
9757{
9758 int *target;
9759
9760 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9761 target = &global.tune.sslcachesize;
9762 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009763 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009764 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009765 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009766 else if (strcmp(args[0], "maxsslconn") == 0)
9767 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009768 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9769 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009770 else {
9771 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9772 return -1;
9773 }
9774
9775 if (too_many_args(1, args, err, NULL))
9776 return -1;
9777
9778 if (*(args[1]) == 0) {
9779 memprintf(err, "'%s' expects an integer argument.", args[0]);
9780 return -1;
9781 }
9782
9783 *target = atoi(args[1]);
9784 if (*target < 0) {
9785 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9786 return -1;
9787 }
9788 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009789}
9790
9791static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9792 struct proxy *defpx, const char *file, int line,
9793 char **err)
9794{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009795 int ret;
9796
9797 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9798 if (ret != 0)
9799 return ret;
9800
Willy Tarreaubafbe012017-11-24 17:34:44 +01009801 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009802 memprintf(err, "'%s' is already configured.", args[0]);
9803 return -1;
9804 }
9805
Willy Tarreaubafbe012017-11-24 17:34:44 +01009806 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9807 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009808 memprintf(err, "Out of memory error.");
9809 return -1;
9810 }
9811 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009812}
9813
9814/* parse "ssl.force-private-cache".
9815 * Returns <0 on alert, >0 on warning, 0 on success.
9816 */
9817static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9818 struct proxy *defpx, const char *file, int line,
9819 char **err)
9820{
9821 if (too_many_args(0, args, err, NULL))
9822 return -1;
9823
Willy Tarreauef934602016-12-22 23:12:01 +01009824 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009825 return 0;
9826}
9827
9828/* parse "ssl.lifetime".
9829 * Returns <0 on alert, >0 on warning, 0 on success.
9830 */
9831static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9832 struct proxy *defpx, const char *file, int line,
9833 char **err)
9834{
9835 const char *res;
9836
9837 if (too_many_args(1, args, err, NULL))
9838 return -1;
9839
9840 if (*(args[1]) == 0) {
9841 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9842 return -1;
9843 }
9844
Willy Tarreauef934602016-12-22 23:12:01 +01009845 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009846 if (res == PARSE_TIME_OVER) {
9847 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9848 args[1], args[0]);
9849 return -1;
9850 }
9851 else if (res == PARSE_TIME_UNDER) {
9852 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9853 args[1], args[0]);
9854 return -1;
9855 }
9856 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009857 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9858 return -1;
9859 }
9860 return 0;
9861}
9862
9863#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009864/* parse "ssl-dh-param-file".
9865 * Returns <0 on alert, >0 on warning, 0 on success.
9866 */
9867static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9868 struct proxy *defpx, const char *file, int line,
9869 char **err)
9870{
9871 if (too_many_args(1, args, err, NULL))
9872 return -1;
9873
9874 if (*(args[1]) == 0) {
9875 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9876 return -1;
9877 }
9878
9879 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9880 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9881 return -1;
9882 }
9883 return 0;
9884}
9885
Willy Tarreau9ceda382016-12-21 23:13:03 +01009886/* parse "ssl.default-dh-param".
9887 * Returns <0 on alert, >0 on warning, 0 on success.
9888 */
9889static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9890 struct proxy *defpx, const char *file, int line,
9891 char **err)
9892{
9893 if (too_many_args(1, args, err, NULL))
9894 return -1;
9895
9896 if (*(args[1]) == 0) {
9897 memprintf(err, "'%s' expects an integer argument.", args[0]);
9898 return -1;
9899 }
9900
Willy Tarreauef934602016-12-22 23:12:01 +01009901 global_ssl.default_dh_param = atoi(args[1]);
9902 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009903 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9904 return -1;
9905 }
9906 return 0;
9907}
9908#endif
9909
9910
William Lallemand32af2032016-10-29 18:09:35 +02009911/* This function is used with TLS ticket keys management. It permits to browse
9912 * each reference. The variable <getnext> must contain the current node,
9913 * <end> point to the root node.
9914 */
9915#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9916static inline
9917struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
9918{
9919 struct tls_keys_ref *ref = getnext;
9920
9921 while (1) {
9922
9923 /* Get next list entry. */
9924 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
9925
9926 /* If the entry is the last of the list, return NULL. */
9927 if (&ref->list == end)
9928 return NULL;
9929
9930 return ref;
9931 }
9932}
9933
9934static inline
9935struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
9936{
9937 int id;
9938 char *error;
9939
9940 /* If the reference starts by a '#', this is numeric id. */
9941 if (reference[0] == '#') {
9942 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
9943 id = strtol(reference + 1, &error, 10);
9944 if (*error != '\0')
9945 return NULL;
9946
9947 /* Perform the unique id lookup. */
9948 return tlskeys_ref_lookupid(id);
9949 }
9950
9951 /* Perform the string lookup. */
9952 return tlskeys_ref_lookup(reference);
9953}
9954#endif
9955
9956
9957#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9958
9959static int cli_io_handler_tlskeys_files(struct appctx *appctx);
9960
9961static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
9962 return cli_io_handler_tlskeys_files(appctx);
9963}
9964
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009965/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
9966 * (next index to be dumped), and cli.p0 (next key reference).
9967 */
William Lallemand32af2032016-10-29 18:09:35 +02009968static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
9969
9970 struct stream_interface *si = appctx->owner;
9971
9972 switch (appctx->st2) {
9973 case STAT_ST_INIT:
9974 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08009975 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +02009976 * later and restart at the state "STAT_ST_INIT".
9977 */
9978 chunk_reset(&trash);
9979
9980 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
9981 chunk_appendf(&trash, "# id secret\n");
9982 else
9983 chunk_appendf(&trash, "# id (file)\n");
9984
Willy Tarreau06d80a92017-10-19 14:32:15 +02009985 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01009986 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009987 return 0;
9988 }
9989
William Lallemand32af2032016-10-29 18:09:35 +02009990 /* Now, we start the browsing of the references lists.
9991 * Note that the following call to LIST_ELEM return bad pointer. The only
9992 * available field of this pointer is <list>. It is used with the function
9993 * tlskeys_list_get_next() for retruning the first available entry
9994 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009995 if (appctx->ctx.cli.p0 == NULL) {
9996 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
9997 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009998 }
9999
10000 appctx->st2 = STAT_ST_LIST;
10001 /* fall through */
10002
10003 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010004 while (appctx->ctx.cli.p0) {
10005 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010006
10007 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010008 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010009 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010010
10011 if (appctx->ctx.cli.i1 == 0)
10012 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10013
William Lallemand32af2032016-10-29 18:09:35 +020010014 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010015 int head;
10016
10017 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10018 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010019 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010020 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010021
10022 chunk_reset(t2);
10023 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010024 if (ref->key_size_bits == 128) {
10025 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10026 sizeof(struct tls_sess_key_128),
10027 t2->area, t2->size);
10028 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10029 t2->area);
10030 }
10031 else if (ref->key_size_bits == 256) {
10032 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10033 sizeof(struct tls_sess_key_256),
10034 t2->area, t2->size);
10035 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10036 t2->area);
10037 }
10038 else {
10039 /* This case should never happen */
10040 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10041 }
William Lallemand32af2032016-10-29 18:09:35 +020010042
Willy Tarreau06d80a92017-10-19 14:32:15 +020010043 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010044 /* let's try again later from this stream. We add ourselves into
10045 * this stream's users so that it can remove us upon termination.
10046 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010047 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010048 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010049 return 0;
10050 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010051 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010052 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010053 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010054 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010055 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010056 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010057 /* let's try again later from this stream. We add ourselves into
10058 * this stream's users so that it can remove us upon termination.
10059 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010060 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010061 return 0;
10062 }
10063
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010064 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010065 break;
10066
10067 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010068 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010069 }
10070
10071 appctx->st2 = STAT_ST_FIN;
10072 /* fall through */
10073
10074 default:
10075 appctx->st2 = STAT_ST_FIN;
10076 return 1;
10077 }
10078 return 0;
10079}
10080
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010081/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010082static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010083{
William Lallemand32af2032016-10-29 18:09:35 +020010084 /* no parameter, shows only file list */
10085 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010086 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010087 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010088 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010089 }
10090
10091 if (args[2][0] == '*') {
10092 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010093 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010094 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010095 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010096 if (!appctx->ctx.cli.p0)
10097 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010098 }
William Lallemand32af2032016-10-29 18:09:35 +020010099 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010100 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010101}
10102
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010103static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010104{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010105 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010106 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010107
William Lallemand32af2032016-10-29 18:09:35 +020010108 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010109 if (!*args[3] || !*args[4])
10110 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 +020010111
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010112 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010113 if (!ref)
10114 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010115
Willy Tarreau1c913e42018-08-22 05:26:57 +020010116 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010117 if (ret < 0)
10118 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010119
Willy Tarreau1c913e42018-08-22 05:26:57 +020010120 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010121 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10122 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010123
Willy Tarreau9d008692019-08-09 11:21:01 +020010124 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010125}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010126#endif
William Lallemand32af2032016-10-29 18:09:35 +020010127
William Lallemand44b35322019-10-17 16:28:40 +020010128
10129/* Type of SSL payloads that can be updated over the CLI */
10130
10131enum {
10132 CERT_TYPE_PEM = 0,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010133#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010134 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010135#endif
William Lallemand44b35322019-10-17 16:28:40 +020010136 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010137#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010138 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010139#endif
William Lallemand44b35322019-10-17 16:28:40 +020010140 CERT_TYPE_MAX,
10141};
10142
10143struct {
10144 const char *ext;
10145 int type;
10146 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
10147 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020010148} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020010149 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand541a5342019-10-23 14:11:54 +020010150#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010151 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010152#endif
10153#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010154 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010155#endif
William Lallemand44b35322019-10-17 16:28:40 +020010156 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020010157 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020010158};
10159
William Lallemand430413e2019-10-28 14:30:47 +010010160/* states of the CLI IO handler for 'set ssl cert' */
10161enum {
10162 SETCERT_ST_INIT = 0,
10163 SETCERT_ST_GEN,
10164 SETCERT_ST_INSERT,
10165 SETCERT_ST_FIN,
10166};
William Lallemand8f840d72019-10-23 10:53:05 +020010167
William Lallemandd4f946c2019-12-05 10:26:40 +010010168/* release function of the `show ssl cert' command */
10169static void cli_release_show_cert(struct appctx *appctx)
10170{
10171 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10172}
10173
10174/* IO handler of "show ssl cert <filename>" */
10175static int cli_io_handler_show_cert(struct appctx *appctx)
10176{
10177 struct buffer *trash = alloc_trash_chunk();
10178 struct ebmb_node *node;
10179 struct stream_interface *si = appctx->owner;
10180 struct ckch_store *ckchs;
10181 int n;
10182
10183 if (trash == NULL)
10184 return 1;
10185
10186 if (!appctx->ctx.ssl.old_ckchs) {
10187 if (ckchs_transaction.old_ckchs) {
10188 ckchs = ckchs_transaction.old_ckchs;
10189 chunk_appendf(trash, "# transaction\n");
10190 if (!ckchs->multi) {
10191 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010192#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010193 } else {
10194 chunk_appendf(trash, "*%s:", ckchs->path);
10195 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10196 if (ckchs->ckch[n].cert)
10197 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10198 }
10199 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010200#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010201 }
10202 }
10203 }
10204
10205 if (!appctx->ctx.cli.p0) {
10206 chunk_appendf(trash, "# filename\n");
10207 node = ebmb_first(&ckchs_tree);
10208 } else {
10209 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
10210 }
10211 while (node) {
10212 ckchs = ebmb_entry(node, struct ckch_store, node);
10213 if (!ckchs->multi) {
10214 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010215#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010216 } else {
10217 chunk_appendf(trash, "%s:", ckchs->path);
10218 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10219 if (ckchs->ckch[n].cert)
10220 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10221 }
10222 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010223#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010224 }
10225
10226 node = ebmb_next(node);
10227 if (ci_putchk(si_ic(si), trash) == -1) {
10228 si_rx_room_blk(si);
10229 goto yield;
10230 }
10231 }
10232
10233 appctx->ctx.cli.p0 = NULL;
10234 free_trash_chunk(trash);
10235 return 1;
10236yield:
10237
10238 free_trash_chunk(trash);
10239 appctx->ctx.cli.p0 = ckchs;
10240 return 0; /* should come back */
10241}
10242
10243/* IO handler of the details "show ssl cert <filename>" */
10244static int cli_io_handler_show_cert_detail(struct appctx *appctx)
10245{
10246 struct stream_interface *si = appctx->owner;
10247 struct ckch_store *ckchs = appctx->ctx.cli.p0;
10248 struct buffer *out = alloc_trash_chunk();
10249 struct buffer *tmp = alloc_trash_chunk();
10250 X509_NAME *name = NULL;
10251 int write = -1;
10252 BIO *bio = NULL;
10253
10254 if (!tmp || !out)
10255 goto end;
10256
10257 if (!ckchs->multi) {
10258 chunk_appendf(out, "Filename: ");
10259 if (ckchs == ckchs_transaction.new_ckchs)
10260 chunk_appendf(out, "*");
10261 chunk_appendf(out, "%s\n", ckchs->path);
10262 chunk_appendf(out, "Serial: ");
10263 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
10264 goto end;
10265 dump_binary(out, tmp->area, tmp->data);
10266 chunk_appendf(out, "\n");
10267
10268 chunk_appendf(out, "notBefore: ");
10269 chunk_reset(tmp);
10270 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10271 goto end;
10272 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
10273 goto end;
10274 write = BIO_read(bio, tmp->area, tmp->size-1);
10275 tmp->area[write] = '\0';
10276 BIO_free(bio);
10277 chunk_appendf(out, "%s\n", tmp->area);
10278
10279 chunk_appendf(out, "notAfter: ");
10280 chunk_reset(tmp);
10281 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10282 goto end;
10283 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
10284 goto end;
10285 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
10286 goto end;
10287 tmp->area[write] = '\0';
10288 BIO_free(bio);
10289 chunk_appendf(out, "%s\n", tmp->area);
10290
10291
10292 chunk_appendf(out, "Issuer: ");
10293 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
10294 goto end;
10295 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10296 goto end;
10297 *(tmp->area + tmp->data) = '\0';
10298 chunk_appendf(out, "%s\n", tmp->area);
10299
10300 chunk_appendf(out, "Subject: ");
10301 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
10302 goto end;
10303 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10304 goto end;
10305 *(tmp->area + tmp->data) = '\0';
10306 chunk_appendf(out, "%s\n", tmp->area);
10307
10308
10309#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10310 chunk_appendf(out, "Subject Alternative Name: ");
10311 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
10312 goto end;
10313 *(out->area + out->data) = '\0';
10314 chunk_appendf(out, "\n");
10315#endif
10316 chunk_reset(tmp);
10317 chunk_appendf(out, "Algorithm: ");
10318 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
10319 goto end;
10320 chunk_appendf(out, "%s\n", tmp->area);
10321
10322 chunk_reset(tmp);
10323 chunk_appendf(out, "SHA1 FingerPrint: ");
10324 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area,
10325 (unsigned int *)&tmp->data) == 0)
10326 goto end;
10327 dump_binary(out, tmp->area, tmp->data);
10328 chunk_appendf(out, "\n");
10329 }
10330
10331 if (ci_putchk(si_ic(si), out) == -1) {
10332 si_rx_room_blk(si);
10333 goto yield;
10334 }
10335
10336end:
10337 free_trash_chunk(tmp);
10338 free_trash_chunk(out);
10339 return 1;
10340yield:
10341 free_trash_chunk(tmp);
10342 free_trash_chunk(out);
10343 return 0; /* should come back */
10344}
10345
10346/* parsing function for 'show ssl cert [certfile]' */
10347static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
10348{
10349 struct ckch_store *ckchs;
10350
10351 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
10352 return cli_err(appctx, "Can't allocate memory!\n");
10353
10354 /* The operations on the CKCH architecture are locked so we can
10355 * manipulate ckch_store and ckch_inst */
10356 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10357 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
10358
10359 /* check if there is a certificate to lookup */
10360 if (*args[3]) {
10361 if (*args[3] == '*') {
10362 if (!ckchs_transaction.new_ckchs)
10363 goto error;
10364
10365 ckchs = ckchs_transaction.new_ckchs;
10366
10367 if (strcmp(args[3] + 1, ckchs->path))
10368 goto error;
10369
10370 } else {
10371 if ((ckchs = ckchs_lookup(args[3])) == NULL)
10372 goto error;
10373
10374 }
10375
10376 if (ckchs->multi)
10377 goto error;
10378
10379 appctx->ctx.cli.p0 = ckchs;
10380 /* use the IO handler that shows details */
10381 appctx->io_handler = cli_io_handler_show_cert_detail;
10382 }
10383
10384 return 0;
10385
10386error:
10387 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10388 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
10389}
10390
William Lallemand430413e2019-10-28 14:30:47 +010010391/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010392static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010393{
10394 struct ckch_store *new_ckchs;
10395 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010396
William Lallemand430413e2019-10-28 14:30:47 +010010397 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020010398
William Lallemand430413e2019-10-28 14:30:47 +010010399 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020010400 /* 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 +010010401 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010402
William Lallemandbeea2a42019-10-30 17:45:33 +010010403 if (!new_ckchs)
10404 return;
William Lallemand8f840d72019-10-23 10:53:05 +020010405
William Lallemandbeea2a42019-10-30 17:45:33 +010010406 /* if the allocation failed, we need to free everything from the temporary list */
10407 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10408 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010409
William Lallemandbeea2a42019-10-30 17:45:33 +010010410 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10411 if (sc0->order == 0) /* we only free if it's the first inserted */
10412 SSL_CTX_free(sc0->ctx);
10413 LIST_DEL(&sc0->by_ckch_inst);
10414 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020010415 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010416 LIST_DEL(&ckchi->by_ckchs);
10417 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020010418 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010419 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010420 }
10421}
10422
10423
10424/*
10425 * This function tries to create the new ckch_inst and their SNIs
10426 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010427static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010428{
10429 struct stream_interface *si = appctx->owner;
10430 int y = 0;
10431 char *err = NULL;
10432 int errcode = 0;
10433 struct ckch_store *old_ckchs, *new_ckchs = NULL;
10434 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010435 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010436 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010437
William Lallemand33cc76f2019-10-31 11:43:45 +010010438 if (trash == NULL)
10439 goto error;
10440
William Lallemand8f840d72019-10-23 10:53:05 +020010441 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
10442 goto error;
10443
William Lallemand430413e2019-10-28 14:30:47 +010010444 while (1) {
10445 switch (appctx->st2) {
10446 case SETCERT_ST_INIT:
10447 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010448 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010010449 if (ci_putchk(si_ic(si), trash) == -1) {
10450 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020010451 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010010452 }
10453 appctx->st2 = SETCERT_ST_GEN;
10454 /* fallthrough */
10455 case SETCERT_ST_GEN:
10456 /*
10457 * This state generates the ckch instances with their
10458 * sni_ctxs and SSL_CTX.
10459 *
William Lallemand430413e2019-10-28 14:30:47 +010010460 * Since the SSL_CTX generation can be CPU consumer, we
10461 * yield every 10 instances.
10462 */
William Lallemand8f840d72019-10-23 10:53:05 +020010463
William Lallemandbeea2a42019-10-30 17:45:33 +010010464 old_ckchs = appctx->ctx.ssl.old_ckchs;
10465 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010466
William Lallemandbeea2a42019-10-30 17:45:33 +010010467 if (!new_ckchs)
10468 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020010469
William Lallemandbeea2a42019-10-30 17:45:33 +010010470 /* get the next ckchi to regenerate */
10471 ckchi = appctx->ctx.ssl.next_ckchi;
10472 /* we didn't start yet, set it to the first elem */
10473 if (ckchi == NULL)
10474 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010475
William Lallemandbeea2a42019-10-30 17:45:33 +010010476 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
10477 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
10478 struct ckch_inst *new_inst;
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010479 int verify = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020010480
William Lallemandbeea2a42019-10-30 17:45:33 +010010481 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
10482 if (y >= 10) {
10483 /* save the next ckchi to compute */
10484 appctx->ctx.ssl.next_ckchi = ckchi;
10485 goto yield;
10486 }
William Lallemand8f840d72019-10-23 10:53:05 +020010487
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010488 /* prevent ssl_sock_prepare_ctx() to do file access which is only for verify (crl/ca file) */
10489 verify = (ckchi->ssl_conf && ckchi->ssl_conf->verify) ? ckchi->ssl_conf->verify : ckchi->bind_conf->ssl_conf.verify;
10490 if (verify & SSL_VERIFY_PEER) {
10491 memprintf(&err, "%sCan't commit a certificate which use the 'verify' bind SSL option [%s:%d]\n", err ? err : "", ckchi->bind_conf->file, ckchi->bind_conf->line);
10492 errcode |= ERR_FATAL | ERR_ABORT;
10493 goto error;
10494 }
10495
10496
William Lallemandbeea2a42019-10-30 17:45:33 +010010497 if (new_ckchs->multi)
10498 errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
10499 else
10500 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 +020010501
William Lallemandbeea2a42019-10-30 17:45:33 +010010502 if (errcode & ERR_CODE)
10503 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020010504
William Lallemand21724f02019-11-04 17:56:13 +010010505 /* if the previous ckchi was used as the default */
10506 if (ckchi->is_default)
10507 new_inst->is_default = 1;
10508
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010509 /* we need to initialize the SSL_CTX generated */
10510 /* TODO: the prepare_ctx function need to be reworked to be safer there */
10511 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10512 if (!sc0->order) { /* we initiliazed only the first SSL_CTX because it's the same in the other sni_ctx's */
10513 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
10514 if (errcode & ERR_CODE)
10515 goto error;
10516 }
10517 }
10518
10519
William Lallemandbeea2a42019-10-30 17:45:33 +010010520 /* display one dot per new instance */
10521 chunk_appendf(trash, ".");
10522 /* link the new ckch_inst to the duplicate */
10523 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
10524 y++;
10525 }
William Lallemand430413e2019-10-28 14:30:47 +010010526 appctx->st2 = SETCERT_ST_INSERT;
10527 /* fallthrough */
10528 case SETCERT_ST_INSERT:
10529 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020010530
William Lallemandbeea2a42019-10-30 17:45:33 +010010531 old_ckchs = appctx->ctx.ssl.old_ckchs;
10532 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010533
William Lallemandbeea2a42019-10-30 17:45:33 +010010534 if (!new_ckchs)
10535 continue;
William Lallemand430413e2019-10-28 14:30:47 +010010536
William Lallemand21724f02019-11-04 17:56:13 +010010537 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010010538 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10539 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10540 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
10541 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10542 }
William Lallemand8f840d72019-10-23 10:53:05 +020010543
William Lallemandbeea2a42019-10-30 17:45:33 +010010544 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
10545 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemand430413e2019-10-28 14:30:47 +010010546
William Lallemandbeea2a42019-10-30 17:45:33 +010010547 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10548 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10549 ebmb_delete(&sc0->name);
10550 LIST_DEL(&sc0->by_ckch_inst);
10551 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010010552 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010553 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10554 LIST_DEL(&ckchi->by_ckchs);
10555 free(ckchi);
10556 }
William Lallemand8f840d72019-10-23 10:53:05 +020010557
William Lallemandbeea2a42019-10-30 17:45:33 +010010558 /* Replace the old ckchs by the new one */
10559 ebmb_delete(&old_ckchs->node);
10560 ckchs_free(old_ckchs);
10561 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010010562 appctx->st2 = SETCERT_ST_FIN;
10563 /* fallthrough */
10564 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010565 /* we achieved the transaction, we can set everything to NULL */
10566 free(ckchs_transaction.path);
10567 ckchs_transaction.path = NULL;
10568 ckchs_transaction.new_ckchs = NULL;
10569 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010010570 goto end;
10571 }
William Lallemand8f840d72019-10-23 10:53:05 +020010572 }
William Lallemand430413e2019-10-28 14:30:47 +010010573end:
William Lallemand8f840d72019-10-23 10:53:05 +020010574
William Lallemanded442432019-11-21 16:41:07 +010010575 chunk_appendf(trash, "\n");
10576 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010010577 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010010578 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010010579 if (ci_putchk(si_ic(si), trash) == -1)
10580 si_rx_room_blk(si);
10581 free_trash_chunk(trash);
10582 /* success: call the release function and don't come back */
10583 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010584yield:
10585 /* store the state */
10586 if (ci_putchk(si_ic(si), trash) == -1)
10587 si_rx_room_blk(si);
10588 free_trash_chunk(trash);
10589 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020010590 return 0; /* should come back */
10591
10592error:
10593 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010010594 if (trash) {
10595 chunk_appendf(trash, "\n%sFailed!\n", err);
10596 if (ci_putchk(si_ic(si), trash) == -1)
10597 si_rx_room_blk(si);
10598 free_trash_chunk(trash);
10599 }
William Lallemand430413e2019-10-28 14:30:47 +010010600 /* error: call the release function and don't come back */
10601 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010602}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010603
10604/*
10605 * Parsing function of 'commit ssl cert'
10606 */
10607static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
10608{
10609 char *err = NULL;
10610
William Lallemand230662a2019-12-03 13:32:54 +010010611 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10612 return 1;
10613
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010614 if (!*args[3])
10615 return cli_err(appctx, "'commit ssl cert expects a filename\n");
10616
10617 /* The operations on the CKCH architecture are locked so we can
10618 * manipulate ckch_store and ckch_inst */
10619 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10620 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
10621
10622 if (!ckchs_transaction.path) {
10623 memprintf(&err, "No ongoing transaction! !\n");
10624 goto error;
10625 }
10626
10627 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10628 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
10629 goto error;
10630 }
10631
10632 /* init the appctx structure */
10633 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010634 appctx->ctx.ssl.next_ckchi = NULL;
10635 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
10636 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
10637
10638 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
10639 return 0;
10640
10641error:
10642
10643 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10644 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
10645
10646 return cli_dynerr(appctx, err);
10647}
10648
10649
William Lallemand8f840d72019-10-23 10:53:05 +020010650/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010651 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020010652 */
William Lallemand150bfa82019-09-19 17:12:49 +020010653static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
10654{
William Lallemand0c3b7d92019-10-18 11:27:07 +020010655 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020010656 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020010657 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020010658 int i;
William Lallemand849eed62019-10-17 16:23:50 +020010659 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010660 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020010661 char *end;
10662 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010663 struct cert_key_and_chain *ckch;
10664 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020010665
William Lallemand230662a2019-12-03 13:32:54 +010010666 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10667 return 1;
10668
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010669 if ((buf = alloc_trash_chunk()) == NULL)
10670 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020010671
10672 if (!*args[3] || !payload)
10673 return cli_err(appctx, "'set ssl cert expects a filename and a certificat as a payload\n");
10674
10675 /* The operations on the CKCH architecture are locked so we can
10676 * manipulate ckch_store and ckch_inst */
10677 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10678 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
10679
William Lallemand8f840d72019-10-23 10:53:05 +020010680 if (!chunk_strcpy(buf, args[3])) {
10681 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10682 errcode |= ERR_ALERT | ERR_FATAL;
10683 goto end;
10684 }
10685
William Lallemand44b35322019-10-17 16:28:40 +020010686 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020010687 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020010688 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020010689 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
10690 *end = '\0';
10691 type = cert_exts[i].type;
10692 break;
10693 }
10694 }
10695
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010696 appctx->ctx.ssl.old_ckchs = NULL;
10697 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020010698
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010699 /* if there is an ongoing transaction */
10700 if (ckchs_transaction.path) {
10701 /* 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 +020010702#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010703 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010704 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020010705 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020010706
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010707 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020010708 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020010709 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010710 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020010711 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10712 bundle = j; /* keep the type of certificate so we insert it at the right place */
10713 *end = '\0'; /* it's a bundle let's end the string*/
10714 break;
10715 }
William Lallemand150bfa82019-09-19 17:12:49 +020010716 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010717 if (bundle < 0) {
10718 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);
10719 errcode |= ERR_ALERT | ERR_FATAL;
10720 goto end;
10721 }
10722 }
10723#endif
10724
10725 /* if there is an ongoing transaction, check if this is the same file */
10726 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
10727 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
10728 errcode |= ERR_ALERT | ERR_FATAL;
10729 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010730 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010731
10732 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
10733
10734 } else {
10735 struct ckch_store *find_ckchs[2] = { NULL, NULL };
10736
10737 /* lookup for the certificate in the tree:
10738 * check if this is used as a bundle AND as a unique certificate */
10739 for (i = 0; i < 2; i++) {
10740
10741 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
10742 /* only the bundle name is in the tree and you should
10743 * never update a bundle name, only a filename */
10744 if (bundle < 0 && find_ckchs[i]->multi) {
10745 /* we tried to look for a non-bundle and we found a bundle */
10746 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
10747 err ? err : "", args[3], args[3]);
10748 errcode |= ERR_ALERT | ERR_FATAL;
10749 goto end;
10750 }
William Lallemand3246d942019-11-04 14:02:11 +010010751 /* If we want a bundle but this is not a bundle
10752 * example: When you try to update <file>.rsa, but
10753 * <file> is a regular file */
10754 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
10755 find_ckchs[i] = NULL;
10756 break;
10757 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010758 }
10759#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
10760 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010761 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010762 int j;
10763
10764 /* check if it was used in a bundle by removing the
10765 * .dsa/.rsa/.ecdsa at the end of the filename */
10766 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010767 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010768 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10769 bundle = j; /* keep the type of certificate so we insert it at the right place */
10770 *end = '\0'; /* it's a bundle let's end the string*/
10771 break;
10772 }
10773 }
William Lallemand37031b82019-11-04 13:38:53 +010010774 if (bundle < 0) /* we didn't find a bundle extension */
10775 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010776 }
William Lallemand963b2e72019-10-14 11:38:36 +020010777#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010778 /* bundles are not supported here, so we don't need to lookup again */
10779 break;
William Lallemand963b2e72019-10-14 11:38:36 +020010780#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010781 }
10782
10783 if (find_ckchs[0] && find_ckchs[1]) {
10784 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",
10785 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
10786 errcode |= ERR_ALERT | ERR_FATAL;
10787 goto end;
10788 }
10789
10790 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020010791 }
10792
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010793 if (!appctx->ctx.ssl.old_ckchs) {
10794 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020010795 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010796 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020010797 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010798 }
10799
William Lallemand8a7fdf02019-11-04 10:59:32 +010010800 if (!appctx->ctx.ssl.path) {
10801 /* this is a new transaction, set the path of the transaction */
10802 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
10803 if (!appctx->ctx.ssl.path) {
10804 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10805 errcode |= ERR_ALERT | ERR_FATAL;
10806 goto end;
10807 }
10808 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010809
10810 old_ckchs = appctx->ctx.ssl.old_ckchs;
10811
10812 /* TODO: handle filters */
10813 if (old_ckchs->filters) {
10814 memprintf(&err, "%sCertificates used in crt-list with filters are not supported!\n",
10815 err ? err : "");
10816 errcode |= ERR_ALERT | ERR_FATAL;
10817 goto end;
10818 }
10819
10820 /* duplicate the ckch store */
10821 new_ckchs = ckchs_dup(old_ckchs);
10822 if (!new_ckchs) {
10823 memprintf(&err, "%sCannot allocate memory!\n",
10824 err ? err : "");
10825 errcode |= ERR_ALERT | ERR_FATAL;
10826 goto end;
10827 }
10828
10829 if (!new_ckchs->multi)
10830 ckch = new_ckchs->ckch;
10831 else
10832 ckch = &new_ckchs->ckch[bundle];
10833
10834 /* appply the change on the duplicate */
10835 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
10836 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
10837 errcode |= ERR_ALERT | ERR_FATAL;
10838 goto end;
10839 }
10840
10841 appctx->ctx.ssl.new_ckchs = new_ckchs;
10842
10843 /* we succeed, we can save the ckchs in the transaction */
10844
10845 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010010846 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010847 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
10848 ckchs_transaction.path = appctx->ctx.ssl.path;
10849 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
10850 } else {
10851 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
10852
10853 }
10854
10855 /* free the previous ckchs if there was a transaction */
10856 ckchs_free(ckchs_transaction.new_ckchs);
10857
10858 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
10859
10860
William Lallemand8f840d72019-10-23 10:53:05 +020010861 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010862
William Lallemand8f840d72019-10-23 10:53:05 +020010863end:
10864 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020010865
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010866 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010867
10868 ckchs_free(appctx->ctx.ssl.new_ckchs);
10869 appctx->ctx.ssl.new_ckchs = NULL;
10870
10871 appctx->ctx.ssl.old_ckchs = NULL;
10872
10873 free(appctx->ctx.ssl.path);
10874 appctx->ctx.ssl.path = NULL;
10875
10876 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020010877 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010010878 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010879
10880 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10881 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010010882 }
William Lallemand8f840d72019-10-23 10:53:05 +020010883 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020010884}
10885
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010886/* parsing function of 'abort ssl cert' */
10887static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
10888{
10889 char *err = NULL;
10890
William Lallemand230662a2019-12-03 13:32:54 +010010891 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10892 return 1;
10893
William Lallemand0bc9c8a2019-11-19 15:51:51 +010010894 if (!*args[3])
10895 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
10896
10897 /* The operations on the CKCH architecture are locked so we can
10898 * manipulate ckch_store and ckch_inst */
10899 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10900 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
10901
10902 if (!ckchs_transaction.path) {
10903 memprintf(&err, "No ongoing transaction!\n");
10904 goto error;
10905 }
10906
10907 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10908 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
10909 goto error;
10910 }
10911
10912 /* Only free the ckchs there, because the SNI and instances were not generated yet */
10913 ckchs_free(ckchs_transaction.new_ckchs);
10914 ckchs_transaction.new_ckchs = NULL;
10915 ckchs_free(ckchs_transaction.old_ckchs);
10916 ckchs_transaction.old_ckchs = NULL;
10917 free(ckchs_transaction.path);
10918 ckchs_transaction.path = NULL;
10919
10920 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10921
10922 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
10923 return cli_dynmsg(appctx, LOG_NOTICE, err);
10924
10925error:
10926 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10927
10928 return cli_dynerr(appctx, err);
10929}
10930
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010931static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010932{
10933#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
10934 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010935 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010936
10937 if (!payload)
10938 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020010939
10940 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010941 if (!*payload)
10942 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020010943
10944 /* remove \r and \n from the payload */
10945 for (i = 0, j = 0; payload[i]; i++) {
10946 if (payload[i] == '\r' || payload[i] == '\n')
10947 continue;
10948 payload[j++] = payload[i];
10949 }
10950 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010951
Willy Tarreau1c913e42018-08-22 05:26:57 +020010952 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010953 if (ret < 0)
10954 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010955
Willy Tarreau1c913e42018-08-22 05:26:57 +020010956 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020010957 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020010958 if (err)
10959 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
10960 else
10961 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010962 }
Willy Tarreau9d008692019-08-09 11:21:01 +020010963
10964 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010965#else
Willy Tarreau9d008692019-08-09 11:21:01 +020010966 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 +020010967#endif
10968
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010969}
10970
Willy Tarreau86a394e2019-05-09 14:15:32 +020010971#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010010972static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
10973{
10974 switch (arg->type) {
10975 case ARGT_STR:
10976 smp->data.type = SMP_T_STR;
10977 smp->data.u.str = arg->data.str;
10978 return 1;
10979 case ARGT_VAR:
10980 if (!vars_get_by_desc(&arg->data.var, smp))
10981 return 0;
10982 if (!sample_casts[smp->data.type][SMP_T_STR])
10983 return 0;
10984 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
10985 return 0;
10986 return 1;
10987 default:
10988 return 0;
10989 }
10990}
10991
10992static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
10993 const char *file, int line, char **err)
10994{
10995 switch(args[0].data.sint) {
10996 case 128:
10997 case 192:
10998 case 256:
10999 break;
11000 default:
11001 memprintf(err, "key size must be 128, 192 or 256 (bits).");
11002 return 0;
11003 }
11004 /* Try to decode a variable. */
11005 vars_check_arg(&args[1], NULL);
11006 vars_check_arg(&args[2], NULL);
11007 vars_check_arg(&args[3], NULL);
11008 return 1;
11009}
11010
11011/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
11012static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
11013{
11014 struct sample nonce, key, aead_tag;
11015 struct buffer *smp_trash, *smp_trash_alloc;
11016 EVP_CIPHER_CTX *ctx;
11017 int dec_size, ret;
11018
11019 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
11020 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
11021 return 0;
11022
11023 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
11024 if (!sample_conv_var2smp_str(&arg_p[2], &key))
11025 return 0;
11026
11027 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
11028 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
11029 return 0;
11030
11031 smp_trash = get_trash_chunk();
11032 smp_trash_alloc = alloc_trash_chunk();
11033 if (!smp_trash_alloc)
11034 return 0;
11035
11036 ctx = EVP_CIPHER_CTX_new();
11037
11038 if (!ctx)
11039 goto err;
11040
11041 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
11042 if (dec_size < 0)
11043 goto err;
11044 smp_trash->data = dec_size;
11045
11046 /* Set cipher type and mode */
11047 switch(arg_p[0].data.sint) {
11048 case 128:
11049 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
11050 break;
11051 case 192:
11052 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
11053 break;
11054 case 256:
11055 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
11056 break;
11057 }
11058
11059 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
11060
11061 /* Initialise IV */
11062 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
11063 goto err;
11064
11065 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
11066 if (dec_size < 0)
11067 goto err;
11068 smp_trash->data = dec_size;
11069
11070 /* Initialise key */
11071 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
11072 goto err;
11073
11074 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
11075 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
11076 goto err;
11077
11078 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
11079 if (dec_size < 0)
11080 goto err;
11081 smp_trash_alloc->data = dec_size;
11082 dec_size = smp_trash->data;
11083
11084 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
11085 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
11086
11087 if (ret <= 0)
11088 goto err;
11089
11090 smp->data.u.str.data = dec_size + smp_trash->data;
11091 smp->data.u.str.area = smp_trash->area;
11092 smp->data.type = SMP_T_BIN;
11093 smp->flags &= ~SMP_F_CONST;
11094 free_trash_chunk(smp_trash_alloc);
11095 return 1;
11096
11097err:
11098 free_trash_chunk(smp_trash_alloc);
11099 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020011100}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011101# endif
William Lallemand32af2032016-10-29 18:09:35 +020011102
11103/* register cli keywords */
11104static struct cli_kw_list cli_kws = {{ },{
11105#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11106 { { "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 +020011107 { { "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 +020011108#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010011109 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011110 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
11111 { { "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 +010011112 { { "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 +010011113 { { "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 +020011114 { { NULL }, NULL, NULL, NULL }
11115}};
11116
Willy Tarreau0108d902018-11-25 19:14:37 +010011117INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020011118
Willy Tarreau7875d092012-09-10 08:20:03 +020011119/* Note: must not be declared <const> as its list will be overwritten.
11120 * Please take care of keeping this list alphabetically sorted.
11121 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011122static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020011123 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011124 { "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 +010011125#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010011126 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011127#endif
Emeric Brun645ae792014-04-30 14:21:06 +020011128 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011129#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
11130 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
11131#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010011132 { "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 +020011133 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020011134 { "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 +020011135 { "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 +020011136#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020011137 { "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 -040011138#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011139#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011140 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11141 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040011142 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11143#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011144 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11145 { "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 +010011146 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011147 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020011148 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
11149 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11150 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11151 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11152 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11153 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
11154 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11155 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011156 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011157 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11158 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010011159 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020011160 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
11161 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11162 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11163 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11164 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11165 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
11166 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020011167 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011168 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011169 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011170 { "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 +010011171 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011172 { "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 +020011173 { "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 +010011174 { "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 +020011175 { "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 +010011176#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011177 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020011178#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010011179#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011180 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020011181#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011182 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011183#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020011184 { "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 -040011185#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011186 { "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 +020011187#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011188 { "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 -040011189#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011190#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011191 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11192 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040011193 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11194#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040011195#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011196 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040011197#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011198 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11199 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11200 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11201 { "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 +020011202 { NULL, NULL, 0, 0, 0 },
11203}};
11204
Willy Tarreau0108d902018-11-25 19:14:37 +010011205INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
11206
Willy Tarreau7875d092012-09-10 08:20:03 +020011207/* Note: must not be declared <const> as its list will be overwritten.
11208 * Please take care of keeping this list alphabetically sorted.
11209 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011210static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011211 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
11212 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011213 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020011214}};
11215
Willy Tarreau0108d902018-11-25 19:14:37 +010011216INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
11217
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011218/* Note: must not be declared <const> as its list will be overwritten.
11219 * Please take care of keeping this list alphabetically sorted, doing so helps
11220 * all code contributors.
11221 * Optional keywords are also declared with a NULL ->parse() function so that
11222 * the config parser can report an appropriate error when a known keyword was
11223 * not enabled.
11224 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011225static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011226 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011227 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11228 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11229 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011230#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011231 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11232#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011233 { "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 +010011234 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011235 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011236 { "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 +010011237 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020011238 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
11239 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011240 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
11241 { NULL, NULL, 0 },
11242};
11243
Willy Tarreau0108d902018-11-25 19:14:37 +010011244/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
11245
Willy Tarreau51fb7652012-09-18 18:24:39 +020011246static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011247 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011248 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11249 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11250 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
11251 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
11252 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
11253 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011254#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011255 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11256#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011257 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
11258 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
11259 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
11260 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
11261 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
11262 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
11263 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
11264 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
11265 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
11266 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011267 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011268 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011269 { "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 +020011270 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
11271 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
11272 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
11273 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011274 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011275 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
11276 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011277 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
11278 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011279 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
11280 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
11281 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
11282 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
11283 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011284 { NULL, NULL, 0 },
11285}};
Emeric Brun46591952012-05-18 15:47:34 +020011286
Willy Tarreau0108d902018-11-25 19:14:37 +010011287INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
11288
Willy Tarreau92faadf2012-10-10 23:04:25 +020011289/* Note: must not be declared <const> as its list will be overwritten.
11290 * Please take care of keeping this list alphabetically sorted, doing so helps
11291 * all code contributors.
11292 * Optional keywords are also declared with a NULL ->parse() function so that
11293 * the config parser can report an appropriate error when a known keyword was
11294 * not enabled.
11295 */
11296static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010011297 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010011298 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011299 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010011300 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020011301 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011302 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
11303 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011304#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011305 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
11306#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011307 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
11308 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
11309 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
11310 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
11311 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
11312 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
11313 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
11314 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
11315 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
11316 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
11317 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
11318 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
11319 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
11320 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
11321 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
11322 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
11323 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
11324 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010011325 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011326 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
11327 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
11328 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
11329 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
11330 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
11331 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
11332 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
11333 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
11334 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
11335 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020011336 { NULL, NULL, 0, 0 },
11337}};
11338
Willy Tarreau0108d902018-11-25 19:14:37 +010011339INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
11340
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011341static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010011342 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
11343 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010011344 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011345 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
11346 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010011347#ifndef OPENSSL_NO_DH
11348 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
11349#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011350 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011351#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011352 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011353#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010011354 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
11355#ifndef OPENSSL_NO_DH
11356 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
11357#endif
11358 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
11359 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
11360 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
11361 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011362 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010011363 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
11364 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011365#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011366 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
11367 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
11368#endif
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011369 { 0, NULL, NULL },
11370}};
11371
Willy Tarreau0108d902018-11-25 19:14:37 +010011372INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
11373
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011374/* Note: must not be declared <const> as its list will be overwritten */
11375static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020011376#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011377 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
11378#endif
11379 { NULL, NULL, 0, 0, 0 },
11380}};
11381
11382INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
11383
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020011384/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010011385static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020011386 .snd_buf = ssl_sock_from_buf,
11387 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010011388 .subscribe = ssl_subscribe,
11389 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020011390 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020011391 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020011392 .rcv_pipe = NULL,
11393 .snd_pipe = NULL,
11394 .shutr = NULL,
11395 .shutw = ssl_sock_shutw,
11396 .close = ssl_sock_close,
11397 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010011398 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010011399 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010011400 .prepare_srv = ssl_sock_prepare_srv_ctx,
11401 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010011402 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010011403 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020011404};
11405
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011406enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
11407 struct session *sess, struct stream *s, int flags)
11408{
11409 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011410 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011411
11412 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011413 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011414
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011415 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011416 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011417 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011418 s->req.flags |= CF_READ_NULL;
11419 return ACT_RET_YIELD;
11420 }
11421 }
11422 return (ACT_RET_CONT);
11423}
11424
11425static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
11426{
11427 rule->action_ptr = ssl_action_wait_for_hs;
11428
11429 return ACT_RET_PRS_OK;
11430}
11431
11432static struct action_kw_list http_req_actions = {ILH, {
11433 { "wait-for-handshake", ssl_parse_wait_for_hs },
11434 { /* END */ }
11435}};
11436
Willy Tarreau0108d902018-11-25 19:14:37 +010011437INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
11438
Willy Tarreau5db847a2019-05-09 14:13:35 +020011439#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011440
11441static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11442{
11443 if (ptr) {
11444 chunk_destroy(ptr);
11445 free(ptr);
11446 }
11447}
11448
11449#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011450static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11451{
Willy Tarreaubafbe012017-11-24 17:34:44 +010011452 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011453}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011454
Emeric Brun46591952012-05-18 15:47:34 +020011455__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020011456static void __ssl_sock_init(void)
11457{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011458#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011459 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011460 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011461#endif
Emeric Brun46591952012-05-18 15:47:34 +020011462
Willy Tarreauef934602016-12-22 23:12:01 +010011463 if (global_ssl.listen_default_ciphers)
11464 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
11465 if (global_ssl.connect_default_ciphers)
11466 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011467#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011468 if (global_ssl.listen_default_ciphersuites)
11469 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
11470 if (global_ssl.connect_default_ciphersuites)
11471 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
11472#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010011473
Willy Tarreau13e14102016-12-22 20:25:26 +010011474 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011475#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020011476 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080011477#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011478#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011479 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011480 n = sk_SSL_COMP_num(cm);
11481 while (n--) {
11482 (void) sk_SSL_COMP_pop(cm);
11483 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011484#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011485
Willy Tarreau5db847a2019-05-09 14:13:35 +020011486#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011487 ssl_locking_init();
11488#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020011489#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011490 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
11491#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020011492 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020011493 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 +020011494#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011495 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011496 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011497#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010011498#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11499 hap_register_post_check(tlskeys_finalize_config);
11500#endif
Willy Tarreau80713382018-11-26 10:19:54 +010011501
11502 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
11503 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
11504
11505#ifndef OPENSSL_NO_DH
11506 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
11507 hap_register_post_deinit(ssl_free_dh);
11508#endif
11509#ifndef OPENSSL_NO_ENGINE
11510 hap_register_post_deinit(ssl_free_engines);
11511#endif
11512 /* Load SSL string for the verbose & debug mode. */
11513 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020011514 ha_meth = BIO_meth_new(0x666, "ha methods");
11515 BIO_meth_set_write(ha_meth, ha_ssl_write);
11516 BIO_meth_set_read(ha_meth, ha_ssl_read);
11517 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
11518 BIO_meth_set_create(ha_meth, ha_ssl_new);
11519 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
11520 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
11521 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020011522
11523 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010011524}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010011525
Willy Tarreau80713382018-11-26 10:19:54 +010011526/* Compute and register the version string */
11527static void ssl_register_build_options()
11528{
11529 char *ptr = NULL;
11530 int i;
11531
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011532 memprintf(&ptr, "Built with OpenSSL version : "
11533#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011534 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011535#else /* OPENSSL_IS_BORINGSSL */
11536 OPENSSL_VERSION_TEXT
11537 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080011538 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020011539 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011540#endif
11541 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011542#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011543 "no (library version too old)"
11544#elif defined(OPENSSL_NO_TLSEXT)
11545 "no (disabled via OPENSSL_NO_TLSEXT)"
11546#else
11547 "yes"
11548#endif
11549 "", ptr);
11550
11551 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
11552#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11553 "yes"
11554#else
11555#ifdef OPENSSL_NO_TLSEXT
11556 "no (because of OPENSSL_NO_TLSEXT)"
11557#else
11558 "no (version might be too old, 0.9.8f min needed)"
11559#endif
11560#endif
11561 "", ptr);
11562
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020011563 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
11564 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
11565 if (methodVersions[i].option)
11566 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011567
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011568 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010011569}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011570
Willy Tarreau80713382018-11-26 10:19:54 +010011571INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020011572
Emeric Brun46591952012-05-18 15:47:34 +020011573
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011574#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011575void ssl_free_engines(void) {
11576 struct ssl_engine_list *wl, *wlb;
11577 /* free up engine list */
11578 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
11579 ENGINE_finish(wl->e);
11580 ENGINE_free(wl->e);
11581 LIST_DEL(&wl->list);
11582 free(wl);
11583 }
11584}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011585#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020011586
Remi Gacogned3a23c32015-05-28 16:39:47 +020011587#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000011588void ssl_free_dh(void) {
11589 if (local_dh_1024) {
11590 DH_free(local_dh_1024);
11591 local_dh_1024 = NULL;
11592 }
11593 if (local_dh_2048) {
11594 DH_free(local_dh_2048);
11595 local_dh_2048 = NULL;
11596 }
11597 if (local_dh_4096) {
11598 DH_free(local_dh_4096);
11599 local_dh_4096 = NULL;
11600 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020011601 if (global_dh) {
11602 DH_free(global_dh);
11603 global_dh = NULL;
11604 }
Grant Zhang872f9c22017-01-21 01:10:18 +000011605}
11606#endif
11607
11608__attribute__((destructor))
11609static void __ssl_sock_deinit(void)
11610{
11611#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011612 if (ssl_ctx_lru_tree) {
11613 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010011614 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020011615 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020011616#endif
11617
Willy Tarreau5db847a2019-05-09 14:13:35 +020011618#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011619 ERR_remove_state(0);
11620 ERR_free_strings();
11621
11622 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080011623#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020011624
Willy Tarreau5db847a2019-05-09 14:13:35 +020011625#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011626 CRYPTO_cleanup_all_ex_data();
11627#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020011628 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020011629}
11630
11631
Emeric Brun46591952012-05-18 15:47:34 +020011632/*
11633 * Local variables:
11634 * c-indent-level: 8
11635 * c-basic-offset: 8
11636 * End:
11637 */