blob: 00793b750d22177fe73dbfce7ddb8f460cff33bb [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
William Lallemand3af48e72020-02-03 17:15:52 +0100127/* file to guess during file loading */
128#define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */
129#define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */
130#define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */
131#define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */
132#define SSL_GF_OCSP_ISSUER 0x00000008 /* try to open the .issuer file if an OCSP file was loaded */
William Lallemand4c5adbf2020-02-24 14:23:22 +0100133#define SSL_GF_KEY 0x00000010 /* try to open the .key file to load a private key */
William Lallemand3af48e72020-02-03 17:15:52 +0100134
William Lallemand4c5adbf2020-02-24 14:23:22 +0100135#define SSL_GF_ALL (SSL_GF_BUNDLE|SSL_GF_SCTL|SSL_GF_OCSP|SSL_GF_OCSP_ISSUER|SSL_GF_KEY)
William Lallemand3af48e72020-02-03 17:15:52 +0100136
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200137/* ssl_methods versions */
138enum {
139 CONF_TLSV_NONE = 0,
140 CONF_TLSV_MIN = 1,
141 CONF_SSLV3 = 1,
142 CONF_TLSV10 = 2,
143 CONF_TLSV11 = 3,
144 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200145 CONF_TLSV13 = 5,
146 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200147};
148
Emeric Brun850efd52014-01-29 12:24:34 +0100149/* server and bind verify method, it uses a global value as default */
150enum {
151 SSL_SOCK_VERIFY_DEFAULT = 0,
152 SSL_SOCK_VERIFY_REQUIRED = 1,
153 SSL_SOCK_VERIFY_OPTIONAL = 2,
154 SSL_SOCK_VERIFY_NONE = 3,
155};
156
Willy Tarreau71b734c2014-01-28 15:19:44 +0100157int sslconns = 0;
158int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100159static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100160int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200161
William Lallemande0f3fd52020-02-25 14:53:06 +0100162static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +0100163static struct issuer_chain* ssl_get_issuer_chain(X509 *cert);
William Lallemande0f3fd52020-02-25 14:53:06 +0100164
Willy Tarreauef934602016-12-22 23:12:01 +0100165static struct {
166 char *crt_base; /* base directory path for certificates */
167 char *ca_base; /* base directory path for CAs and CRLs */
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +0100168 char *issuers_chain_path; /* from "issuers-chain-path" */
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +0100169
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000170 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100171
172 char *listen_default_ciphers;
173 char *connect_default_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200174#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200175 char *listen_default_ciphersuites;
176 char *connect_default_ciphersuites;
177#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100178 int listen_default_ssloptions;
179 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200180 struct tls_version_filter listen_default_sslmethods;
181 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100182
183 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
184 unsigned int life_time; /* SSL session lifetime in seconds */
185 unsigned int max_record; /* SSL max record size */
186 unsigned int default_dh_param; /* SSL maximum DH parameter size */
187 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100188 int capture_cipherlist; /* Size of the cipherlist buffer. */
William Lallemand3af48e72020-02-03 17:15:52 +0100189 int extra_files; /* which files not defined in the configuration file are we looking for */
Willy Tarreauef934602016-12-22 23:12:01 +0100190} global_ssl = {
191#ifdef LISTEN_DEFAULT_CIPHERS
192 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
193#endif
194#ifdef CONNECT_DEFAULT_CIPHERS
195 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
196#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200197#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200198#ifdef LISTEN_DEFAULT_CIPHERSUITES
199 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
200#endif
201#ifdef CONNECT_DEFAULT_CIPHERSUITES
202 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
203#endif
204#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100205 .listen_default_ssloptions = BC_SSL_O_NONE,
206 .connect_default_ssloptions = SRV_SSL_O_NONE,
207
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200208 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
209 .listen_default_sslmethods.min = CONF_TLSV_NONE,
210 .listen_default_sslmethods.max = CONF_TLSV_NONE,
211 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
212 .connect_default_sslmethods.min = CONF_TLSV_NONE,
213 .connect_default_sslmethods.max = CONF_TLSV_NONE,
214
Willy Tarreauef934602016-12-22 23:12:01 +0100215#ifdef DEFAULT_SSL_MAX_RECORD
216 .max_record = DEFAULT_SSL_MAX_RECORD,
217#endif
218 .default_dh_param = SSL_DEFAULT_DH_PARAM,
219 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100220 .capture_cipherlist = 0,
William Lallemand3af48e72020-02-03 17:15:52 +0100221 .extra_files = SSL_GF_ALL,
Willy Tarreauef934602016-12-22 23:12:01 +0100222};
223
Olivier Houcharda8955d52019-04-07 22:00:38 +0200224static BIO_METHOD *ha_meth;
225
Olivier Houchard66ab4982019-02-26 18:37:15 +0100226struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200227 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100228 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200229 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200230 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100231 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200232 struct wait_event wait_event;
Willy Tarreau113d52b2020-01-10 09:20:26 +0100233 struct wait_event *subs;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100234 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100235 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100236 int sent_early_data; /* Amount of early data we sent so far */
237
Olivier Houchard66ab4982019-02-26 18:37:15 +0100238};
239
240DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
241
Olivier Houchardea8dd942019-05-20 14:02:16 +0200242static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200243static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200244
Olivier Houcharda8955d52019-04-07 22:00:38 +0200245/* Methods to implement OpenSSL BIO */
246static int ha_ssl_write(BIO *h, const char *buf, int num)
247{
248 struct buffer tmpbuf;
249 struct ssl_sock_ctx *ctx;
250 int ret;
251
252 ctx = BIO_get_data(h);
253 tmpbuf.size = num;
254 tmpbuf.area = (void *)(uintptr_t)buf;
255 tmpbuf.data = num;
256 tmpbuf.head = 0;
257 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200258 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200259 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200260 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200261 } else if (ret == 0)
262 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200263 return ret;
264}
265
266static int ha_ssl_gets(BIO *h, char *buf, int size)
267{
268
269 return 0;
270}
271
272static int ha_ssl_puts(BIO *h, const char *str)
273{
274
275 return ha_ssl_write(h, str, strlen(str));
276}
277
278static int ha_ssl_read(BIO *h, char *buf, int size)
279{
280 struct buffer tmpbuf;
281 struct ssl_sock_ctx *ctx;
282 int ret;
283
284 ctx = BIO_get_data(h);
285 tmpbuf.size = size;
286 tmpbuf.area = buf;
287 tmpbuf.data = 0;
288 tmpbuf.head = 0;
289 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200290 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200291 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200292 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200293 } else if (ret == 0)
294 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200295
296 return ret;
297}
298
299static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
300{
301 int ret = 0;
302 switch (cmd) {
303 case BIO_CTRL_DUP:
304 case BIO_CTRL_FLUSH:
305 ret = 1;
306 break;
307 }
308 return ret;
309}
310
311static int ha_ssl_new(BIO *h)
312{
313 BIO_set_init(h, 1);
314 BIO_set_data(h, NULL);
315 BIO_clear_flags(h, ~0);
316 return 1;
317}
318
319static int ha_ssl_free(BIO *data)
320{
321
322 return 1;
323}
324
325
Willy Tarreau5db847a2019-05-09 14:13:35 +0200326#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100327
Emeric Brun821bb9b2017-06-15 16:37:39 +0200328static HA_RWLOCK_T *ssl_rwlocks;
329
330
331unsigned long ssl_id_function(void)
332{
333 return (unsigned long)tid;
334}
335
336void ssl_locking_function(int mode, int n, const char * file, int line)
337{
338 if (mode & CRYPTO_LOCK) {
339 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100340 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200341 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100342 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200343 }
344 else {
345 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100346 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200347 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100348 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200349 }
350}
351
352static int ssl_locking_init(void)
353{
354 int i;
355
356 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
357 if (!ssl_rwlocks)
358 return -1;
359
360 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100361 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200362
363 CRYPTO_set_id_callback(ssl_id_function);
364 CRYPTO_set_locking_callback(ssl_locking_function);
365
366 return 0;
367}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100368
Emeric Brun821bb9b2017-06-15 16:37:39 +0200369#endif
370
William Lallemand150bfa82019-09-19 17:12:49 +0200371__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200372
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100373/* Uncommitted CKCH transaction */
374
375static struct {
376 struct ckch_store *new_ckchs;
377 struct ckch_store *old_ckchs;
378 char *path;
379} ckchs_transaction;
380
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200381/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100382 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200383 */
384struct cafile_entry {
385 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200386 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200387 struct ebmb_node node;
388 char path[0];
389};
390
391static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
392
393static X509_STORE* ssl_store_get0_locations_file(char *path)
394{
395 struct ebmb_node *eb;
396
397 eb = ebst_lookup(&cafile_tree, path);
398 if (eb) {
399 struct cafile_entry *ca_e;
400 ca_e = ebmb_entry(eb, struct cafile_entry, node);
401 return ca_e->ca_store;
402 }
403 return NULL;
404}
405
406static int ssl_store_load_locations_file(char *path)
407{
408 if (ssl_store_get0_locations_file(path) == NULL) {
409 struct cafile_entry *ca_e;
410 X509_STORE *store = X509_STORE_new();
411 if (X509_STORE_load_locations(store, path, NULL)) {
412 int pathlen;
413 pathlen = strlen(path);
414 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
415 if (ca_e) {
416 memcpy(ca_e->path, path, pathlen + 1);
417 ca_e->ca_store = store;
418 ebst_insert(&cafile_tree, &ca_e->node);
419 return 1;
420 }
421 }
422 X509_STORE_free(store);
423 return 0;
424 }
425 return 1;
426}
427
428/* mimic what X509_STORE_load_locations do with store_ctx */
429static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
430{
431 X509_STORE *store;
432 store = ssl_store_get0_locations_file(path);
433 if (store_ctx && store) {
434 int i;
435 X509_OBJECT *obj;
436 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
437 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
438 obj = sk_X509_OBJECT_value(objs, i);
439 switch (X509_OBJECT_get_type(obj)) {
440 case X509_LU_X509:
441 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
442 break;
443 case X509_LU_CRL:
444 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
445 break;
446 default:
447 break;
448 }
449 }
450 return 1;
451 }
452 return 0;
453}
454
455/* SSL_CTX_load_verify_locations substitute, internaly call X509_STORE_load_locations */
456static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
457{
458 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
459 return ssl_set_cert_crl_file(store_ctx, path);
460}
461
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200462/*
463 Extract CA_list from CA_file already in tree.
464 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
465 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
466*/
467static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
468{
469 struct ebmb_node *eb;
470 struct cafile_entry *ca_e;
471
472 eb = ebst_lookup(&cafile_tree, path);
473 if (!eb)
474 return NULL;
475 ca_e = ebmb_entry(eb, struct cafile_entry, node);
476
477 if (ca_e->ca_list == NULL) {
478 int i;
479 unsigned long key;
480 struct eb_root ca_name_tree = EB_ROOT;
481 struct eb64_node *node, *back;
482 struct {
483 struct eb64_node node;
484 X509_NAME *xname;
485 } *ca_name;
486 STACK_OF(X509_OBJECT) *objs;
487 STACK_OF(X509_NAME) *skn;
488 X509 *x;
489 X509_NAME *xn;
490
491 skn = sk_X509_NAME_new_null();
492 /* take x509 from cafile_tree */
493 objs = X509_STORE_get0_objects(ca_e->ca_store);
494 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
495 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
496 if (!x)
497 continue;
498 xn = X509_get_subject_name(x);
499 if (!xn)
500 continue;
501 /* Check for duplicates. */
502 key = X509_NAME_hash(xn);
503 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
504 node && ca_name == NULL;
505 node = eb64_next(node)) {
506 ca_name = container_of(node, typeof(*ca_name), node);
507 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
508 ca_name = NULL;
509 }
510 /* find a duplicate */
511 if (ca_name)
512 continue;
513 ca_name = calloc(1, sizeof *ca_name);
514 xn = X509_NAME_dup(xn);
515 if (!ca_name ||
516 !xn ||
517 !sk_X509_NAME_push(skn, xn)) {
518 free(ca_name);
519 X509_NAME_free(xn);
520 sk_X509_NAME_pop_free(skn, X509_NAME_free);
521 sk_X509_NAME_free(skn);
522 skn = NULL;
523 break;
524 }
525 ca_name->node.key = key;
526 ca_name->xname = xn;
527 eb64_insert(&ca_name_tree, &ca_name->node);
528 }
529 ca_e->ca_list = skn;
530 /* remove temporary ca_name tree */
531 node = eb64_first(&ca_name_tree);
532 while (node) {
533 ca_name = container_of(node, typeof(*ca_name), node);
534 back = eb64_next(node);
535 eb64_delete(node);
536 free(ca_name);
537 node = back;
538 }
539 }
540 return ca_e->ca_list;
541}
542
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100543/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100544struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100545 unsigned long long int xxh64;
546 unsigned char ciphersuite_len;
547 char ciphersuite[0];
548};
Willy Tarreaubafbe012017-11-24 17:34:44 +0100549struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100550static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200551static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100552
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200553#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
554struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
555#endif
556
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200557#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000558static unsigned int openssl_engines_initialized;
559struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
560struct ssl_engine_list {
561 struct list list;
562 ENGINE *e;
563};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200564#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000565
Remi Gacogne8de54152014-07-15 11:36:40 +0200566#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200567static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200568static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200569static DH *local_dh_1024 = NULL;
570static DH *local_dh_2048 = NULL;
571static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100572static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200573#endif /* OPENSSL_NO_DH */
574
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100575#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200576/* X509V3 Extensions that will be added on generated certificates */
577#define X509V3_EXT_SIZE 5
578static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
579 "basicConstraints",
580 "nsComment",
581 "subjectKeyIdentifier",
582 "authorityKeyIdentifier",
583 "keyUsage",
584};
585static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
586 "CA:FALSE",
587 "\"OpenSSL Generated Certificate\"",
588 "hash",
589 "keyid,issuer:always",
590 "nonRepudiation,digitalSignature,keyEncipherment"
591};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200592/* LRU cache to store generated certificate */
593static struct lru64_head *ssl_ctx_lru_tree = NULL;
594static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200595static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100596__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200597
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200598#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
599
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100600static struct ssl_bind_kw ssl_bind_kws[];
601
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200602#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500603/* The order here matters for picking a default context,
604 * keep the most common keytype at the bottom of the list
605 */
606const char *SSL_SOCK_KEYTYPE_NAMES[] = {
607 "dsa",
608 "ecdsa",
609 "rsa"
610};
611#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100612#else
613#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500614#endif
615
William Lallemandc3cd35f2017-11-28 11:04:43 +0100616static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100617static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
618
619#define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key);
620
621#define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \
622 &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH);
623
624#define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \
625 (k), SSL_MAX_SSL_SESSION_ID_LENGTH);
William Lallemand3f85c9a2017-10-09 16:30:50 +0200626
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100627/*
628 * This function gives the detail of the SSL error. It is used only
629 * if the debug mode and the verbose mode are activated. It dump all
630 * the SSL error until the stack was empty.
631 */
632static forceinline void ssl_sock_dump_errors(struct connection *conn)
633{
634 unsigned long ret;
635
636 if (unlikely(global.mode & MODE_DEBUG)) {
637 while(1) {
638 ret = ERR_get_error();
639 if (ret == 0)
640 return;
641 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200642 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100643 ERR_func_error_string(ret), ERR_reason_error_string(ret));
644 }
645 }
646}
647
yanbzhube2774d2015-12-10 15:07:30 -0500648
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200649#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000650static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
651{
652 int err_code = ERR_ABORT;
653 ENGINE *engine;
654 struct ssl_engine_list *el;
655
656 /* grab the structural reference to the engine */
657 engine = ENGINE_by_id(engine_id);
658 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100659 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000660 goto fail_get;
661 }
662
663 if (!ENGINE_init(engine)) {
664 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100665 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000666 goto fail_init;
667 }
668
669 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100670 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000671 goto fail_set_method;
672 }
673
674 el = calloc(1, sizeof(*el));
675 el->e = engine;
676 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100677 nb_engines++;
678 if (global_ssl.async)
679 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000680 return 0;
681
682fail_set_method:
683 /* release the functional reference from ENGINE_init() */
684 ENGINE_finish(engine);
685
686fail_init:
687 /* release the structural reference from ENGINE_by_id() */
688 ENGINE_free(engine);
689
690fail_get:
691 return err_code;
692}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200693#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000694
Willy Tarreau5db847a2019-05-09 14:13:35 +0200695#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200696/*
697 * openssl async fd handler
698 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200699void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000700{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200701 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000702
Emeric Brun3854e012017-05-17 20:42:48 +0200703 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000704 * to poll this fd until it is requested
705 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000706 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000707 fd_cant_recv(fd);
708
709 /* crypto engine is available, let's notify the associated
710 * connection that it can pursue its processing.
711 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200712 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000713}
714
Emeric Brun3854e012017-05-17 20:42:48 +0200715/*
716 * openssl async delayed SSL_free handler
717 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200718void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000719{
720 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200721 OSSL_ASYNC_FD all_fd[32];
722 size_t num_all_fds = 0;
723 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000724
Emeric Brun3854e012017-05-17 20:42:48 +0200725 /* We suppose that the async job for a same SSL *
726 * are serialized. So if we are awake it is
727 * because the running job has just finished
728 * and we can remove all async fds safely
729 */
730 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
731 if (num_all_fds > 32) {
732 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
733 return;
734 }
735
736 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
737 for (i=0 ; i < num_all_fds ; i++)
738 fd_remove(all_fd[i]);
739
740 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000741 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100742 _HA_ATOMIC_SUB(&sslconns, 1);
743 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000744}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000745/*
Emeric Brun3854e012017-05-17 20:42:48 +0200746 * function used to manage a returned SSL_ERROR_WANT_ASYNC
747 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000748 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200749static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000750{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100751 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200752 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200753 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000754 size_t num_add_fds = 0;
755 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200756 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000757
758 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
759 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200760 if (num_add_fds > 32 || num_del_fds > 32) {
761 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 +0000762 return;
763 }
764
Emeric Brun3854e012017-05-17 20:42:48 +0200765 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000766
Emeric Brun3854e012017-05-17 20:42:48 +0200767 /* We remove unused fds from the fdtab */
768 for (i=0 ; i < num_del_fds ; i++)
769 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000770
Emeric Brun3854e012017-05-17 20:42:48 +0200771 /* We add new fds to the fdtab */
772 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200773 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000774 }
775
Emeric Brun3854e012017-05-17 20:42:48 +0200776 num_add_fds = 0;
777 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
778 if (num_add_fds > 32) {
779 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
780 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000781 }
Emeric Brun3854e012017-05-17 20:42:48 +0200782
783 /* We activate the polling for all known async fds */
784 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000785 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200786 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000787 /* To ensure that the fd cache won't be used
788 * We'll prefer to catch a real RD event
789 * because handling an EAGAIN on this fd will
790 * result in a context switch and also
791 * some engines uses a fd in blocking mode.
792 */
793 fd_cant_recv(add_fd[i]);
794 }
Emeric Brun3854e012017-05-17 20:42:48 +0200795
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000796}
797#endif
798
William Lallemand104a7a62019-10-14 14:14:59 +0200799#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200800/*
801 * This function returns the number of seconds elapsed
802 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
803 * date presented un ASN1_GENERALIZEDTIME.
804 *
805 * In parsing error case, it returns -1.
806 */
807static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
808{
809 long epoch;
810 char *p, *end;
811 const unsigned short month_offset[12] = {
812 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
813 };
814 int year, month;
815
816 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
817
818 p = (char *)d->data;
819 end = p + d->length;
820
821 if (end - p < 4) return -1;
822 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
823 p += 4;
824 if (end - p < 2) return -1;
825 month = 10 * (p[0] - '0') + p[1] - '0';
826 if (month < 1 || month > 12) return -1;
827 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
828 We consider leap years and the current month (<marsh or not) */
829 epoch = ( ((year - 1970) * 365)
830 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
831 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
832 + month_offset[month-1]
833 ) * 24 * 60 * 60;
834 p += 2;
835 if (end - p < 2) return -1;
836 /* Add the number of seconds of completed days of current month */
837 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
838 p += 2;
839 if (end - p < 2) return -1;
840 /* Add the completed hours of the current day */
841 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
842 p += 2;
843 if (end - p < 2) return -1;
844 /* Add the completed minutes of the current hour */
845 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
846 p += 2;
847 if (p == end) return -1;
848 /* Test if there is available seconds */
849 if (p[0] < '0' || p[0] > '9')
850 goto nosec;
851 if (end - p < 2) return -1;
852 /* Add the seconds of the current minute */
853 epoch += 10 * (p[0] - '0') + p[1] - '0';
854 p += 2;
855 if (p == end) return -1;
856 /* Ignore seconds float part if present */
857 if (p[0] == '.') {
858 do {
859 if (++p == end) return -1;
860 } while (p[0] >= '0' && p[0] <= '9');
861 }
862
863nosec:
864 if (p[0] == 'Z') {
865 if (end - p != 1) return -1;
866 return epoch;
867 }
868 else if (p[0] == '+') {
869 if (end - p != 5) return -1;
870 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700871 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 +0200872 }
873 else if (p[0] == '-') {
874 if (end - p != 5) return -1;
875 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700876 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 +0200877 }
878
879 return -1;
880}
881
William Lallemand104a7a62019-10-14 14:14:59 +0200882/*
883 * struct alignment works here such that the key.key is the same as key_data
884 * Do not change the placement of key_data
885 */
886struct certificate_ocsp {
887 struct ebmb_node key;
888 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
889 struct buffer response;
890 long expire;
891};
892
893struct ocsp_cbk_arg {
894 int is_single;
895 int single_kt;
896 union {
897 struct certificate_ocsp *s_ocsp;
898 /*
899 * m_ocsp will have multiple entries dependent on key type
900 * Entry 0 - DSA
901 * Entry 1 - ECDSA
902 * Entry 2 - RSA
903 */
904 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
905 };
906};
907
Emeric Brun1d3865b2014-06-20 15:37:32 +0200908static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200909
910/* This function starts to check if the OCSP response (in DER format) contained
911 * in chunk 'ocsp_response' is valid (else exits on error).
912 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
913 * contained in the OCSP Response and exits on error if no match.
914 * If it's a valid OCSP Response:
915 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
916 * pointed by 'ocsp'.
917 * If 'ocsp' is NULL, the function looks up into the OCSP response's
918 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
919 * from the response) and exits on error if not found. Finally, If an OCSP response is
920 * already present in the container, it will be overwritten.
921 *
922 * Note: OCSP response containing more than one OCSP Single response is not
923 * considered valid.
924 *
925 * Returns 0 on success, 1 in error case.
926 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200927static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
928 struct certificate_ocsp *ocsp,
929 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200930{
931 OCSP_RESPONSE *resp;
932 OCSP_BASICRESP *bs = NULL;
933 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200934 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200935 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200936 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200937 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200938 int reason;
939 int ret = 1;
940
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200941 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
942 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200943 if (!resp) {
944 memprintf(err, "Unable to parse OCSP response");
945 goto out;
946 }
947
948 rc = OCSP_response_status(resp);
949 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
950 memprintf(err, "OCSP response status not successful");
951 goto out;
952 }
953
954 bs = OCSP_response_get1_basic(resp);
955 if (!bs) {
956 memprintf(err, "Failed to get basic response from OCSP Response");
957 goto out;
958 }
959
960 count_sr = OCSP_resp_count(bs);
961 if (count_sr > 1) {
962 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
963 goto out;
964 }
965
966 sr = OCSP_resp_get0(bs, 0);
967 if (!sr) {
968 memprintf(err, "Failed to get OCSP single response");
969 goto out;
970 }
971
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200972 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
973
Emeric Brun4147b2e2014-06-16 18:36:30 +0200974 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200975 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200976 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200977 goto out;
978 }
979
Emeric Brun13a6b482014-06-20 15:44:34 +0200980 if (!nextupd) {
981 memprintf(err, "OCSP single response: missing nextupdate");
982 goto out;
983 }
984
Emeric Brunc8b27b62014-06-19 14:16:17 +0200985 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200986 if (!rc) {
987 memprintf(err, "OCSP single response: no longer valid.");
988 goto out;
989 }
990
991 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200992 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200993 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
994 goto out;
995 }
996 }
997
998 if (!ocsp) {
999 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
1000 unsigned char *p;
1001
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001002 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001003 if (!rc) {
1004 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
1005 goto out;
1006 }
1007
1008 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
1009 memprintf(err, "OCSP single response: Certificate ID too long");
1010 goto out;
1011 }
1012
1013 p = key;
1014 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001015 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001016 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
1017 if (!ocsp) {
1018 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
1019 goto out;
1020 }
1021 }
1022
1023 /* According to comments on "chunk_dup", the
1024 previous chunk buffer will be freed */
1025 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1026 memprintf(err, "OCSP response: Memory allocation error");
1027 goto out;
1028 }
1029
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001030 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1031
Emeric Brun4147b2e2014-06-16 18:36:30 +02001032 ret = 0;
1033out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001034 ERR_clear_error();
1035
Emeric Brun4147b2e2014-06-16 18:36:30 +02001036 if (bs)
1037 OCSP_BASICRESP_free(bs);
1038
1039 if (resp)
1040 OCSP_RESPONSE_free(resp);
1041
1042 return ret;
1043}
1044/*
1045 * External function use to update the OCSP response in the OCSP response's
1046 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1047 * to update in DER format.
1048 *
1049 * Returns 0 on success, 1 in error case.
1050 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001051int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001052{
1053 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1054}
1055
William Lallemand4a660132019-10-14 14:51:41 +02001056#endif
1057
1058#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001059/*
1060 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +02001061 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +02001062 *
1063 * Returns 0 on success, 1 in error case.
1064 */
William Lallemand3b5f3602019-10-16 18:05:05 +02001065static 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 +02001066{
1067 int fd = -1;
1068 int r = 0;
1069 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +02001070 struct buffer *ocsp_response;
1071 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001072
William Lallemand3b5f3602019-10-16 18:05:05 +02001073 if (buf) {
1074 int i, j;
1075 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001076
William Lallemand3b5f3602019-10-16 18:05:05 +02001077 /* remove \r and \n from the payload */
1078 for (i = 0, j = 0; buf[i]; i++) {
1079 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +02001080 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +02001081 buf[j++] = buf[i];
1082 }
1083 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001084
William Lallemand3b5f3602019-10-16 18:05:05 +02001085 ret = base64dec(buf, j, trash.area, trash.size);
1086 if (ret < 0) {
1087 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +02001088 goto end;
1089 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001090 trash.data = ret;
1091 src = &trash;
1092 } else {
1093 fd = open(ocsp_path, O_RDONLY);
1094 if (fd == -1) {
1095 memprintf(err, "Error opening OCSP response file");
1096 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001097 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001098
1099 trash.data = 0;
1100 while (trash.data < trash.size) {
1101 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1102 if (r < 0) {
1103 if (errno == EINTR)
1104 continue;
1105
1106 memprintf(err, "Error reading OCSP response from file");
1107 goto end;
1108 }
1109 else if (r == 0) {
1110 break;
1111 }
1112 trash.data += r;
1113 }
1114 close(fd);
1115 fd = -1;
1116 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001117 }
1118
William Lallemand3b5f3602019-10-16 18:05:05 +02001119 ocsp_response = calloc(1, sizeof(*ocsp_response));
1120 if (!chunk_dup(ocsp_response, src)) {
1121 free(ocsp_response);
1122 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001123 goto end;
1124 }
Emmanuel Hocdet0667fae2020-01-16 14:41:36 +01001125 /* no error, fill ckch with new context, old context must be free */
1126 if (ckch->ocsp_response) {
1127 free(ckch->ocsp_response->area);
1128 ckch->ocsp_response->area = NULL;
1129 free(ckch->ocsp_response);
1130 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001131 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001132 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001133end:
1134 if (fd != -1)
1135 close(fd);
1136
1137 return ret;
1138}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001139#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001140
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001141#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1142static 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)
1143{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001144 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001145 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001146 struct connection *conn;
1147 int head;
1148 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001149 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001150
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001151 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001152 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001153 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1154
1155 keys = ref->tlskeys;
1156 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001157
1158 if (enc) {
1159 memcpy(key_name, keys[head].name, 16);
1160
1161 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001162 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001163
Emeric Brun9e754772019-01-10 17:51:55 +01001164 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001165
Emeric Brun9e754772019-01-10 17:51:55 +01001166 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1167 goto end;
1168
Willy Tarreau9356dac2019-05-10 09:22:53 +02001169 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001170 ret = 1;
1171 }
1172 else if (ref->key_size_bits == 256 ) {
1173
1174 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1175 goto end;
1176
Willy Tarreau9356dac2019-05-10 09:22:53 +02001177 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001178 ret = 1;
1179 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001180 } else {
1181 for (i = 0; i < TLS_TICKETS_NO; i++) {
1182 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1183 goto found;
1184 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001185 ret = 0;
1186 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001187
Christopher Faulet16f45c82018-02-16 11:23:49 +01001188 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001189 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001190 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 +01001191 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1192 goto end;
1193 /* 2 for key renewal, 1 if current key is still valid */
1194 ret = i ? 2 : 1;
1195 }
1196 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001197 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 +01001198 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1199 goto end;
1200 /* 2 for key renewal, 1 if current key is still valid */
1201 ret = i ? 2 : 1;
1202 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001203 }
Emeric Brun9e754772019-01-10 17:51:55 +01001204
Christopher Faulet16f45c82018-02-16 11:23:49 +01001205 end:
1206 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1207 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001208}
1209
1210struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1211{
1212 struct tls_keys_ref *ref;
1213
1214 list_for_each_entry(ref, &tlskeys_reference, list)
1215 if (ref->filename && strcmp(filename, ref->filename) == 0)
1216 return ref;
1217 return NULL;
1218}
1219
1220struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1221{
1222 struct tls_keys_ref *ref;
1223
1224 list_for_each_entry(ref, &tlskeys_reference, list)
1225 if (ref->unique_id == unique_id)
1226 return ref;
1227 return NULL;
1228}
1229
Emeric Brun9e754772019-01-10 17:51:55 +01001230/* Update the key into ref: if keysize doesnt
1231 * match existing ones, this function returns -1
1232 * else it returns 0 on success.
1233 */
1234int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001235 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001236{
Emeric Brun9e754772019-01-10 17:51:55 +01001237 if (ref->key_size_bits == 128) {
1238 if (tlskey->data != sizeof(struct tls_sess_key_128))
1239 return -1;
1240 }
1241 else if (ref->key_size_bits == 256) {
1242 if (tlskey->data != sizeof(struct tls_sess_key_256))
1243 return -1;
1244 }
1245 else
1246 return -1;
1247
Christopher Faulet16f45c82018-02-16 11:23:49 +01001248 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001249 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1250 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001251 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1252 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001253
1254 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001255}
1256
Willy Tarreau83061a82018-07-13 11:56:34 +02001257int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001258{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001259 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1260
1261 if(!ref) {
1262 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1263 return 1;
1264 }
Emeric Brun9e754772019-01-10 17:51:55 +01001265 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1266 memprintf(err, "Invalid key size");
1267 return 1;
1268 }
1269
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001270 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001271}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001272
1273/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001274 * automatic ids. It's called just after the basic checks. It returns
1275 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001276 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001277static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001278{
1279 int i = 0;
1280 struct tls_keys_ref *ref, *ref2, *ref3;
1281 struct list tkr = LIST_HEAD_INIT(tkr);
1282
1283 list_for_each_entry(ref, &tlskeys_reference, list) {
1284 if (ref->unique_id == -1) {
1285 /* Look for the first free id. */
1286 while (1) {
1287 list_for_each_entry(ref2, &tlskeys_reference, list) {
1288 if (ref2->unique_id == i) {
1289 i++;
1290 break;
1291 }
1292 }
1293 if (&ref2->list == &tlskeys_reference)
1294 break;
1295 }
1296
1297 /* Uses the unique id and increment it for the next entry. */
1298 ref->unique_id = i;
1299 i++;
1300 }
1301 }
1302
1303 /* This sort the reference list by id. */
1304 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1305 LIST_DEL(&ref->list);
1306 list_for_each_entry(ref3, &tkr, list) {
1307 if (ref->unique_id < ref3->unique_id) {
1308 LIST_ADDQ(&ref3->list, &ref->list);
1309 break;
1310 }
1311 }
1312 if (&ref3->list == &tkr)
1313 LIST_ADDQ(&tkr, &ref->list);
1314 }
1315
1316 /* swap root */
1317 LIST_ADD(&tkr, &tlskeys_reference);
1318 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001319 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001320}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001321#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1322
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001323#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001324int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1325{
1326 switch (evp_keytype) {
1327 case EVP_PKEY_RSA:
1328 return 2;
1329 case EVP_PKEY_DSA:
1330 return 0;
1331 case EVP_PKEY_EC:
1332 return 1;
1333 }
1334
1335 return -1;
1336}
1337
Emeric Brun4147b2e2014-06-16 18:36:30 +02001338/*
1339 * Callback used to set OCSP status extension content in server hello.
1340 */
1341int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1342{
yanbzhube2774d2015-12-10 15:07:30 -05001343 struct certificate_ocsp *ocsp;
1344 struct ocsp_cbk_arg *ocsp_arg;
1345 char *ssl_buf;
1346 EVP_PKEY *ssl_pkey;
1347 int key_type;
1348 int index;
1349
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001350 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001351
1352 ssl_pkey = SSL_get_privatekey(ssl);
1353 if (!ssl_pkey)
1354 return SSL_TLSEXT_ERR_NOACK;
1355
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001356 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001357
1358 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1359 ocsp = ocsp_arg->s_ocsp;
1360 else {
1361 /* For multiple certs per context, we have to find the correct OCSP response based on
1362 * the certificate type
1363 */
1364 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1365
1366 if (index < 0)
1367 return SSL_TLSEXT_ERR_NOACK;
1368
1369 ocsp = ocsp_arg->m_ocsp[index];
1370
1371 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001372
1373 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001374 !ocsp->response.area ||
1375 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001376 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001377 return SSL_TLSEXT_ERR_NOACK;
1378
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001379 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001380 if (!ssl_buf)
1381 return SSL_TLSEXT_ERR_NOACK;
1382
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001383 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1384 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001385
1386 return SSL_TLSEXT_ERR_OK;
1387}
1388
William Lallemand4a660132019-10-14 14:51:41 +02001389#endif
1390
1391#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001392/*
1393 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001394 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1395 * status extension, the issuer's certificate is mandatory. It should be
1396 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001397 *
William Lallemand246c0242019-10-11 08:59:13 +02001398 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1399 * OCSP response. If file is empty or content is not a valid OCSP response,
1400 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1401 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001402 *
1403 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001404 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001405 */
William Lallemand4a660132019-10-14 14:51:41 +02001406#ifndef OPENSSL_IS_BORINGSSL
William Lallemand246c0242019-10-11 08:59:13 +02001407static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001408{
William Lallemand246c0242019-10-11 08:59:13 +02001409 X509 *x = NULL, *issuer = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001410 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001411 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001412 struct certificate_ocsp *ocsp = NULL, *iocsp;
1413 char *warn = NULL;
1414 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001415 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001416
Emeric Brun4147b2e2014-06-16 18:36:30 +02001417
William Lallemand246c0242019-10-11 08:59:13 +02001418 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001419 if (!x)
1420 goto out;
1421
William Lallemand246c0242019-10-11 08:59:13 +02001422 issuer = ckch->ocsp_issuer;
1423 if (!issuer)
1424 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001425
1426 cid = OCSP_cert_to_id(0, x, issuer);
1427 if (!cid)
1428 goto out;
1429
1430 i = i2d_OCSP_CERTID(cid, NULL);
1431 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1432 goto out;
1433
Vincent Bernat02779b62016-04-03 13:48:43 +02001434 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001435 if (!ocsp)
1436 goto out;
1437
1438 p = ocsp->key_data;
1439 i2d_OCSP_CERTID(cid, &p);
1440
1441 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1442 if (iocsp == ocsp)
1443 ocsp = NULL;
1444
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001445#ifndef SSL_CTX_get_tlsext_status_cb
1446# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1447 *cb = (void (*) (void))ctx->tlsext_status_cb;
1448#endif
1449 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1450
1451 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001452 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001453 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001454
1455 cb_arg->is_single = 1;
1456 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001457
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001458 pkey = X509_get_pubkey(x);
1459 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1460 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001461
1462 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1463 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1464 } else {
1465 /*
1466 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1467 * Update that cb_arg with the new cert's staple
1468 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001469 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001470 struct certificate_ocsp *tmp_ocsp;
1471 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001472 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001473 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001474
1475#ifdef SSL_CTX_get_tlsext_status_arg
1476 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1477#else
1478 cb_arg = ctx->tlsext_status_arg;
1479#endif
yanbzhube2774d2015-12-10 15:07:30 -05001480
1481 /*
1482 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1483 * the order of operations below matter, take care when changing it
1484 */
1485 tmp_ocsp = cb_arg->s_ocsp;
1486 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1487 cb_arg->s_ocsp = NULL;
1488 cb_arg->m_ocsp[index] = tmp_ocsp;
1489 cb_arg->is_single = 0;
1490 cb_arg->single_kt = 0;
1491
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001492 pkey = X509_get_pubkey(x);
1493 key_type = EVP_PKEY_base_id(pkey);
1494 EVP_PKEY_free(pkey);
1495
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001496 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001497 if (index >= 0 && !cb_arg->m_ocsp[index])
1498 cb_arg->m_ocsp[index] = iocsp;
1499
1500 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001501
1502 ret = 0;
1503
1504 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001505 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001506 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001507 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001508 }
1509
1510out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001511 if (cid)
1512 OCSP_CERTID_free(cid);
1513
1514 if (ocsp)
1515 free(ocsp);
1516
1517 if (warn)
1518 free(warn);
1519
Emeric Brun4147b2e2014-06-16 18:36:30 +02001520 return ret;
1521}
William Lallemand4a660132019-10-14 14:51:41 +02001522#else /* OPENSSL_IS_BORINGSSL */
1523static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001524{
William Lallemand4a660132019-10-14 14:51:41 +02001525 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 +02001526}
1527#endif
1528
William Lallemand4a660132019-10-14 14:51:41 +02001529#endif
1530
1531
Willy Tarreau5db847a2019-05-09 14:13:35 +02001532#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001533
1534#define CT_EXTENSION_TYPE 18
1535
1536static int sctl_ex_index = -1;
1537
1538/*
1539 * Try to parse Signed Certificate Timestamp List structure. This function
1540 * makes only basic test if the data seems like SCTL. No signature validation
1541 * is performed.
1542 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001543static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001544{
1545 int ret = 1;
1546 int len, pos, sct_len;
1547 unsigned char *data;
1548
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001549 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001550 goto out;
1551
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001552 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001553 len = (data[0] << 8) | data[1];
1554
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001555 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001556 goto out;
1557
1558 data = data + 2;
1559 pos = 0;
1560 while (pos < len) {
1561 if (len - pos < 2)
1562 goto out;
1563
1564 sct_len = (data[pos] << 8) | data[pos + 1];
1565 if (pos + sct_len + 2 > len)
1566 goto out;
1567
1568 pos += sct_len + 2;
1569 }
1570
1571 ret = 0;
1572
1573out:
1574 return ret;
1575}
1576
William Lallemand0dfae6c2019-10-16 18:06:58 +02001577/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1578 * It fills the ckch->sctl buffer
1579 * return 0 on success or != 0 on failure */
1580static 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 +01001581{
1582 int fd = -1;
1583 int r = 0;
1584 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001585 struct buffer tmp;
1586 struct buffer *src;
1587 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001588
William Lallemand0dfae6c2019-10-16 18:06:58 +02001589 if (buf) {
1590 tmp.area = buf;
1591 tmp.data = strlen(buf);
1592 tmp.size = tmp.data + 1;
1593 src = &tmp;
1594 } else {
1595 fd = open(sctl_path, O_RDONLY);
1596 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001597 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001598
1599 trash.data = 0;
1600 while (trash.data < trash.size) {
1601 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1602 if (r < 0) {
1603 if (errno == EINTR)
1604 continue;
1605 goto end;
1606 }
1607 else if (r == 0) {
1608 break;
1609 }
1610 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001611 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001612 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001613 }
1614
William Lallemand0dfae6c2019-10-16 18:06:58 +02001615 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001616 if (ret)
1617 goto end;
1618
William Lallemand0dfae6c2019-10-16 18:06:58 +02001619 sctl = calloc(1, sizeof(*sctl));
1620 if (!chunk_dup(sctl, src)) {
1621 free(sctl);
1622 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001623 goto end;
1624 }
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001625 /* no error, fill ckch with new context, old context must be free */
1626 if (ckch->sctl) {
1627 free(ckch->sctl->area);
1628 ckch->sctl->area = NULL;
1629 free(ckch->sctl);
1630 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001631 ckch->sctl = sctl;
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001632 ret = 0;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001633end:
1634 if (fd != -1)
1635 close(fd);
1636
1637 return ret;
1638}
1639
1640int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1641{
Willy Tarreau83061a82018-07-13 11:56:34 +02001642 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001643
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001644 *out = (unsigned char *) sctl->area;
1645 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001646
1647 return 1;
1648}
1649
1650int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1651{
1652 return 1;
1653}
1654
William Lallemanda17f4112019-10-10 15:16:44 +02001655static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001656{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001657 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001658
William Lallemanda17f4112019-10-10 15:16:44 +02001659 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 +01001660 goto out;
1661
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001662 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1663
1664 ret = 0;
1665
1666out:
1667 return ret;
1668}
1669
1670#endif
1671
Emeric Brune1f38db2012-09-03 20:36:47 +02001672void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1673{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001674 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001675 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001676 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001677 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001678
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001679#ifndef SSL_OP_NO_RENEGOTIATION
1680 /* Please note that BoringSSL defines this macro to zero so don't
1681 * change this to #if and do not assign a default value to this macro!
1682 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001683 if (where & SSL_CB_HANDSHAKE_START) {
1684 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001685 if ((conn->flags & (CO_FL_WAIT_L6_CONN | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == 0) {
Emeric Brune1f38db2012-09-03 20:36:47 +02001686 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001687 conn->err_code = CO_ER_SSL_RENEG;
1688 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001689 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001690#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001691
1692 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001693 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001694 /* Long certificate chains optimz
1695 If write and read bios are differents, we
1696 consider that the buffering was activated,
1697 so we rise the output buffer size from 4k
1698 to 16k */
1699 write_bio = SSL_get_wbio(ssl);
1700 if (write_bio != SSL_get_rbio(ssl)) {
1701 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001702 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001703 }
1704 }
1705 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001706}
1707
Emeric Brune64aef12012-09-21 13:15:06 +02001708/* Callback is called for each certificate of the chain during a verify
1709 ok is set to 1 if preverify detect no error on current certificate.
1710 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001711int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001712{
1713 SSL *ssl;
1714 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001715 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001716 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001717
1718 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001719 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001720
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001721 ctx = conn->xprt_ctx;
1722
1723 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001724
Emeric Brun81c00f02012-09-21 14:31:21 +02001725 if (ok) /* no errors */
1726 return ok;
1727
1728 depth = X509_STORE_CTX_get_error_depth(x_store);
1729 err = X509_STORE_CTX_get_error(x_store);
1730
1731 /* check if CA error needs to be ignored */
1732 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001733 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1734 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1735 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001736 }
1737
Willy Tarreau731248f2020-02-04 14:02:02 +01001738 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001739 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001740 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001741 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001742 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001743
Willy Tarreau20879a02012-12-03 16:32:10 +01001744 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001745 return 0;
1746 }
1747
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001748 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1749 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001750
Emeric Brun81c00f02012-09-21 14:31:21 +02001751 /* check if certificate error needs to be ignored */
Willy Tarreau731248f2020-02-04 14:02:02 +01001752 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001753 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001754 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001755 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001756 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001757
Willy Tarreau20879a02012-12-03 16:32:10 +01001758 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001759 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001760}
1761
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001762static inline
1763void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001764 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001765{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001766 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001767 unsigned char *msg;
1768 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001769 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001770
1771 /* This function is called for "from client" and "to server"
1772 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001773 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001774 */
1775
1776 /* "write_p" is set to 0 is the bytes are received messages,
1777 * otherwise it is set to 1.
1778 */
1779 if (write_p != 0)
1780 return;
1781
1782 /* content_type contains the type of message received or sent
1783 * according with the SSL/TLS protocol spec. This message is
1784 * encoded with one byte. The value 256 (two bytes) is used
1785 * for designing the SSL/TLS record layer. According with the
1786 * rfc6101, the expected message (other than 256) are:
1787 * - change_cipher_spec(20)
1788 * - alert(21)
1789 * - handshake(22)
1790 * - application_data(23)
1791 * - (255)
1792 * We are interessed by the handshake and specially the client
1793 * hello.
1794 */
1795 if (content_type != 22)
1796 return;
1797
1798 /* The message length is at least 4 bytes, containing the
1799 * message type and the message length.
1800 */
1801 if (len < 4)
1802 return;
1803
1804 /* First byte of the handshake message id the type of
1805 * message. The konwn types are:
1806 * - hello_request(0)
1807 * - client_hello(1)
1808 * - server_hello(2)
1809 * - certificate(11)
1810 * - server_key_exchange (12)
1811 * - certificate_request(13)
1812 * - server_hello_done(14)
1813 * We are interested by the client hello.
1814 */
1815 msg = (unsigned char *)buf;
1816 if (msg[0] != 1)
1817 return;
1818
1819 /* Next three bytes are the length of the message. The total length
1820 * must be this decoded length + 4. If the length given as argument
1821 * is not the same, we abort the protocol dissector.
1822 */
1823 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1824 if (len < rec_len + 4)
1825 return;
1826 msg += 4;
1827 end = msg + rec_len;
1828 if (end < msg)
1829 return;
1830
1831 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1832 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001833 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1834 */
1835 msg += 1 + 1 + 4 + 28;
1836 if (msg > end)
1837 return;
1838
1839 /* Next, is session id:
1840 * if present, we have to jump by length + 1 for the size information
1841 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001842 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001843 if (msg[0] > 0)
1844 msg += msg[0];
1845 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001846 if (msg > end)
1847 return;
1848
1849 /* Next two bytes are the ciphersuite length. */
1850 if (msg + 2 > end)
1851 return;
1852 rec_len = (msg[0] << 8) + msg[1];
1853 msg += 2;
1854 if (msg + rec_len > end || msg + rec_len < msg)
1855 return;
1856
Willy Tarreaubafbe012017-11-24 17:34:44 +01001857 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001858 if (!capture)
1859 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001860 /* Compute the xxh64 of the ciphersuite. */
1861 capture->xxh64 = XXH64(msg, rec_len, 0);
1862
1863 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001864 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1865 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001866 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001867
1868 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001869}
1870
Emeric Brun29f037d2014-04-25 19:05:36 +02001871/* Callback is called for ssl protocol analyse */
1872void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1873{
Emeric Brun29f037d2014-04-25 19:05:36 +02001874#ifdef TLS1_RT_HEARTBEAT
1875 /* test heartbeat received (write_p is set to 0
1876 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001877 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001878 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
William Lallemand7e1770b2019-05-13 14:31:34 +02001879 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001880 const unsigned char *p = buf;
1881 unsigned int payload;
1882
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001883 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001884
1885 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1886 if (*p != TLS1_HB_REQUEST)
1887 return;
1888
Willy Tarreauaeed6722014-04-25 23:59:58 +02001889 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001890 goto kill_it;
1891
1892 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001893 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001894 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001895 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001896 /* We have a clear heartbleed attack (CVE-2014-0160), the
1897 * advertised payload is larger than the advertised packet
1898 * length, so we have garbage in the buffer between the
1899 * payload and the end of the buffer (p+len). We can't know
1900 * if the SSL stack is patched, and we don't know if we can
1901 * safely wipe out the area between p+3+len and payload.
1902 * So instead, we prevent the response from being sent by
1903 * setting the max_send_fragment to 0 and we report an SSL
1904 * error, which will kill this connection. It will be reported
1905 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001906 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1907 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001908 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001909 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1910 return;
1911 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001912#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001913 if (global_ssl.capture_cipherlist > 0)
1914 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001915}
1916
Bernard Spil13c53f82018-02-15 13:34:58 +01001917#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001918static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1919 const unsigned char *in, unsigned int inlen,
1920 void *arg)
1921{
1922 struct server *srv = arg;
1923
1924 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1925 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1926 return SSL_TLSEXT_ERR_OK;
1927 return SSL_TLSEXT_ERR_NOACK;
1928}
1929#endif
1930
1931#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001932/* This callback is used so that the server advertises the list of
1933 * negociable protocols for NPN.
1934 */
1935static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1936 unsigned int *len, void *arg)
1937{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001938 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001939
1940 *data = (const unsigned char *)conf->npn_str;
1941 *len = conf->npn_len;
1942 return SSL_TLSEXT_ERR_OK;
1943}
1944#endif
1945
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001946#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001947/* This callback is used so that the server advertises the list of
1948 * negociable protocols for ALPN.
1949 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001950static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1951 unsigned char *outlen,
1952 const unsigned char *server,
1953 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001954{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001955 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001956
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001957 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1958 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1959 return SSL_TLSEXT_ERR_NOACK;
1960 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001961 return SSL_TLSEXT_ERR_OK;
1962}
1963#endif
1964
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001965#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001966#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001967
Christopher Faulet30548802015-06-11 13:39:32 +02001968/* Create a X509 certificate with the specified servername and serial. This
1969 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001970static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001971ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001972{
Christopher Faulet7969a332015-10-09 11:15:03 +02001973 X509 *cacert = bind_conf->ca_sign_cert;
1974 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001975 SSL_CTX *ssl_ctx = NULL;
1976 X509 *newcrt = NULL;
1977 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001978 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001979 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001980 X509_NAME *name;
1981 const EVP_MD *digest;
1982 X509V3_CTX ctx;
1983 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001984 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001985
Christopher Faulet48a83322017-07-28 16:56:09 +02001986 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001987#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001988 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1989#else
1990 tmp_ssl = SSL_new(bind_conf->default_ctx);
1991 if (tmp_ssl)
1992 pkey = SSL_get_privatekey(tmp_ssl);
1993#endif
1994 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001995 goto mkcert_error;
1996
1997 /* Create the certificate */
1998 if (!(newcrt = X509_new()))
1999 goto mkcert_error;
2000
2001 /* Set version number for the certificate (X509v3) and the serial
2002 * number */
2003 if (X509_set_version(newcrt, 2L) != 1)
2004 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01002005 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002006
2007 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08002008 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
2009 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002010 goto mkcert_error;
2011
2012 /* set public key in the certificate */
2013 if (X509_set_pubkey(newcrt, pkey) != 1)
2014 goto mkcert_error;
2015
2016 /* Set issuer name from the CA */
2017 if (!(name = X509_get_subject_name(cacert)))
2018 goto mkcert_error;
2019 if (X509_set_issuer_name(newcrt, name) != 1)
2020 goto mkcert_error;
2021
2022 /* Set the subject name using the same, but the CN */
2023 name = X509_NAME_dup(name);
2024 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2025 (const unsigned char *)servername,
2026 -1, -1, 0) != 1) {
2027 X509_NAME_free(name);
2028 goto mkcert_error;
2029 }
2030 if (X509_set_subject_name(newcrt, name) != 1) {
2031 X509_NAME_free(name);
2032 goto mkcert_error;
2033 }
2034 X509_NAME_free(name);
2035
2036 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002037 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002038 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2039 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2040 X509_EXTENSION *ext;
2041
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002042 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002043 goto mkcert_error;
2044 if (!X509_add_ext(newcrt, ext, -1)) {
2045 X509_EXTENSION_free(ext);
2046 goto mkcert_error;
2047 }
2048 X509_EXTENSION_free(ext);
2049 }
2050
2051 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002052
2053 key_type = EVP_PKEY_base_id(capkey);
2054
2055 if (key_type == EVP_PKEY_DSA)
2056 digest = EVP_sha1();
2057 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002058 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002059 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002060 digest = EVP_sha256();
2061 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002062#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002063 int nid;
2064
2065 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2066 goto mkcert_error;
2067 if (!(digest = EVP_get_digestbynid(nid)))
2068 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002069#else
2070 goto mkcert_error;
2071#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002072 }
2073
Christopher Faulet31af49d2015-06-09 17:29:50 +02002074 if (!(X509_sign(newcrt, capkey, digest)))
2075 goto mkcert_error;
2076
2077 /* Create and set the new SSL_CTX */
2078 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2079 goto mkcert_error;
2080 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2081 goto mkcert_error;
2082 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2083 goto mkcert_error;
2084 if (!SSL_CTX_check_private_key(ssl_ctx))
2085 goto mkcert_error;
2086
2087 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002088
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002089#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002090 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002091#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002092#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2093 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002094 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002095 EC_KEY *ecc;
2096 int nid;
2097
2098 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2099 goto end;
2100 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2101 goto end;
2102 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2103 EC_KEY_free(ecc);
2104 }
2105#endif
2106 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002107 return ssl_ctx;
2108
2109 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002110 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002111 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002112 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2113 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002114 return NULL;
2115}
2116
Christopher Faulet7969a332015-10-09 11:15:03 +02002117SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002118ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002119{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002120 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002121 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002122
Olivier Houchard66ab4982019-02-26 18:37:15 +01002123 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002124}
2125
Christopher Faulet30548802015-06-11 13:39:32 +02002126/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002127 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002128SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002129ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002130{
2131 struct lru64 *lru = NULL;
2132
2133 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002134 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002135 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002136 if (lru && lru->domain) {
2137 if (ssl)
2138 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002139 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002140 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002141 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002142 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002143 }
2144 return NULL;
2145}
2146
Emeric Brun821bb9b2017-06-15 16:37:39 +02002147/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2148 * function is not thread-safe, it should only be used to check if a certificate
2149 * exists in the lru cache (with no warranty it will not be removed by another
2150 * thread). It is kept for backward compatibility. */
2151SSL_CTX *
2152ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2153{
2154 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2155}
2156
Christopher Fauletd2cab922015-07-28 16:03:47 +02002157/* Set a certificate int the LRU cache used to store generated
2158 * certificate. Return 0 on success, otherwise -1 */
2159int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002160ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002161{
2162 struct lru64 *lru = NULL;
2163
2164 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002165 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002166 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002167 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002168 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002169 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002170 }
Christopher Faulet30548802015-06-11 13:39:32 +02002171 if (lru->domain && lru->data)
2172 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002173 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002174 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002175 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002176 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002177 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002178}
2179
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002180/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002181unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002182ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002183{
2184 return XXH32(data, len, ssl_ctx_lru_seed);
2185}
2186
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002187/* Generate a cert and immediately assign it to the SSL session so that the cert's
2188 * refcount is maintained regardless of the cert's presence in the LRU cache.
2189 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002190static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002191ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002192{
2193 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002194 SSL_CTX *ssl_ctx = NULL;
2195 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002196 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002197
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002198 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002199 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002200 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002201 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002202 if (lru && lru->domain)
2203 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002204 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002205 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002206 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002207 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002208 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002209 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002210 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002211 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002212 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002213 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002214 SSL_set_SSL_CTX(ssl, ssl_ctx);
2215 /* No LRU cache, this CTX will be released as soon as the session dies */
2216 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002217 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002218 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002219 return 0;
2220}
2221static int
2222ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2223{
2224 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002225 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002226
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002227 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002228 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002229 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002230 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002231 }
2232 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002233}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002234#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002235
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002236#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002237typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
2238
2239static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002240{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002241#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002242 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002243 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2244#endif
2245}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002246static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2247 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002248 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2249}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002250static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002251#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002252 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002253 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2254#endif
2255}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002256static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002257#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002258 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002259 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2260#endif
2261}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002262/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002263static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2264/* Unusable in this context. */
2265static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2266static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2267static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2268static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2269static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002270#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002271typedef enum { SET_MIN, SET_MAX } set_context_func;
2272
2273static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2274 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002275 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2276}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002277static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2278 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2279 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2280}
2281static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2282 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002283 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2284}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002285static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2286 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2287 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2288}
2289static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2290 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002291 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2292}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002293static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2294 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2295 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2296}
2297static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2298 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002299 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2300}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002301static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2302 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2303 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2304}
2305static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002306#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002307 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002308 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2309#endif
2310}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002311static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2312#if SSL_OP_NO_TLSv1_3
2313 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2314 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002315#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002316}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002317#endif
2318static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2319static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002320
2321static struct {
2322 int option;
2323 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002324 void (*ctx_set_version)(SSL_CTX *, set_context_func);
2325 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002326 const char *name;
2327} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002328 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2329 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2330 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2331 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2332 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2333 {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 +02002334};
2335
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002336static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2337{
2338 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2339 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2340 SSL_set_SSL_CTX(ssl, ctx);
2341}
2342
Willy Tarreau5db847a2019-05-09 14:13:35 +02002343#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002344
2345static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2346{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002347 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002348 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002349
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002350 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2351 return SSL_TLSEXT_ERR_OK;
2352 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002353}
2354
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002355#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002356static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2357{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002358 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002359#else
2360static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2361{
2362#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002363 struct connection *conn;
2364 struct bind_conf *s;
2365 const uint8_t *extension_data;
2366 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002367 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002368
2369 char *wildp = NULL;
2370 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002371 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002372 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002373 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002374 int i;
2375
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002376 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002377 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002378
Olivier Houchard9679ac92017-10-27 14:58:08 +02002379 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002380 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002381#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002382 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2383 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002384#else
2385 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2386#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002387 /*
2388 * The server_name extension was given too much extensibility when it
2389 * was written, so parsing the normal case is a bit complex.
2390 */
2391 size_t len;
2392 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002393 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002394 /* Extract the length of the supplied list of names. */
2395 len = (*extension_data++) << 8;
2396 len |= *extension_data++;
2397 if (len + 2 != extension_len)
2398 goto abort;
2399 /*
2400 * The list in practice only has a single element, so we only consider
2401 * the first one.
2402 */
2403 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2404 goto abort;
2405 extension_len = len - 1;
2406 /* Now we can finally pull out the byte array with the actual hostname. */
2407 if (extension_len <= 2)
2408 goto abort;
2409 len = (*extension_data++) << 8;
2410 len |= *extension_data++;
2411 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2412 || memchr(extension_data, 0, len) != NULL)
2413 goto abort;
2414 servername = extension_data;
2415 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002416 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002417#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2418 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002419 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002420 }
2421#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002422 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002423 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002424 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002425 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002426 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002427 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002428 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002429 goto abort;
2430 }
2431
2432 /* extract/check clientHello informations */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002433#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002434 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002435#else
2436 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2437#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002438 uint8_t sign;
2439 size_t len;
2440 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002441 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002442 len = (*extension_data++) << 8;
2443 len |= *extension_data++;
2444 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002445 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002446 if (len % 2 != 0)
2447 goto abort;
2448 for (; len > 0; len -= 2) {
2449 extension_data++; /* hash */
2450 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002451 switch (sign) {
2452 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002453 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002454 break;
2455 case TLSEXT_signature_ecdsa:
2456 has_ecdsa_sig = 1;
2457 break;
2458 default:
2459 continue;
2460 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002461 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002462 break;
2463 }
2464 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002465 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002466 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002467 }
2468 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002469 const SSL_CIPHER *cipher;
2470 size_t len;
2471 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002472 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002473#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002474 len = ctx->cipher_suites_len;
2475 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002476#else
2477 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2478#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002479 if (len % 2 != 0)
2480 goto abort;
2481 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002482#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002483 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002484 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002485#else
2486 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2487#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002488 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002489 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002490 break;
2491 }
2492 }
2493 }
2494
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002495 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002496 trash.area[i] = tolower(servername[i]);
2497 if (!wildp && (trash.area[i] == '.'))
2498 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002499 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002500 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002501
William Lallemand150bfa82019-09-19 17:12:49 +02002502 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002503
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002504 for (i = 0; i < 2; i++) {
2505 if (i == 0) /* lookup in full qualified names */
2506 node = ebst_lookup(&s->sni_ctx, trash.area);
2507 else if (i == 1 && wildp) /* lookup in wildcards names */
2508 node = ebst_lookup(&s->sni_w_ctx, wildp);
2509 else
2510 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002511 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002512 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002513 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002514 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002515 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002516 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002517 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002518 break;
2519 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002520 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002521 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002522 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002523 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002524 if (!node_anonymous)
2525 node_anonymous = n;
2526 break;
2527 }
2528 }
2529 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002530 /* select by key_signature priority order */
2531 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2532 : ((has_rsa_sig && node_rsa) ? node_rsa
2533 : (node_anonymous ? node_anonymous
2534 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2535 : node_rsa /* no rsa signature case (far far away) */
2536 )));
2537 if (node) {
2538 /* switch ctx */
2539 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2540 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002541 if (conf) {
2542 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2543 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2544 if (conf->early_data)
2545 allow_early = 1;
2546 }
William Lallemand02010472019-10-18 11:02:19 +02002547 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002548 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002549 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002550 }
William Lallemand150bfa82019-09-19 17:12:49 +02002551
William Lallemand02010472019-10-18 11:02:19 +02002552 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002553#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002554 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002555 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002556 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002557 }
2558#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002559 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002560 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002561 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002562 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002563 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002564 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002565allow_early:
2566#ifdef OPENSSL_IS_BORINGSSL
2567 if (allow_early)
2568 SSL_set_early_data_enabled(ssl, 1);
2569#else
2570 if (!allow_early)
2571 SSL_set_max_early_data(ssl, 0);
2572#endif
2573 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002574 abort:
2575 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2576 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002577#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002578 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002579#else
2580 *al = SSL_AD_UNRECOGNIZED_NAME;
2581 return 0;
2582#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002583}
2584
2585#else /* OPENSSL_IS_BORINGSSL */
2586
Emeric Brunfc0421f2012-09-07 17:30:07 +02002587/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2588 * warning when no match is found, which implies the default (first) cert
2589 * will keep being used.
2590 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002591static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002592{
2593 const char *servername;
2594 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002595 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002596 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002597 int i;
2598 (void)al; /* shut gcc stupid warning */
2599
2600 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002601 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002602#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002603 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2604 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002605#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002606 if (s->strict_sni)
2607 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002608 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002609 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002610 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002611 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002612 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002613
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002614 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002615 if (!servername[i])
2616 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002617 trash.area[i] = tolower(servername[i]);
2618 if (!wildp && (trash.area[i] == '.'))
2619 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002620 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002621 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002622
William Lallemand150bfa82019-09-19 17:12:49 +02002623 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002624 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002625 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002626 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2627 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002628 if (!container_of(n, struct sni_ctx, name)->neg) {
2629 node = n;
2630 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002631 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002632 }
2633 if (!node && wildp) {
2634 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002635 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2636 /* lookup a not neg filter */
2637 if (!container_of(n, struct sni_ctx, name)->neg) {
2638 node = n;
2639 break;
2640 }
2641 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002642 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002643 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002644#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002645 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2646 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002647 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002648 return SSL_TLSEXT_ERR_OK;
2649 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002650#endif
William Lallemand21724f02019-11-04 17:56:13 +01002651 if (s->strict_sni) {
2652 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002653 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002654 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002655 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002656 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002657 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002658 }
2659
2660 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002661 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002662 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002663 return SSL_TLSEXT_ERR_OK;
2664}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002665#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002666#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2667
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002668#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002669
2670static DH * ssl_get_dh_1024(void)
2671{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002672 static unsigned char dh1024_p[]={
2673 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2674 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2675 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2676 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2677 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2678 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2679 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2680 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2681 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2682 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2683 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2684 };
2685 static unsigned char dh1024_g[]={
2686 0x02,
2687 };
2688
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002689 BIGNUM *p;
2690 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002691 DH *dh = DH_new();
2692 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002693 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2694 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002695
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002696 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002697 DH_free(dh);
2698 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002699 } else {
2700 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002701 }
2702 }
2703 return dh;
2704}
2705
2706static DH *ssl_get_dh_2048(void)
2707{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002708 static unsigned char dh2048_p[]={
2709 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2710 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2711 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2712 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2713 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2714 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2715 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2716 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2717 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2718 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2719 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2720 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2721 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2722 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2723 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2724 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2725 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2726 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2727 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2728 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2729 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2730 0xB7,0x1F,0x77,0xF3,
2731 };
2732 static unsigned char dh2048_g[]={
2733 0x02,
2734 };
2735
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002736 BIGNUM *p;
2737 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002738 DH *dh = DH_new();
2739 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002740 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2741 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002742
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002743 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002744 DH_free(dh);
2745 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002746 } else {
2747 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002748 }
2749 }
2750 return dh;
2751}
2752
2753static DH *ssl_get_dh_4096(void)
2754{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002755 static unsigned char dh4096_p[]={
2756 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2757 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2758 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2759 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2760 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2761 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2762 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2763 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2764 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2765 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2766 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2767 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2768 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2769 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2770 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2771 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2772 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2773 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2774 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2775 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2776 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2777 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2778 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2779 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2780 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2781 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2782 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2783 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2784 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2785 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2786 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2787 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2788 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2789 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2790 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2791 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2792 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2793 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2794 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2795 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2796 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2797 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2798 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002799 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002800 static unsigned char dh4096_g[]={
2801 0x02,
2802 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002803
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002804 BIGNUM *p;
2805 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002806 DH *dh = DH_new();
2807 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002808 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2809 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002810
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002811 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002812 DH_free(dh);
2813 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002814 } else {
2815 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002816 }
2817 }
2818 return dh;
2819}
2820
2821/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002822 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002823static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2824{
2825 DH *dh = NULL;
2826 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002827 int type;
2828
2829 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002830
2831 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2832 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2833 */
2834 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2835 keylen = EVP_PKEY_bits(pkey);
2836 }
2837
Willy Tarreauef934602016-12-22 23:12:01 +01002838 if (keylen > global_ssl.default_dh_param) {
2839 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002840 }
2841
Remi Gacogned3a341a2015-05-29 16:26:17 +02002842 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002843 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002844 }
2845 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002846 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002847 }
2848 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002849 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002850 }
2851
2852 return dh;
2853}
2854
Remi Gacogne47783ef2015-05-29 15:53:22 +02002855static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002856{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002857 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002858 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002859
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002860 if (in == NULL)
2861 goto end;
2862
Remi Gacogne47783ef2015-05-29 15:53:22 +02002863 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002864 goto end;
2865
Remi Gacogne47783ef2015-05-29 15:53:22 +02002866 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2867
2868end:
2869 if (in)
2870 BIO_free(in);
2871
Emeric Brune1b4ed42018-08-16 15:14:12 +02002872 ERR_clear_error();
2873
Remi Gacogne47783ef2015-05-29 15:53:22 +02002874 return dh;
2875}
2876
2877int ssl_sock_load_global_dh_param_from_file(const char *filename)
2878{
2879 global_dh = ssl_sock_get_dh_from_file(filename);
2880
2881 if (global_dh) {
2882 return 0;
2883 }
2884
2885 return -1;
2886}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002887#endif
2888
William Lallemand9117de92019-10-04 00:29:42 +02002889/* Alloc and init a ckch_inst */
2890static struct ckch_inst *ckch_inst_new()
2891{
2892 struct ckch_inst *ckch_inst;
2893
2894 ckch_inst = calloc(1, sizeof *ckch_inst);
2895 if (ckch_inst)
2896 LIST_INIT(&ckch_inst->sni_ctx);
2897
2898 return ckch_inst;
2899}
2900
2901
2902/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002903static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002904 struct bind_conf *s, struct ssl_bind_conf *conf,
2905 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002906{
2907 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002908 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002909
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002910 if (*name == '!') {
2911 neg = 1;
2912 name++;
2913 }
2914 if (*name == '*') {
2915 wild = 1;
2916 name++;
2917 }
2918 /* !* filter is a nop */
2919 if (neg && wild)
2920 return order;
2921 if (*name) {
2922 int j, len;
2923 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002924 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002925 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002926 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002927 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002928 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002929
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002930 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002931 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002932 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002933 memcpy(sc->name.key, trash.area, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002934 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002935 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002936 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002937 sc->order = order++;
2938 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002939 sc->wild = wild;
2940 sc->name.node.leaf_p = NULL;
William Lallemand1d29c742019-10-04 00:53:29 +02002941 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002942 }
2943 return order;
2944}
2945
William Lallemand6af03992019-07-23 15:00:54 +02002946/*
William Lallemand1d29c742019-10-04 00:53:29 +02002947 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2948 * This function can't return an error.
2949 *
2950 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2951 */
2952static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2953{
2954
2955 struct sni_ctx *sc0, *sc0b, *sc1;
2956 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002957 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002958
2959 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2960
2961 /* ignore if sc0 was already inserted in a tree */
2962 if (sc0->name.node.leaf_p)
2963 continue;
2964
2965 /* Check for duplicates. */
2966 if (sc0->wild)
2967 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2968 else
2969 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2970
2971 for (; node; node = ebmb_next_dup(node)) {
2972 sc1 = ebmb_entry(node, struct sni_ctx, name);
2973 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2974 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2975 /* it's a duplicate, we should remove and free it */
2976 LIST_DEL(&sc0->by_ckch_inst);
2977 free(sc0);
2978 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002979 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002980 }
2981 }
2982
2983 /* if duplicate, ignore the insertion */
2984 if (!sc0)
2985 continue;
2986
2987 if (sc0->wild)
2988 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
2989 else
2990 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01002991
2992 /* replace the default_ctx if required with the first ctx */
2993 if (ckch_inst->is_default && !def) {
2994 /* we don't need to free the default_ctx because the refcount was not incremented */
2995 bind_conf->default_ctx = sc0->ctx;
2996 def = 1;
2997 }
William Lallemand1d29c742019-10-04 00:53:29 +02002998 }
2999}
3000
3001/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003002 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02003003 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003004struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02003005
William Lallemandfa892222019-07-23 16:06:08 +02003006
Emeric Brun7a883362019-10-17 13:27:40 +02003007/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
3008 * If there is no DH paramater availaible in the ckchs, the global
3009 * DH parameter is loaded into the SSL_CTX and if there is no
3010 * DH parameter available in ckchs nor in global, the default
3011 * DH parameters are applied on the SSL_CTX.
3012 * Returns a bitfield containing the flags:
3013 * ERR_FATAL in any fatal error case
3014 * ERR_ALERT if a reason of the error is availabine in err
3015 * ERR_WARN if a warning is available into err
3016 * The value 0 means there is no error nor warning and
3017 * the operation succeed.
3018 */
William Lallemandfa892222019-07-23 16:06:08 +02003019#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003020static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3021 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003022{
Emeric Brun7a883362019-10-17 13:27:40 +02003023 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003024 DH *dh = NULL;
3025
William Lallemanda8c73742019-07-31 18:31:34 +02003026 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003027 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003028 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3029 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3030 err && *err ? *err : "", path);
3031#if defined(SSL_CTX_set_dh_auto)
3032 SSL_CTX_set_dh_auto(ctx, 1);
3033 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3034 err && *err ? *err : "");
3035#else
3036 memprintf(err, "%s, DH ciphers won't be available.\n",
3037 err && *err ? *err : "");
3038#endif
3039 ret |= ERR_WARN;
3040 goto end;
3041 }
William Lallemandfa892222019-07-23 16:06:08 +02003042
3043 if (ssl_dh_ptr_index >= 0) {
3044 /* store a pointer to the DH params to avoid complaining about
3045 ssl-default-dh-param not being set for this SSL_CTX */
3046 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3047 }
3048 }
3049 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003050 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3051 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3052 err && *err ? *err : "", path);
3053#if defined(SSL_CTX_set_dh_auto)
3054 SSL_CTX_set_dh_auto(ctx, 1);
3055 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3056 err && *err ? *err : "");
3057#else
3058 memprintf(err, "%s, DH ciphers won't be available.\n",
3059 err && *err ? *err : "");
3060#endif
3061 ret |= ERR_WARN;
3062 goto end;
3063 }
William Lallemandfa892222019-07-23 16:06:08 +02003064 }
3065 else {
3066 /* Clear openssl global errors stack */
3067 ERR_clear_error();
3068
3069 if (global_ssl.default_dh_param <= 1024) {
3070 /* we are limited to DH parameter of 1024 bits anyway */
3071 if (local_dh_1024 == NULL)
3072 local_dh_1024 = ssl_get_dh_1024();
3073
Emeric Brun7a883362019-10-17 13:27:40 +02003074 if (local_dh_1024 == NULL) {
3075 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3076 err && *err ? *err : "", path);
3077 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003078 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003079 }
William Lallemandfa892222019-07-23 16:06:08 +02003080
Emeric Bruna9363eb2019-10-17 14:53:03 +02003081 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3082 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3083 err && *err ? *err : "", path);
3084#if defined(SSL_CTX_set_dh_auto)
3085 SSL_CTX_set_dh_auto(ctx, 1);
3086 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3087 err && *err ? *err : "");
3088#else
3089 memprintf(err, "%s, DH ciphers won't be available.\n",
3090 err && *err ? *err : "");
3091#endif
3092 ret |= ERR_WARN;
3093 goto end;
3094 }
William Lallemandfa892222019-07-23 16:06:08 +02003095 }
3096 else {
3097 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3098 }
William Lallemandfa892222019-07-23 16:06:08 +02003099 }
3100
3101end:
William Lallemand4dd145a2020-02-05 11:46:33 +01003102 ERR_clear_error();
William Lallemandfa892222019-07-23 16:06:08 +02003103 return ret;
3104}
3105#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003106
yanbzhu488a4d22015-12-01 15:16:07 -05003107/* Frees the contents of a cert_key_and_chain
3108 */
3109static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3110{
yanbzhu488a4d22015-12-01 15:16:07 -05003111 if (!ckch)
3112 return;
3113
3114 /* Free the certificate and set pointer to NULL */
3115 if (ckch->cert)
3116 X509_free(ckch->cert);
3117 ckch->cert = NULL;
3118
3119 /* Free the key and set pointer to NULL */
3120 if (ckch->key)
3121 EVP_PKEY_free(ckch->key);
3122 ckch->key = NULL;
3123
3124 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003125 if (ckch->chain)
3126 sk_X509_pop_free(ckch->chain, X509_free);
3127 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003128
William Lallemand455af502019-10-17 18:04:45 +02003129 if (ckch->dh)
3130 DH_free(ckch->dh);
3131 ckch->dh = NULL;
3132
3133 if (ckch->sctl) {
3134 free(ckch->sctl->area);
3135 ckch->sctl->area = NULL;
3136 free(ckch->sctl);
3137 ckch->sctl = NULL;
3138 }
3139
3140 if (ckch->ocsp_response) {
3141 free(ckch->ocsp_response->area);
3142 ckch->ocsp_response->area = NULL;
3143 free(ckch->ocsp_response);
3144 ckch->ocsp_response = NULL;
3145 }
William Lallemand5c3c96f2020-01-23 11:53:13 +01003146
3147 if (ckch->ocsp_issuer)
William Lallemanddad239d2020-01-23 11:59:02 +01003148 X509_free(ckch->ocsp_issuer);
William Lallemand5c3c96f2020-01-23 11:53:13 +01003149 ckch->ocsp_issuer = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003150}
3151
William Lallemand8d0f8932019-10-17 18:03:58 +02003152/*
3153 *
3154 * This function copy a cert_key_and_chain in memory
3155 *
3156 * It's used to try to apply changes on a ckch before committing them, because
3157 * most of the time it's not possible to revert those changes
3158 *
3159 * Return a the dst or NULL
3160 */
3161static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3162 struct cert_key_and_chain *dst)
3163{
3164 if (src->cert) {
3165 dst->cert = src->cert;
3166 X509_up_ref(src->cert);
3167 }
3168
3169 if (src->key) {
3170 dst->key = src->key;
3171 EVP_PKEY_up_ref(src->key);
3172 }
3173
3174 if (src->chain) {
3175 dst->chain = X509_chain_up_ref(src->chain);
3176 }
3177
3178 if (src->dh) {
3179 DH_up_ref(src->dh);
3180 dst->dh = src->dh;
3181 }
3182
3183 if (src->sctl) {
3184 struct buffer *sctl;
3185
3186 sctl = calloc(1, sizeof(*sctl));
3187 if (!chunk_dup(sctl, src->sctl)) {
3188 free(sctl);
3189 sctl = NULL;
3190 goto error;
3191 }
3192 dst->sctl = sctl;
3193 }
3194
3195 if (src->ocsp_response) {
3196 struct buffer *ocsp_response;
3197
3198 ocsp_response = calloc(1, sizeof(*ocsp_response));
3199 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3200 free(ocsp_response);
3201 ocsp_response = NULL;
3202 goto error;
3203 }
3204 dst->ocsp_response = ocsp_response;
3205 }
3206
3207 if (src->ocsp_issuer) {
3208 X509_up_ref(src->ocsp_issuer);
3209 dst->ocsp_issuer = src->ocsp_issuer;
3210 }
3211
3212 return dst;
3213
3214error:
3215
3216 /* free everything */
3217 ssl_sock_free_cert_key_and_chain_contents(dst);
3218
3219 return NULL;
3220}
3221
3222
yanbzhu488a4d22015-12-01 15:16:07 -05003223/* checks if a key and cert exists in the ckch
3224 */
William Lallemand1633e392019-09-30 12:58:13 +02003225#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003226static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3227{
3228 return (ckch->cert != NULL && ckch->key != NULL);
3229}
William Lallemand1633e392019-09-30 12:58:13 +02003230#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003231
William Lallemandf9568fc2019-10-16 18:27:58 +02003232/*
3233 * return 0 on success or != 0 on failure
3234 */
3235static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3236{
3237 int ret = 1;
3238 BIO *in = NULL;
3239 X509 *issuer;
3240
3241 if (buf) {
3242 /* reading from a buffer */
3243 in = BIO_new_mem_buf(buf, -1);
3244 if (in == NULL) {
3245 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3246 goto end;
3247 }
3248
3249 } else {
3250 /* reading from a file */
3251 in = BIO_new(BIO_s_file());
3252 if (in == NULL)
3253 goto end;
3254
3255 if (BIO_read_filename(in, path) <= 0)
3256 goto end;
3257 }
3258
3259 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3260 if (!issuer) {
3261 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003262 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003263 goto end;
3264 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003265 /* no error, fill ckch with new context, old context must be free */
3266 if (ckch->ocsp_issuer)
3267 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003268 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003269 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003270
3271end:
3272
3273 ERR_clear_error();
3274 if (in)
3275 BIO_free(in);
3276
3277 return ret;
3278}
3279
William Lallemand96a9c972019-10-17 11:56:17 +02003280
3281/*
3282 * Try to load a PEM file from a <path> or a buffer <buf>
William Lallemand4c5adbf2020-02-24 14:23:22 +01003283 * The PEM must contain at least a Certificate,
3284 * It could contain a DH, a certificate chain and a PrivateKey.
yanbzhu488a4d22015-12-01 15:16:07 -05003285 *
William Lallemand96a9c972019-10-17 11:56:17 +02003286 * If it failed you should not attempt to use the ckch but free it.
3287 *
3288 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003289 */
William Lallemand96a9c972019-10-17 11:56:17 +02003290static 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 -05003291{
William Lallemandf11365b2019-09-19 14:25:58 +02003292 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003293 int ret = 1;
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003294 int i;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003295 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003296 X509 *cert = NULL;
3297 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003298 DH *dh = NULL;
3299 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003300
3301 if (buf) {
3302 /* reading from a buffer */
3303 in = BIO_new_mem_buf(buf, -1);
3304 if (in == NULL) {
3305 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3306 goto end;
3307 }
yanbzhu488a4d22015-12-01 15:16:07 -05003308
William Lallemand96a9c972019-10-17 11:56:17 +02003309 } else {
3310 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003311 in = BIO_new(BIO_s_file());
3312 if (in == NULL)
3313 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003314
William Lallemandf11365b2019-09-19 14:25:58 +02003315 if (BIO_read_filename(in, path) <= 0)
3316 goto end;
William Lallemandf11365b2019-09-19 14:25:58 +02003317 }
yanbzhu488a4d22015-12-01 15:16:07 -05003318
yanbzhu488a4d22015-12-01 15:16:07 -05003319 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003320 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
William Lallemand4c5adbf2020-02-24 14:23:22 +01003321 /* no need to check for errors here, because the private key could be loaded later */
yanbzhu488a4d22015-12-01 15:16:07 -05003322
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003323#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003324 /* Seek back to beginning of file */
3325 if (BIO_reset(in) == -1) {
3326 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3327 err && *err ? *err : "", path);
3328 goto end;
3329 }
3330
William Lallemand96a9c972019-10-17 11:56:17 +02003331 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3332 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003333#endif
William Lallemandfa892222019-07-23 16:06:08 +02003334
Willy Tarreaubb137a82016-04-06 19:02:38 +02003335 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003336 if (BIO_reset(in) == -1) {
3337 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3338 err && *err ? *err : "", path);
3339 goto end;
3340 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003341
3342 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003343 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3344 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003345 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003346 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003347 goto end;
3348 }
3349
William Lallemand96a9c972019-10-17 11:56:17 +02003350 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003351 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3352 if (chain == NULL)
3353 chain = sk_X509_new_null();
3354 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003355 X509_free(ca);
3356 goto end;
3357 }
3358 }
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003359
yanbzhu488a4d22015-12-01 15:16:07 -05003360 ret = ERR_get_error();
3361 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3362 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003363 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003364 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003365 }
3366
William Lallemand75b15f72020-01-23 10:56:05 +01003367 /* once it loaded the PEM, it should remove everything else in the ckch */
3368 if (ckch->ocsp_response) {
3369 free(ckch->ocsp_response->area);
3370 ckch->ocsp_response->area = NULL;
3371 free(ckch->ocsp_response);
3372 ckch->ocsp_response = NULL;
3373 }
3374
3375 if (ckch->sctl) {
3376 free(ckch->sctl->area);
3377 ckch->sctl->area = NULL;
3378 free(ckch->sctl);
3379 ckch->sctl = NULL;
3380 }
3381
3382 if (ckch->ocsp_issuer) {
3383 X509_free(ckch->ocsp_issuer);
3384 ckch->ocsp_issuer = NULL;
3385 }
3386
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003387 /* no error, fill ckch with new context, old context will be free at end: */
3388 SWAP(ckch->key, key);
3389 SWAP(ckch->dh, dh);
3390 SWAP(ckch->cert, cert);
3391 SWAP(ckch->chain, chain);
3392
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003393 /* check if one of the certificate of the chain is the issuer */
3394 for (i = 0; i < sk_X509_num(ckch->chain); i++) {
3395 X509 *issuer = sk_X509_value(ckch->chain, i);
3396 if (X509_check_issued(issuer, ckch->cert) == X509_V_OK) {
3397 ckch->ocsp_issuer = issuer;
3398 X509_up_ref(issuer);
3399 break;
3400 }
3401 }
William Lallemand246c0242019-10-11 08:59:13 +02003402 ret = 0;
3403
William Lallemand96a9c972019-10-17 11:56:17 +02003404end:
William Lallemand246c0242019-10-11 08:59:13 +02003405
3406 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003407 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003408 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003409 if (key)
3410 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003411 if (dh)
3412 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003413 if (cert)
3414 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003415 if (chain)
3416 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003417
William Lallemand96a9c972019-10-17 11:56:17 +02003418 return ret;
3419}
3420
3421/*
William Lallemand4c5adbf2020-02-24 14:23:22 +01003422 * Try to load a private key file from a <path> or a buffer <buf>
3423 *
3424 * If it failed you should not attempt to use the ckch but free it.
3425 *
3426 * Return 0 on success or != 0 on failure
3427 */
3428static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
3429{
3430 BIO *in = NULL;
3431 int ret = 1;
3432 EVP_PKEY *key = NULL;
3433
3434 if (buf) {
3435 /* reading from a buffer */
3436 in = BIO_new_mem_buf(buf, -1);
3437 if (in == NULL) {
3438 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3439 goto end;
3440 }
3441
3442 } else {
3443 /* reading from a file */
3444 in = BIO_new(BIO_s_file());
3445 if (in == NULL)
3446 goto end;
3447
3448 if (BIO_read_filename(in, path) <= 0)
3449 goto end;
3450 }
3451
3452 /* Read Private Key */
3453 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3454 if (key == NULL) {
3455 memprintf(err, "%sunable to load private key from file '%s'.\n",
3456 err && *err ? *err : "", path);
3457 goto end;
3458 }
3459
3460 ret = 0;
3461
3462 SWAP(ckch->key, key);
3463
3464end:
3465
3466 ERR_clear_error();
3467 if (in)
3468 BIO_free(in);
3469 if (key)
3470 EVP_PKEY_free(key);
3471
3472 return ret;
3473}
3474
3475/*
William Lallemand96a9c972019-10-17 11:56:17 +02003476 * Try to load in a ckch every files related to a ckch.
3477 * (PEM, sctl, ocsp, issuer etc.)
3478 *
3479 * This function is only used to load files during the configuration parsing,
3480 * it is not used with the CLI.
3481 *
3482 * This allows us to carry the contents of the file without having to read the
3483 * file multiple times. The caller must call
3484 * ssl_sock_free_cert_key_and_chain_contents.
3485 *
3486 * returns:
3487 * 0 on Success
3488 * 1 on SSL Failure
3489 */
3490static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3491{
3492 int ret = 1;
3493
3494 /* try to load the PEM */
3495 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3496 goto end;
3497 }
3498
William Lallemand4c5adbf2020-02-24 14:23:22 +01003499 /* try to load an external private key if it wasn't in the PEM */
3500 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
3501 char fp[MAXPATHLEN+1];
3502 struct stat st;
3503
3504 snprintf(fp, MAXPATHLEN+1, "%s.key", path);
3505 if (stat(fp, &st) == 0) {
3506 if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) {
3507 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
3508 err && *err ? *err : "", fp);
3509 goto end;
3510 }
3511 }
3512 }
3513
3514 if (ckch->key == NULL) {
3515 memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path);
3516 goto end;
3517 }
3518
3519 if (!X509_check_private_key(ckch->cert, ckch->key)) {
3520 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
3521 err && *err ? *err : "", path);
3522 goto end;
3523 }
3524
William Lallemanda17f4112019-10-10 15:16:44 +02003525#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3526 /* try to load the sctl file */
William Lallemand3af48e72020-02-03 17:15:52 +01003527 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemanda17f4112019-10-10 15:16:44 +02003528 char fp[MAXPATHLEN+1];
3529 struct stat st;
3530
3531 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3532 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003533 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003534 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003535 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003536 ret = 1;
3537 goto end;
3538 }
3539 }
3540 }
3541#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003542
William Lallemand246c0242019-10-11 08:59:13 +02003543 /* try to load an ocsp response file */
William Lallemand3af48e72020-02-03 17:15:52 +01003544 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand246c0242019-10-11 08:59:13 +02003545 char fp[MAXPATHLEN+1];
3546 struct stat st;
3547
3548 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3549 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003550 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003551 ret = 1;
3552 goto end;
3553 }
3554 }
3555 }
3556
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003557#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand3af48e72020-02-03 17:15:52 +01003558 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
William Lallemand246c0242019-10-11 08:59:13 +02003559 /* if no issuer was found, try to load an issuer from the .issuer */
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003560 if (!ckch->ocsp_issuer) {
William Lallemand246c0242019-10-11 08:59:13 +02003561 struct stat st;
3562 char fp[MAXPATHLEN+1];
3563
3564 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3565 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003566 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003567 ret = 1;
3568 goto end;
3569 }
3570
3571 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003572 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003573 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003574 ret = 1;
3575 goto end;
3576 }
3577 } else {
3578 memprintf(err, "%sNo issuer found, cannot use the OCSP response'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003579 err && *err ? *err : "");
William Lallemand246c0242019-10-11 08:59:13 +02003580 ret = 1;
3581 goto end;
3582 }
3583 }
3584 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003585#endif
William Lallemand246c0242019-10-11 08:59:13 +02003586
yanbzhu488a4d22015-12-01 15:16:07 -05003587 ret = 0;
3588
3589end:
3590
3591 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003592
3593 /* Something went wrong in one of the reads */
3594 if (ret != 0)
3595 ssl_sock_free_cert_key_and_chain_contents(ckch);
3596
3597 return ret;
3598}
3599
3600/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003601 * Returns a bitfield containing the flags:
3602 * ERR_FATAL in any fatal error case
3603 * ERR_ALERT if the reason of the error is available in err
3604 * ERR_WARN if a warning is available into err
3605 * The value 0 means there is no error nor warning and
3606 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003607 */
3608static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3609{
Emeric Bruna96b5822019-10-17 13:25:14 +02003610 int errcode = 0;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003611 STACK_OF(X509) *find_chain = NULL;
Emeric Bruna96b5822019-10-17 13:25:14 +02003612
yanbzhu488a4d22015-12-01 15:16:07 -05003613 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3614 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3615 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003616 errcode |= ERR_ALERT | ERR_FATAL;
3617 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003618 }
3619
3620 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3621 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3622 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003623 errcode |= ERR_ALERT | ERR_FATAL;
3624 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003625 }
3626
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003627 if (ckch->chain) {
3628 find_chain = ckch->chain;
3629 } else {
3630 /* Find Certificate Chain in global */
3631 struct issuer_chain *issuer;
3632 issuer = ssl_get_issuer_chain(ckch->cert);
3633 if (issuer)
3634 find_chain = issuer->chain;
3635 }
yanbzhu488a4d22015-12-01 15:16:07 -05003636 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003637#ifdef SSL_CTX_set1_chain
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003638 if (!SSL_CTX_set1_chain(ctx, find_chain)) {
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003639 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
3640 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003641 errcode |= ERR_ALERT | ERR_FATAL;
3642 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003643 }
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003644#else
3645 { /* legacy compat (< openssl 1.0.2) */
3646 X509 *ca;
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003647 STACK_OF(X509) *chain;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003648 chain = X509_chain_up_ref(find_chain);
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003649 while ((ca = sk_X509_shift(chain)))
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003650 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3651 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3652 err && *err ? *err : "", path);
3653 X509_free(ca);
Emmanuel Hocdet140b64f2019-10-24 18:33:10 +02003654 sk_X509_pop_free(chain, X509_free);
Emeric Bruna96b5822019-10-17 13:25:14 +02003655 errcode |= ERR_ALERT | ERR_FATAL;
3656 goto end;
Emmanuel Hocdet1c65fdd2018-12-03 18:07:44 +01003657 }
3658 }
3659#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003660
William Lallemandfa892222019-07-23 16:06:08 +02003661#ifndef OPENSSL_NO_DH
3662 /* store a NULL pointer to indicate we have not yet loaded
3663 a custom DH param file */
3664 if (ssl_dh_ptr_index >= 0) {
3665 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3666 }
3667
Emeric Brun7a883362019-10-17 13:27:40 +02003668 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3669 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003670 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3671 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003672 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003673 }
3674#endif
3675
William Lallemanda17f4112019-10-10 15:16:44 +02003676#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3677 if (sctl_ex_index >= 0 && ckch->sctl) {
3678 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3679 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003680 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003681 errcode |= ERR_ALERT | ERR_FATAL;
3682 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003683 }
3684 }
3685#endif
3686
William Lallemand4a660132019-10-14 14:51:41 +02003687#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003688 /* Load OCSP Info into context */
3689 if (ckch->ocsp_response) {
3690 if (ssl_sock_load_ocsp(ctx, ckch) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003691 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",
3692 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003693 errcode |= ERR_ALERT | ERR_FATAL;
3694 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003695 }
3696 }
William Lallemand246c0242019-10-11 08:59:13 +02003697#endif
3698
Emeric Bruna96b5822019-10-17 13:25:14 +02003699 end:
3700 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003701}
3702
William Lallemandc4ecddf2019-07-31 16:50:08 +02003703#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003704
William Lallemand28a8fce2019-10-04 17:36:55 +02003705static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003706{
3707 struct sni_keytype *s_kt = NULL;
3708 struct ebmb_node *node;
3709 int i;
3710
3711 for (i = 0; i < trash.size; i++) {
3712 if (!str[i])
3713 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003714 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003715 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003716 trash.area[i] = 0;
3717 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003718 if (!node) {
3719 /* CN not found in tree */
3720 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3721 /* Using memcpy here instead of strncpy.
3722 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3723 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3724 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003725 if (!s_kt)
3726 return -1;
3727
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003728 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003729 s_kt->keytypes = 0;
3730 ebst_insert(sni_keytypes, &s_kt->name);
3731 } else {
3732 /* CN found in tree */
3733 s_kt = container_of(node, struct sni_keytype, name);
3734 }
3735
3736 /* Mark that this CN has the keytype of key_index via keytypes mask */
3737 s_kt->keytypes |= 1<<key_index;
3738
William Lallemand28a8fce2019-10-04 17:36:55 +02003739 return 0;
3740
yanbzhu08ce6ab2015-12-02 13:01:29 -05003741}
3742
William Lallemandc4ecddf2019-07-31 16:50:08 +02003743#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003744/*
3745 * Free a ckch_store and its ckch(s)
3746 * The linked ckch_inst are not free'd
3747 */
3748void ckchs_free(struct ckch_store *ckchs)
3749{
3750 if (!ckchs)
3751 return;
3752
3753#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3754 if (ckchs->multi) {
3755 int n;
3756
3757 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3758 ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]);
3759 } else
3760#endif
3761 {
3762 ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch);
3763 ckchs->ckch = NULL;
3764 }
3765
3766 free(ckchs);
3767}
3768
3769/* allocate and duplicate a ckch_store
3770 * Return a new ckch_store or NULL */
3771static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3772{
3773 struct ckch_store *dst;
3774 int pathlen;
3775
3776 pathlen = strlen(src->path);
3777 dst = calloc(1, sizeof(*dst) + pathlen + 1);
3778 if (!dst)
3779 return NULL;
3780 /* copy previous key */
3781 memcpy(dst->path, src->path, pathlen + 1);
3782 dst->multi = src->multi;
3783 LIST_INIT(&dst->ckch_inst);
3784
3785 dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch));
3786 if (!dst->ckch)
3787 goto error;
3788
3789#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3790 if (src->multi) {
3791 int n;
3792
3793 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3794 if (&src->ckch[n]) {
3795 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3796 goto error;
3797 }
3798 }
3799 } else
3800#endif
3801 {
3802 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3803 goto error;
3804 }
3805
3806 return dst;
3807
3808error:
3809 ckchs_free(dst);
3810
3811 return NULL;
3812}
William Lallemandc4ecddf2019-07-31 16:50:08 +02003813
William Lallemand36b84632019-07-18 19:28:17 +02003814/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003815 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02003816 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003817static inline struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02003818{
3819 struct ebmb_node *eb;
3820
William Lallemande3af8fb2019-10-08 11:36:53 +02003821 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02003822 if (!eb)
3823 return NULL;
3824
William Lallemande3af8fb2019-10-08 11:36:53 +02003825 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02003826}
3827
3828/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003829 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02003830 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003831static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02003832{
William Lallemande3af8fb2019-10-08 11:36:53 +02003833 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02003834
William Lallemande3af8fb2019-10-08 11:36:53 +02003835 ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1);
3836 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02003837 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3838 goto end;
3839 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003840 ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1));
William Lallemand36b84632019-07-18 19:28:17 +02003841
William Lallemande3af8fb2019-10-08 11:36:53 +02003842 if (!ckchs->ckch) {
William Lallemand36b84632019-07-18 19:28:17 +02003843 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
3844 goto end;
3845 }
3846
William Lallemand9117de92019-10-04 00:29:42 +02003847 LIST_INIT(&ckchs->ckch_inst);
3848
William Lallemand36b84632019-07-18 19:28:17 +02003849 if (!multi) {
3850
William Lallemand96a9c972019-10-17 11:56:17 +02003851 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003852 goto end;
3853
William Lallemande3af8fb2019-10-08 11:36:53 +02003854 /* insert into the ckchs tree */
3855 memcpy(ckchs->path, path, strlen(path) + 1);
3856 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003857 } else {
3858 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02003859#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3860 char fp[MAXPATHLEN+1] = {0};
3861 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02003862
3863 /* Load all possible certs and keys */
3864 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3865 struct stat buf;
3866 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3867 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02003868 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02003869 goto end;
3870 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02003871 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02003872 }
3873 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02003874#endif
William Lallemand36b84632019-07-18 19:28:17 +02003875
3876 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02003877 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02003878 goto end;
3879 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003880 /* insert into the ckchs tree */
3881 memcpy(ckchs->path, path, strlen(path) + 1);
3882 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02003883 }
William Lallemande3af8fb2019-10-08 11:36:53 +02003884 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003885
William Lallemand36b84632019-07-18 19:28:17 +02003886end:
William Lallemande3af8fb2019-10-08 11:36:53 +02003887 if (ckchs) {
3888 free(ckchs->ckch);
3889 ebmb_delete(&ckchs->node);
William Lallemand6af03992019-07-23 15:00:54 +02003890 }
3891
William Lallemande3af8fb2019-10-08 11:36:53 +02003892 free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02003893
3894 return NULL;
3895}
3896
William Lallemandc4ecddf2019-07-31 16:50:08 +02003897#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3898
William Lallemand36b84632019-07-18 19:28:17 +02003899/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003900 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003901 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003902 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3903 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003904 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003905 *
Emeric Brun054563d2019-10-17 13:16:58 +02003906 * Returns a bitfield containing the flags:
3907 * ERR_FATAL in any fatal error case
3908 * ERR_ALERT if the reason of the error is available in err
3909 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003910 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003911 */
Emeric Brun054563d2019-10-17 13:16:58 +02003912static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3913 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3914 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003915{
William Lallemand36b84632019-07-18 19:28:17 +02003916 int i = 0, n = 0;
3917 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003918 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003919 struct ebmb_node *node;
3920 struct ebmb_node *next;
3921 /* Array of SSL_CTX pointers corresponding to each possible combo
3922 * of keytypes
3923 */
3924 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003925 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003926 X509_NAME *xname = NULL;
3927 char *str = NULL;
3928#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3929 STACK_OF(GENERAL_NAME) *names = NULL;
3930#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003931 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003932
Emeric Brun054563d2019-10-17 13:16:58 +02003933 *ckchi = NULL;
3934
William Lallemande3af8fb2019-10-08 11:36:53 +02003935 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003936 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3937 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003938 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003939 }
3940
3941 ckch_inst = ckch_inst_new();
3942 if (!ckch_inst) {
3943 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3944 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003945 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003946 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003947 }
3948
William Lallemande3af8fb2019-10-08 11:36:53 +02003949 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003950
William Lallemand150bfa82019-09-19 17:12:49 +02003951 /* at least one of the instances is using filters during the config
3952 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01003953 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02003954
yanbzhu08ce6ab2015-12-02 13:01:29 -05003955 /* Process each ckch and update keytypes for each CN/SAN
3956 * for example, if CN/SAN www.a.com is associated with
3957 * certs with keytype 0 and 2, then at the end of the loop,
3958 * www.a.com will have:
3959 * keyindex = 0 | 1 | 4 = 5
3960 */
3961 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003962 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003963
3964 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3965 continue;
3966
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003967 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003968 for (i = 0; i < fcount; i++) {
3969 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3970 if (ret < 0) {
3971 memprintf(err, "%sunable to allocate SSL context.\n",
3972 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003973 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003974 goto end;
3975 }
3976 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003977 } else {
3978 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3979 * so the line that contains logic is marked via comments
3980 */
3981 xname = X509_get_subject_name(certs_and_keys[n].cert);
3982 i = -1;
3983 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3984 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003985 ASN1_STRING *value;
3986 value = X509_NAME_ENTRY_get_data(entry);
3987 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003988 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003989 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003990
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003991 OPENSSL_free(str);
3992 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003993 if (ret < 0) {
3994 memprintf(err, "%sunable to allocate SSL context.\n",
3995 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003996 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003997 goto end;
3998 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003999 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004000 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004001
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004002 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004003#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004004 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
4005 if (names) {
4006 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4007 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004008
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004009 if (name->type == GEN_DNS) {
4010 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
4011 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004012 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004013
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004014 OPENSSL_free(str);
4015 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004016 if (ret < 0) {
4017 memprintf(err, "%sunable to allocate SSL context.\n",
4018 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004019 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004020 goto end;
4021 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004022 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004023 }
4024 }
4025 }
4026 }
4027#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
4028 }
4029
4030 /* If no files found, return error */
4031 if (eb_is_empty(&sni_keytypes_map)) {
4032 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4033 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004034 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004035 goto end;
4036 }
4037
4038 /* We now have a map of CN/SAN to keytypes that are loaded in
4039 * Iterate through the map to create the SSL_CTX's (if needed)
4040 * and add each CTX to the SNI tree
4041 *
4042 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08004043 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05004044 * combination is denoted by the key in the map. Each key
4045 * has a value between 1 and 2^n - 1. Conveniently, the array
4046 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
4047 * entry in the array to correspond to the unique combo (key)
4048 * associated with i. This unique key combo (i) will be associated
4049 * with combos[i-1]
4050 */
4051
4052 node = ebmb_first(&sni_keytypes_map);
4053 while (node) {
4054 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00004055 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004056 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05004057
4058 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
4059 i = container_of(node, struct sni_keytype, name)->keytypes;
4060 cur_ctx = key_combos[i-1].ctx;
4061
4062 if (cur_ctx == NULL) {
4063 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004064 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05004065 if (cur_ctx == NULL) {
4066 memprintf(err, "%sunable to allocate SSL context.\n",
4067 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004068 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004069 goto end;
4070 }
4071
yanbzhube2774d2015-12-10 15:07:30 -05004072 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004073 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4074 if (i & (1<<n)) {
4075 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00004076 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02004077 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
4078 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004079 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004080 }
4081 }
4082
yanbzhu08ce6ab2015-12-02 13:01:29 -05004083 /* Update key_combos */
4084 key_combos[i-1].ctx = cur_ctx;
4085 }
4086
4087 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004088
William Lallemand1d29c742019-10-04 00:53:29 +02004089 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 +02004090 kinfo, str, key_combos[i-1].order);
4091 if (key_combos[i-1].order < 0) {
4092 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004093 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004094 goto end;
4095 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004096 node = ebmb_next(node);
4097 }
4098
4099
4100 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4101 if (!bind_conf->default_ctx) {
4102 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4103 if (key_combos[i].ctx) {
4104 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004105 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004106 ckch_inst->is_default = 1;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004107 break;
4108 }
4109 }
4110 }
4111
William Lallemand614ca0d2019-10-07 13:52:11 +02004112 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004113 ckch_inst->ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004114end:
4115
4116 if (names)
4117 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4118
yanbzhu08ce6ab2015-12-02 13:01:29 -05004119 node = ebmb_first(&sni_keytypes_map);
4120 while (node) {
4121 next = ebmb_next(node);
4122 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004123 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004124 node = next;
4125 }
4126
Emeric Brun054563d2019-10-17 13:16:58 +02004127 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand0c6d12f2019-10-04 18:38:51 +02004128 struct sni_ctx *sc0, *sc0b;
4129
4130 /* free the SSL_CTX in case of error */
4131 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4132 if (key_combos[i].ctx)
4133 SSL_CTX_free(key_combos[i].ctx);
4134 }
4135
4136 /* free the sni_ctx in case of error */
4137 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4138
4139 ebmb_delete(&sc0->name);
4140 LIST_DEL(&sc0->by_ckch_inst);
4141 free(sc0);
4142 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004143 free(ckch_inst);
4144 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004145 }
4146
Emeric Brun054563d2019-10-17 13:16:58 +02004147 *ckchi = ckch_inst;
4148 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004149}
4150#else
4151/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004152static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4153 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4154 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004155{
4156 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4157 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004158 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004159}
4160
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004161#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004162
William Lallemand614ca0d2019-10-07 13:52:11 +02004163/*
4164 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004165 *
4166 * Returns a bitfield containing the flags:
4167 * ERR_FATAL in any fatal error case
4168 * ERR_ALERT if the reason of the error is available in err
4169 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004170 */
Emeric Brun054563d2019-10-17 13:16:58 +02004171static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4172 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004173{
William Lallemandc9402072019-05-15 15:33:54 +02004174 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004175 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004176 int order = 0;
4177 X509_NAME *xname;
4178 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004179 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004180 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004181#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4182 STACK_OF(GENERAL_NAME) *names;
4183#endif
William Lallemand36b84632019-07-18 19:28:17 +02004184 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004185 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004186 int errcode = 0;
4187
4188 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004189
William Lallemande3af8fb2019-10-08 11:36:53 +02004190 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004191 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004192
William Lallemande3af8fb2019-10-08 11:36:53 +02004193 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004194
William Lallemand150bfa82019-09-19 17:12:49 +02004195 /* at least one of the instances is using filters during the config
4196 * parsing, that's ok to inherit this during loading on CLI */
William Lallemand920b0352019-12-04 15:33:01 +01004197 ckchs->filters |= !!fcount;
William Lallemand150bfa82019-09-19 17:12:49 +02004198
William Lallemandc9402072019-05-15 15:33:54 +02004199 ctx = SSL_CTX_new(SSLv23_server_method());
4200 if (!ctx) {
4201 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4202 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004203 errcode |= ERR_ALERT | ERR_FATAL;
4204 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004205 }
4206
Emeric Bruna96b5822019-10-17 13:25:14 +02004207 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4208 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004209 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004210
4211 ckch_inst = ckch_inst_new();
4212 if (!ckch_inst) {
4213 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4214 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004215 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004216 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004217 }
4218
William Lallemand36b84632019-07-18 19:28:17 +02004219 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004220 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004221 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004222 switch(EVP_PKEY_base_id(pkey)) {
4223 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004224 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004225 break;
4226 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004227 kinfo.sig = TLSEXT_signature_ecdsa;
4228 break;
4229 case EVP_PKEY_DSA:
4230 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004231 break;
4232 }
4233 EVP_PKEY_free(pkey);
4234 }
4235
Emeric Brun50bcecc2013-04-22 13:05:23 +02004236 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004237 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004238 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 +02004239 if (order < 0) {
4240 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004241 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004242 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004243 }
4244 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004245 }
4246 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004247#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004248 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004249 if (names) {
4250 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4251 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4252 if (name->type == GEN_DNS) {
4253 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004254 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004255 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004256 if (order < 0) {
4257 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004258 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004259 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004260 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004261 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004262 }
4263 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004264 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004265 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004266#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004267 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004268 i = -1;
4269 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4270 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004271 ASN1_STRING *value;
4272
4273 value = X509_NAME_ENTRY_get_data(entry);
4274 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004275 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004276 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004277 if (order < 0) {
4278 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004279 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004280 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004281 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004282 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004283 }
4284 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004285 /* we must not free the SSL_CTX anymore below, since it's already in
4286 * the tree, so it will be discovered and cleaned in time.
4287 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004288
Emeric Brunfc0421f2012-09-07 17:30:07 +02004289#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004290 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004291 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4292 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004293 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004294 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004295 }
4296#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004297 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004298 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004299 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004300 ckch_inst->is_default = 1;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004301 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004302
William Lallemand9117de92019-10-04 00:29:42 +02004303 /* everything succeed, the ckch instance can be used */
4304 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004305 ckch_inst->ssl_conf = ssl_conf;
William Lallemand9117de92019-10-04 00:29:42 +02004306
Emeric Brun054563d2019-10-17 13:16:58 +02004307 *ckchi = ckch_inst;
4308 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004309
4310error:
4311 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004312 if (ckch_inst) {
William Lallemandd9199372019-10-04 15:37:05 +02004313 struct sni_ctx *sc0, *sc0b;
4314
4315 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
4316
4317 ebmb_delete(&sc0->name);
4318 LIST_DEL(&sc0->by_ckch_inst);
4319 free(sc0);
4320 }
William Lallemand614ca0d2019-10-07 13:52:11 +02004321 free(ckch_inst);
4322 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004323 }
4324 /* We only created 1 SSL_CTX so we can free it there */
4325 SSL_CTX_free(ctx);
4326
Emeric Brun054563d2019-10-17 13:16:58 +02004327 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004328}
4329
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004330/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004331static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4332 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4333 char **sni_filter, int fcount, char **err)
4334{
4335 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004336 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004337
4338 /* we found the ckchs in the tree, we can use it directly */
4339 if (ckchs->multi)
Emeric Brun054563d2019-10-17 13:16:58 +02004340 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 +02004341 else
Emeric Brun054563d2019-10-17 13:16:58 +02004342 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 +02004343
Emeric Brun054563d2019-10-17 13:16:58 +02004344 if (errcode & ERR_CODE)
4345 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004346
4347 ssl_sock_load_cert_sni(ckch_inst, bind_conf);
4348
4349 /* succeed, add the instance to the ckch_store's list of instance */
4350 LIST_ADDQ(&ckchs->ckch_inst, &ckch_inst->by_ckchs);
Emeric Brun054563d2019-10-17 13:16:58 +02004351 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004352}
4353
4354
Willy Tarreaubbc91962019-10-16 16:42:19 +02004355/* Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau03209342016-12-22 17:08:28 +01004356int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004357{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004358 struct dirent **de_list;
4359 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004360 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004361 char *end;
4362 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004363 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004364 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004365#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004366 int is_bundle;
4367 int j;
4368#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004369 if ((ckchs = ckchs_lookup(path))) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004370 /* we found the ckchs in the tree, we can use it directly */
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004371 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand6af03992019-07-23 15:00:54 +02004372 }
4373
yanbzhu08ce6ab2015-12-02 13:01:29 -05004374 if (stat(path, &buf) == 0) {
William Dauchy9a8ef7f2020-01-13 17:52:49 +01004375 if (S_ISDIR(buf.st_mode) == 0) {
William Lallemande3af8fb2019-10-08 11:36:53 +02004376 ckchs = ckchs_load_cert_file(path, 0, err);
4377 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004378 return ERR_ALERT | ERR_FATAL;
4379
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004380 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004381 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004382
yanbzhu08ce6ab2015-12-02 13:01:29 -05004383 /* strip trailing slashes, including first one */
4384 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
4385 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004386
yanbzhu08ce6ab2015-12-02 13:01:29 -05004387 n = scandir(path, &de_list, 0, alphasort);
4388 if (n < 0) {
4389 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4390 err && *err ? *err : "", path, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004391 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004392 }
4393 else {
4394 for (i = 0; i < n; i++) {
4395 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004396
yanbzhu08ce6ab2015-12-02 13:01:29 -05004397 end = strrchr(de->d_name, '.');
William Lallemand3f25ae32020-02-24 16:30:12 +01004398 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
yanbzhu08ce6ab2015-12-02 13:01:29 -05004399 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004400
yanbzhu08ce6ab2015-12-02 13:01:29 -05004401 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4402 if (stat(fp, &buf) != 0) {
4403 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4404 err && *err ? *err : "", fp, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004405 cfgerr |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004406 goto ignore_entry;
4407 }
4408 if (!S_ISREG(buf.st_mode))
4409 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004410
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004411#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004412 is_bundle = 0;
4413 /* Check if current entry in directory is part of a multi-cert bundle */
4414
William Lallemand3af48e72020-02-03 17:15:52 +01004415 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
yanbzhu63ea8462015-12-09 13:35:14 -05004416 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4417 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4418 is_bundle = 1;
4419 break;
4420 }
4421 }
4422
4423 if (is_bundle) {
yanbzhu63ea8462015-12-09 13:35:14 -05004424 int dp_len;
4425
4426 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004427
4428 /* increment i and free de until we get to a non-bundle cert
4429 * Note here that we look at de_list[i + 1] before freeing de
Willy Tarreau05800522019-10-29 10:48:50 +01004430 * this is important since ignore_entry will free de. This also
4431 * guarantees that de->d_name continues to hold the same prefix.
yanbzhu63ea8462015-12-09 13:35:14 -05004432 */
Willy Tarreau05800522019-10-29 10:48:50 +01004433 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
yanbzhu63ea8462015-12-09 13:35:14 -05004434 free(de);
4435 i++;
4436 de = de_list[i];
4437 }
4438
Willy Tarreau05800522019-10-29 10:48:50 +01004439 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemande3af8fb2019-10-08 11:36:53 +02004440 if ((ckchs = ckchs_lookup(fp)) == NULL)
4441 ckchs = ckchs_load_cert_file(fp, 1, err);
4442 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004443 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004444 else
4445 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05004446 /* Successfully processed the bundle */
4447 goto ignore_entry;
4448 }
4449 }
4450
4451#endif
William Lallemande3af8fb2019-10-08 11:36:53 +02004452 if ((ckchs = ckchs_lookup(fp)) == NULL)
4453 ckchs = ckchs_load_cert_file(fp, 0, err);
4454 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004455 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004456 else
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004457 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
William Lallemand36b84632019-07-18 19:28:17 +02004458
yanbzhu08ce6ab2015-12-02 13:01:29 -05004459ignore_entry:
4460 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004461 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004462 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004463 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004464 return cfgerr;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004465
William Lallemand3af48e72020-02-03 17:15:52 +01004466 } else {
4467 /* stat failed */
Willy Tarreaubbc91962019-10-16 16:42:19 +02004468
William Lallemand3af48e72020-02-03 17:15:52 +01004469 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
4470 /* try to load a bundle if it is permitted */
4471 ckchs = ckchs_load_cert_file(path, 1, err);
4472 if (!ckchs)
4473 return ERR_ALERT | ERR_FATAL;
4474 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err);
4475 } else {
4476 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4477 err && *err ? *err : "", fp, strerror(errno));
4478 cfgerr |= ERR_ALERT | ERR_FATAL;
4479 }
4480 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004481
Emeric Brunfc0421f2012-09-07 17:30:07 +02004482 return cfgerr;
4483}
4484
Thierry Fournier383085f2013-01-24 14:15:43 +01004485/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4486 * done once. Zero is returned if the operation fails. No error is returned
4487 * if the random is said as not implemented, because we expect that openssl
4488 * will use another method once needed.
4489 */
4490static int ssl_initialize_random()
4491{
4492 unsigned char random;
4493 static int random_initialized = 0;
4494
4495 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4496 random_initialized = 1;
4497
4498 return random_initialized;
4499}
4500
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004501/* release ssl bind conf */
4502void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004503{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004504 if (conf) {
Bernard Spil13c53f82018-02-15 13:34:58 +01004505#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004506 free(conf->npn_str);
4507 conf->npn_str = NULL;
4508#endif
4509#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4510 free(conf->alpn_str);
4511 conf->alpn_str = NULL;
4512#endif
4513 free(conf->ca_file);
4514 conf->ca_file = NULL;
4515 free(conf->crl_file);
4516 conf->crl_file = NULL;
4517 free(conf->ciphers);
4518 conf->ciphers = NULL;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004519#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004520 free(conf->ciphersuites);
4521 conf->ciphersuites = NULL;
4522#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004523 free(conf->curves);
4524 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004525 free(conf->ecdhe);
4526 conf->ecdhe = NULL;
4527 }
4528}
4529
Willy Tarreaubbc91962019-10-16 16:42:19 +02004530/* Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004531int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
4532{
4533 char thisline[CRT_LINESIZE];
4534 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004535 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004536 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004537 int linenum = 0;
4538 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004539 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004540
Willy Tarreauad1731d2013-04-02 17:35:58 +02004541 if ((f = fopen(file, "r")) == NULL) {
4542 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004543 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004544 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004545
4546 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004547 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004548 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004549 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004550 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004551 char *crt_path;
4552 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004553
4554 linenum++;
4555 end = line + strlen(line);
4556 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4557 /* Check if we reached the limit and the last char is not \n.
4558 * Watch out for the last line without the terminating '\n'!
4559 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004560 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4561 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004562 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004563 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004564 }
4565
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004566 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004567 newarg = 1;
4568 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004569 if (*line == '#' || *line == '\n' || *line == '\r') {
4570 /* end of string, end of loop */
4571 *line = 0;
4572 break;
Willy Tarreauded15b72020-02-25 07:51:59 +01004573 } else if (isspace((unsigned char)*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02004574 newarg = 1;
4575 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004576 } else if (*line == '[') {
4577 if (ssl_b) {
4578 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004579 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004580 break;
4581 }
4582 if (!arg) {
4583 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004584 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004585 break;
4586 }
4587 ssl_b = arg;
4588 newarg = 1;
4589 *line = 0;
4590 } else if (*line == ']') {
4591 if (ssl_e) {
4592 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004593 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun50bcecc2013-04-22 13:05:23 +02004594 break;
4595 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004596 if (!ssl_b) {
4597 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004598 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004599 break;
4600 }
4601 ssl_e = arg;
4602 newarg = 1;
4603 *line = 0;
4604 } else if (newarg) {
4605 if (arg == MAX_CRT_ARGS) {
4606 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004607 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004608 break;
4609 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004610 newarg = 0;
4611 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004612 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02004613 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004614 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004615 if (cfgerr)
4616 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004617 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004618
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004619 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004620 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004621 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004622
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004623 crt_path = args[0];
4624 if (*crt_path != '/' && global_ssl.crt_base) {
4625 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4626 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4627 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004628 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004629 break;
4630 }
4631 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4632 crt_path = path;
4633 }
4634
4635 ssl_conf = calloc(1, sizeof *ssl_conf);
4636 cur_arg = ssl_b ? ssl_b : 1;
4637 while (cur_arg < ssl_e) {
4638 newarg = 0;
4639 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4640 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4641 newarg = 1;
Willy Tarreaubbc91962019-10-16 16:42:19 +02004642 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004643 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4644 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4645 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004646 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004647 }
4648 cur_arg += 1 + ssl_bind_kws[i].skip;
4649 break;
4650 }
4651 }
4652 if (!cfgerr && !newarg) {
4653 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4654 args[cur_arg], linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004655 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004656 break;
4657 }
4658 }
Willy Tarreaubbc91962019-10-16 16:42:19 +02004659
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004660 if (cfgerr) {
4661 ssl_sock_free_ssl_conf(ssl_conf);
4662 free(ssl_conf);
4663 ssl_conf = NULL;
4664 break;
4665 }
4666
William Lallemande3af8fb2019-10-08 11:36:53 +02004667 if ((ckchs = ckchs_lookup(crt_path)) == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004668 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004669 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004670 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004671 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004672 }
4673
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004674 if (!ckchs)
Willy Tarreaubbc91962019-10-16 16:42:19 +02004675 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004676 else
4677 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 +02004678
Willy Tarreauad1731d2013-04-02 17:35:58 +02004679 if (cfgerr) {
4680 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004681 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004682 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004683 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004684 fclose(f);
4685 return cfgerr;
4686}
4687
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004688/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004689static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004690ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004691{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004692 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004693 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004694 SSL_OP_ALL | /* all known workarounds for bugs */
4695 SSL_OP_NO_SSLv2 |
4696 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004697 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02004698 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004699 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02004700 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004701 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004702 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004703 SSL_MODE_ENABLE_PARTIAL_WRITE |
4704 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004705 SSL_MODE_RELEASE_BUFFERS |
4706 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004707 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004708 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004709 int flags = MC_SSL_O_ALL;
4710 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004711
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004712 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004713 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004714
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004715 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004716 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
4717 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4718 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004719 else
4720 flags = conf_ssl_methods->flags;
4721
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004722 min = conf_ssl_methods->min;
4723 max = conf_ssl_methods->max;
4724 /* start with TLSv10 to remove SSLv3 per default */
4725 if (!min && (!max || max >= CONF_TLSV10))
4726 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004727 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004728 if (min)
4729 flags |= (methodVersions[min].flag - 1);
4730 if (max)
4731 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004732 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004733 min = max = CONF_TLSV_NONE;
4734 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004735 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004736 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004737 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004738 if (min) {
4739 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004740 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
4741 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4742 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
4743 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004744 hole = 0;
4745 }
4746 max = i;
4747 }
4748 else {
4749 min = max = i;
4750 }
4751 }
4752 else {
4753 if (min)
4754 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004755 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004756 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004757 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4758 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004759 cfgerr += 1;
4760 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004761 /* save real min/max in bind_conf */
4762 conf_ssl_methods->min = min;
4763 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004764
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004765#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004766 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004767 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004768 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004769 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004770 else
4771 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4772 if (flags & methodVersions[i].flag)
4773 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004774#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004775 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004776 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4777 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004778#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004779
4780 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4781 options |= SSL_OP_NO_TICKET;
4782 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4783 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004784
4785#ifdef SSL_OP_NO_RENEGOTIATION
4786 options |= SSL_OP_NO_RENEGOTIATION;
4787#endif
4788
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004789 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004790
Willy Tarreau5db847a2019-05-09 14:13:35 +02004791#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004792 if (global_ssl.async)
4793 mode |= SSL_MODE_ASYNC;
4794#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004795 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004796 if (global_ssl.life_time)
4797 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004798
4799#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4800#ifdef OPENSSL_IS_BORINGSSL
4801 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4802 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05004803#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01004804 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004805 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004806 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4807 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004808#else
4809 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004810#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004811 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004812#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004813 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004814}
4815
William Lallemand4f45bb92017-10-30 20:08:51 +01004816
4817static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4818{
4819 if (first == block) {
4820 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4821 if (first->len > 0)
4822 sh_ssl_sess_tree_delete(sh_ssl_sess);
4823 }
4824}
4825
4826/* return first block from sh_ssl_sess */
4827static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4828{
4829 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
4830
4831}
4832
4833/* store a session into the cache
4834 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4835 * data: asn1 encoded session
4836 * data_len: asn1 encoded session length
4837 * Returns 1 id session was stored (else 0)
4838 */
4839static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4840{
4841 struct shared_block *first;
4842 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4843
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004844 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004845 if (!first) {
4846 /* Could not retrieve enough free blocks to store that session */
4847 return 0;
4848 }
4849
4850 /* STORE the key in the first elem */
4851 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4852 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4853 first->len = sizeof(struct sh_ssl_sess_hdr);
4854
4855 /* it returns the already existing node
4856 or current node if none, never returns null */
4857 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4858 if (oldsh_ssl_sess != sh_ssl_sess) {
4859 /* NOTE: Row couldn't be in use because we lock read & write function */
4860 /* release the reserved row */
4861 shctx_row_dec_hot(ssl_shctx, first);
4862 /* replace the previous session already in the tree */
4863 sh_ssl_sess = oldsh_ssl_sess;
4864 /* ignore the previous session data, only use the header */
4865 first = sh_ssl_sess_first_block(sh_ssl_sess);
4866 shctx_row_inc_hot(ssl_shctx, first);
4867 first->len = sizeof(struct sh_ssl_sess_hdr);
4868 }
4869
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004870 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004871 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004872 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004873 }
4874
4875 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004876
4877 return 1;
4878}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004879
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004880/* SSL callback used when a new session is created while connecting to a server */
4881static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4882{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004883 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004884 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004885
Willy Tarreau07d94e42018-09-20 10:57:52 +02004886 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004887
Olivier Houcharde6060c52017-11-16 17:42:52 +01004888 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4889 int len;
4890 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004891
Olivier Houcharde6060c52017-11-16 17:42:52 +01004892 len = i2d_SSL_SESSION(sess, NULL);
4893 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4894 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4895 } else {
4896 free(s->ssl_ctx.reused_sess[tid].ptr);
4897 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
4898 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4899 }
4900 if (s->ssl_ctx.reused_sess[tid].ptr) {
4901 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4902 &ptr);
4903 }
4904 } else {
4905 free(s->ssl_ctx.reused_sess[tid].ptr);
4906 s->ssl_ctx.reused_sess[tid].ptr = NULL;
4907 }
4908
4909 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004910}
4911
Olivier Houcharde6060c52017-11-16 17:42:52 +01004912
William Lallemanded0b5ad2017-10-30 19:36:36 +01004913/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004914int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004915{
4916 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4917 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4918 unsigned char *p;
4919 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02004920 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004921 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004922
4923 /* Session id is already stored in to key and session id is known
4924 * so we dont store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02004925 * note: SSL_SESSION_set1_id is using
4926 * a memcpy so we need to use a different pointer
4927 * than sid_data or sid_ctx_data to avoid valgrind
4928 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004929 */
4930
4931 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02004932
4933 /* copy value in an other buffer */
4934 memcpy(encid, sid_data, sid_length);
4935
4936 /* pad with 0 */
4937 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4938 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4939
4940 /* force length to zero to avoid ASN1 encoding */
4941 SSL_SESSION_set1_id(sess, encid, 0);
4942
4943 /* force length to zero to avoid ASN1 encoding */
4944 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004945
4946 /* check if buffer is large enough for the ASN1 encoded session */
4947 data_len = i2d_SSL_SESSION(sess, NULL);
4948 if (data_len > SHSESS_MAX_DATA_LEN)
4949 goto err;
4950
4951 p = encsess;
4952
4953 /* process ASN1 session encoding before the lock */
4954 i2d_SSL_SESSION(sess, &p);
4955
William Lallemanded0b5ad2017-10-30 19:36:36 +01004956
William Lallemanda3c77cf2017-10-30 23:44:40 +01004957 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004958 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004959 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004960 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004961err:
4962 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02004963 SSL_SESSION_set1_id(sess, encid, sid_length);
4964 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004965
4966 return 0; /* do not increment session reference count */
4967}
4968
4969/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004970SSL_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 +01004971{
William Lallemand4f45bb92017-10-30 20:08:51 +01004972 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004973 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4974 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004975 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004976 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004977
4978 global.shctx_lookups++;
4979
4980 /* allow the session to be freed automatically by openssl */
4981 *do_copy = 0;
4982
4983 /* tree key is zeros padded sessionid */
4984 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4985 memcpy(tmpkey, key, key_len);
4986 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4987 key = tmpkey;
4988 }
4989
4990 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004991 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004992
4993 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004994 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4995 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004996 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004997 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004998 global.shctx_misses++;
4999 return NULL;
5000 }
5001
William Lallemand4f45bb92017-10-30 20:08:51 +01005002 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
5003 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005004
William Lallemand4f45bb92017-10-30 20:08:51 +01005005 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 +01005006
William Lallemanda3c77cf2017-10-30 23:44:40 +01005007 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005008
5009 /* decode ASN1 session */
5010 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01005011 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005012 /* Reset session id and session id contenxt */
5013 if (sess) {
5014 SSL_SESSION_set1_id(sess, key, key_len);
5015 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5016 }
5017
5018 return sess;
5019}
5020
William Lallemand4f45bb92017-10-30 20:08:51 +01005021
William Lallemanded0b5ad2017-10-30 19:36:36 +01005022/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005023void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005024{
William Lallemand4f45bb92017-10-30 20:08:51 +01005025 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005026 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
5027 unsigned int sid_length;
5028 const unsigned char *sid_data;
5029 (void)ctx;
5030
5031 sid_data = SSL_SESSION_get_id(sess, &sid_length);
5032 /* tree key is zeros padded sessionid */
5033 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5034 memcpy(tmpkey, sid_data, sid_length);
5035 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
5036 sid_data = tmpkey;
5037 }
5038
William Lallemanda3c77cf2017-10-30 23:44:40 +01005039 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005040
5041 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005042 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
5043 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005044 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005045 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005046 }
5047
5048 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005049 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005050}
5051
5052/* Set session cache mode to server and disable openssl internal cache.
5053 * Set shared cache callbacks on an ssl context.
5054 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01005055void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005056{
5057 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5058
5059 if (!ssl_shctx) {
5060 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
5061 return;
5062 }
5063
5064 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
5065 SSL_SESS_CACHE_NO_INTERNAL |
5066 SSL_SESS_CACHE_NO_AUTO_CLEAR);
5067
5068 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01005069 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
5070 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
5071 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005072}
5073
William Lallemand8b453912019-11-21 15:48:10 +01005074/*
5075 * This function applies the SSL configuration on a SSL_CTX
5076 * It returns an error code and fills the <err> buffer
5077 */
5078int 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 +01005079{
5080 struct proxy *curproxy = bind_conf->frontend;
5081 int cfgerr = 0;
5082 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005083 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005084 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005085#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005086 const char *conf_ciphersuites;
5087#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005088 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005089
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005090 if (ssl_conf) {
5091 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5092 int i, min, max;
5093 int flags = MC_SSL_O_ALL;
5094
5095 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005096 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5097 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005098 if (min)
5099 flags |= (methodVersions[min].flag - 1);
5100 if (max)
5101 flags |= ~((methodVersions[max].flag << 1) - 1);
5102 min = max = CONF_TLSV_NONE;
5103 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5104 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5105 if (min)
5106 max = i;
5107 else
5108 min = max = i;
5109 }
5110 /* save real min/max */
5111 conf_ssl_methods->min = min;
5112 conf_ssl_methods->max = max;
5113 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005114 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5115 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005116 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005117 }
5118 }
5119
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005120 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005121 case SSL_SOCK_VERIFY_NONE:
5122 verify = SSL_VERIFY_NONE;
5123 break;
5124 case SSL_SOCK_VERIFY_OPTIONAL:
5125 verify = SSL_VERIFY_PEER;
5126 break;
5127 case SSL_SOCK_VERIFY_REQUIRED:
5128 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5129 break;
5130 }
5131 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5132 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005133 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
5134 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
5135 if (ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005136 /* set CAfile to verify */
5137 if (!ssl_set_verify_locations_file(ctx, ca_file)) {
5138 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005139 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005140 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005141 }
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005142 if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
5143 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005144 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 +02005145 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005146 }
Emeric Brun850efd52014-01-29 12:24:34 +01005147 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005148 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5149 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005150 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005151 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005152#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005153 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005154 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5155
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005156 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005157 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5158 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005159 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005160 }
Emeric Brun561e5742012-10-02 15:20:55 +02005161 else {
5162 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5163 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005164 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005165#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005166 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005167 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005168#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005169 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005170 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005171 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5172 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005173 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005174 }
5175 }
5176#endif
5177
William Lallemand4f45bb92017-10-30 20:08:51 +01005178 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005179 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5180 if (conf_ciphers &&
5181 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005182 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5183 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005184 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005185 }
5186
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005187#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005188 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5189 if (conf_ciphersuites &&
5190 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005191 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5192 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005193 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005194 }
5195#endif
5196
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005197#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005198 /* If tune.ssl.default-dh-param has not been set,
5199 neither has ssl-default-dh-file and no static DH
5200 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005201 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005202 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005203 (ssl_dh_ptr_index == -1 ||
5204 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005205 STACK_OF(SSL_CIPHER) * ciphers = NULL;
5206 const SSL_CIPHER * cipher = NULL;
5207 char cipher_description[128];
5208 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
5209 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
5210 which is not ephemeral DH. */
5211 const char dhe_description[] = " Kx=DH ";
5212 const char dhe_export_description[] = " Kx=DH(";
5213 int idx = 0;
5214 int dhe_found = 0;
5215 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005216
Remi Gacogne23d5d372014-10-10 17:04:26 +02005217 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005218
Remi Gacogne23d5d372014-10-10 17:04:26 +02005219 if (ssl) {
5220 ciphers = SSL_get_ciphers(ssl);
5221
5222 if (ciphers) {
5223 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
5224 cipher = sk_SSL_CIPHER_value(ciphers, idx);
5225 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
5226 if (strstr(cipher_description, dhe_description) != NULL ||
5227 strstr(cipher_description, dhe_export_description) != NULL) {
5228 dhe_found = 1;
5229 break;
5230 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02005231 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005232 }
5233 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02005234 SSL_free(ssl);
5235 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02005236 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005237
Lukas Tribus90132722014-08-18 00:56:33 +02005238 if (dhe_found) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005239 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",
5240 err && *err ? *err : "");
William Lallemand8b453912019-11-21 15:48:10 +01005241 cfgerr |= ERR_WARN;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005242 }
5243
Willy Tarreauef934602016-12-22 23:12:01 +01005244 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005245 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005246
Willy Tarreauef934602016-12-22 23:12:01 +01005247 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005248 if (local_dh_1024 == NULL) {
5249 local_dh_1024 = ssl_get_dh_1024();
5250 }
Willy Tarreauef934602016-12-22 23:12:01 +01005251 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005252 if (local_dh_2048 == NULL) {
5253 local_dh_2048 = ssl_get_dh_2048();
5254 }
Willy Tarreauef934602016-12-22 23:12:01 +01005255 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005256 if (local_dh_4096 == NULL) {
5257 local_dh_4096 = ssl_get_dh_4096();
5258 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005259 }
5260 }
5261 }
5262#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005263
Emeric Brunfc0421f2012-09-07 17:30:07 +02005264 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005265#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005266 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005267#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005268
Bernard Spil13c53f82018-02-15 13:34:58 +01005269#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005270 ssl_conf_cur = NULL;
5271 if (ssl_conf && ssl_conf->npn_str)
5272 ssl_conf_cur = ssl_conf;
5273 else if (bind_conf->ssl_conf.npn_str)
5274 ssl_conf_cur = &bind_conf->ssl_conf;
5275 if (ssl_conf_cur)
5276 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005277#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005278#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005279 ssl_conf_cur = NULL;
5280 if (ssl_conf && ssl_conf->alpn_str)
5281 ssl_conf_cur = ssl_conf;
5282 else if (bind_conf->ssl_conf.alpn_str)
5283 ssl_conf_cur = &bind_conf->ssl_conf;
5284 if (ssl_conf_cur)
5285 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005286#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005287#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005288 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5289 if (conf_curves) {
5290 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005291 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5292 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005293 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005294 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005295 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005296 }
5297#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005298#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005299 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005300 int i;
5301 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005302#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005303 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005304 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5305 NULL);
5306
5307 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005308 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005309 return cfgerr;
5310 }
5311#else
5312 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5313 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5314 ECDHE_DEFAULT_CURVE);
5315#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005316
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005317 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005318 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005319 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5320 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005321 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005322 }
5323 else {
5324 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5325 EC_KEY_free(ecdh);
5326 }
5327 }
5328#endif
5329
Emeric Brunfc0421f2012-09-07 17:30:07 +02005330 return cfgerr;
5331}
5332
Evan Broderbe554312013-06-27 00:05:25 -07005333static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5334{
5335 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5336 size_t prefixlen, suffixlen;
5337
5338 /* Trivial case */
5339 if (strcmp(pattern, hostname) == 0)
5340 return 1;
5341
Evan Broderbe554312013-06-27 00:05:25 -07005342 /* The rest of this logic is based on RFC 6125, section 6.4.3
5343 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5344
Emeric Bruna848dae2013-10-08 11:27:28 +02005345 pattern_wildcard = NULL;
5346 pattern_left_label_end = pattern;
5347 while (*pattern_left_label_end != '.') {
5348 switch (*pattern_left_label_end) {
5349 case 0:
5350 /* End of label not found */
5351 return 0;
5352 case '*':
5353 /* If there is more than one wildcards */
5354 if (pattern_wildcard)
5355 return 0;
5356 pattern_wildcard = pattern_left_label_end;
5357 break;
5358 }
5359 pattern_left_label_end++;
5360 }
5361
5362 /* If it's not trivial and there is no wildcard, it can't
5363 * match */
5364 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005365 return 0;
5366
5367 /* Make sure all labels match except the leftmost */
5368 hostname_left_label_end = strchr(hostname, '.');
5369 if (!hostname_left_label_end
5370 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5371 return 0;
5372
5373 /* Make sure the leftmost label of the hostname is long enough
5374 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005375 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005376 return 0;
5377
5378 /* Finally compare the string on either side of the
5379 * wildcard */
5380 prefixlen = pattern_wildcard - pattern;
5381 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005382 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5383 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005384 return 0;
5385
5386 return 1;
5387}
5388
5389static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5390{
5391 SSL *ssl;
5392 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005393 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005394 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005395 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005396
5397 int depth;
5398 X509 *cert;
5399 STACK_OF(GENERAL_NAME) *alt_names;
5400 int i;
5401 X509_NAME *cert_subject;
5402 char *str;
5403
5404 if (ok == 0)
5405 return ok;
5406
5407 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005408 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005409 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005410
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005411 /* We're checking if the provided hostnames match the desired one. The
5412 * desired hostname comes from the SNI we presented if any, or if not
5413 * provided then it may have been explicitly stated using a "verifyhost"
5414 * directive. If neither is set, we don't care about the name so the
5415 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005416 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005417 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005418 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005419 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005420 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005421 if (!servername)
5422 return ok;
5423 }
Evan Broderbe554312013-06-27 00:05:25 -07005424
5425 /* We only need to verify the CN on the actual server cert,
5426 * not the indirect CAs */
5427 depth = X509_STORE_CTX_get_error_depth(ctx);
5428 if (depth != 0)
5429 return ok;
5430
5431 /* At this point, the cert is *not* OK unless we can find a
5432 * hostname match */
5433 ok = 0;
5434
5435 cert = X509_STORE_CTX_get_current_cert(ctx);
5436 /* It seems like this might happen if verify peer isn't set */
5437 if (!cert)
5438 return ok;
5439
5440 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5441 if (alt_names) {
5442 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5443 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5444 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005445#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005446 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5447#else
Evan Broderbe554312013-06-27 00:05:25 -07005448 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005449#endif
Evan Broderbe554312013-06-27 00:05:25 -07005450 ok = ssl_sock_srv_hostcheck(str, servername);
5451 OPENSSL_free(str);
5452 }
5453 }
5454 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005455 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005456 }
5457
5458 cert_subject = X509_get_subject_name(cert);
5459 i = -1;
5460 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5461 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005462 ASN1_STRING *value;
5463 value = X509_NAME_ENTRY_get_data(entry);
5464 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005465 ok = ssl_sock_srv_hostcheck(str, servername);
5466 OPENSSL_free(str);
5467 }
5468 }
5469
Willy Tarreau71d058c2017-07-26 20:09:56 +02005470 /* report the mismatch and indicate if SNI was used or not */
5471 if (!ok && !conn->err_code)
5472 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005473 return ok;
5474}
5475
Emeric Brun94324a42012-10-11 14:00:19 +02005476/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005477int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005478{
Willy Tarreau03209342016-12-22 17:08:28 +01005479 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005480 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005481 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005482 SSL_OP_ALL | /* all known workarounds for bugs */
5483 SSL_OP_NO_SSLv2 |
5484 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005485 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005486 SSL_MODE_ENABLE_PARTIAL_WRITE |
5487 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005488 SSL_MODE_RELEASE_BUFFERS |
5489 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005490 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005491 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005492 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005493 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005494 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005495
Thierry Fournier383085f2013-01-24 14:15:43 +01005496 /* Make sure openssl opens /dev/urandom before the chroot */
5497 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005498 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005499 cfgerr++;
5500 }
5501
Willy Tarreaufce03112015-01-15 21:32:40 +01005502 /* Automatic memory computations need to know we use SSL there */
5503 global.ssl_used_backend = 1;
5504
5505 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005506 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005507 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005508 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5509 curproxy->id, srv->id,
5510 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005511 cfgerr++;
5512 return cfgerr;
5513 }
5514 }
Emeric Brun94324a42012-10-11 14:00:19 +02005515 if (srv->use_ssl)
5516 srv->xprt = &ssl_sock;
5517 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01005518 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005519
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005520 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005521 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005522 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5523 proxy_type_str(curproxy), curproxy->id,
5524 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005525 cfgerr++;
5526 return cfgerr;
5527 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005528
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005529 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005530 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5531 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5532 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005533 else
5534 flags = conf_ssl_methods->flags;
5535
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005536 /* Real min and max should be determinate with configuration and openssl's capabilities */
5537 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005538 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005539 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005540 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005541
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005542 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005543 min = max = CONF_TLSV_NONE;
5544 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005545 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005546 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005547 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005548 if (min) {
5549 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005550 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5551 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5552 proxy_type_str(curproxy), curproxy->id, srv->id,
5553 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005554 hole = 0;
5555 }
5556 max = i;
5557 }
5558 else {
5559 min = max = i;
5560 }
5561 }
5562 else {
5563 if (min)
5564 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005565 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005566 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005567 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5568 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005569 cfgerr += 1;
5570 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005571
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005572#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005573 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005574 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005575 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005576 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005577 else
5578 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5579 if (flags & methodVersions[i].flag)
5580 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005581#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005582 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005583 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5584 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005585#endif
5586
5587 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5588 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005589 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005590
Willy Tarreau5db847a2019-05-09 14:13:35 +02005591#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005592 if (global_ssl.async)
5593 mode |= SSL_MODE_ASYNC;
5594#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005595 SSL_CTX_set_mode(ctx, mode);
5596 srv->ssl_ctx.ctx = ctx;
5597
Emeric Bruna7aa3092012-10-26 12:58:00 +02005598 if (srv->ssl_ctx.client_crt) {
5599 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 +01005600 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5601 proxy_type_str(curproxy), curproxy->id,
5602 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005603 cfgerr++;
5604 }
5605 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 +01005606 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5607 proxy_type_str(curproxy), curproxy->id,
5608 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005609 cfgerr++;
5610 }
5611 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005612 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5613 proxy_type_str(curproxy), curproxy->id,
5614 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005615 cfgerr++;
5616 }
5617 }
Emeric Brun94324a42012-10-11 14:00:19 +02005618
Emeric Brun850efd52014-01-29 12:24:34 +01005619 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5620 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005621 switch (srv->ssl_ctx.verify) {
5622 case SSL_SOCK_VERIFY_NONE:
5623 verify = SSL_VERIFY_NONE;
5624 break;
5625 case SSL_SOCK_VERIFY_REQUIRED:
5626 verify = SSL_VERIFY_PEER;
5627 break;
5628 }
Evan Broderbe554312013-06-27 00:05:25 -07005629 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005630 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005631 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005632 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005633 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005634 /* set CAfile to verify */
5635 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
5636 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01005637 curproxy->id, srv->id,
5638 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005639 cfgerr++;
5640 }
5641 }
Emeric Brun850efd52014-01-29 12:24:34 +01005642 else {
5643 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01005644 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",
5645 curproxy->id, srv->id,
5646 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005647 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01005648 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
5649 curproxy->id, srv->id,
5650 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005651 cfgerr++;
5652 }
Emeric Brunef42d922012-10-11 16:11:36 +02005653#ifdef X509_V_FLAG_CRL_CHECK
5654 if (srv->ssl_ctx.crl_file) {
5655 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
5656
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005657 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005658 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
5659 curproxy->id, srv->id,
5660 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005661 cfgerr++;
5662 }
5663 else {
5664 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5665 }
5666 }
5667#endif
5668 }
5669
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005670 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
5671 SSL_SESS_CACHE_NO_INTERNAL_STORE);
5672 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02005673 if (srv->ssl_ctx.ciphers &&
5674 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005675 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
5676 curproxy->id, srv->id,
5677 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02005678 cfgerr++;
5679 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005680
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005681#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005682 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00005683 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005684 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
5685 curproxy->id, srv->id,
5686 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
5687 cfgerr++;
5688 }
5689#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01005690#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
5691 if (srv->ssl_ctx.npn_str)
5692 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
5693#endif
5694#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5695 if (srv->ssl_ctx.alpn_str)
5696 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
5697#endif
5698
Emeric Brun94324a42012-10-11 14:00:19 +02005699
5700 return cfgerr;
5701}
5702
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005703/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005704 * be NULL, in which case nothing is done. Returns the number of errors
5705 * encountered.
5706 */
Willy Tarreau03209342016-12-22 17:08:28 +01005707int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005708{
5709 struct ebmb_node *node;
5710 struct sni_ctx *sni;
5711 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01005712 int errcode = 0;
5713 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005714
Willy Tarreaufce03112015-01-15 21:32:40 +01005715 /* Automatic memory computations need to know we use SSL there */
5716 global.ssl_used_frontend = 1;
5717
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005718 /* Make sure openssl opens /dev/urandom before the chroot */
5719 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005720 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005721 err++;
5722 }
5723 /* Create initial_ctx used to start the ssl connection before do switchctx */
5724 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005725 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005726 /* It should not be necessary to call this function, but it's
5727 necessary first to check and move all initialisation related
5728 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01005729 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005730 }
Emeric Brun0bed9942014-10-30 19:25:24 +01005731 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01005732 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01005733
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005734 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005735 while (node) {
5736 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01005737 if (!sni->order && sni->ctx != bind_conf->default_ctx)
5738 /* only initialize the CTX on its first occurrence and
5739 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005740 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005741 node = ebmb_next(node);
5742 }
5743
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005744 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005745 while (node) {
5746 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01005747 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01005748 /* only initialize the CTX on its first occurrence and
5749 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01005750 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
5751 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005752 node = ebmb_next(node);
5753 }
William Lallemand8b453912019-11-21 15:48:10 +01005754
5755 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005756 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005757 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005758 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005759 err++;
5760 }
5761
5762 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005763 return err;
5764}
5765
Willy Tarreau55d37912016-12-21 23:38:39 +01005766/* Prepares all the contexts for a bind_conf and allocates the shared SSL
5767 * context if needed. Returns < 0 on error, 0 on success. The warnings and
5768 * alerts are directly emitted since the rest of the stack does it below.
5769 */
5770int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
5771{
5772 struct proxy *px = bind_conf->frontend;
5773 int alloc_ctx;
5774 int err;
5775
5776 if (!bind_conf->is_ssl) {
5777 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005778 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
5779 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01005780 }
5781 return 0;
5782 }
5783 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005784 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005785 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
5786 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005787 }
5788 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005789 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
5790 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005791 return -1;
5792 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005793 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005794 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005795 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005796 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01005797 sizeof(*sh_ssl_sess_tree),
5798 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005799 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005800 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5801 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");
5802 else
5803 ha_alert("Unable to allocate SSL session cache.\n");
5804 return -1;
5805 }
5806 /* free block callback */
5807 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5808 /* init the root tree within the extra space */
5809 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5810 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005811 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005812 err = 0;
5813 /* initialize all certificate contexts */
5814 err += ssl_sock_prepare_all_ctx(bind_conf);
5815
5816 /* initialize CA variables if the certificates generation is enabled */
5817 err += ssl_sock_load_ca(bind_conf);
5818
5819 return -err;
5820}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005821
5822/* release ssl context allocated for servers. */
5823void ssl_sock_free_srv_ctx(struct server *srv)
5824{
Olivier Houchardc7566002018-11-20 23:33:50 +01005825#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5826 if (srv->ssl_ctx.alpn_str)
5827 free(srv->ssl_ctx.alpn_str);
5828#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005829#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01005830 if (srv->ssl_ctx.npn_str)
5831 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005832#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005833 if (srv->ssl_ctx.ctx)
5834 SSL_CTX_free(srv->ssl_ctx.ctx);
5835}
5836
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005837/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005838 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5839 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005840void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005841{
5842 struct ebmb_node *node, *back;
5843 struct sni_ctx *sni;
5844
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005845 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005846 while (node) {
5847 sni = ebmb_entry(node, struct sni_ctx, name);
5848 back = ebmb_next(node);
5849 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005850 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005851 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005852 ssl_sock_free_ssl_conf(sni->conf);
5853 free(sni->conf);
5854 sni->conf = NULL;
5855 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005856 free(sni);
5857 node = back;
5858 }
5859
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005860 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005861 while (node) {
5862 sni = ebmb_entry(node, struct sni_ctx, name);
5863 back = ebmb_next(node);
5864 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005865 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02005866 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005867 ssl_sock_free_ssl_conf(sni->conf);
5868 free(sni->conf);
5869 sni->conf = NULL;
5870 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005871 free(sni);
5872 node = back;
5873 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005874 SSL_CTX_free(bind_conf->initial_ctx);
5875 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005876 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005877 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005878}
5879
Willy Tarreau795cdab2016-12-22 17:30:54 +01005880/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5881void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5882{
5883 ssl_sock_free_ca(bind_conf);
5884 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005885 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005886 free(bind_conf->ca_sign_file);
5887 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005888 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005889 free(bind_conf->keys_ref->filename);
5890 free(bind_conf->keys_ref->tlskeys);
5891 LIST_DEL(&bind_conf->keys_ref->list);
5892 free(bind_conf->keys_ref);
5893 }
5894 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005895 bind_conf->ca_sign_pass = NULL;
5896 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005897}
5898
Christopher Faulet31af49d2015-06-09 17:29:50 +02005899/* Load CA cert file and private key used to generate certificates */
5900int
Willy Tarreau03209342016-12-22 17:08:28 +01005901ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005902{
Willy Tarreau03209342016-12-22 17:08:28 +01005903 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005904 FILE *fp;
5905 X509 *cacert = NULL;
5906 EVP_PKEY *capkey = NULL;
5907 int err = 0;
5908
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02005909 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005910 return err;
5911
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005912#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005913 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005914 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005915 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005916 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005917 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005918#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005919
Christopher Faulet31af49d2015-06-09 17:29:50 +02005920 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005921 ha_alert("Proxy '%s': cannot enable certificate generation, "
5922 "no CA certificate File configured at [%s:%d].\n",
5923 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005924 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005925 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005926
5927 /* read in the CA certificate */
5928 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005929 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5930 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005931 goto load_error;
5932 }
5933 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005934 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
5935 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005936 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005937 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005938 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005939 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005940 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
5941 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005942 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005943 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005944
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005945 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005946 bind_conf->ca_sign_cert = cacert;
5947 bind_conf->ca_sign_pkey = capkey;
5948 return err;
5949
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005950 read_error:
5951 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02005952 if (capkey) EVP_PKEY_free(capkey);
5953 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005954 load_error:
5955 bind_conf->generate_certs = 0;
5956 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005957 return err;
5958}
5959
5960/* Release CA cert and private key used to generate certificated */
5961void
5962ssl_sock_free_ca(struct bind_conf *bind_conf)
5963{
Christopher Faulet31af49d2015-06-09 17:29:50 +02005964 if (bind_conf->ca_sign_pkey)
5965 EVP_PKEY_free(bind_conf->ca_sign_pkey);
5966 if (bind_conf->ca_sign_cert)
5967 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01005968 bind_conf->ca_sign_pkey = NULL;
5969 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005970}
5971
Emeric Brun46591952012-05-18 15:47:34 +02005972/*
5973 * This function is called if SSL * context is not yet allocated. The function
5974 * is designed to be called before any other data-layer operation and sets the
5975 * handshake flag on the connection. It is safe to call it multiple times.
5976 * It returns 0 on success and -1 in error case.
5977 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005978static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005979{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005980 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005981 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005982 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005983 return 0;
5984
Willy Tarreau3c728722014-01-23 13:50:42 +01005985 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005986 return 0;
5987
Olivier Houchard66ab4982019-02-26 18:37:15 +01005988 ctx = pool_alloc(ssl_sock_ctx_pool);
5989 if (!ctx) {
5990 conn->err_code = CO_ER_SSL_NO_MEM;
5991 return -1;
5992 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005993 ctx->wait_event.tasklet = tasklet_new();
5994 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005995 conn->err_code = CO_ER_SSL_NO_MEM;
5996 pool_free(ssl_sock_ctx_pool, ctx);
5997 return -1;
5998 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005999 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6000 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006001 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006002 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006003 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006004 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006005 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02006006 ctx->xprt_st = 0;
6007 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006008
6009 /* Only work with sockets for now, this should be adapted when we'll
6010 * add QUIC support.
6011 */
6012 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02006013 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006014 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
6015 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02006016 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006017
Willy Tarreau20879a02012-12-03 16:32:10 +01006018 if (global.maxsslconn && sslconns >= global.maxsslconn) {
6019 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006020 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006021 }
Willy Tarreau403edff2012-09-06 11:58:37 +02006022
Emeric Brun46591952012-05-18 15:47:34 +02006023 /* If it is in client mode initiate SSL session
6024 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006025 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006026 int may_retry = 1;
6027
6028 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02006029 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006030 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
6031 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006032 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006033 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006034 goto retry_connect;
6035 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006036 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006037 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006038 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006039 ctx->bio = BIO_new(ha_meth);
6040 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006041 SSL_free(ctx->ssl);
6042 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006043 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006044 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006045 goto retry_connect;
6046 }
Emeric Brun55476152014-11-12 17:35:37 +01006047 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006048 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006049 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006050 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006051 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006052
Evan Broderbe554312013-06-27 00:05:25 -07006053 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006054 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6055 SSL_free(ctx->ssl);
6056 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01006057 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006058 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006059 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006060 goto retry_connect;
6061 }
Emeric Brun55476152014-11-12 17:35:37 +01006062 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006063 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006064 }
6065
Olivier Houchard66ab4982019-02-26 18:37:15 +01006066 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006067 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6068 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
6069 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 +01006070 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01006071 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006072 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6073 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01006074 } else if (sess) {
6075 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01006076 }
6077 }
Evan Broderbe554312013-06-27 00:05:25 -07006078
Emeric Brun46591952012-05-18 15:47:34 +02006079 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006080 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006081
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006082 _HA_ATOMIC_ADD(&sslconns, 1);
6083 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006084 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006085 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006086 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006087 return 0;
6088 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006089 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006090 int may_retry = 1;
6091
6092 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006093 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006094 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6095 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006096 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006097 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006098 goto retry_accept;
6099 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006100 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006101 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006102 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006103 ctx->bio = BIO_new(ha_meth);
6104 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006105 SSL_free(ctx->ssl);
6106 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006107 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006108 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006109 goto retry_accept;
6110 }
Emeric Brun55476152014-11-12 17:35:37 +01006111 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006112 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006113 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006114 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006115 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006116
Emeric Brune1f38db2012-09-03 20:36:47 +02006117 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006118 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6119 SSL_free(ctx->ssl);
6120 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006121 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006122 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006123 goto retry_accept;
6124 }
Emeric Brun55476152014-11-12 17:35:37 +01006125 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006126 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006127 }
6128
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006129#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6130 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6131 b_alloc(&ctx->early_buf);
6132 SSL_set_max_early_data(ctx->ssl,
6133 /* Only allow early data if we managed to allocate
6134 * a buffer.
6135 */
6136 (!b_is_null(&ctx->early_buf)) ?
6137 global.tune.bufsize - global.tune.maxrewrite : 0);
6138 }
6139#endif
6140
Olivier Houchard66ab4982019-02-26 18:37:15 +01006141 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006142
Emeric Brun46591952012-05-18 15:47:34 +02006143 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006144 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006145#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006146 conn->flags |= CO_FL_EARLY_SSL_HS;
6147#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006148
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006149 _HA_ATOMIC_ADD(&sslconns, 1);
6150 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006151 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006152 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006153 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006154 return 0;
6155 }
6156 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006157 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006158err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006159 if (ctx && ctx->wait_event.tasklet)
6160 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006161 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006162 return -1;
6163}
6164
6165
6166/* This is the callback which is used when an SSL handshake is pending. It
6167 * updates the FD status if it wants some polling before being called again.
6168 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6169 * otherwise it returns non-zero and removes itself from the connection's
6170 * flags (the bit is provided in <flag> by the caller).
6171 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006172static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006173{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006174 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006175 int ret;
6176
Willy Tarreau3c728722014-01-23 13:50:42 +01006177 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006178 return 0;
6179
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006180 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006181 goto out_error;
6182
Willy Tarreau5db847a2019-05-09 14:13:35 +02006183#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006184 /*
6185 * Check if we have early data. If we do, we have to read them
6186 * before SSL_do_handshake() is called, And there's no way to
6187 * detect early data, except to try to read them
6188 */
6189 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006190 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006191
Olivier Houchard54907bb2019-12-19 15:02:39 +01006192 while (1) {
6193 ret = SSL_read_early_data(ctx->ssl,
6194 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6195 &read_data);
6196 if (ret == SSL_READ_EARLY_DATA_ERROR)
6197 goto check_error;
6198 if (read_data > 0) {
6199 conn->flags |= CO_FL_EARLY_DATA;
6200 b_add(&ctx->early_buf, read_data);
6201 }
6202 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6203 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6204 if (!b_data(&ctx->early_buf))
6205 b_free(&ctx->early_buf);
6206 break;
6207 }
6208 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006209 }
6210#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006211 /* If we use SSL_do_handshake to process a reneg initiated by
6212 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6213 * Usually SSL_write and SSL_read are used and process implicitly
6214 * the reneg handshake.
6215 * Here we use SSL_peek as a workaround for reneg.
6216 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006217 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006218 char c;
6219
Olivier Houchard66ab4982019-02-26 18:37:15 +01006220 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006221 if (ret <= 0) {
6222 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006223 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006224
Emeric Brun674b7432012-11-08 19:21:55 +01006225 if (ret == SSL_ERROR_WANT_WRITE) {
6226 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006227 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006228 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006229 return 0;
6230 }
6231 else if (ret == SSL_ERROR_WANT_READ) {
6232 /* handshake may have been completed but we have
6233 * no more data to read.
6234 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006235 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006236 ret = 1;
6237 goto reneg_ok;
6238 }
6239 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006240 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006241 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006242 return 0;
6243 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006244#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006245 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006246 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006247 return 0;
6248 }
6249#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006250 else if (ret == SSL_ERROR_SYSCALL) {
6251 /* if errno is null, then connection was successfully established */
6252 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6253 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006254 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006255#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6256 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006257 conn->err_code = CO_ER_SSL_HANDSHAKE;
6258#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006259 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006260#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006261 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006262 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006263 empty_handshake = state == TLS_ST_BEFORE;
6264#else
Lukas Tribus49799162019-07-08 14:29:15 +02006265 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6266 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006267#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006268 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006269 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006270 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006271 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6272 else
6273 conn->err_code = CO_ER_SSL_EMPTY;
6274 }
6275 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006276 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006277 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6278 else
6279 conn->err_code = CO_ER_SSL_ABORT;
6280 }
6281 }
6282 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006283 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006284 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006285 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006286 conn->err_code = CO_ER_SSL_HANDSHAKE;
6287 }
Lukas Tribus49799162019-07-08 14:29:15 +02006288#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006289 }
Emeric Brun674b7432012-11-08 19:21:55 +01006290 goto out_error;
6291 }
6292 else {
6293 /* Fail on all other handshake errors */
6294 /* Note: OpenSSL may leave unread bytes in the socket's
6295 * buffer, causing an RST to be emitted upon close() on
6296 * TCP sockets. We first try to drain possibly pending
6297 * data to avoid this as much as possible.
6298 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006299 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006300 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006301 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006302 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006303 goto out_error;
6304 }
6305 }
6306 /* read some data: consider handshake completed */
6307 goto reneg_ok;
6308 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006309 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006310check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006311 if (ret != 1) {
6312 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006313 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006314
6315 if (ret == SSL_ERROR_WANT_WRITE) {
6316 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006317 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006318 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006319 return 0;
6320 }
6321 else if (ret == SSL_ERROR_WANT_READ) {
6322 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006323 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006324 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6325 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006326 return 0;
6327 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006328#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006329 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006330 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006331 return 0;
6332 }
6333#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006334 else if (ret == SSL_ERROR_SYSCALL) {
6335 /* if errno is null, then connection was successfully established */
6336 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6337 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006338 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006339#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6340 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006341 conn->err_code = CO_ER_SSL_HANDSHAKE;
6342#else
6343 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006344#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006345 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006346 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006347 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006348#else
Lukas Tribus49799162019-07-08 14:29:15 +02006349 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6350 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006351#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006352 if (empty_handshake) {
6353 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006354 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006355 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6356 else
6357 conn->err_code = CO_ER_SSL_EMPTY;
6358 }
6359 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006360 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006361 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6362 else
6363 conn->err_code = CO_ER_SSL_ABORT;
6364 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006365 }
6366 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006367 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006368 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6369 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006370 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006371 }
Lukas Tribus49799162019-07-08 14:29:15 +02006372#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006373 }
Willy Tarreau89230192012-09-28 20:22:13 +02006374 goto out_error;
6375 }
Emeric Brun46591952012-05-18 15:47:34 +02006376 else {
6377 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006378 /* Note: OpenSSL may leave unread bytes in the socket's
6379 * buffer, causing an RST to be emitted upon close() on
6380 * TCP sockets. We first try to drain possibly pending
6381 * data to avoid this as much as possible.
6382 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006383 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006384 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006385 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006386 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006387 goto out_error;
6388 }
6389 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006390#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006391 else {
6392 /*
6393 * If the server refused the early data, we have to send a
6394 * 425 to the client, as we no longer have the data to sent
6395 * them again.
6396 */
6397 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006398 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006399 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6400 goto out_error;
6401 }
6402 }
6403 }
6404#endif
6405
Emeric Brun46591952012-05-18 15:47:34 +02006406
Emeric Brun674b7432012-11-08 19:21:55 +01006407reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006408
Willy Tarreau5db847a2019-05-09 14:13:35 +02006409#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006410 /* ASYNC engine API doesn't support moving read/write
6411 * buffers. So we disable ASYNC mode right after
6412 * the handshake to avoid buffer oveflows.
6413 */
6414 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006415 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006416#endif
Emeric Brun46591952012-05-18 15:47:34 +02006417 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006418 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006419 if (objt_server(conn->target)) {
6420 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6421 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6422 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006423 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006424 else {
6425 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6426 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6427 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6428 }
Emeric Brun46591952012-05-18 15:47:34 +02006429 }
6430
6431 /* The connection is now established at both layers, it's time to leave */
6432 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6433 return 1;
6434
6435 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006436 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006437 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006438 ERR_clear_error();
6439
Emeric Brun9fa89732012-10-04 17:09:56 +02006440 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006441 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6442 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6443 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006444 }
6445
Emeric Brun46591952012-05-18 15:47:34 +02006446 /* Fail on all other handshake errors */
6447 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006448 if (!conn->err_code)
6449 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006450 return 0;
6451}
6452
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006453/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6454 * event subscriber <es> is not allowed to change from a previous call as long
6455 * as at least one event is still subscribed. The <event_type> must only be a
6456 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6457 * unless the transport layer was already released.
6458 */
6459static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006460{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006461 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006462
Olivier Houchard0ff28652019-06-24 18:57:39 +02006463 if (!ctx)
6464 return -1;
6465
Willy Tarreau113d52b2020-01-10 09:20:26 +01006466 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
6467 BUG_ON(ctx->subs && ctx->subs->events & event_type);
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006468 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006469
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006470 ctx->subs = es;
6471 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006472
6473 /* we may have to subscribe to lower layers for new events */
6474 event_type &= ~ctx->wait_event.events;
6475 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6476 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006477 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006478}
6479
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006480/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6481 * The <es> pointer is not allowed to differ from the one passed to the
6482 * subscribe() call. It always returns zero.
6483 */
6484static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006485{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006486 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006487
Willy Tarreau113d52b2020-01-10 09:20:26 +01006488 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006489 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006490
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006491 es->events &= ~event_type;
6492 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006493 ctx->subs = NULL;
6494
6495 /* If we subscribed, and we're not doing the handshake,
6496 * then we subscribed because the upper layer asked for it,
6497 * as the upper layer is no longer interested, we can
6498 * unsubscribe too.
6499 */
6500 event_type &= ctx->wait_event.events;
6501 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6502 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006503
6504 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006505}
6506
Olivier Houchard2e055482019-05-27 19:50:12 +02006507/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6508 * Returns 0 on success, and non-zero on failure.
6509 */
6510static 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)
6511{
6512 struct ssl_sock_ctx *ctx = xprt_ctx;
6513
6514 if (oldxprt_ops != NULL)
6515 *oldxprt_ops = ctx->xprt;
6516 if (oldxprt_ctx != NULL)
6517 *oldxprt_ctx = ctx->xprt_ctx;
6518 ctx->xprt = toadd_ops;
6519 ctx->xprt_ctx = toadd_ctx;
6520 return 0;
6521}
6522
Olivier Houchard5149b592019-05-23 17:47:36 +02006523/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6524 * return 0, otherwise just call the remove_xprt method from the underlying
6525 * XPRT.
6526 */
6527static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6528{
6529 struct ssl_sock_ctx *ctx = xprt_ctx;
6530
6531 if (ctx->xprt_ctx == toremove_ctx) {
6532 ctx->xprt_ctx = newctx;
6533 ctx->xprt = newops;
6534 return 0;
6535 }
6536 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6537}
6538
Olivier Houchardea8dd942019-05-20 14:02:16 +02006539static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6540{
6541 struct ssl_sock_ctx *ctx = context;
6542
6543 /* First if we're doing an handshake, try that */
6544 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6545 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6546 /* If we had an error, or the handshake is done and I/O is available,
6547 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006548 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006549 * we can't be sure conn_fd_handler() will be called again.
6550 */
6551 if ((ctx->conn->flags & CO_FL_ERROR) ||
6552 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6553 int ret = 0;
6554 int woke = 0;
6555
6556 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006557 if (ctx->subs) {
6558 tasklet_wakeup(ctx->subs->tasklet);
6559 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006560 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006561 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006562 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006563
Olivier Houchardea8dd942019-05-20 14:02:16 +02006564 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006565 * upper layers know. If we have no mux, create it,
6566 * and once we have a mux, call its wake method if we didn't
6567 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006568 */
6569 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006570 if (!ctx->conn->mux)
6571 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006572 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6573 ctx->conn->mux->wake(ctx->conn);
6574 return NULL;
6575 }
6576 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006577#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6578 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006579 else if (b_data(&ctx->early_buf) && ctx->subs &&
6580 ctx->subs->events & SUB_RETRY_RECV) {
6581 tasklet_wakeup(ctx->subs->tasklet);
6582 ctx->subs->events &= ~SUB_RETRY_RECV;
6583 if (!ctx->subs->events)
6584 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006585 }
6586#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006587 return NULL;
6588}
6589
Emeric Brun46591952012-05-18 15:47:34 +02006590/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006591 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006592 * buffer wraps, in which case a second call may be performed. The connection's
6593 * flags are updated with whatever special event is detected (error, read0,
6594 * empty). The caller is responsible for taking care of those events and
6595 * avoiding the call if inappropriate. The function does not call the
6596 * connection's polling update function, so the caller is responsible for this.
6597 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006598static 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 +02006599{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006600 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006601 ssize_t ret;
6602 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006603
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006604 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006605 goto out_error;
6606
Olivier Houchard54907bb2019-12-19 15:02:39 +01006607#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6608 if (b_data(&ctx->early_buf)) {
6609 try = b_contig_space(buf);
6610 if (try > b_data(&ctx->early_buf))
6611 try = b_data(&ctx->early_buf);
6612 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6613 b_add(buf, try);
6614 b_del(&ctx->early_buf, try);
6615 if (b_data(&ctx->early_buf) == 0)
6616 b_free(&ctx->early_buf);
6617 return try;
6618 }
6619#endif
6620
Willy Tarreau911db9b2020-01-23 16:27:54 +01006621 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006622 /* a handshake was requested */
6623 return 0;
6624
Emeric Brun46591952012-05-18 15:47:34 +02006625 /* read the largest possible block. For this, we perform only one call
6626 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6627 * in which case we accept to do it once again. A new attempt is made on
6628 * EINTR too.
6629 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006630 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006631
Willy Tarreau591d4452018-06-15 17:21:00 +02006632 try = b_contig_space(buf);
6633 if (!try)
6634 break;
6635
Willy Tarreauabf08d92014-01-14 11:31:27 +01006636 if (try > count)
6637 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006638
Olivier Houchard66ab4982019-02-26 18:37:15 +01006639 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006640
Emeric Brune1f38db2012-09-03 20:36:47 +02006641 if (conn->flags & CO_FL_ERROR) {
6642 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006643 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006644 }
Emeric Brun46591952012-05-18 15:47:34 +02006645 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006646 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006647 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006648 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006649 }
Emeric Brun46591952012-05-18 15:47:34 +02006650 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006651 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006652 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006653 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02006654 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006655 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006656#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006657 /* Async mode can be re-enabled, because we're leaving data state.*/
6658 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006659 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006660#endif
Emeric Brun46591952012-05-18 15:47:34 +02006661 break;
6662 }
6663 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006664 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006665 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6666 SUB_RETRY_RECV,
6667 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01006668 /* handshake is running, and it may need to re-enable read */
6669 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006670#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006671 /* Async mode can be re-enabled, because we're leaving data state.*/
6672 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006673 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006674#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006675 break;
6676 }
Emeric Brun46591952012-05-18 15:47:34 +02006677 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006678 } else if (ret == SSL_ERROR_ZERO_RETURN)
6679 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006680 /* For SSL_ERROR_SYSCALL, make sure to clear the error
6681 * stack before shutting down the connection for
6682 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006683 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
6684 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02006685 /* otherwise it's a real error */
6686 goto out_error;
6687 }
6688 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006689 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006690 return done;
6691
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006692 clear_ssl_error:
6693 /* Clear openssl global errors stack */
6694 ssl_sock_dump_errors(conn);
6695 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02006696 read0:
6697 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006698 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006699
Emeric Brun46591952012-05-18 15:47:34 +02006700 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006701 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01006702 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006703 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006704 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006705 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006706}
6707
6708
Willy Tarreau787db9a2018-06-14 18:31:46 +02006709/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
6710 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
6711 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02006712 * Only one call to send() is performed, unless the buffer wraps, in which case
6713 * a second call may be performed. The connection's flags are updated with
6714 * whatever special event is detected (error, empty). The caller is responsible
6715 * for taking care of those events and avoiding the call if inappropriate. The
6716 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02006717 * is responsible for this. The buffer's output is not adjusted, it's up to the
6718 * caller to take care of this. It's up to the caller to update the buffer's
6719 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02006720 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006721static 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 +02006722{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006723 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006724 ssize_t ret;
6725 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02006726
6727 done = 0;
6728
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006729 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006730 goto out_error;
6731
Willy Tarreau911db9b2020-01-23 16:27:54 +01006732 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006733 /* a handshake was requested */
6734 return 0;
6735
6736 /* send the largest possible block. For this we perform only one call
6737 * to send() unless the buffer wraps and we exactly fill the first hunk,
6738 * in which case we accept to do it once again.
6739 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02006740 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02006741#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006742 size_t written_data;
6743#endif
6744
Willy Tarreau787db9a2018-06-14 18:31:46 +02006745 try = b_contig_data(buf, done);
6746 if (try > count)
6747 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01006748
Willy Tarreau7bed9452014-02-02 02:00:24 +01006749 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006750 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01006751 global_ssl.max_record && try > global_ssl.max_record) {
6752 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006753 }
6754 else {
6755 /* we need to keep the information about the fact that
6756 * we're not limiting the upcoming send(), because if it
6757 * fails, we'll have to retry with at least as many data.
6758 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006759 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006760 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01006761
Willy Tarreau5db847a2019-05-09 14:13:35 +02006762#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02006763 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006764 unsigned int max_early;
6765
Olivier Houchard522eea72017-11-03 16:27:47 +01006766 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006767 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006768 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006769 if (SSL_get0_session(ctx->ssl))
6770 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006771 else
6772 max_early = 0;
6773 }
6774
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006775 if (try + ctx->sent_early_data > max_early) {
6776 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006777 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006778 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006779 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006780 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006781 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006782 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006783 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006784 if (ret == 1) {
6785 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006786 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006787 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006788 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006789 /* Initiate the handshake, now */
6790 tasklet_wakeup(ctx->wait_event.tasklet);
6791 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006792
Olivier Houchardc2aae742017-09-22 18:26:28 +02006793 }
6794
6795 } else
6796#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006797 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006798
Emeric Brune1f38db2012-09-03 20:36:47 +02006799 if (conn->flags & CO_FL_ERROR) {
6800 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006801 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006802 }
Emeric Brun46591952012-05-18 15:47:34 +02006803 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006804 /* A send succeeded, so we can consider ourself connected */
6805 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006806 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006807 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006808 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006809 }
6810 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006811 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006812
Emeric Brun46591952012-05-18 15:47:34 +02006813 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006814 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006815 /* handshake is running, and it may need to re-enable write */
6816 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006817 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006818#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006819 /* Async mode can be re-enabled, because we're leaving data state.*/
6820 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006821 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006822#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006823 break;
6824 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006825
Emeric Brun46591952012-05-18 15:47:34 +02006826 break;
6827 }
6828 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006829 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006830 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006831 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6832 SUB_RETRY_RECV,
6833 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006834#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006835 /* Async mode can be re-enabled, because we're leaving data state.*/
6836 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006837 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006838#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006839 break;
6840 }
Emeric Brun46591952012-05-18 15:47:34 +02006841 goto out_error;
6842 }
6843 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006844 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006845 return done;
6846
6847 out_error:
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 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006853 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006854}
6855
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006856static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006857
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006858 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006859
Olivier Houchardea8dd942019-05-20 14:02:16 +02006860
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006861 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006862 if (ctx->wait_event.events != 0)
6863 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6864 ctx->wait_event.events,
6865 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01006866 if (ctx->subs) {
6867 ctx->subs->events = 0;
6868 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006869 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006870
Olivier Houchard692c1d02019-05-23 18:41:47 +02006871 if (ctx->xprt->close)
6872 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02006873#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02006874 if (global_ssl.async) {
6875 OSSL_ASYNC_FD all_fd[32], afd;
6876 size_t num_all_fds = 0;
6877 int i;
6878
Olivier Houchard66ab4982019-02-26 18:37:15 +01006879 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006880 if (num_all_fds > 32) {
6881 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6882 return;
6883 }
6884
Olivier Houchard66ab4982019-02-26 18:37:15 +01006885 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006886
6887 /* If an async job is pending, we must try to
6888 to catch the end using polling before calling
6889 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006890 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006891 for (i=0 ; i < num_all_fds ; i++) {
6892 /* switch on an handler designed to
6893 * handle the SSL_free
6894 */
6895 afd = all_fd[i];
6896 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006897 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006898 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006899 /* To ensure that the fd cache won't be used
6900 * and we'll catch a real RD event.
6901 */
6902 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006903 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006904 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006905 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006906 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006907 return;
6908 }
Emeric Brun3854e012017-05-17 20:42:48 +02006909 /* Else we can remove the fds from the fdtab
6910 * and call SSL_free.
6911 * note: we do a fd_remove and not a delete
6912 * because the fd is owned by the engine.
6913 * the engine is responsible to close
6914 */
6915 for (i=0 ; i < num_all_fds ; i++)
6916 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006917 }
6918#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006919 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01006920 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006921 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006922 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006923 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006924 }
Emeric Brun46591952012-05-18 15:47:34 +02006925}
6926
6927/* This function tries to perform a clean shutdown on an SSL connection, and in
6928 * any case, flags the connection as reusable if no handshake was in progress.
6929 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006930static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006931{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006932 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006933
Willy Tarreau911db9b2020-01-23 16:27:54 +01006934 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006935 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006936 if (!clean)
6937 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006938 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006939 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006940 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006941 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006942 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006943 ERR_clear_error();
6944 }
Emeric Brun46591952012-05-18 15:47:34 +02006945}
6946
William Lallemandd4f946c2019-12-05 10:26:40 +01006947/* fill a buffer with the algorithm and size of a public key */
6948static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006949{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006950 int bits = 0;
6951 int sig = TLSEXT_signature_anonymous;
6952 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006953 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006954
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006955 pkey = X509_get_pubkey(crt);
6956 if (pkey) {
6957 bits = EVP_PKEY_bits(pkey);
6958 switch(EVP_PKEY_base_id(pkey)) {
6959 case EVP_PKEY_RSA:
6960 sig = TLSEXT_signature_rsa;
6961 break;
6962 case EVP_PKEY_EC:
6963 sig = TLSEXT_signature_ecdsa;
6964 break;
6965 case EVP_PKEY_DSA:
6966 sig = TLSEXT_signature_dsa;
6967 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006968 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01006969 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006970 }
6971
6972 switch(sig) {
6973 case TLSEXT_signature_rsa:
6974 len = chunk_printf(out, "RSA%d", bits);
6975 break;
6976 case TLSEXT_signature_ecdsa:
6977 len = chunk_printf(out, "EC%d", bits);
6978 break;
6979 case TLSEXT_signature_dsa:
6980 len = chunk_printf(out, "DSA%d", bits);
6981 break;
6982 default:
6983 return 0;
6984 }
6985 if (len < 0)
6986 return 0;
6987 return 1;
6988}
6989
William Lallemandd4f946c2019-12-05 10:26:40 +01006990/* used for ppv2 pkey alog (can be used for logging) */
6991int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
6992{
6993 struct ssl_sock_ctx *ctx;
6994 X509 *crt;
6995
6996 if (!ssl_sock_is_ssl(conn))
6997 return 0;
6998
6999 ctx = conn->xprt_ctx;
7000
7001 crt = SSL_get_certificate(ctx->ssl);
7002 if (!crt)
7003 return 0;
7004
7005 return cert_get_pkey_algo(crt, out);
7006}
7007
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007008/* used for ppv2 cert signature (can be used for logging) */
7009const char *ssl_sock_get_cert_sig(struct connection *conn)
7010{
Christopher Faulet82004142019-09-10 10:12:03 +02007011 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007012
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007013 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
7014 X509 *crt;
7015
7016 if (!ssl_sock_is_ssl(conn))
7017 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007018 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007019 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007020 if (!crt)
7021 return NULL;
7022 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7023 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
7024}
7025
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007026/* used for ppv2 authority */
7027const char *ssl_sock_get_sni(struct connection *conn)
7028{
7029#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007030 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007031
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007032 if (!ssl_sock_is_ssl(conn))
7033 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007034 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007035 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007036#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007037 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007038#endif
7039}
7040
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007041/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007042const char *ssl_sock_get_cipher_name(struct connection *conn)
7043{
Christopher Faulet82004142019-09-10 10:12:03 +02007044 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007045
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007046 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007047 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007048 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007049 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007050}
7051
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007052/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007053const char *ssl_sock_get_proto_version(struct connection *conn)
7054{
Christopher Faulet82004142019-09-10 10:12:03 +02007055 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007056
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007057 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007058 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007059 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007060 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007061}
7062
Willy Tarreau8d598402012-10-22 17:58:39 +02007063/* Extract a serial from a cert, and copy it to a chunk.
7064 * Returns 1 if serial is found and copied, 0 if no serial found and
7065 * -1 if output is not large enough.
7066 */
7067static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007068ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02007069{
7070 ASN1_INTEGER *serial;
7071
7072 serial = X509_get_serialNumber(crt);
7073 if (!serial)
7074 return 0;
7075
7076 if (out->size < serial->length)
7077 return -1;
7078
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007079 memcpy(out->area, serial->data, serial->length);
7080 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007081 return 1;
7082}
7083
Emeric Brun43e79582014-10-29 19:03:26 +01007084/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007085 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7086 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007087 */
7088static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007089ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007090{
7091 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007092 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007093
7094 len =i2d_X509(crt, NULL);
7095 if (len <= 0)
7096 return 1;
7097
7098 if (out->size < len)
7099 return -1;
7100
7101 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007102 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007103 return 1;
7104}
7105
Emeric Brunce5ad802012-10-22 14:11:22 +02007106
Willy Tarreau83061a82018-07-13 11:56:34 +02007107/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007108 * Returns 1 if serial is found and copied, 0 if no valid time found
7109 * and -1 if output is not large enough.
7110 */
7111static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007112ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007113{
7114 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7115 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7116
7117 if (gentm->length < 12)
7118 return 0;
7119 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7120 return 0;
7121 if (out->size < gentm->length-2)
7122 return -1;
7123
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007124 memcpy(out->area, gentm->data+2, gentm->length-2);
7125 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007126 return 1;
7127 }
7128 else if (tm->type == V_ASN1_UTCTIME) {
7129 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7130
7131 if (utctm->length < 10)
7132 return 0;
7133 if (utctm->data[0] >= 0x35)
7134 return 0;
7135 if (out->size < utctm->length)
7136 return -1;
7137
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007138 memcpy(out->area, utctm->data, utctm->length);
7139 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007140 return 1;
7141 }
7142
7143 return 0;
7144}
7145
Emeric Brun87855892012-10-17 17:39:35 +02007146/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7147 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7148 */
7149static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007150ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7151 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007152{
7153 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007154 ASN1_OBJECT *obj;
7155 ASN1_STRING *data;
7156 const unsigned char *data_ptr;
7157 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007158 int i, j, n;
7159 int cur = 0;
7160 const char *s;
7161 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007162 int name_count;
7163
7164 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007165
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007166 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007167 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007168 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007169 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007170 else
7171 j = i;
7172
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007173 ne = X509_NAME_get_entry(a, j);
7174 obj = X509_NAME_ENTRY_get_object(ne);
7175 data = X509_NAME_ENTRY_get_data(ne);
7176 data_ptr = ASN1_STRING_get0_data(data);
7177 data_len = ASN1_STRING_length(data);
7178 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007179 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007180 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007181 s = tmp;
7182 }
7183
7184 if (chunk_strcasecmp(entry, s) != 0)
7185 continue;
7186
7187 if (pos < 0)
7188 cur--;
7189 else
7190 cur++;
7191
7192 if (cur != pos)
7193 continue;
7194
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007195 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007196 return -1;
7197
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007198 memcpy(out->area, data_ptr, data_len);
7199 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007200 return 1;
7201 }
7202
7203 return 0;
7204
William Lallemandd4f946c2019-12-05 10:26:40 +01007205}
7206
7207/*
7208 * Extract and format the DNS SAN extensions and copy result into a chuink
7209 * Return 0;
7210 */
7211#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7212static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7213{
7214 int i;
7215 char *str;
7216 STACK_OF(GENERAL_NAME) *names = NULL;
7217
7218 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7219 if (names) {
7220 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7221 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7222 if (i > 0)
7223 chunk_appendf(out, ", ");
7224 if (name->type == GEN_DNS) {
7225 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7226 chunk_appendf(out, "DNS:%s", str);
7227 OPENSSL_free(str);
7228 }
7229 }
7230 }
7231 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7232 }
7233 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007234}
William Lallemandd4f946c2019-12-05 10:26:40 +01007235#endif
Emeric Brun87855892012-10-17 17:39:35 +02007236
Elliot Otchet71f82972020-01-15 08:12:14 -05007237/*
7238 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7239 * Currently supports rfc2253 for returning LDAP V3 DNs.
7240 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7241 */
7242static int
7243ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7244{
7245 BIO *bio = NULL;
7246 int ret = 0;
7247 int data_len = 0;
7248
7249 if (chunk_strcmp(format, "rfc2253") == 0) {
7250 bio = BIO_new(BIO_s_mem());
7251 if (bio == NULL)
7252 goto out;
7253
7254 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7255 goto out;
7256
7257 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7258 goto out;
7259
7260 out->data = data_len;
7261
7262 ret = 1;
7263 }
7264out:
7265 if (bio)
7266 BIO_free(bio);
7267 return ret;
7268}
7269
Emeric Brun87855892012-10-17 17:39:35 +02007270/* Extract and format full DN from a X509_NAME and copy result into a chunk
7271 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7272 */
7273static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007274ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007275{
7276 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007277 ASN1_OBJECT *obj;
7278 ASN1_STRING *data;
7279 const unsigned char *data_ptr;
7280 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007281 int i, n, ln;
7282 int l = 0;
7283 const char *s;
7284 char *p;
7285 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007286 int name_count;
7287
7288
7289 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007290
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007291 out->data = 0;
7292 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007293 for (i = 0; i < name_count; i++) {
7294 ne = X509_NAME_get_entry(a, i);
7295 obj = X509_NAME_ENTRY_get_object(ne);
7296 data = X509_NAME_ENTRY_get_data(ne);
7297 data_ptr = ASN1_STRING_get0_data(data);
7298 data_len = ASN1_STRING_length(data);
7299 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007300 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007301 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007302 s = tmp;
7303 }
7304 ln = strlen(s);
7305
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007306 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007307 if (l > out->size)
7308 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007309 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007310
7311 *(p++)='/';
7312 memcpy(p, s, ln);
7313 p += ln;
7314 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007315 memcpy(p, data_ptr, data_len);
7316 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007317 }
7318
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007319 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007320 return 0;
7321
7322 return 1;
7323}
7324
Olivier Houchardab28a322018-12-21 19:45:40 +01007325void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7326{
7327#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007328 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007329
Olivier Houcharde488ea82019-06-28 14:10:33 +02007330 if (!ssl_sock_is_ssl(conn))
7331 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007332 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007333 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007334#endif
7335}
7336
Willy Tarreau119a4082016-12-22 21:58:38 +01007337/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7338 * to disable SNI.
7339 */
Willy Tarreau63076412015-07-10 11:33:32 +02007340void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7341{
7342#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007343 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007344
Willy Tarreau119a4082016-12-22 21:58:38 +01007345 char *prev_name;
7346
Willy Tarreau63076412015-07-10 11:33:32 +02007347 if (!ssl_sock_is_ssl(conn))
7348 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007349 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007350
Willy Tarreau119a4082016-12-22 21:58:38 +01007351 /* if the SNI changes, we must destroy the reusable context so that a
7352 * new connection will present a new SNI. As an optimization we could
7353 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7354 * server.
7355 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007356 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007357 if ((!prev_name && hostname) ||
7358 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007359 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007360
Olivier Houchard66ab4982019-02-26 18:37:15 +01007361 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007362#endif
7363}
7364
Emeric Brun0abf8362014-06-24 18:26:41 +02007365/* Extract peer certificate's common name into the chunk dest
7366 * Returns
7367 * the len of the extracted common name
7368 * or 0 if no CN found in DN
7369 * or -1 on error case (i.e. no peer certificate)
7370 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007371int ssl_sock_get_remote_common_name(struct connection *conn,
7372 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007373{
Christopher Faulet82004142019-09-10 10:12:03 +02007374 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007375 X509 *crt = NULL;
7376 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007377 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007378 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007379 .area = (char *)&find_cn,
7380 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007381 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007382 int result = -1;
David Safb76832014-05-08 23:42:08 -04007383
7384 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007385 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007386 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007387
7388 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007389 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007390 if (!crt)
7391 goto out;
7392
7393 name = X509_get_subject_name(crt);
7394 if (!name)
7395 goto out;
David Safb76832014-05-08 23:42:08 -04007396
Emeric Brun0abf8362014-06-24 18:26:41 +02007397 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7398out:
David Safb76832014-05-08 23:42:08 -04007399 if (crt)
7400 X509_free(crt);
7401
7402 return result;
7403}
7404
Dave McCowan328fb582014-07-30 10:39:13 -04007405/* returns 1 if client passed a certificate for this session, 0 if not */
7406int ssl_sock_get_cert_used_sess(struct connection *conn)
7407{
Christopher Faulet82004142019-09-10 10:12:03 +02007408 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007409 X509 *crt = NULL;
7410
7411 if (!ssl_sock_is_ssl(conn))
7412 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007413 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007414
7415 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007416 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007417 if (!crt)
7418 return 0;
7419
7420 X509_free(crt);
7421 return 1;
7422}
7423
7424/* returns 1 if client passed a certificate for this connection, 0 if not */
7425int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007426{
Christopher Faulet82004142019-09-10 10:12:03 +02007427 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007428
David Safb76832014-05-08 23:42:08 -04007429 if (!ssl_sock_is_ssl(conn))
7430 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007431 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007432 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007433}
7434
7435/* returns result from SSL verify */
7436unsigned int ssl_sock_get_verify_result(struct connection *conn)
7437{
Christopher Faulet82004142019-09-10 10:12:03 +02007438 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007439
David Safb76832014-05-08 23:42:08 -04007440 if (!ssl_sock_is_ssl(conn))
7441 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007442 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007443 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007444}
7445
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007446/* Returns the application layer protocol name in <str> and <len> when known.
7447 * Zero is returned if the protocol name was not found, otherwise non-zero is
7448 * returned. The string is allocated in the SSL context and doesn't have to be
7449 * freed by the caller. NPN is also checked if available since older versions
7450 * of openssl (1.0.1) which are more common in field only support this one.
7451 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007452static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007453{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007454#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7455 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007456 struct ssl_sock_ctx *ctx = xprt_ctx;
7457 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007458 return 0;
7459
7460 *str = NULL;
7461
7462#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007463 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007464 if (*str)
7465 return 1;
7466#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007467#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007468 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007469 if (*str)
7470 return 1;
7471#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007472#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007473 return 0;
7474}
7475
Willy Tarreau7875d092012-09-10 08:20:03 +02007476/***** Below are some sample fetching functions for ACL/patterns *****/
7477
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007478static int
7479smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7480{
7481 struct connection *conn;
7482
7483 conn = objt_conn(smp->sess->origin);
7484 if (!conn || conn->xprt != &ssl_sock)
7485 return 0;
7486
7487 smp->flags = 0;
7488 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007489#ifdef OPENSSL_IS_BORINGSSL
7490 {
7491 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
7492 smp->data.u.sint = (SSL_in_early_data(ctx->ssl) &&
7493 SSL_early_data_accepted(ctx->ssl));
7494 }
7495#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007496 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007497 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007498#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007499 return 1;
7500}
7501
Emeric Brune64aef12012-09-21 13:15:06 +02007502/* boolean, returns true if client cert was present */
7503static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007504smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007505{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007506 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007507 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +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)
Emeric Brune64aef12012-09-21 13:15:06 +02007511 return 0;
7512
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007513 ctx = conn->xprt_ctx;
7514
Willy Tarreau911db9b2020-01-23 16:27:54 +01007515 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007516 smp->flags |= SMP_F_MAY_CHANGE;
7517 return 0;
7518 }
7519
7520 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007521 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007522 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007523
7524 return 1;
7525}
7526
Emeric Brun43e79582014-10-29 19:03:26 +01007527/* binary, returns a certificate in a binary chunk (der/raw).
7528 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7529 * should be use.
7530 */
7531static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007532smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007533{
7534 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7535 X509 *crt = NULL;
7536 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007537 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007538 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007539 struct ssl_sock_ctx *ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007540
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007541 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01007542 if (!conn || conn->xprt != &ssl_sock)
7543 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007544 ctx = conn->xprt_ctx;
Emeric Brun43e79582014-10-29 19:03:26 +01007545
Willy Tarreau911db9b2020-01-23 16:27:54 +01007546 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007547 smp->flags |= SMP_F_MAY_CHANGE;
7548 return 0;
7549 }
7550
7551 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007552 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007553 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007554 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007555
7556 if (!crt)
7557 goto out;
7558
7559 smp_trash = get_trash_chunk();
7560 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7561 goto out;
7562
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007563 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007564 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007565 ret = 1;
7566out:
7567 /* SSL_get_peer_certificate, it increase X509 * ref count */
7568 if (cert_peer && crt)
7569 X509_free(crt);
7570 return ret;
7571}
7572
Emeric Brunba841a12014-04-30 17:05:08 +02007573/* binary, returns serial of certificate in a binary chunk.
7574 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7575 * should be use.
7576 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007577static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007578smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007579{
Emeric Brunba841a12014-04-30 17:05:08 +02007580 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007581 X509 *crt = NULL;
7582 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007583 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007584 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007585 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007586
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007587 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007588 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02007589 return 0;
7590
Olivier Houchard66ab4982019-02-26 18:37:15 +01007591 ctx = conn->xprt_ctx;
7592
Willy Tarreau911db9b2020-01-23 16:27:54 +01007593 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007594 smp->flags |= SMP_F_MAY_CHANGE;
7595 return 0;
7596 }
7597
Emeric Brunba841a12014-04-30 17:05:08 +02007598 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007599 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007600 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007601 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007602
Willy Tarreau8d598402012-10-22 17:58:39 +02007603 if (!crt)
7604 goto out;
7605
Willy Tarreau47ca5452012-12-23 20:22:19 +01007606 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007607 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7608 goto out;
7609
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007610 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007611 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007612 ret = 1;
7613out:
Emeric Brunba841a12014-04-30 17:05:08 +02007614 /* SSL_get_peer_certificate, it increase X509 * ref count */
7615 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007616 X509_free(crt);
7617 return ret;
7618}
Emeric Brune64aef12012-09-21 13:15:06 +02007619
Emeric Brunba841a12014-04-30 17:05:08 +02007620/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7621 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7622 * should be use.
7623 */
James Votha051b4a2013-05-14 20:37:59 +02007624static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007625smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007626{
Emeric Brunba841a12014-04-30 17:05:08 +02007627 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007628 X509 *crt = NULL;
7629 const EVP_MD *digest;
7630 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01007631 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007632 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007633 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007634 struct ssl_sock_ctx *ctx;
James Votha051b4a2013-05-14 20:37:59 +02007635
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007636 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007637 if (!conn || conn->xprt != &ssl_sock)
7638 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007639 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007640
Willy Tarreau911db9b2020-01-23 16:27:54 +01007641 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02007642 smp->flags |= SMP_F_MAY_CHANGE;
7643 return 0;
7644 }
7645
Emeric Brunba841a12014-04-30 17:05:08 +02007646 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007647 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007648 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007649 crt = SSL_get_certificate(ctx->ssl);
James Votha051b4a2013-05-14 20:37:59 +02007650 if (!crt)
7651 goto out;
7652
7653 smp_trash = get_trash_chunk();
7654 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01007655 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
7656 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007657 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007658 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02007659 ret = 1;
7660out:
Emeric Brunba841a12014-04-30 17:05:08 +02007661 /* SSL_get_peer_certificate, it increase X509 * ref count */
7662 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02007663 X509_free(crt);
7664 return ret;
7665}
7666
Emeric Brunba841a12014-04-30 17:05:08 +02007667/* string, returns certificate's notafter date in ASN1_UTCTIME format.
7668 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7669 * should be use.
7670 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007671static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007672smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007673{
Emeric Brunba841a12014-04-30 17:05:08 +02007674 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007675 X509 *crt = NULL;
7676 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007677 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007678 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007679 struct ssl_sock_ctx *ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007680
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007681 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007682 if (!conn || conn->xprt != &ssl_sock)
7683 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007684 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007685
Willy Tarreau911db9b2020-01-23 16:27:54 +01007686 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007687 smp->flags |= SMP_F_MAY_CHANGE;
7688 return 0;
7689 }
7690
Emeric Brunba841a12014-04-30 17:05:08 +02007691 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007692 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007693 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007694 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007695 if (!crt)
7696 goto out;
7697
Willy Tarreau47ca5452012-12-23 20:22:19 +01007698 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007699 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007700 goto out;
7701
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007702 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007703 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007704 ret = 1;
7705out:
Emeric Brunba841a12014-04-30 17:05:08 +02007706 /* SSL_get_peer_certificate, it increase X509 * ref count */
7707 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007708 X509_free(crt);
7709 return ret;
7710}
7711
Emeric Brunba841a12014-04-30 17:05:08 +02007712/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
7713 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7714 * should be use.
7715 */
Emeric Brun87855892012-10-17 17:39:35 +02007716static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007717smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007718{
Emeric Brunba841a12014-04-30 17:05:08 +02007719 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007720 X509 *crt = NULL;
7721 X509_NAME *name;
7722 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007723 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007724 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007725 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007726
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007727 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007728 if (!conn || conn->xprt != &ssl_sock)
7729 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007730 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007731
Willy Tarreau911db9b2020-01-23 16:27:54 +01007732 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02007733 smp->flags |= SMP_F_MAY_CHANGE;
7734 return 0;
7735 }
7736
Emeric Brunba841a12014-04-30 17:05:08 +02007737 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007738 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007739 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007740 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007741 if (!crt)
7742 goto out;
7743
7744 name = X509_get_issuer_name(crt);
7745 if (!name)
7746 goto out;
7747
Willy Tarreau47ca5452012-12-23 20:22:19 +01007748 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007749 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007750 int pos = 1;
7751
7752 if (args[1].type == ARGT_SINT)
7753 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007754
7755 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7756 goto out;
7757 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007758 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7759 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7760 goto out;
7761 }
Emeric Brun87855892012-10-17 17:39:35 +02007762 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7763 goto out;
7764
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007765 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007766 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007767 ret = 1;
7768out:
Emeric Brunba841a12014-04-30 17:05:08 +02007769 /* SSL_get_peer_certificate, it increase X509 * ref count */
7770 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007771 X509_free(crt);
7772 return ret;
7773}
7774
Emeric Brunba841a12014-04-30 17:05:08 +02007775/* string, returns notbefore date in ASN1_UTCTIME format.
7776 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7777 * should be use.
7778 */
Emeric Brunce5ad802012-10-22 14:11:22 +02007779static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007780smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02007781{
Emeric Brunba841a12014-04-30 17:05:08 +02007782 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02007783 X509 *crt = NULL;
7784 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007785 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007786 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007787 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007788
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007789 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007790 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02007791 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007792 ctx = conn->xprt_ctx;
Emeric Brunce5ad802012-10-22 14:11:22 +02007793
Willy Tarreau911db9b2020-01-23 16:27:54 +01007794 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02007795 smp->flags |= SMP_F_MAY_CHANGE;
7796 return 0;
7797 }
7798
Emeric Brunba841a12014-04-30 17:05:08 +02007799 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007800 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007801 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007802 crt = SSL_get_certificate(ctx->ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02007803 if (!crt)
7804 goto out;
7805
Willy Tarreau47ca5452012-12-23 20:22:19 +01007806 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08007807 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02007808 goto out;
7809
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007810 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007811 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02007812 ret = 1;
7813out:
Emeric Brunba841a12014-04-30 17:05:08 +02007814 /* SSL_get_peer_certificate, it increase X509 * ref count */
7815 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02007816 X509_free(crt);
7817 return ret;
7818}
7819
Emeric Brunba841a12014-04-30 17:05:08 +02007820/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
7821 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7822 * should be use.
7823 */
Emeric Brun87855892012-10-17 17:39:35 +02007824static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007825smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02007826{
Emeric Brunba841a12014-04-30 17:05:08 +02007827 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02007828 X509 *crt = NULL;
7829 X509_NAME *name;
7830 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007831 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007832 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007833 struct ssl_sock_ctx *ctx;
Emeric Brun87855892012-10-17 17:39:35 +02007834
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007835 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007836 if (!conn || conn->xprt != &ssl_sock)
7837 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007838 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007839
Willy Tarreau911db9b2020-01-23 16:27:54 +01007840 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02007841 smp->flags |= SMP_F_MAY_CHANGE;
7842 return 0;
7843 }
7844
Emeric Brunba841a12014-04-30 17:05:08 +02007845 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007846 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007847 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007848 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun87855892012-10-17 17:39:35 +02007849 if (!crt)
7850 goto out;
7851
7852 name = X509_get_subject_name(crt);
7853 if (!name)
7854 goto out;
7855
Willy Tarreau47ca5452012-12-23 20:22:19 +01007856 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05007857 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02007858 int pos = 1;
7859
7860 if (args[1].type == ARGT_SINT)
7861 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02007862
7863 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
7864 goto out;
7865 }
Elliot Otchet71f82972020-01-15 08:12:14 -05007866 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
7867 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
7868 goto out;
7869 }
Emeric Brun87855892012-10-17 17:39:35 +02007870 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
7871 goto out;
7872
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007873 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007874 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02007875 ret = 1;
7876out:
Emeric Brunba841a12014-04-30 17:05:08 +02007877 /* SSL_get_peer_certificate, it increase X509 * ref count */
7878 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02007879 X509_free(crt);
7880 return ret;
7881}
Emeric Brun9143d372012-12-20 15:44:16 +01007882
7883/* integer, returns true if current session use a client certificate */
7884static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007885smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01007886{
7887 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007888 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007889 struct ssl_sock_ctx *ctx;
Emeric Brun9143d372012-12-20 15:44:16 +01007890
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007891 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007892 if (!conn || conn->xprt != &ssl_sock)
7893 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007894 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007895
Willy Tarreau911db9b2020-01-23 16:27:54 +01007896 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01007897 smp->flags |= SMP_F_MAY_CHANGE;
7898 return 0;
7899 }
7900
7901 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007902 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01007903 if (crt) {
7904 X509_free(crt);
7905 }
7906
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007907 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007908 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01007909 return 1;
7910}
7911
Emeric Brunba841a12014-04-30 17:05:08 +02007912/* integer, returns the certificate version
7913 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7914 * should be use.
7915 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02007916static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007917smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007918{
Emeric Brunba841a12014-04-30 17:05:08 +02007919 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007920 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007921 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007922 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007923
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007924 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007925 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02007926 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007927 ctx = conn->xprt_ctx;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007928
Willy Tarreau911db9b2020-01-23 16:27:54 +01007929 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02007930 smp->flags |= SMP_F_MAY_CHANGE;
7931 return 0;
7932 }
7933
Emeric Brunba841a12014-04-30 17:05:08 +02007934 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007935 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007936 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007937 crt = SSL_get_certificate(ctx->ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02007938 if (!crt)
7939 return 0;
7940
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007941 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02007942 /* SSL_get_peer_certificate increase X509 * ref count */
7943 if (cert_peer)
7944 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007945 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02007946
7947 return 1;
7948}
7949
Emeric Brunba841a12014-04-30 17:05:08 +02007950/* string, returns the certificate's signature algorithm.
7951 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7952 * should be use.
7953 */
Emeric Brun7f56e742012-10-19 18:15:40 +02007954static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007955smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02007956{
Emeric Brunba841a12014-04-30 17:05:08 +02007957 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02007958 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007959 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02007960 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007961 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007962 struct ssl_sock_ctx *ctx;
Emeric Brun7f56e742012-10-19 18:15:40 +02007963
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007964 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007965 if (!conn || conn->xprt != &ssl_sock)
7966 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007967 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007968
Willy Tarreau911db9b2020-01-23 16:27:54 +01007969 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02007970 smp->flags |= SMP_F_MAY_CHANGE;
7971 return 0;
7972 }
7973
Emeric Brunba841a12014-04-30 17:05:08 +02007974 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007975 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007976 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007977 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02007978 if (!crt)
7979 return 0;
7980
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007981 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7982 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02007983
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007984 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
7985 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02007986 /* SSL_get_peer_certificate increase X509 * ref count */
7987 if (cert_peer)
7988 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007989 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02007990 }
Emeric Brun7f56e742012-10-19 18:15:40 +02007991
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007992 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007993 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007994 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02007995 /* SSL_get_peer_certificate increase X509 * ref count */
7996 if (cert_peer)
7997 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02007998
7999 return 1;
8000}
8001
Emeric Brunba841a12014-04-30 17:05:08 +02008002/* string, returns the certificate's key algorithm.
8003 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8004 * should be use.
8005 */
Emeric Brun521a0112012-10-22 12:22:55 +02008006static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008007smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02008008{
Emeric Brunba841a12014-04-30 17:05:08 +02008009 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02008010 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008011 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02008012 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008013 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008014 struct ssl_sock_ctx *ctx;
Emeric Brun521a0112012-10-22 12:22:55 +02008015
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008016 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008017 if (!conn || conn->xprt != &ssl_sock)
8018 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008019 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008020
Willy Tarreau911db9b2020-01-23 16:27:54 +01008021 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02008022 smp->flags |= SMP_F_MAY_CHANGE;
8023 return 0;
8024 }
8025
Emeric Brunba841a12014-04-30 17:05:08 +02008026 if (cert_peer)
Olivier Houchard66ab4982019-02-26 18:37:15 +01008027 crt = SSL_get_peer_certificate(ctx->ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008028 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008029 crt = SSL_get_certificate(ctx->ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02008030 if (!crt)
8031 return 0;
8032
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008033 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
8034 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02008035
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008036 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8037 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008038 /* SSL_get_peer_certificate increase X509 * ref count */
8039 if (cert_peer)
8040 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008041 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008042 }
Emeric Brun521a0112012-10-22 12:22:55 +02008043
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008044 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008045 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008046 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008047 if (cert_peer)
8048 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008049
8050 return 1;
8051}
8052
Emeric Brun645ae792014-04-30 14:21:06 +02008053/* boolean, returns true if front conn. transport layer is SSL.
8054 * This function is also usable on backend conn if the fetch keyword 5th
8055 * char is 'b'.
8056 */
Willy Tarreau7875d092012-09-10 08:20:03 +02008057static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008058smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008059{
Emeric Bruneb8def92018-02-19 15:59:48 +01008060 struct connection *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
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008063 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008064 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02008065 return 1;
8066}
8067
Emeric Brun2525b6b2012-10-18 15:59:43 +02008068/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02008069static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008070smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008071{
8072#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008073 struct connection *conn = objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008074 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008075
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008076 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008077 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008078 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008079 SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02008080 return 1;
8081#else
8082 return 0;
8083#endif
8084}
8085
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008086/* boolean, returns true if client session has been resumed.
8087 * This function is also usable on backend conn if the fetch keyword 5th
8088 * char is 'b'.
8089 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008090static int
8091smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8092{
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008093 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8094 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008095 struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008096
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008097
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008098 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008099 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008100 conn->xprt_ctx &&
Olivier Houchard66ab4982019-02-26 18:37:15 +01008101 SSL_session_reused(ctx->ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008102 return 1;
8103}
8104
Emeric Brun645ae792014-04-30 14:21:06 +02008105/* string, returns the used cipher if front conn. transport layer is SSL.
8106 * This function is also usable on backend conn if the fetch keyword 5th
8107 * char is 'b'.
8108 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008109static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008110smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008111{
Emeric Bruneb8def92018-02-19 15:59:48 +01008112 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8113 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008114 struct ssl_sock_ctx *ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008115
Willy Tarreaube508f12016-03-10 11:47:01 +01008116 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008117 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008118 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008119 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008120
Olivier Houchard66ab4982019-02-26 18:37:15 +01008121 smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008122 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008123 return 0;
8124
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008125 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008126 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008127 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008128
8129 return 1;
8130}
8131
Emeric Brun645ae792014-04-30 14:21:06 +02008132/* integer, returns the algoritm's keysize if front conn. transport layer
8133 * is SSL.
8134 * This function is also usable on backend conn if the fetch keyword 5th
8135 * char is 'b'.
8136 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008137static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008138smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008139{
Emeric Bruneb8def92018-02-19 15:59:48 +01008140 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8141 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008142 struct ssl_sock_ctx *ctx;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008143 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008144
Emeric Brun589fcad2012-10-16 14:13:26 +02008145 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008146 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02008147 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008148 ctx = conn->xprt_ctx;
Emeric Brun589fcad2012-10-16 14:13:26 +02008149
Olivier Houchard66ab4982019-02-26 18:37:15 +01008150 if (!SSL_get_cipher_bits(ctx->ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008151 return 0;
8152
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008153 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008154 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008155
8156 return 1;
8157}
8158
Emeric Brun645ae792014-04-30 14:21:06 +02008159/* integer, returns the used keysize if front conn. transport layer is SSL.
8160 * This function is also usable on backend conn if the fetch keyword 5th
8161 * char is 'b'.
8162 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008163static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008164smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008165{
Emeric Bruneb8def92018-02-19 15:59:48 +01008166 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8167 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008168 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008169
Emeric Brun589fcad2012-10-16 14:13:26 +02008170 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008171 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8172 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008173 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008174
Olivier Houchard66ab4982019-02-26 18:37:15 +01008175 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008176 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008177 return 0;
8178
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008179 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008180
8181 return 1;
8182}
8183
Bernard Spil13c53f82018-02-15 13:34:58 +01008184#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008185static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008186smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008187{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008188 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008189 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008190 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008191
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008192 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008193 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008194
Olivier Houchard6b77f492018-11-22 18:18:29 +01008195 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8196 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008197 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8198 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008199 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008200
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008201 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008202 SSL_get0_next_proto_negotiated(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008203 (const unsigned char **)&smp->data.u.str.area,
8204 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008205
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008206 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008207 return 0;
8208
Willy Tarreau105599c2020-02-25 08:59:23 +01008209 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008210 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008211}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008212#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008213
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008214#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008215static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008216smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008217{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008218 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008219 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008220 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008221
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008222 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008223 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008224
Olivier Houchard6b77f492018-11-22 18:18:29 +01008225 conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) :
8226 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8227
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008228 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02008229 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008230 ctx = conn->xprt_ctx;
Willy Tarreauab861d32013-04-02 02:30:41 +02008231
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008232 smp->data.u.str.area = NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008233 SSL_get0_alpn_selected(ctx->ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008234 (const unsigned char **)&smp->data.u.str.area,
8235 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008236
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008237 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008238 return 0;
8239
Willy Tarreau105599c2020-02-25 08:59:23 +01008240 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02008241 return 1;
8242}
8243#endif
8244
Emeric Brun645ae792014-04-30 14:21:06 +02008245/* string, returns the used protocol if front conn. transport layer is SSL.
8246 * This function is also usable on backend conn if the fetch keyword 5th
8247 * char is 'b'.
8248 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008249static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008250smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008251{
Emeric Bruneb8def92018-02-19 15:59:48 +01008252 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8253 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008254 struct ssl_sock_ctx *ctx;
Willy Tarreaube508f12016-03-10 11:47:01 +01008255
Emeric Brun589fcad2012-10-16 14:13:26 +02008256 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008257 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8258 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008259 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008260
Olivier Houchard66ab4982019-02-26 18:37:15 +01008261 smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008262 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008263 return 0;
8264
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008265 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008266 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008267 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008268
8269 return 1;
8270}
8271
Willy Tarreau87b09662015-04-03 00:22:06 +02008272/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008273 * This function is also usable on backend conn if the fetch keyword 5th
8274 * char is 'b'.
8275 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008276#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008277static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008278smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008279{
Emeric Bruneb8def92018-02-19 15:59:48 +01008280 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8281 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008282 SSL_SESSION *ssl_sess;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008283 struct ssl_sock_ctx *ctx;
Willy Tarreau105599c2020-02-25 08:59:23 +01008284 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008285
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008286 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008287 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008288
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008289 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8290 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008291 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008292
Olivier Houchard66ab4982019-02-26 18:37:15 +01008293 ssl_sess = SSL_get_session(ctx->ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008294 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008295 return 0;
8296
Willy Tarreau105599c2020-02-25 08:59:23 +01008297 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008298 if (!smp->data.u.str.area || !smp->data.u.str.data)
Emeric Brunfe68f682012-10-16 14:59:28 +02008299 return 0;
8300
Willy Tarreau105599c2020-02-25 08:59:23 +01008301 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02008302 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008303}
Patrick Hemmer41966772018-04-28 19:15:48 -04008304#endif
8305
Emeric Brunfe68f682012-10-16 14:59:28 +02008306
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008307#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008308static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008309smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8310{
8311 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8312 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8313 struct buffer *data;
8314 struct ssl_sock_ctx *ctx;
8315
8316 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8317 return 0;
8318 ctx = conn->xprt_ctx;
8319
8320 data = get_trash_chunk();
8321 if (kw[7] == 'c')
8322 data->data = SSL_get_client_random(ctx->ssl,
8323 (unsigned char *) data->area,
8324 data->size);
8325 else
8326 data->data = SSL_get_server_random(ctx->ssl,
8327 (unsigned char *) data->area,
8328 data->size);
8329 if (!data->data)
8330 return 0;
8331
8332 smp->flags = 0;
8333 smp->data.type = SMP_T_BIN;
8334 smp->data.u.str = *data;
8335
8336 return 1;
8337}
8338
8339static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008340smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8341{
8342 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8343 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8344 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008345 struct buffer *data;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008346 struct ssl_sock_ctx *ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008347
8348 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8349 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008350 ctx = conn->xprt_ctx;
Patrick Hemmere0275472018-04-28 19:15:51 -04008351
Olivier Houchard66ab4982019-02-26 18:37:15 +01008352 ssl_sess = SSL_get_session(ctx->ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008353 if (!ssl_sess)
8354 return 0;
8355
8356 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008357 data->data = SSL_SESSION_get_master_key(ssl_sess,
8358 (unsigned char *) data->area,
8359 data->size);
8360 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008361 return 0;
8362
8363 smp->flags = 0;
8364 smp->data.type = SMP_T_BIN;
8365 smp->data.u.str = *data;
8366
8367 return 1;
8368}
8369#endif
8370
Patrick Hemmer41966772018-04-28 19:15:48 -04008371#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008372static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008373smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008374{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008375 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008376 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008377
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008378 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008379 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008380
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008381 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008382 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8383 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008384 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008385
Olivier Houchard66ab4982019-02-26 18:37:15 +01008386 smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008387 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008388 return 0;
8389
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008390 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008391 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008392}
Patrick Hemmer41966772018-04-28 19:15:48 -04008393#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008394
David Sc1ad52e2014-04-08 18:48:47 -04008395static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008396smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8397{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008398 struct connection *conn;
8399 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008400 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008401
8402 conn = objt_conn(smp->sess->origin);
8403 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8404 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008405 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008406
Olivier Houchard66ab4982019-02-26 18:37:15 +01008407 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008408 if (!capture)
8409 return 0;
8410
8411 smp->flags = SMP_F_CONST;
8412 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008413 smp->data.u.str.area = capture->ciphersuite;
8414 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008415 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008416}
8417
8418static int
8419smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8420{
Willy Tarreau83061a82018-07-13 11:56:34 +02008421 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008422
8423 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8424 return 0;
8425
8426 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008427 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008428 smp->data.type = SMP_T_BIN;
8429 smp->data.u.str = *data;
8430 return 1;
8431}
8432
8433static int
8434smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8435{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008436 struct connection *conn;
8437 struct ssl_capture *capture;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008438 struct ssl_sock_ctx *ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008439
8440 conn = objt_conn(smp->sess->origin);
8441 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8442 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008443 ctx = conn->xprt_ctx;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008444
Olivier Houchard66ab4982019-02-26 18:37:15 +01008445 capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008446 if (!capture)
8447 return 0;
8448
8449 smp->data.type = SMP_T_SINT;
8450 smp->data.u.sint = capture->xxh64;
8451 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008452}
8453
8454static int
8455smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8456{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008457#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008458 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008459 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008460
8461 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8462 return 0;
8463
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008464 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008465 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008466 const char *str;
8467 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008468 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008469 uint16_t id = (bin[0] << 8) | bin[1];
8470#if defined(OPENSSL_IS_BORINGSSL)
8471 cipher = SSL_get_cipher_by_value(id);
8472#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008473 struct connection *conn = __objt_conn(smp->sess->origin);
Olivier Houchard66ab4982019-02-26 18:37:15 +01008474 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
8475 cipher = SSL_CIPHER_find(ctx->ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008476#endif
8477 str = SSL_CIPHER_get_name(cipher);
8478 if (!str || strcmp(str, "(NONE)") == 0)
8479 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008480 else
8481 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8482 }
8483 smp->data.type = SMP_T_STR;
8484 smp->data.u.str = *data;
8485 return 1;
8486#else
8487 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8488#endif
8489}
8490
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008491#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008492static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008493smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008494{
Emeric Bruneb8def92018-02-19 15:59:48 +01008495 struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8496 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
David Sc1ad52e2014-04-08 18:48:47 -04008497 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008498 struct buffer *finished_trash;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008499 struct ssl_sock_ctx *ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008500
8501 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04008502 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
8503 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008504 ctx = conn->xprt_ctx;
David Sc1ad52e2014-04-08 18:48:47 -04008505
Willy Tarreau911db9b2020-01-23 16:27:54 +01008506 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008507 smp->flags |= SMP_F_MAY_CHANGE;
8508 return 0;
8509 }
8510
8511 finished_trash = get_trash_chunk();
Olivier Houchard66ab4982019-02-26 18:37:15 +01008512 if (!SSL_session_reused(ctx->ssl))
8513 finished_len = SSL_get_peer_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008514 finished_trash->area,
8515 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008516 else
Olivier Houchard66ab4982019-02-26 18:37:15 +01008517 finished_len = SSL_get_finished(ctx->ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008518 finished_trash->area,
8519 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008520
8521 if (!finished_len)
8522 return 0;
8523
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008524 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008525 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008526 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008527
8528 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008529}
Patrick Hemmer41966772018-04-28 19:15:48 -04008530#endif
David Sc1ad52e2014-04-08 18:48:47 -04008531
Emeric Brun2525b6b2012-10-18 15:59:43 +02008532/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008533static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008534smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008535{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008536 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008537 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008538
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008539 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008540 if (!conn || conn->xprt != &ssl_sock)
8541 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008542 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008543
Willy Tarreau911db9b2020-01-23 16:27:54 +01008544 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008545 smp->flags = SMP_F_MAY_CHANGE;
8546 return 0;
8547 }
8548
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008549 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008550 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008551 smp->flags = 0;
8552
8553 return 1;
8554}
8555
Emeric Brun2525b6b2012-10-18 15:59:43 +02008556/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008557static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008558smp_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 +02008559{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008560 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008561 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008562
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008563 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008564 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008565 return 0;
8566
Willy Tarreau911db9b2020-01-23 16:27:54 +01008567 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008568 smp->flags = SMP_F_MAY_CHANGE;
8569 return 0;
8570 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008571 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008572
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008573 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008574 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008575 smp->flags = 0;
8576
8577 return 1;
8578}
8579
Emeric Brun2525b6b2012-10-18 15:59:43 +02008580/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008581static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008582smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008583{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008584 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008585 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008586
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008587 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008588 if (!conn || conn->xprt != &ssl_sock)
8589 return 0;
8590
Willy Tarreau911db9b2020-01-23 16:27:54 +01008591 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008592 smp->flags = SMP_F_MAY_CHANGE;
8593 return 0;
8594 }
8595
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008596 ctx = conn->xprt_ctx;
8597
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008598 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008599 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008600 smp->flags = 0;
8601
8602 return 1;
8603}
8604
Emeric Brun2525b6b2012-10-18 15:59:43 +02008605/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008606static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008607smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008608{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008609 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008610 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008611
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008612 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008613 if (!conn || conn->xprt != &ssl_sock)
8614 return 0;
8615
Willy Tarreau911db9b2020-01-23 16:27:54 +01008616 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008617 smp->flags = SMP_F_MAY_CHANGE;
8618 return 0;
8619 }
8620
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008621 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008622 return 0;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008623 ctx = conn->xprt_ctx;
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008624
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008625 smp->data.type = SMP_T_SINT;
Olivier Houchard66ab4982019-02-26 18:37:15 +01008626 smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02008627 smp->flags = 0;
8628
8629 return 1;
8630}
8631
Emeric Brunfb510ea2012-10-05 12:00:26 +02008632/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008633static 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 +02008634{
8635 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008636 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008637 return ERR_ALERT | ERR_FATAL;
8638 }
8639
Willy Tarreauef934602016-12-22 23:12:01 +01008640 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8641 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008642 else
8643 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008644
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02008645 if (!ssl_store_load_locations_file(conf->ca_file)) {
8646 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->ca_file);
8647 return ERR_ALERT | ERR_FATAL;
8648 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02008649 return 0;
8650}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008651static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8652{
8653 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
8654}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008655
Christopher Faulet31af49d2015-06-09 17:29:50 +02008656/* parse the "ca-sign-file" bind keyword */
8657static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8658{
8659 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008660 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008661 return ERR_ALERT | ERR_FATAL;
8662 }
8663
Willy Tarreauef934602016-12-22 23:12:01 +01008664 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8665 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008666 else
8667 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
8668
8669 return 0;
8670}
8671
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008672/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02008673static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8674{
8675 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008676 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02008677 return ERR_ALERT | ERR_FATAL;
8678 }
8679 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
8680 return 0;
8681}
8682
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008683/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008684static 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 +02008685{
8686 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008687 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008688 return ERR_ALERT | ERR_FATAL;
8689 }
8690
Emeric Brun76d88952012-10-05 15:47:31 +02008691 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02008692 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008693 return 0;
8694}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008695static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8696{
8697 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
8698}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008699
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008700#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008701/* parse the "ciphersuites" bind keyword */
8702static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8703{
8704 if (!*args[cur_arg + 1]) {
8705 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
8706 return ERR_ALERT | ERR_FATAL;
8707 }
8708
8709 free(conf->ciphersuites);
8710 conf->ciphersuites = strdup(args[cur_arg + 1]);
8711 return 0;
8712}
8713static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8714{
8715 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
8716}
8717#endif
8718
Willy Tarreaubbc91962019-10-16 16:42:19 +02008719/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008720static 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 +02008721{
Willy Tarreau38011032013-08-13 16:59:39 +02008722 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02008723
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008724 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008725 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008726 return ERR_ALERT | ERR_FATAL;
8727 }
8728
Willy Tarreauef934602016-12-22 23:12:01 +01008729 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
8730 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02008731 memprintf(err, "'%s' : path too long", args[cur_arg]);
8732 return ERR_ALERT | ERR_FATAL;
8733 }
Willy Tarreauef934602016-12-22 23:12:01 +01008734 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02008735 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008736 }
8737
Willy Tarreaubbc91962019-10-16 16:42:19 +02008738 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008739}
8740
Willy Tarreaubbc91962019-10-16 16:42:19 +02008741/* 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 +01008742static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8743{
Willy Tarreaubbc91962019-10-16 16:42:19 +02008744 int err_code;
8745
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008746 if (!*args[cur_arg + 1]) {
8747 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
8748 return ERR_ALERT | ERR_FATAL;
8749 }
8750
Willy Tarreaubbc91962019-10-16 16:42:19 +02008751 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err);
8752 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02008753 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008754
Willy Tarreaubbc91962019-10-16 16:42:19 +02008755 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01008756}
8757
Emeric Brunfb510ea2012-10-05 12:00:26 +02008758/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008759static 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 +02008760{
Emeric Brun051cdab2012-10-02 19:25:50 +02008761#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01008762 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02008763 return ERR_ALERT | ERR_FATAL;
8764#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02008765 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008766 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008767 return ERR_ALERT | ERR_FATAL;
8768 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008769
Willy Tarreauef934602016-12-22 23:12:01 +01008770 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
8771 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008772 else
8773 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02008774
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01008775 if (!ssl_store_load_locations_file(conf->crl_file)) {
8776 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
8777 return ERR_ALERT | ERR_FATAL;
8778 }
Emeric Brun2b58d042012-09-20 17:10:03 +02008779 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02008780#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02008781}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008782static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8783{
8784 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
8785}
Emeric Brun2b58d042012-09-20 17:10:03 +02008786
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008787/* parse the "curves" bind keyword keyword */
8788static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8789{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01008790#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008791 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008792 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008793 return ERR_ALERT | ERR_FATAL;
8794 }
8795 conf->curves = strdup(args[cur_arg + 1]);
8796 return 0;
8797#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008798 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01008799 return ERR_ALERT | ERR_FATAL;
8800#endif
8801}
8802static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8803{
8804 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
8805}
8806
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008807/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008808static 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 +02008809{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008810#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01008811 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008812 return ERR_ALERT | ERR_FATAL;
8813#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01008814 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 +02008815 return ERR_ALERT | ERR_FATAL;
8816#else
8817 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008818 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02008819 return ERR_ALERT | ERR_FATAL;
8820 }
8821
8822 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008823
8824 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02008825#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008826}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008827static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8828{
8829 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
8830}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008831
Bertrand Jacquinff13c062016-11-13 16:37:11 +00008832/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02008833static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8834{
8835 int code;
8836 char *p = args[cur_arg + 1];
8837 unsigned long long *ignerr = &conf->crt_ignerr;
8838
8839 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008840 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008841 return ERR_ALERT | ERR_FATAL;
8842 }
8843
8844 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
8845 ignerr = &conf->ca_ignerr;
8846
8847 if (strcmp(p, "all") == 0) {
8848 *ignerr = ~0ULL;
8849 return 0;
8850 }
8851
8852 while (p) {
8853 code = atoi(p);
8854 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008855 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
8856 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02008857 return ERR_ALERT | ERR_FATAL;
8858 }
8859 *ignerr |= 1ULL << code;
8860 p = strchr(p, ',');
8861 if (p)
8862 p++;
8863 }
8864
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008865 return 0;
8866}
8867
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008868/* parse tls_method_options "no-xxx" and "force-xxx" */
8869static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008870{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008871 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008872 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008873 p = strchr(arg, '-');
8874 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008875 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008876 p++;
8877 if (!strcmp(p, "sslv3"))
8878 v = CONF_SSLV3;
8879 else if (!strcmp(p, "tlsv10"))
8880 v = CONF_TLSV10;
8881 else if (!strcmp(p, "tlsv11"))
8882 v = CONF_TLSV11;
8883 else if (!strcmp(p, "tlsv12"))
8884 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02008885 else if (!strcmp(p, "tlsv13"))
8886 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008887 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008888 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008889 if (!strncmp(arg, "no-", 3))
8890 methods->flags |= methodVersions[v].flag;
8891 else if (!strncmp(arg, "force-", 6))
8892 methods->min = methods->max = v;
8893 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008894 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008895 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008896 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01008897 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008898 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02008899}
8900
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008901static 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 +02008902{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008903 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008904}
8905
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008906static 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 +02008907{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008908 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
8909}
8910
8911/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
8912static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
8913{
8914 uint16_t i, v = 0;
8915 char *argv = args[cur_arg + 1];
8916 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008917 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008918 return ERR_ALERT | ERR_FATAL;
8919 }
8920 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8921 if (!strcmp(argv, methodVersions[i].name))
8922 v = i;
8923 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008924 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008925 return ERR_ALERT | ERR_FATAL;
8926 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008927 if (!strcmp("ssl-min-ver", args[cur_arg]))
8928 methods->min = v;
8929 else if (!strcmp("ssl-max-ver", args[cur_arg]))
8930 methods->max = v;
8931 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01008932 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008933 return ERR_ALERT | ERR_FATAL;
8934 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008935 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008936}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02008937
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02008938static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8939{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008940#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01008941 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 +02008942#endif
8943 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
8944}
8945
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008946static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8947{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008948 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008949}
8950
8951static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8952{
8953 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
8954}
8955
Emeric Brun2d0c4822012-10-02 13:45:20 +02008956/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008957static 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 +02008958{
Emeric Brun89675492012-10-05 13:48:26 +02008959 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02008960 return 0;
8961}
Emeric Brun2d0c4822012-10-02 13:45:20 +02008962
Olivier Houchardc2aae742017-09-22 18:26:28 +02008963/* parse the "allow-0rtt" bind keyword */
8964static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8965{
8966 conf->early_data = 1;
8967 return 0;
8968}
8969
8970static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8971{
Olivier Houchard9679ac92017-10-27 14:58:08 +02008972 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02008973 return 0;
8974}
8975
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008976/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008977static 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 +02008978{
Bernard Spil13c53f82018-02-15 13:34:58 +01008979#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008980 char *p1, *p2;
8981
8982 if (!*args[cur_arg + 1]) {
8983 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
8984 return ERR_ALERT | ERR_FATAL;
8985 }
8986
8987 free(conf->npn_str);
8988
Willy Tarreau3724da12016-02-12 17:11:12 +01008989 /* the NPN string is built as a suite of (<len> <name>)*,
8990 * so we reuse each comma to store the next <len> and need
8991 * one more for the end of the string.
8992 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008993 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01008994 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008995 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
8996
8997 /* replace commas with the name length */
8998 p1 = conf->npn_str;
8999 p2 = p1 + 1;
9000 while (1) {
9001 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
9002 if (!p2)
9003 p2 = p1 + 1 + strlen(p1 + 1);
9004
9005 if (p2 - (p1 + 1) > 255) {
9006 *p2 = '\0';
9007 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9008 return ERR_ALERT | ERR_FATAL;
9009 }
9010
9011 *p1 = p2 - (p1 + 1);
9012 p1 = p2;
9013
9014 if (!*p2)
9015 break;
9016
9017 *(p2++) = '\0';
9018 }
9019 return 0;
9020#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009021 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009022 return ERR_ALERT | ERR_FATAL;
9023#endif
9024}
9025
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009026static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9027{
9028 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
9029}
9030
Willy Tarreauab861d32013-04-02 02:30:41 +02009031/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009032static 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 +02009033{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01009034#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02009035 char *p1, *p2;
9036
9037 if (!*args[cur_arg + 1]) {
9038 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
9039 return ERR_ALERT | ERR_FATAL;
9040 }
9041
9042 free(conf->alpn_str);
9043
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009044 /* the ALPN string is built as a suite of (<len> <name>)*,
9045 * so we reuse each comma to store the next <len> and need
9046 * one more for the end of the string.
9047 */
Willy Tarreauab861d32013-04-02 02:30:41 +02009048 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009049 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02009050 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
9051
9052 /* replace commas with the name length */
9053 p1 = conf->alpn_str;
9054 p2 = p1 + 1;
9055 while (1) {
9056 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
9057 if (!p2)
9058 p2 = p1 + 1 + strlen(p1 + 1);
9059
9060 if (p2 - (p1 + 1) > 255) {
9061 *p2 = '\0';
9062 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9063 return ERR_ALERT | ERR_FATAL;
9064 }
9065
9066 *p1 = p2 - (p1 + 1);
9067 p1 = p2;
9068
9069 if (!*p2)
9070 break;
9071
9072 *(p2++) = '\0';
9073 }
9074 return 0;
9075#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009076 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02009077 return ERR_ALERT | ERR_FATAL;
9078#endif
9079}
9080
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009081static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9082{
9083 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
9084}
9085
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009086/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009087static 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 +02009088{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009089 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009090 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009091
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009092 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9093 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009094#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009095 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9096 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9097#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009098 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009099 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9100 if (!conf->ssl_conf.ssl_methods.min)
9101 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9102 if (!conf->ssl_conf.ssl_methods.max)
9103 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009104
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009105 return 0;
9106}
9107
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009108/* parse the "prefer-client-ciphers" bind keyword */
9109static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9110{
9111 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9112 return 0;
9113}
9114
Christopher Faulet31af49d2015-06-09 17:29:50 +02009115/* parse the "generate-certificates" bind keyword */
9116static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9117{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009118#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009119 conf->generate_certs = 1;
9120#else
9121 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9122 err && *err ? *err : "");
9123#endif
9124 return 0;
9125}
9126
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009127/* parse the "strict-sni" bind keyword */
9128static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9129{
9130 conf->strict_sni = 1;
9131 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009132}
9133
9134/* parse the "tls-ticket-keys" bind keyword */
9135static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9136{
9137#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009138 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009139 int i = 0;
9140 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009141 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009142
9143 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009144 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009145 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009146 }
9147
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009148 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009149 if (keys_ref) {
9150 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009151 conf->keys_ref = keys_ref;
9152 return 0;
9153 }
9154
Christopher Faulete566f3d2019-10-21 09:55:49 +02009155 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009156 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009157 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009158 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009159 }
9160
Emeric Brun9e754772019-01-10 17:51:55 +01009161 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009162 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009163 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009164 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009165 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009166
9167 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009168 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009169 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009170 }
9171
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009172 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009173 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009174 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009175 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009176 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009177
Emeric Brun9e754772019-01-10 17:51:55 +01009178 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009179 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9180 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009181 int dec_size;
9182
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009183 /* Strip newline characters from the end */
9184 if(thisline[len - 1] == '\n')
9185 thisline[--len] = 0;
9186
9187 if(thisline[len - 1] == '\r')
9188 thisline[--len] = 0;
9189
Emeric Brun9e754772019-01-10 17:51:55 +01009190 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9191 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009192 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009193 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009194 }
Emeric Brun9e754772019-01-10 17:51:55 +01009195 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9196 keys_ref->key_size_bits = 128;
9197 }
9198 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9199 keys_ref->key_size_bits = 256;
9200 }
9201 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9202 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9203 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009204 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009205 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009206 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009207 i++;
9208 }
9209
9210 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009211 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 +02009212 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009213 }
9214
9215 fclose(f);
9216
9217 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009218 i -= 2;
9219 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009220 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009221 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009222 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009223 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009224
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009225 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9226
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009227 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009228
9229 fail:
9230 if (f)
9231 fclose(f);
9232 if (keys_ref) {
9233 free(keys_ref->filename);
9234 free(keys_ref->tlskeys);
9235 free(keys_ref);
9236 }
9237 return ERR_ALERT | ERR_FATAL;
9238
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009239#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009240 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009241 return ERR_ALERT | ERR_FATAL;
9242#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009243}
9244
Emeric Brund94b3fe2012-09-20 18:23:56 +02009245/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009246static 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 +02009247{
9248 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009249 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009250 return ERR_ALERT | ERR_FATAL;
9251 }
9252
9253 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009254 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009255 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009256 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009257 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009258 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009259 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009260 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9261 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009262 return ERR_ALERT | ERR_FATAL;
9263 }
9264
9265 return 0;
9266}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009267static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9268{
9269 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9270}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009271
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009272/* parse the "no-ca-names" bind keyword */
9273static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9274{
9275 conf->no_ca_names = 1;
9276 return 0;
9277}
9278static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9279{
9280 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9281}
9282
Willy Tarreau92faadf2012-10-10 23:04:25 +02009283/************** "server" keywords ****************/
9284
Olivier Houchardc7566002018-11-20 23:33:50 +01009285/* parse the "npn" bind keyword */
9286static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9287{
9288#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9289 char *p1, *p2;
9290
9291 if (!*args[*cur_arg + 1]) {
9292 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9293 return ERR_ALERT | ERR_FATAL;
9294 }
9295
9296 free(newsrv->ssl_ctx.npn_str);
9297
9298 /* the NPN string is built as a suite of (<len> <name>)*,
9299 * so we reuse each comma to store the next <len> and need
9300 * one more for the end of the string.
9301 */
9302 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9303 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9304 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9305 newsrv->ssl_ctx.npn_len);
9306
9307 /* replace commas with the name length */
9308 p1 = newsrv->ssl_ctx.npn_str;
9309 p2 = p1 + 1;
9310 while (1) {
9311 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9312 newsrv->ssl_ctx.npn_len - (p1 + 1));
9313 if (!p2)
9314 p2 = p1 + 1 + strlen(p1 + 1);
9315
9316 if (p2 - (p1 + 1) > 255) {
9317 *p2 = '\0';
9318 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9319 return ERR_ALERT | ERR_FATAL;
9320 }
9321
9322 *p1 = p2 - (p1 + 1);
9323 p1 = p2;
9324
9325 if (!*p2)
9326 break;
9327
9328 *(p2++) = '\0';
9329 }
9330 return 0;
9331#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009332 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009333 return ERR_ALERT | ERR_FATAL;
9334#endif
9335}
9336
Olivier Houchard92150142018-12-21 19:47:01 +01009337/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009338static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9339{
9340#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9341 char *p1, *p2;
Olivier Houchard92150142018-12-21 19:47:01 +01009342 char **alpn_str;
9343 int *alpn_len;
Olivier Houchardc7566002018-11-20 23:33:50 +01009344
Olivier Houchard92150142018-12-21 19:47:01 +01009345 if (*args[*cur_arg] == 'c') {
9346 alpn_str = &newsrv->check.alpn_str;
9347 alpn_len = &newsrv->check.alpn_len;
9348 } else {
9349 alpn_str = &newsrv->ssl_ctx.alpn_str;
9350 alpn_len = &newsrv->ssl_ctx.alpn_len;
9351
9352 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009353 if (!*args[*cur_arg + 1]) {
9354 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]);
9355 return ERR_ALERT | ERR_FATAL;
9356 }
9357
Olivier Houchard92150142018-12-21 19:47:01 +01009358 free(*alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01009359
9360 /* the ALPN string is built as a suite of (<len> <name>)*,
9361 * so we reuse each comma to store the next <len> and need
9362 * one more for the end of the string.
9363 */
Olivier Houchard92150142018-12-21 19:47:01 +01009364 *alpn_len = strlen(args[*cur_arg + 1]) + 1;
9365 *alpn_str = calloc(1, *alpn_len + 1);
9366 memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len);
Olivier Houchardc7566002018-11-20 23:33:50 +01009367
9368 /* replace commas with the name length */
Olivier Houchard92150142018-12-21 19:47:01 +01009369 p1 = *alpn_str;
Olivier Houchardc7566002018-11-20 23:33:50 +01009370 p2 = p1 + 1;
9371 while (1) {
Olivier Houchard92150142018-12-21 19:47:01 +01009372 p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1));
Olivier Houchardc7566002018-11-20 23:33:50 +01009373 if (!p2)
9374 p2 = p1 + 1 + strlen(p1 + 1);
9375
9376 if (p2 - (p1 + 1) > 255) {
9377 *p2 = '\0';
9378 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9379 return ERR_ALERT | ERR_FATAL;
9380 }
9381
9382 *p1 = p2 - (p1 + 1);
9383 p1 = p2;
9384
9385 if (!*p2)
9386 break;
9387
9388 *(p2++) = '\0';
9389 }
9390 return 0;
9391#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009392 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009393 return ERR_ALERT | ERR_FATAL;
9394#endif
9395}
9396
Emeric Brunef42d922012-10-11 16:11:36 +02009397/* parse the "ca-file" server keyword */
9398static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9399{
9400 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009401 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009402 return ERR_ALERT | ERR_FATAL;
9403 }
9404
Willy Tarreauef934602016-12-22 23:12:01 +01009405 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9406 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009407 else
9408 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9409
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009410 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9411 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9412 return ERR_ALERT | ERR_FATAL;
9413 }
Emeric Brunef42d922012-10-11 16:11:36 +02009414 return 0;
9415}
9416
Olivier Houchard9130a962017-10-17 17:33:43 +02009417/* parse the "check-sni" server keyword */
9418static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9419{
9420 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009421 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009422 return ERR_ALERT | ERR_FATAL;
9423 }
9424
9425 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9426 if (!newsrv->check.sni) {
9427 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9428 return ERR_ALERT | ERR_FATAL;
9429 }
9430 return 0;
9431
9432}
9433
Willy Tarreau92faadf2012-10-10 23:04:25 +02009434/* parse the "check-ssl" server keyword */
9435static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9436{
9437 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009438 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9439 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009440#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009441 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9442 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9443#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009444 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009445 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9446 if (!newsrv->ssl_ctx.methods.min)
9447 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9448 if (!newsrv->ssl_ctx.methods.max)
9449 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9450
Willy Tarreau92faadf2012-10-10 23:04:25 +02009451 return 0;
9452}
9453
9454/* parse the "ciphers" server keyword */
9455static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9456{
9457 if (!*args[*cur_arg + 1]) {
9458 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9459 return ERR_ALERT | ERR_FATAL;
9460 }
9461
9462 free(newsrv->ssl_ctx.ciphers);
9463 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9464 return 0;
9465}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009466
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009467#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009468/* parse the "ciphersuites" server keyword */
9469static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9470{
9471 if (!*args[*cur_arg + 1]) {
9472 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9473 return ERR_ALERT | ERR_FATAL;
9474 }
9475
9476 free(newsrv->ssl_ctx.ciphersuites);
9477 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9478 return 0;
9479}
9480#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009481
Emeric Brunef42d922012-10-11 16:11:36 +02009482/* parse the "crl-file" server keyword */
9483static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9484{
9485#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009486 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009487 return ERR_ALERT | ERR_FATAL;
9488#else
9489 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009490 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009491 return ERR_ALERT | ERR_FATAL;
9492 }
9493
Willy Tarreauef934602016-12-22 23:12:01 +01009494 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9495 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009496 else
9497 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9498
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009499 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9500 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9501 return ERR_ALERT | ERR_FATAL;
9502 }
Emeric Brunef42d922012-10-11 16:11:36 +02009503 return 0;
9504#endif
9505}
9506
Emeric Bruna7aa3092012-10-26 12:58:00 +02009507/* parse the "crt" server keyword */
9508static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9509{
9510 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009511 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009512 return ERR_ALERT | ERR_FATAL;
9513 }
9514
Willy Tarreauef934602016-12-22 23:12:01 +01009515 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009516 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009517 else
9518 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9519
9520 return 0;
9521}
Emeric Brunef42d922012-10-11 16:11:36 +02009522
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009523/* parse the "no-check-ssl" server keyword */
9524static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9525{
9526 newsrv->check.use_ssl = 0;
9527 free(newsrv->ssl_ctx.ciphers);
9528 newsrv->ssl_ctx.ciphers = NULL;
9529 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9530 return 0;
9531}
9532
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009533/* parse the "no-send-proxy-v2-ssl" server keyword */
9534static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9535{
9536 newsrv->pp_opts &= ~SRV_PP_V2;
9537 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9538 return 0;
9539}
9540
9541/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9542static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9543{
9544 newsrv->pp_opts &= ~SRV_PP_V2;
9545 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9546 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9547 return 0;
9548}
9549
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009550/* parse the "no-ssl" server keyword */
9551static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9552{
9553 newsrv->use_ssl = 0;
9554 free(newsrv->ssl_ctx.ciphers);
9555 newsrv->ssl_ctx.ciphers = NULL;
9556 return 0;
9557}
9558
Olivier Houchard522eea72017-11-03 16:27:47 +01009559/* parse the "allow-0rtt" server keyword */
9560static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9561{
9562 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9563 return 0;
9564}
9565
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009566/* parse the "no-ssl-reuse" server keyword */
9567static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9568{
9569 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9570 return 0;
9571}
9572
Emeric Brunf9c5c472012-10-11 15:28:34 +02009573/* parse the "no-tls-tickets" server keyword */
9574static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9575{
9576 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9577 return 0;
9578}
David Safb76832014-05-08 23:42:08 -04009579/* parse the "send-proxy-v2-ssl" server keyword */
9580static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9581{
9582 newsrv->pp_opts |= SRV_PP_V2;
9583 newsrv->pp_opts |= SRV_PP_V2_SSL;
9584 return 0;
9585}
9586
9587/* parse the "send-proxy-v2-ssl-cn" server keyword */
9588static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9589{
9590 newsrv->pp_opts |= SRV_PP_V2;
9591 newsrv->pp_opts |= SRV_PP_V2_SSL;
9592 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
9593 return 0;
9594}
Emeric Brunf9c5c472012-10-11 15:28:34 +02009595
Willy Tarreau732eac42015-07-09 11:40:25 +02009596/* parse the "sni" server keyword */
9597static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9598{
9599#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
9600 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
9601 return ERR_ALERT | ERR_FATAL;
9602#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009603 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02009604
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009605 arg = args[*cur_arg + 1];
9606 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02009607 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
9608 return ERR_ALERT | ERR_FATAL;
9609 }
9610
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01009611 free(newsrv->sni_expr);
9612 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02009613
Willy Tarreau732eac42015-07-09 11:40:25 +02009614 return 0;
9615#endif
9616}
9617
Willy Tarreau92faadf2012-10-10 23:04:25 +02009618/* parse the "ssl" server keyword */
9619static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9620{
9621 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009622 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9623 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009624#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009625 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9626 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9627#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009628 return 0;
9629}
9630
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009631/* parse the "ssl-reuse" server keyword */
9632static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9633{
9634 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
9635 return 0;
9636}
9637
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01009638/* parse the "tls-tickets" server keyword */
9639static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9640{
9641 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
9642 return 0;
9643}
9644
Emeric Brunef42d922012-10-11 16:11:36 +02009645/* parse the "verify" server keyword */
9646static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9647{
9648 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009649 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009650 return ERR_ALERT | ERR_FATAL;
9651 }
9652
9653 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009654 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02009655 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009656 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02009657 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009658 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
9659 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009660 return ERR_ALERT | ERR_FATAL;
9661 }
9662
Evan Broderbe554312013-06-27 00:05:25 -07009663 return 0;
9664}
9665
9666/* parse the "verifyhost" server keyword */
9667static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9668{
9669 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009670 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -07009671 return ERR_ALERT | ERR_FATAL;
9672 }
9673
Frédéric Lécaille273f3212017-03-13 15:52:01 +01009674 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07009675 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
9676
Emeric Brunef42d922012-10-11 16:11:36 +02009677 return 0;
9678}
9679
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009680/* parse the "ssl-default-bind-options" keyword in global section */
9681static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
9682 struct proxy *defpx, const char *file, int line,
9683 char **err) {
9684 int i = 1;
9685
9686 if (*(args[i]) == 0) {
9687 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9688 return -1;
9689 }
9690 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009691 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009692 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009693 else if (!strcmp(args[i], "prefer-client-ciphers"))
9694 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009695 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9696 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
9697 i++;
9698 else {
9699 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9700 return -1;
9701 }
9702 }
9703 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009704 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9705 return -1;
9706 }
9707 i++;
9708 }
9709 return 0;
9710}
9711
9712/* parse the "ssl-default-server-options" keyword in global section */
9713static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
9714 struct proxy *defpx, const char *file, int line,
9715 char **err) {
9716 int i = 1;
9717
9718 if (*(args[i]) == 0) {
9719 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
9720 return -1;
9721 }
9722 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009723 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01009724 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009725 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
9726 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
9727 i++;
9728 else {
9729 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
9730 return -1;
9731 }
9732 }
9733 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01009734 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
9735 return -1;
9736 }
9737 i++;
9738 }
9739 return 0;
9740}
9741
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009742/* parse the "ca-base" / "crt-base" keywords in global section.
9743 * Returns <0 on alert, >0 on warning, 0 on success.
9744 */
9745static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
9746 struct proxy *defpx, const char *file, int line,
9747 char **err)
9748{
9749 char **target;
9750
Willy Tarreauef934602016-12-22 23:12:01 +01009751 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009752
9753 if (too_many_args(1, args, err, NULL))
9754 return -1;
9755
9756 if (*target) {
9757 memprintf(err, "'%s' already specified.", args[0]);
9758 return -1;
9759 }
9760
9761 if (*(args[1]) == 0) {
9762 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
9763 return -1;
9764 }
9765 *target = strdup(args[1]);
9766 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009767}
9768
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01009769/* "issuers-chain-path" load chain certificate in global */
9770static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
9771{
9772 X509 *ca;
9773 X509_NAME *name = NULL;
9774 ASN1_OCTET_STRING *skid = NULL;
9775 STACK_OF(X509) *chain = NULL;
9776 struct issuer_chain *issuer;
9777 struct eb64_node *node;
9778 char *path;
9779 u64 key;
9780 int ret = 0;
9781
9782 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
9783 if (chain == NULL) {
9784 chain = sk_X509_new_null();
9785 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
9786 name = X509_get_subject_name(ca);
9787 }
9788 if (!sk_X509_push(chain, ca)) {
9789 X509_free(ca);
9790 goto end;
9791 }
9792 }
9793 if (!chain) {
9794 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
9795 goto end;
9796 }
9797 if (!skid) {
9798 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
9799 goto end;
9800 }
9801 if (!name) {
9802 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
9803 goto end;
9804 }
9805 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +01009806 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01009807 issuer = container_of(node, typeof(*issuer), node);
9808 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
9809 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
9810 goto end;
9811 }
9812 }
9813 issuer = calloc(1, sizeof *issuer);
9814 path = strdup(fp);
9815 if (!issuer || !path) {
9816 free(issuer);
9817 free(path);
9818 goto end;
9819 }
9820 issuer->node.key = key;
9821 issuer->path = path;
9822 issuer->chain = chain;
9823 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +01009824 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01009825 ret = 1;
9826 end:
9827 if (skid)
9828 ASN1_OCTET_STRING_free(skid);
9829 if (chain)
9830 sk_X509_pop_free(chain, X509_free);
9831 return ret;
9832}
9833
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +01009834static struct issuer_chain* ssl_get_issuer_chain(X509 *cert)
9835{
9836 AUTHORITY_KEYID *akid;
9837 struct issuer_chain *issuer = NULL;
9838
9839 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
9840 if (akid) {
9841 struct eb64_node *node;
9842 u64 hk;
9843 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
9844 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
9845 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
9846 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
9847 issuer = ti;
9848 break;
9849 }
9850 }
9851 AUTHORITY_KEYID_free(akid);
9852 }
9853 return issuer;
9854}
9855
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01009856static void ssl_free_global_issuers(void)
9857{
9858 struct eb64_node *node, *back;
9859 struct issuer_chain *issuer;
9860
William Lallemande0f3fd52020-02-25 14:53:06 +01009861 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01009862 while (node) {
9863 issuer = container_of(node, typeof(*issuer), node);
9864 back = eb64_next(node);
9865 eb64_delete(node);
9866 free(issuer->path);
9867 sk_X509_pop_free(issuer->chain, X509_free);
9868 free(issuer);
9869 node = back;
9870 }
9871}
9872
9873static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
9874 struct proxy *defpx, const char *file, int line,
9875 char **err)
9876{
9877 char *path;
9878 struct dirent **de_list;
9879 int i, n;
9880 struct stat buf;
9881 char *end;
9882 char fp[MAXPATHLEN+1];
9883
9884 if (too_many_args(1, args, err, NULL))
9885 return -1;
9886
9887 path = args[1];
9888 if (*path == 0 || stat(path, &buf)) {
9889 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
9890 err && *err ? *err : "", args[0]);
9891 return -1;
9892 }
9893 if (S_ISDIR(buf.st_mode) == 0) {
9894 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
9895 err && *err ? *err : "", args[0], path);
9896 return -1;
9897 }
9898
9899 /* strip trailing slashes, including first one */
9900 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
9901 *end = 0;
9902 /* path already parsed? */
9903 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
9904 return 0;
9905 /* overwrite old issuers_chain_path */
9906 free(global_ssl.issuers_chain_path);
9907 global_ssl.issuers_chain_path = strdup(path);
9908 ssl_free_global_issuers();
9909
9910 n = scandir(path, &de_list, 0, alphasort);
9911 if (n < 0) {
9912 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
9913 err && *err ? *err : "", args[0], path, strerror(errno));
9914 return -1;
9915 }
9916 for (i = 0; i < n; i++) {
9917 struct dirent *de = de_list[i];
9918 BIO *in = NULL;
9919 char *warn = NULL;
9920
9921 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
9922 free(de);
9923 if (stat(fp, &buf) != 0) {
9924 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
9925 goto next;
9926 }
9927 if (!S_ISREG(buf.st_mode))
9928 goto next;
9929
9930 in = BIO_new(BIO_s_file());
9931 if (in == NULL)
9932 goto next;
9933 if (BIO_read_filename(in, fp) <= 0)
9934 goto next;
9935 ssl_load_global_issuer_from_BIO(in, fp, &warn);
9936 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +01009937 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01009938 free(warn);
9939 warn = NULL;
9940 }
9941 next:
9942 if (in)
9943 BIO_free(in);
9944 }
9945 free(de_list);
9946
9947 return 0;
9948}
9949
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009950/* parse the "ssl-mode-async" keyword in global section.
9951 * Returns <0 on alert, >0 on warning, 0 on success.
9952 */
9953static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
9954 struct proxy *defpx, const char *file, int line,
9955 char **err)
9956{
Willy Tarreau5db847a2019-05-09 14:13:35 +02009957#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009958 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01009959 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009960 return 0;
9961#else
9962 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
9963 return -1;
9964#endif
9965}
9966
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009967#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009968static int ssl_check_async_engine_count(void) {
9969 int err_code = 0;
9970
Emeric Brun3854e012017-05-17 20:42:48 +02009971 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01009972 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00009973 err_code = ERR_ABORT;
9974 }
9975 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01009976}
9977
Grant Zhang872f9c22017-01-21 01:10:18 +00009978/* parse the "ssl-engine" keyword in global section.
9979 * Returns <0 on alert, >0 on warning, 0 on success.
9980 */
9981static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9982 struct proxy *defpx, const char *file, int line,
9983 char **err)
9984{
9985 char *algo;
9986 int ret = -1;
9987
9988 if (*(args[1]) == 0) {
9989 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9990 return ret;
9991 }
9992
9993 if (*(args[2]) == 0) {
9994 /* if no list of algorithms is given, it defaults to ALL */
9995 algo = strdup("ALL");
9996 goto add_engine;
9997 }
9998
9999 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
10000 if (strcmp(args[2], "algo") != 0) {
10001 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
10002 return ret;
10003 }
10004
10005 if (*(args[3]) == 0) {
10006 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
10007 return ret;
10008 }
10009 algo = strdup(args[3]);
10010
10011add_engine:
10012 if (ssl_init_single_engine(args[1], algo)==0) {
10013 openssl_engines_initialized++;
10014 ret = 0;
10015 }
10016 free(algo);
10017 return ret;
10018}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010019#endif
Grant Zhang872f9c22017-01-21 01:10:18 +000010020
Willy Tarreauf22e9682016-12-21 23:23:19 +010010021/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
10022 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10023 */
10024static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
10025 struct proxy *defpx, const char *file, int line,
10026 char **err)
10027{
10028 char **target;
10029
Willy Tarreauef934602016-12-22 23:12:01 +010010030 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +010010031
10032 if (too_many_args(1, args, err, NULL))
10033 return -1;
10034
10035 if (*(args[1]) == 0) {
10036 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10037 return -1;
10038 }
10039
10040 free(*target);
10041 *target = strdup(args[1]);
10042 return 0;
10043}
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010044
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010045#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010046/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
10047 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10048 */
10049static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
10050 struct proxy *defpx, const char *file, int line,
10051 char **err)
10052{
10053 char **target;
10054
10055 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
10056
10057 if (too_many_args(1, args, err, NULL))
10058 return -1;
10059
10060 if (*(args[1]) == 0) {
10061 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10062 return -1;
10063 }
10064
10065 free(*target);
10066 *target = strdup(args[1]);
10067 return 0;
10068}
10069#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +010010070
Willy Tarreau9ceda382016-12-21 23:13:03 +010010071/* parse various global tune.ssl settings consisting in positive integers.
10072 * Returns <0 on alert, >0 on warning, 0 on success.
10073 */
10074static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
10075 struct proxy *defpx, const char *file, int line,
10076 char **err)
10077{
10078 int *target;
10079
10080 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
10081 target = &global.tune.sslcachesize;
10082 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010083 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010084 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010085 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010086 else if (strcmp(args[0], "maxsslconn") == 0)
10087 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010088 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
10089 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010090 else {
10091 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
10092 return -1;
10093 }
10094
10095 if (too_many_args(1, args, err, NULL))
10096 return -1;
10097
10098 if (*(args[1]) == 0) {
10099 memprintf(err, "'%s' expects an integer argument.", args[0]);
10100 return -1;
10101 }
10102
10103 *target = atoi(args[1]);
10104 if (*target < 0) {
10105 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
10106 return -1;
10107 }
10108 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010109}
10110
10111static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
10112 struct proxy *defpx, const char *file, int line,
10113 char **err)
10114{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010115 int ret;
10116
10117 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
10118 if (ret != 0)
10119 return ret;
10120
Willy Tarreaubafbe012017-11-24 17:34:44 +010010121 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010122 memprintf(err, "'%s' is already configured.", args[0]);
10123 return -1;
10124 }
10125
Willy Tarreaubafbe012017-11-24 17:34:44 +010010126 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
10127 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010128 memprintf(err, "Out of memory error.");
10129 return -1;
10130 }
10131 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010132}
10133
10134/* parse "ssl.force-private-cache".
10135 * Returns <0 on alert, >0 on warning, 0 on success.
10136 */
10137static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
10138 struct proxy *defpx, const char *file, int line,
10139 char **err)
10140{
10141 if (too_many_args(0, args, err, NULL))
10142 return -1;
10143
Willy Tarreauef934602016-12-22 23:12:01 +010010144 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010145 return 0;
10146}
10147
10148/* parse "ssl.lifetime".
10149 * Returns <0 on alert, >0 on warning, 0 on success.
10150 */
10151static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
10152 struct proxy *defpx, const char *file, int line,
10153 char **err)
10154{
10155 const char *res;
10156
10157 if (too_many_args(1, args, err, NULL))
10158 return -1;
10159
10160 if (*(args[1]) == 0) {
10161 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
10162 return -1;
10163 }
10164
Willy Tarreauef934602016-12-22 23:12:01 +010010165 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +020010166 if (res == PARSE_TIME_OVER) {
10167 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
10168 args[1], args[0]);
10169 return -1;
10170 }
10171 else if (res == PARSE_TIME_UNDER) {
10172 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
10173 args[1], args[0]);
10174 return -1;
10175 }
10176 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010177 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
10178 return -1;
10179 }
10180 return 0;
10181}
10182
10183#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +010010184/* parse "ssl-dh-param-file".
10185 * Returns <0 on alert, >0 on warning, 0 on success.
10186 */
10187static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
10188 struct proxy *defpx, const char *file, int line,
10189 char **err)
10190{
10191 if (too_many_args(1, args, err, NULL))
10192 return -1;
10193
10194 if (*(args[1]) == 0) {
10195 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
10196 return -1;
10197 }
10198
10199 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
10200 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
10201 return -1;
10202 }
10203 return 0;
10204}
10205
Willy Tarreau9ceda382016-12-21 23:13:03 +010010206/* parse "ssl.default-dh-param".
10207 * Returns <0 on alert, >0 on warning, 0 on success.
10208 */
10209static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
10210 struct proxy *defpx, const char *file, int line,
10211 char **err)
10212{
10213 if (too_many_args(1, args, err, NULL))
10214 return -1;
10215
10216 if (*(args[1]) == 0) {
10217 memprintf(err, "'%s' expects an integer argument.", args[0]);
10218 return -1;
10219 }
10220
Willy Tarreauef934602016-12-22 23:12:01 +010010221 global_ssl.default_dh_param = atoi(args[1]);
10222 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010223 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
10224 return -1;
10225 }
10226 return 0;
10227}
10228#endif
10229
William Lallemand3af48e72020-02-03 17:15:52 +010010230
10231/*
10232 * parse "ssl-load-extra-files".
10233 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
10234 */
10235static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
10236 struct proxy *defpx, const char *file, int line,
10237 char **err)
10238{
10239 int i;
10240 int gf = SSL_GF_NONE;
10241
10242 if (*(args[1]) == 0)
10243 goto err_arg;
10244
10245 for (i = 1; *args[i]; i++) {
10246
10247 if (!strcmp("bundle", args[i])) {
10248 gf |= SSL_GF_BUNDLE;
10249
10250 } else if (!strcmp("sctl", args[i])) {
10251 gf |= SSL_GF_SCTL;
10252
10253 } else if (!strcmp("ocsp", args[i])){
10254 gf |= SSL_GF_OCSP;
10255
10256 } else if (!strcmp("issuer", args[i])){
10257 gf |= SSL_GF_OCSP_ISSUER;
10258
William Lallemand4c5adbf2020-02-24 14:23:22 +010010259 } else if (!strcmp("key", args[i])) {
10260 gf |= SSL_GF_KEY;
10261
William Lallemand3af48e72020-02-03 17:15:52 +010010262 } else if (!strcmp("none", args[i])) {
10263 if (gf != SSL_GF_NONE)
10264 goto err_alone;
10265 gf = SSL_GF_NONE;
10266 i++;
10267 break;
10268
10269 } else if (!strcmp("all", args[i])) {
10270 if (gf != SSL_GF_NONE)
10271 goto err_alone;
10272 gf = SSL_GF_ALL;
10273 i++;
10274 break;
10275 } else {
10276 goto err_arg;
10277 }
10278 }
10279 /* break from loop but there are still arguments */
10280 if (*args[i])
10281 goto err_alone;
10282
10283 global_ssl.extra_files = gf;
10284
10285 return 0;
10286
10287err_alone:
10288 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10289 return -1;
10290
10291err_arg:
10292 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10293 return -1;
10294}
10295
Willy Tarreau9ceda382016-12-21 23:13:03 +010010296
William Lallemand32af2032016-10-29 18:09:35 +020010297/* This function is used with TLS ticket keys management. It permits to browse
10298 * each reference. The variable <getnext> must contain the current node,
10299 * <end> point to the root node.
10300 */
10301#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10302static inline
10303struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10304{
10305 struct tls_keys_ref *ref = getnext;
10306
10307 while (1) {
10308
10309 /* Get next list entry. */
10310 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10311
10312 /* If the entry is the last of the list, return NULL. */
10313 if (&ref->list == end)
10314 return NULL;
10315
10316 return ref;
10317 }
10318}
10319
10320static inline
10321struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10322{
10323 int id;
10324 char *error;
10325
10326 /* If the reference starts by a '#', this is numeric id. */
10327 if (reference[0] == '#') {
10328 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10329 id = strtol(reference + 1, &error, 10);
10330 if (*error != '\0')
10331 return NULL;
10332
10333 /* Perform the unique id lookup. */
10334 return tlskeys_ref_lookupid(id);
10335 }
10336
10337 /* Perform the string lookup. */
10338 return tlskeys_ref_lookup(reference);
10339}
10340#endif
10341
10342
10343#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10344
10345static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10346
10347static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10348 return cli_io_handler_tlskeys_files(appctx);
10349}
10350
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010351/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10352 * (next index to be dumped), and cli.p0 (next key reference).
10353 */
William Lallemand32af2032016-10-29 18:09:35 +020010354static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10355
10356 struct stream_interface *si = appctx->owner;
10357
10358 switch (appctx->st2) {
10359 case STAT_ST_INIT:
10360 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010361 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010362 * later and restart at the state "STAT_ST_INIT".
10363 */
10364 chunk_reset(&trash);
10365
10366 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10367 chunk_appendf(&trash, "# id secret\n");
10368 else
10369 chunk_appendf(&trash, "# id (file)\n");
10370
Willy Tarreau06d80a92017-10-19 14:32:15 +020010371 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010372 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010373 return 0;
10374 }
10375
William Lallemand32af2032016-10-29 18:09:35 +020010376 /* Now, we start the browsing of the references lists.
10377 * Note that the following call to LIST_ELEM return bad pointer. The only
10378 * available field of this pointer is <list>. It is used with the function
10379 * tlskeys_list_get_next() for retruning the first available entry
10380 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010381 if (appctx->ctx.cli.p0 == NULL) {
10382 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10383 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010384 }
10385
10386 appctx->st2 = STAT_ST_LIST;
10387 /* fall through */
10388
10389 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010390 while (appctx->ctx.cli.p0) {
10391 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010392
10393 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010394 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010395 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010396
10397 if (appctx->ctx.cli.i1 == 0)
10398 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10399
William Lallemand32af2032016-10-29 18:09:35 +020010400 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010401 int head;
10402
10403 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10404 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010405 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010406 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010407
10408 chunk_reset(t2);
10409 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010410 if (ref->key_size_bits == 128) {
10411 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10412 sizeof(struct tls_sess_key_128),
10413 t2->area, t2->size);
10414 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10415 t2->area);
10416 }
10417 else if (ref->key_size_bits == 256) {
10418 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10419 sizeof(struct tls_sess_key_256),
10420 t2->area, t2->size);
10421 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10422 t2->area);
10423 }
10424 else {
10425 /* This case should never happen */
10426 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10427 }
William Lallemand32af2032016-10-29 18:09:35 +020010428
Willy Tarreau06d80a92017-10-19 14:32:15 +020010429 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010430 /* let's try again later from this stream. We add ourselves into
10431 * this stream's users so that it can remove us upon termination.
10432 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010433 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010434 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010435 return 0;
10436 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010437 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010438 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010439 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010440 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010441 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010442 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010443 /* let's try again later from this stream. We add ourselves into
10444 * this stream's users so that it can remove us upon termination.
10445 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010446 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010447 return 0;
10448 }
10449
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010450 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010451 break;
10452
10453 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010454 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010455 }
10456
10457 appctx->st2 = STAT_ST_FIN;
10458 /* fall through */
10459
10460 default:
10461 appctx->st2 = STAT_ST_FIN;
10462 return 1;
10463 }
10464 return 0;
10465}
10466
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010467/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010468static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010469{
William Lallemand32af2032016-10-29 18:09:35 +020010470 /* no parameter, shows only file list */
10471 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010472 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010473 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010474 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010475 }
10476
10477 if (args[2][0] == '*') {
10478 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010479 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010480 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010481 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010482 if (!appctx->ctx.cli.p0)
10483 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010484 }
William Lallemand32af2032016-10-29 18:09:35 +020010485 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010486 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010487}
10488
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010489static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010490{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010491 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010492 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010493
William Lallemand32af2032016-10-29 18:09:35 +020010494 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010495 if (!*args[3] || !*args[4])
10496 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 +020010497
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010498 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010499 if (!ref)
10500 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010501
Willy Tarreau1c913e42018-08-22 05:26:57 +020010502 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010503 if (ret < 0)
10504 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010505
Willy Tarreau1c913e42018-08-22 05:26:57 +020010506 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010507 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10508 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010509
Willy Tarreau9d008692019-08-09 11:21:01 +020010510 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010511}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010512#endif
William Lallemand32af2032016-10-29 18:09:35 +020010513
William Lallemand44b35322019-10-17 16:28:40 +020010514
10515/* Type of SSL payloads that can be updated over the CLI */
10516
10517enum {
10518 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010010519 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010520#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010521 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010522#endif
William Lallemand44b35322019-10-17 16:28:40 +020010523 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010524#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010525 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010526#endif
William Lallemand44b35322019-10-17 16:28:40 +020010527 CERT_TYPE_MAX,
10528};
10529
10530struct {
10531 const char *ext;
10532 int type;
10533 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
10534 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020010535} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020010536 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010010537 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020010538#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010539 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010540#endif
10541#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010542 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010543#endif
William Lallemand44b35322019-10-17 16:28:40 +020010544 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020010545 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020010546};
10547
William Lallemand430413e2019-10-28 14:30:47 +010010548/* states of the CLI IO handler for 'set ssl cert' */
10549enum {
10550 SETCERT_ST_INIT = 0,
10551 SETCERT_ST_GEN,
10552 SETCERT_ST_INSERT,
10553 SETCERT_ST_FIN,
10554};
William Lallemand8f840d72019-10-23 10:53:05 +020010555
William Lallemandd4f946c2019-12-05 10:26:40 +010010556/* release function of the `show ssl cert' command */
10557static void cli_release_show_cert(struct appctx *appctx)
10558{
10559 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10560}
10561
10562/* IO handler of "show ssl cert <filename>" */
10563static int cli_io_handler_show_cert(struct appctx *appctx)
10564{
10565 struct buffer *trash = alloc_trash_chunk();
10566 struct ebmb_node *node;
10567 struct stream_interface *si = appctx->owner;
10568 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010010569
10570 if (trash == NULL)
10571 return 1;
10572
10573 if (!appctx->ctx.ssl.old_ckchs) {
10574 if (ckchs_transaction.old_ckchs) {
10575 ckchs = ckchs_transaction.old_ckchs;
10576 chunk_appendf(trash, "# transaction\n");
10577 if (!ckchs->multi) {
10578 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010579#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010580 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010010581 int n;
10582
William Lallemandd4f946c2019-12-05 10:26:40 +010010583 chunk_appendf(trash, "*%s:", ckchs->path);
10584 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10585 if (ckchs->ckch[n].cert)
10586 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10587 }
10588 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010589#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010590 }
10591 }
10592 }
10593
10594 if (!appctx->ctx.cli.p0) {
10595 chunk_appendf(trash, "# filename\n");
10596 node = ebmb_first(&ckchs_tree);
10597 } else {
10598 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
10599 }
10600 while (node) {
10601 ckchs = ebmb_entry(node, struct ckch_store, node);
10602 if (!ckchs->multi) {
10603 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010604#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010605 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010010606 int n;
10607
William Lallemandd4f946c2019-12-05 10:26:40 +010010608 chunk_appendf(trash, "%s:", ckchs->path);
10609 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10610 if (ckchs->ckch[n].cert)
10611 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10612 }
10613 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010614#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010615 }
10616
10617 node = ebmb_next(node);
10618 if (ci_putchk(si_ic(si), trash) == -1) {
10619 si_rx_room_blk(si);
10620 goto yield;
10621 }
10622 }
10623
10624 appctx->ctx.cli.p0 = NULL;
10625 free_trash_chunk(trash);
10626 return 1;
10627yield:
10628
10629 free_trash_chunk(trash);
10630 appctx->ctx.cli.p0 = ckchs;
10631 return 0; /* should come back */
10632}
10633
10634/* IO handler of the details "show ssl cert <filename>" */
10635static int cli_io_handler_show_cert_detail(struct appctx *appctx)
10636{
10637 struct stream_interface *si = appctx->owner;
10638 struct ckch_store *ckchs = appctx->ctx.cli.p0;
10639 struct buffer *out = alloc_trash_chunk();
10640 struct buffer *tmp = alloc_trash_chunk();
10641 X509_NAME *name = NULL;
Willy Tarreau105599c2020-02-25 08:59:23 +010010642 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010010643 int write = -1;
10644 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010010645 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010010646
10647 if (!tmp || !out)
10648 goto end;
10649
10650 if (!ckchs->multi) {
10651 chunk_appendf(out, "Filename: ");
10652 if (ckchs == ckchs_transaction.new_ckchs)
10653 chunk_appendf(out, "*");
10654 chunk_appendf(out, "%s\n", ckchs->path);
10655 chunk_appendf(out, "Serial: ");
10656 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
10657 goto end;
10658 dump_binary(out, tmp->area, tmp->data);
10659 chunk_appendf(out, "\n");
10660
10661 chunk_appendf(out, "notBefore: ");
10662 chunk_reset(tmp);
10663 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10664 goto end;
10665 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
10666 goto end;
10667 write = BIO_read(bio, tmp->area, tmp->size-1);
10668 tmp->area[write] = '\0';
10669 BIO_free(bio);
10670 chunk_appendf(out, "%s\n", tmp->area);
10671
10672 chunk_appendf(out, "notAfter: ");
10673 chunk_reset(tmp);
10674 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10675 goto end;
10676 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
10677 goto end;
10678 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
10679 goto end;
10680 tmp->area[write] = '\0';
10681 BIO_free(bio);
10682 chunk_appendf(out, "%s\n", tmp->area);
10683
William Lallemandd4f946c2019-12-05 10:26:40 +010010684#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10685 chunk_appendf(out, "Subject Alternative Name: ");
10686 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
10687 goto end;
10688 *(out->area + out->data) = '\0';
10689 chunk_appendf(out, "\n");
10690#endif
10691 chunk_reset(tmp);
10692 chunk_appendf(out, "Algorithm: ");
10693 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
10694 goto end;
10695 chunk_appendf(out, "%s\n", tmp->area);
10696
10697 chunk_reset(tmp);
10698 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010010699 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010010700 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010010701 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010010702 dump_binary(out, tmp->area, tmp->data);
10703 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010010704
William Lallemanda90e5932020-02-25 14:07:58 +010010705 chunk_appendf(out, "Subject: ");
10706 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
10707 goto end;
10708 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10709 goto end;
10710 *(tmp->area + tmp->data) = '\0';
10711 chunk_appendf(out, "%s\n", tmp->area);
10712
10713 chunk_appendf(out, "Issuer: ");
10714 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
10715 goto end;
10716 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10717 goto end;
10718 *(tmp->area + tmp->data) = '\0';
10719 chunk_appendf(out, "%s\n", tmp->area);
10720
William Lallemand35f4a9d2020-02-25 11:56:32 +010010721 /* Displays subject of each certificate in the chain */
10722 for (i = 0; i < sk_X509_num(ckchs->ckch->chain); i++) {
10723 X509 *ca = sk_X509_value(ckchs->ckch->chain, i);
10724
William Lallemandbb7288a2020-02-25 14:04:33 +010010725 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010010726 if ((name = X509_get_subject_name(ca)) == NULL)
10727 goto end;
10728 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10729 goto end;
10730 *(tmp->area + tmp->data) = '\0';
10731 chunk_appendf(out, "%s\n", tmp->area);
10732
William Lallemandbb7288a2020-02-25 14:04:33 +010010733 chunk_appendf(out, "Chain Issuer: ");
10734 if ((name = X509_get_issuer_name(ca)) == NULL)
10735 goto end;
10736 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10737 goto end;
10738 *(tmp->area + tmp->data) = '\0';
10739 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010010740 }
William Lallemandd4f946c2019-12-05 10:26:40 +010010741 }
10742
10743 if (ci_putchk(si_ic(si), out) == -1) {
10744 si_rx_room_blk(si);
10745 goto yield;
10746 }
10747
10748end:
10749 free_trash_chunk(tmp);
10750 free_trash_chunk(out);
10751 return 1;
10752yield:
10753 free_trash_chunk(tmp);
10754 free_trash_chunk(out);
10755 return 0; /* should come back */
10756}
10757
10758/* parsing function for 'show ssl cert [certfile]' */
10759static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
10760{
10761 struct ckch_store *ckchs;
10762
10763 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
10764 return cli_err(appctx, "Can't allocate memory!\n");
10765
10766 /* The operations on the CKCH architecture are locked so we can
10767 * manipulate ckch_store and ckch_inst */
10768 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10769 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
10770
10771 /* check if there is a certificate to lookup */
10772 if (*args[3]) {
10773 if (*args[3] == '*') {
10774 if (!ckchs_transaction.new_ckchs)
10775 goto error;
10776
10777 ckchs = ckchs_transaction.new_ckchs;
10778
10779 if (strcmp(args[3] + 1, ckchs->path))
10780 goto error;
10781
10782 } else {
10783 if ((ckchs = ckchs_lookup(args[3])) == NULL)
10784 goto error;
10785
10786 }
10787
10788 if (ckchs->multi)
10789 goto error;
10790
10791 appctx->ctx.cli.p0 = ckchs;
10792 /* use the IO handler that shows details */
10793 appctx->io_handler = cli_io_handler_show_cert_detail;
10794 }
10795
10796 return 0;
10797
10798error:
10799 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10800 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
10801}
10802
William Lallemand430413e2019-10-28 14:30:47 +010010803/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010804static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010805{
10806 struct ckch_store *new_ckchs;
10807 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010808
William Lallemand430413e2019-10-28 14:30:47 +010010809 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020010810
William Lallemand430413e2019-10-28 14:30:47 +010010811 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020010812 /* 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 +010010813 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010814
William Lallemandbeea2a42019-10-30 17:45:33 +010010815 if (!new_ckchs)
10816 return;
William Lallemand8f840d72019-10-23 10:53:05 +020010817
William Lallemandbeea2a42019-10-30 17:45:33 +010010818 /* if the allocation failed, we need to free everything from the temporary list */
10819 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10820 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010821
William Lallemandbeea2a42019-10-30 17:45:33 +010010822 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10823 if (sc0->order == 0) /* we only free if it's the first inserted */
10824 SSL_CTX_free(sc0->ctx);
10825 LIST_DEL(&sc0->by_ckch_inst);
10826 free(sc0);
William Lallemand8f840d72019-10-23 10:53:05 +020010827 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010828 LIST_DEL(&ckchi->by_ckchs);
10829 free(ckchi);
William Lallemand8f840d72019-10-23 10:53:05 +020010830 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010831 ckchs_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010832 }
10833}
10834
10835
10836/*
10837 * This function tries to create the new ckch_inst and their SNIs
10838 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010839static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010840{
10841 struct stream_interface *si = appctx->owner;
10842 int y = 0;
10843 char *err = NULL;
10844 int errcode = 0;
10845 struct ckch_store *old_ckchs, *new_ckchs = NULL;
10846 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010847 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010848 struct sni_ctx *sc0, *sc0s;
William Lallemand8f840d72019-10-23 10:53:05 +020010849
William Lallemand33cc76f2019-10-31 11:43:45 +010010850 if (trash == NULL)
10851 goto error;
10852
William Lallemand8f840d72019-10-23 10:53:05 +020010853 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
10854 goto error;
10855
William Lallemand430413e2019-10-28 14:30:47 +010010856 while (1) {
10857 switch (appctx->st2) {
10858 case SETCERT_ST_INIT:
10859 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010860 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010010861 if (ci_putchk(si_ic(si), trash) == -1) {
10862 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020010863 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010010864 }
10865 appctx->st2 = SETCERT_ST_GEN;
10866 /* fallthrough */
10867 case SETCERT_ST_GEN:
10868 /*
10869 * This state generates the ckch instances with their
10870 * sni_ctxs and SSL_CTX.
10871 *
William Lallemand430413e2019-10-28 14:30:47 +010010872 * Since the SSL_CTX generation can be CPU consumer, we
10873 * yield every 10 instances.
10874 */
William Lallemand8f840d72019-10-23 10:53:05 +020010875
William Lallemandbeea2a42019-10-30 17:45:33 +010010876 old_ckchs = appctx->ctx.ssl.old_ckchs;
10877 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010878
William Lallemandbeea2a42019-10-30 17:45:33 +010010879 if (!new_ckchs)
10880 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020010881
William Lallemandbeea2a42019-10-30 17:45:33 +010010882 /* get the next ckchi to regenerate */
10883 ckchi = appctx->ctx.ssl.next_ckchi;
10884 /* we didn't start yet, set it to the first elem */
10885 if (ckchi == NULL)
10886 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010887
William Lallemandbeea2a42019-10-30 17:45:33 +010010888 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
10889 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
10890 struct ckch_inst *new_inst;
William Lallemand8f840d72019-10-23 10:53:05 +020010891
William Lallemandbeea2a42019-10-30 17:45:33 +010010892 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
10893 if (y >= 10) {
10894 /* save the next ckchi to compute */
10895 appctx->ctx.ssl.next_ckchi = ckchi;
10896 goto yield;
10897 }
William Lallemand8f840d72019-10-23 10:53:05 +020010898
William Lallemandbeea2a42019-10-30 17:45:33 +010010899 if (new_ckchs->multi)
10900 errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err);
10901 else
10902 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 +020010903
William Lallemandbeea2a42019-10-30 17:45:33 +010010904 if (errcode & ERR_CODE)
10905 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020010906
William Lallemand21724f02019-11-04 17:56:13 +010010907 /* if the previous ckchi was used as the default */
10908 if (ckchi->is_default)
10909 new_inst->is_default = 1;
10910
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010911 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010010912 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
10913 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010914 if (!sc0->order) { /* we initiliazed only the first SSL_CTX because it's the same in the other sni_ctx's */
10915 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
10916 if (errcode & ERR_CODE)
10917 goto error;
10918 }
10919 }
10920
10921
William Lallemandbeea2a42019-10-30 17:45:33 +010010922 /* display one dot per new instance */
10923 chunk_appendf(trash, ".");
10924 /* link the new ckch_inst to the duplicate */
10925 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
10926 y++;
10927 }
William Lallemand430413e2019-10-28 14:30:47 +010010928 appctx->st2 = SETCERT_ST_INSERT;
10929 /* fallthrough */
10930 case SETCERT_ST_INSERT:
10931 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020010932
William Lallemandbeea2a42019-10-30 17:45:33 +010010933 old_ckchs = appctx->ctx.ssl.old_ckchs;
10934 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010935
William Lallemandbeea2a42019-10-30 17:45:33 +010010936 if (!new_ckchs)
10937 continue;
William Lallemand430413e2019-10-28 14:30:47 +010010938
William Lallemand21724f02019-11-04 17:56:13 +010010939 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010010940 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10941 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10942 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
10943 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10944 }
William Lallemand8f840d72019-10-23 10:53:05 +020010945
William Lallemandbeea2a42019-10-30 17:45:33 +010010946 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
10947 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
William Lallemand430413e2019-10-28 14:30:47 +010010948
William Lallemandbeea2a42019-10-30 17:45:33 +010010949 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10950 list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) {
10951 ebmb_delete(&sc0->name);
10952 LIST_DEL(&sc0->by_ckch_inst);
10953 free(sc0);
William Lallemand430413e2019-10-28 14:30:47 +010010954 }
William Lallemandbeea2a42019-10-30 17:45:33 +010010955 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10956 LIST_DEL(&ckchi->by_ckchs);
10957 free(ckchi);
10958 }
William Lallemand8f840d72019-10-23 10:53:05 +020010959
William Lallemandbeea2a42019-10-30 17:45:33 +010010960 /* Replace the old ckchs by the new one */
10961 ebmb_delete(&old_ckchs->node);
10962 ckchs_free(old_ckchs);
10963 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010010964 appctx->st2 = SETCERT_ST_FIN;
10965 /* fallthrough */
10966 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010967 /* we achieved the transaction, we can set everything to NULL */
10968 free(ckchs_transaction.path);
10969 ckchs_transaction.path = NULL;
10970 ckchs_transaction.new_ckchs = NULL;
10971 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010010972 goto end;
10973 }
William Lallemand8f840d72019-10-23 10:53:05 +020010974 }
William Lallemand430413e2019-10-28 14:30:47 +010010975end:
William Lallemand8f840d72019-10-23 10:53:05 +020010976
William Lallemanded442432019-11-21 16:41:07 +010010977 chunk_appendf(trash, "\n");
10978 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010010979 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010010980 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010010981 if (ci_putchk(si_ic(si), trash) == -1)
10982 si_rx_room_blk(si);
10983 free_trash_chunk(trash);
10984 /* success: call the release function and don't come back */
10985 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010986yield:
10987 /* store the state */
10988 if (ci_putchk(si_ic(si), trash) == -1)
10989 si_rx_room_blk(si);
10990 free_trash_chunk(trash);
10991 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020010992 return 0; /* should come back */
10993
10994error:
10995 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010010996 if (trash) {
10997 chunk_appendf(trash, "\n%sFailed!\n", err);
10998 if (ci_putchk(si_ic(si), trash) == -1)
10999 si_rx_room_blk(si);
11000 free_trash_chunk(trash);
11001 }
William Lallemand430413e2019-10-28 14:30:47 +010011002 /* error: call the release function and don't come back */
11003 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020011004}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011005
11006/*
11007 * Parsing function of 'commit ssl cert'
11008 */
11009static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
11010{
11011 char *err = NULL;
11012
William Lallemand230662a2019-12-03 13:32:54 +010011013 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11014 return 1;
11015
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011016 if (!*args[3])
11017 return cli_err(appctx, "'commit ssl cert expects a filename\n");
11018
11019 /* The operations on the CKCH architecture are locked so we can
11020 * manipulate ckch_store and ckch_inst */
11021 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11022 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
11023
11024 if (!ckchs_transaction.path) {
11025 memprintf(&err, "No ongoing transaction! !\n");
11026 goto error;
11027 }
11028
11029 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
11030 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
11031 goto error;
11032 }
11033
William Lallemand4c5adbf2020-02-24 14:23:22 +010011034#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
11035 if (ckchs_transaction.new_ckchs->multi) {
11036 int n;
11037
11038 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11039 if (ckchs_transaction.new_ckchs->ckch[n].cert && !X509_check_private_key(ckchs_transaction.new_ckchs->ckch[n].cert, ckchs_transaction.new_ckchs->ckch[n].key)) {
11040 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
11041 goto error;
11042 }
11043 }
11044 } else
11045#endif
11046 {
11047 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
11048 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
11049 goto error;
11050 }
11051 }
11052
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011053 /* init the appctx structure */
11054 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011055 appctx->ctx.ssl.next_ckchi = NULL;
11056 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
11057 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
11058
11059 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
11060 return 0;
11061
11062error:
11063
11064 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11065 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
11066
11067 return cli_dynerr(appctx, err);
11068}
11069
11070
William Lallemand8f840d72019-10-23 10:53:05 +020011071/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011072 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020011073 */
William Lallemand150bfa82019-09-19 17:12:49 +020011074static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
11075{
William Lallemand0c3b7d92019-10-18 11:27:07 +020011076 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020011077 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020011078 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020011079 int i;
William Lallemand849eed62019-10-17 16:23:50 +020011080 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020011081 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020011082 char *end;
11083 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011084 struct cert_key_and_chain *ckch;
11085 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020011086
William Lallemand230662a2019-12-03 13:32:54 +010011087 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11088 return 1;
11089
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011090 if ((buf = alloc_trash_chunk()) == NULL)
11091 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020011092
11093 if (!*args[3] || !payload)
11094 return cli_err(appctx, "'set ssl cert expects a filename and a certificat as a payload\n");
11095
11096 /* The operations on the CKCH architecture are locked so we can
11097 * manipulate ckch_store and ckch_inst */
11098 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11099 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
11100
William Lallemand8f840d72019-10-23 10:53:05 +020011101 if (!chunk_strcpy(buf, args[3])) {
11102 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
11103 errcode |= ERR_ALERT | ERR_FATAL;
11104 goto end;
11105 }
11106
William Lallemand44b35322019-10-17 16:28:40 +020011107 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020011108 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020011109 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020011110 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
11111 *end = '\0';
11112 type = cert_exts[i].type;
11113 break;
11114 }
11115 }
11116
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011117 appctx->ctx.ssl.old_ckchs = NULL;
11118 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020011119
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011120 /* if there is an ongoing transaction */
11121 if (ckchs_transaction.path) {
11122 /* 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 +020011123#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011124 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010011125 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020011126 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020011127
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011128 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020011129 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020011130 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010011131 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020011132 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
11133 bundle = j; /* keep the type of certificate so we insert it at the right place */
11134 *end = '\0'; /* it's a bundle let's end the string*/
11135 break;
11136 }
William Lallemand150bfa82019-09-19 17:12:49 +020011137 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011138 if (bundle < 0) {
11139 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);
11140 errcode |= ERR_ALERT | ERR_FATAL;
11141 goto end;
11142 }
11143 }
11144#endif
11145
11146 /* if there is an ongoing transaction, check if this is the same file */
11147 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
11148 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
11149 errcode |= ERR_ALERT | ERR_FATAL;
11150 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020011151 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011152
11153 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
11154
11155 } else {
11156 struct ckch_store *find_ckchs[2] = { NULL, NULL };
11157
11158 /* lookup for the certificate in the tree:
11159 * check if this is used as a bundle AND as a unique certificate */
11160 for (i = 0; i < 2; i++) {
11161
11162 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
11163 /* only the bundle name is in the tree and you should
11164 * never update a bundle name, only a filename */
11165 if (bundle < 0 && find_ckchs[i]->multi) {
11166 /* we tried to look for a non-bundle and we found a bundle */
11167 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
11168 err ? err : "", args[3], args[3]);
11169 errcode |= ERR_ALERT | ERR_FATAL;
11170 goto end;
11171 }
William Lallemand3246d942019-11-04 14:02:11 +010011172 /* If we want a bundle but this is not a bundle
11173 * example: When you try to update <file>.rsa, but
11174 * <file> is a regular file */
11175 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
11176 find_ckchs[i] = NULL;
11177 break;
11178 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011179 }
11180#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
11181 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010011182 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011183 int j;
11184
11185 /* check if it was used in a bundle by removing the
11186 * .dsa/.rsa/.ecdsa at the end of the filename */
11187 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010011188 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011189 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
11190 bundle = j; /* keep the type of certificate so we insert it at the right place */
11191 *end = '\0'; /* it's a bundle let's end the string*/
11192 break;
11193 }
11194 }
William Lallemand37031b82019-11-04 13:38:53 +010011195 if (bundle < 0) /* we didn't find a bundle extension */
11196 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011197 }
William Lallemand963b2e72019-10-14 11:38:36 +020011198#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011199 /* bundles are not supported here, so we don't need to lookup again */
11200 break;
William Lallemand963b2e72019-10-14 11:38:36 +020011201#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011202 }
11203
11204 if (find_ckchs[0] && find_ckchs[1]) {
11205 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",
11206 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
11207 errcode |= ERR_ALERT | ERR_FATAL;
11208 goto end;
11209 }
11210
11211 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020011212 }
11213
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011214 if (!appctx->ctx.ssl.old_ckchs) {
11215 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020011216 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020011217 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020011218 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020011219 }
11220
William Lallemand8a7fdf02019-11-04 10:59:32 +010011221 if (!appctx->ctx.ssl.path) {
11222 /* this is a new transaction, set the path of the transaction */
11223 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
11224 if (!appctx->ctx.ssl.path) {
11225 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
11226 errcode |= ERR_ALERT | ERR_FATAL;
11227 goto end;
11228 }
11229 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011230
11231 old_ckchs = appctx->ctx.ssl.old_ckchs;
11232
11233 /* TODO: handle filters */
11234 if (old_ckchs->filters) {
11235 memprintf(&err, "%sCertificates used in crt-list with filters are not supported!\n",
11236 err ? err : "");
11237 errcode |= ERR_ALERT | ERR_FATAL;
11238 goto end;
11239 }
11240
11241 /* duplicate the ckch store */
11242 new_ckchs = ckchs_dup(old_ckchs);
11243 if (!new_ckchs) {
11244 memprintf(&err, "%sCannot allocate memory!\n",
11245 err ? err : "");
11246 errcode |= ERR_ALERT | ERR_FATAL;
11247 goto end;
11248 }
11249
11250 if (!new_ckchs->multi)
11251 ckch = new_ckchs->ckch;
11252 else
11253 ckch = &new_ckchs->ckch[bundle];
11254
11255 /* appply the change on the duplicate */
11256 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
11257 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
11258 errcode |= ERR_ALERT | ERR_FATAL;
11259 goto end;
11260 }
11261
11262 appctx->ctx.ssl.new_ckchs = new_ckchs;
11263
11264 /* we succeed, we can save the ckchs in the transaction */
11265
11266 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010011267 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011268 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
11269 ckchs_transaction.path = appctx->ctx.ssl.path;
11270 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
11271 } else {
11272 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
11273
11274 }
11275
11276 /* free the previous ckchs if there was a transaction */
11277 ckchs_free(ckchs_transaction.new_ckchs);
11278
11279 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
11280
11281
William Lallemand8f840d72019-10-23 10:53:05 +020011282 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020011283
William Lallemand8f840d72019-10-23 10:53:05 +020011284end:
11285 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020011286
Emeric Brunf69ed1d2019-10-17 11:56:56 +020011287 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011288
11289 ckchs_free(appctx->ctx.ssl.new_ckchs);
11290 appctx->ctx.ssl.new_ckchs = NULL;
11291
11292 appctx->ctx.ssl.old_ckchs = NULL;
11293
11294 free(appctx->ctx.ssl.path);
11295 appctx->ctx.ssl.path = NULL;
11296
11297 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020011298 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010011299 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011300
11301 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11302 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010011303 }
William Lallemand8f840d72019-10-23 10:53:05 +020011304 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020011305}
11306
William Lallemand0bc9c8a2019-11-19 15:51:51 +010011307/* parsing function of 'abort ssl cert' */
11308static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
11309{
11310 char *err = NULL;
11311
William Lallemand230662a2019-12-03 13:32:54 +010011312 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11313 return 1;
11314
William Lallemand0bc9c8a2019-11-19 15:51:51 +010011315 if (!*args[3])
11316 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
11317
11318 /* The operations on the CKCH architecture are locked so we can
11319 * manipulate ckch_store and ckch_inst */
11320 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11321 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
11322
11323 if (!ckchs_transaction.path) {
11324 memprintf(&err, "No ongoing transaction!\n");
11325 goto error;
11326 }
11327
11328 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
11329 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
11330 goto error;
11331 }
11332
11333 /* Only free the ckchs there, because the SNI and instances were not generated yet */
11334 ckchs_free(ckchs_transaction.new_ckchs);
11335 ckchs_transaction.new_ckchs = NULL;
11336 ckchs_free(ckchs_transaction.old_ckchs);
11337 ckchs_transaction.old_ckchs = NULL;
11338 free(ckchs_transaction.path);
11339 ckchs_transaction.path = NULL;
11340
11341 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11342
11343 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
11344 return cli_dynmsg(appctx, LOG_NOTICE, err);
11345
11346error:
11347 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11348
11349 return cli_dynerr(appctx, err);
11350}
11351
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020011352static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020011353{
11354#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
11355 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020011356 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020011357
11358 if (!payload)
11359 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020011360
11361 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020011362 if (!*payload)
11363 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020011364
11365 /* remove \r and \n from the payload */
11366 for (i = 0, j = 0; payload[i]; i++) {
11367 if (payload[i] == '\r' || payload[i] == '\n')
11368 continue;
11369 payload[j++] = payload[i];
11370 }
11371 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020011372
Willy Tarreau1c913e42018-08-22 05:26:57 +020011373 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020011374 if (ret < 0)
11375 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020011376
Willy Tarreau1c913e42018-08-22 05:26:57 +020011377 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020011378 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020011379 if (err)
11380 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
11381 else
11382 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020011383 }
Willy Tarreau9d008692019-08-09 11:21:01 +020011384
11385 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020011386#else
Willy Tarreau9d008692019-08-09 11:21:01 +020011387 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 +020011388#endif
11389
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011390}
11391
Willy Tarreau86a394e2019-05-09 14:15:32 +020011392#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011393static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp)
11394{
11395 switch (arg->type) {
11396 case ARGT_STR:
11397 smp->data.type = SMP_T_STR;
11398 smp->data.u.str = arg->data.str;
11399 return 1;
11400 case ARGT_VAR:
11401 if (!vars_get_by_desc(&arg->data.var, smp))
11402 return 0;
11403 if (!sample_casts[smp->data.type][SMP_T_STR])
11404 return 0;
11405 if (!sample_casts[smp->data.type][SMP_T_STR](smp))
11406 return 0;
11407 return 1;
11408 default:
11409 return 0;
11410 }
11411}
11412
11413static int check_aes_gcm(struct arg *args, struct sample_conv *conv,
11414 const char *file, int line, char **err)
11415{
11416 switch(args[0].data.sint) {
11417 case 128:
11418 case 192:
11419 case 256:
11420 break;
11421 default:
11422 memprintf(err, "key size must be 128, 192 or 256 (bits).");
11423 return 0;
11424 }
11425 /* Try to decode a variable. */
11426 vars_check_arg(&args[1], NULL);
11427 vars_check_arg(&args[2], NULL);
11428 vars_check_arg(&args[3], NULL);
11429 return 1;
11430}
11431
11432/* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */
11433static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private)
11434{
11435 struct sample nonce, key, aead_tag;
11436 struct buffer *smp_trash, *smp_trash_alloc;
11437 EVP_CIPHER_CTX *ctx;
11438 int dec_size, ret;
11439
11440 smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt);
11441 if (!sample_conv_var2smp_str(&arg_p[1], &nonce))
11442 return 0;
11443
11444 smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt);
11445 if (!sample_conv_var2smp_str(&arg_p[2], &key))
11446 return 0;
11447
11448 smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt);
11449 if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag))
11450 return 0;
11451
11452 smp_trash = get_trash_chunk();
11453 smp_trash_alloc = alloc_trash_chunk();
11454 if (!smp_trash_alloc)
11455 return 0;
11456
11457 ctx = EVP_CIPHER_CTX_new();
11458
11459 if (!ctx)
11460 goto err;
11461
11462 dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size);
11463 if (dec_size < 0)
11464 goto err;
11465 smp_trash->data = dec_size;
11466
11467 /* Set cipher type and mode */
11468 switch(arg_p[0].data.sint) {
11469 case 128:
11470 EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL);
11471 break;
11472 case 192:
11473 EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL);
11474 break;
11475 case 256:
11476 EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
11477 break;
11478 }
11479
11480 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL);
11481
11482 /* Initialise IV */
11483 if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area))
11484 goto err;
11485
11486 dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size);
11487 if (dec_size < 0)
11488 goto err;
11489 smp_trash->data = dec_size;
11490
11491 /* Initialise key */
11492 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL))
11493 goto err;
11494
11495 if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data,
11496 (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data))
11497 goto err;
11498
11499 dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size);
11500 if (dec_size < 0)
11501 goto err;
11502 smp_trash_alloc->data = dec_size;
11503 dec_size = smp_trash->data;
11504
11505 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area);
11506 ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data);
11507
11508 if (ret <= 0)
11509 goto err;
11510
11511 smp->data.u.str.data = dec_size + smp_trash->data;
11512 smp->data.u.str.area = smp_trash->area;
11513 smp->data.type = SMP_T_BIN;
11514 smp->flags &= ~SMP_F_CONST;
11515 free_trash_chunk(smp_trash_alloc);
11516 return 1;
11517
11518err:
11519 free_trash_chunk(smp_trash_alloc);
11520 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020011521}
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011522# endif
William Lallemand32af2032016-10-29 18:09:35 +020011523
Elliot Otchet71f82972020-01-15 08:12:14 -050011524/* Argument validation functions */
11525
11526/* This function is used to validate the arguments passed to any "x_dn" ssl
11527 * keywords. These keywords support specifying a third parameter that must be
11528 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
11529 */
11530int val_dnfmt(struct arg *arg, char **err_msg)
11531{
11532 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
11533 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
11534 return 0;
11535 }
11536 return 1;
11537}
11538
William Lallemand32af2032016-10-29 18:09:35 +020011539/* register cli keywords */
11540static struct cli_kw_list cli_kws = {{ },{
11541#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11542 { { "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 +020011543 { { "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 +020011544#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010011545 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011546 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
11547 { { "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 +010011548 { { "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 +010011549 { { "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 +020011550 { { NULL }, NULL, NULL, NULL }
11551}};
11552
Willy Tarreau0108d902018-11-25 19:14:37 +010011553INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020011554
Willy Tarreau7875d092012-09-10 08:20:03 +020011555/* Note: must not be declared <const> as its list will be overwritten.
11556 * Please take care of keeping this list alphabetically sorted.
11557 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011558static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020011559 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011560 { "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 +010011561#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010011562 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011563#endif
Emeric Brun645ae792014-04-30 14:21:06 +020011564 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011565#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
11566 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
11567#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010011568 { "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 +020011569 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020011570 { "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 +020011571 { "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 +020011572#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020011573 { "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 -040011574#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011575#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011576 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11577 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040011578 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11579#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011580 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11581 { "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 +010011582 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011583 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011584 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020011585 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11586 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11587 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11588 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011589 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020011590 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11591 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011592 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011593 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11594 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010011595 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011596 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020011597 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11598 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11599 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11600 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011601 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020011602 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020011603 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011604 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011605 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011606 { "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 +010011607 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011608 { "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 +020011609 { "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 +010011610 { "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 +020011611 { "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 +010011612#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011613 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020011614#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010011615#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011616 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020011617#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011618 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011619#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020011620 { "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 -040011621#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011622 { "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 +020011623#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011624 { "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 -040011625#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011626#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011627 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11628 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040011629 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11630#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040011631#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011632 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040011633#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011634 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11635 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11636 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11637 { "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 +020011638 { NULL, NULL, 0, 0, 0 },
11639}};
11640
Willy Tarreau0108d902018-11-25 19:14:37 +010011641INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
11642
Willy Tarreau7875d092012-09-10 08:20:03 +020011643/* Note: must not be declared <const> as its list will be overwritten.
11644 * Please take care of keeping this list alphabetically sorted.
11645 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011646static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011647 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
11648 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011649 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020011650}};
11651
Willy Tarreau0108d902018-11-25 19:14:37 +010011652INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
11653
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011654/* Note: must not be declared <const> as its list will be overwritten.
11655 * Please take care of keeping this list alphabetically sorted, doing so helps
11656 * all code contributors.
11657 * Optional keywords are also declared with a NULL ->parse() function so that
11658 * the config parser can report an appropriate error when a known keyword was
11659 * not enabled.
11660 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011661static struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011662 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011663 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11664 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11665 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011666#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011667 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11668#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011669 { "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 +010011670 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011671 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011672 { "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 +010011673 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020011674 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
11675 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011676 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
11677 { NULL, NULL, 0 },
11678};
11679
Willy Tarreau0108d902018-11-25 19:14:37 +010011680/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
11681
Willy Tarreau51fb7652012-09-18 18:24:39 +020011682static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011683 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011684 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
11685 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
11686 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
11687 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
11688 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
11689 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011690#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011691 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11692#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011693 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
11694 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
11695 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
11696 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
11697 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
11698 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
11699 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
11700 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
11701 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
11702 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011703 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011704 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011705 { "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 +020011706 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
11707 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
11708 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
11709 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011710 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011711 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
11712 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011713 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
11714 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011715 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
11716 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
11717 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
11718 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
11719 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011720 { NULL, NULL, 0 },
11721}};
Emeric Brun46591952012-05-18 15:47:34 +020011722
Willy Tarreau0108d902018-11-25 19:14:37 +010011723INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
11724
Willy Tarreau92faadf2012-10-10 23:04:25 +020011725/* Note: must not be declared <const> as its list will be overwritten.
11726 * Please take care of keeping this list alphabetically sorted, doing so helps
11727 * all code contributors.
11728 * Optional keywords are also declared with a NULL ->parse() function so that
11729 * the config parser can report an appropriate error when a known keyword was
11730 * not enabled.
11731 */
11732static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010011733 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010011734 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011735 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010011736 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020011737 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011738 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
11739 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011740#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011741 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
11742#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011743 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
11744 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
11745 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
11746 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
11747 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
11748 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
11749 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
11750 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
11751 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
11752 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
11753 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
11754 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
11755 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
11756 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
11757 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
11758 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
11759 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
11760 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010011761 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011762 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
11763 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
11764 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
11765 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
11766 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
11767 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
11768 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
11769 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
11770 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
11771 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020011772 { NULL, NULL, 0, 0 },
11773}};
11774
Willy Tarreau0108d902018-11-25 19:14:37 +010011775INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
11776
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011777static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010011778 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
11779 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010011780 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010011781 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011782 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
11783 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010011784#ifndef OPENSSL_NO_DH
11785 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
11786#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011787 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011788#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011789 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011790#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010011791 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
11792#ifndef OPENSSL_NO_DH
11793 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
11794#endif
11795 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
11796 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
11797 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
11798 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011799 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010011800 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
11801 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011802#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011803 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
11804 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
11805#endif
William Lallemand3af48e72020-02-03 17:15:52 +010011806 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011807 { 0, NULL, NULL },
11808}};
11809
Willy Tarreau0108d902018-11-25 19:14:37 +010011810INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
11811
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011812/* Note: must not be declared <const> as its list will be overwritten */
11813static struct sample_conv_kw_list conv_kws = {ILH, {
Willy Tarreau86a394e2019-05-09 14:15:32 +020011814#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL)
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011815 { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN },
11816#endif
11817 { NULL, NULL, 0, 0, 0 },
11818}};
11819
11820INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws);
11821
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020011822/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010011823static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020011824 .snd_buf = ssl_sock_from_buf,
11825 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010011826 .subscribe = ssl_subscribe,
11827 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020011828 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020011829 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020011830 .rcv_pipe = NULL,
11831 .snd_pipe = NULL,
11832 .shutr = NULL,
11833 .shutw = ssl_sock_shutw,
11834 .close = ssl_sock_close,
11835 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010011836 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010011837 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010011838 .prepare_srv = ssl_sock_prepare_srv_ctx,
11839 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010011840 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010011841 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020011842};
11843
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011844enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
11845 struct session *sess, struct stream *s, int flags)
11846{
11847 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011848 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011849
11850 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011851 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011852
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011853 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011854 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011855 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011856 s->req.flags |= CF_READ_NULL;
11857 return ACT_RET_YIELD;
11858 }
11859 }
11860 return (ACT_RET_CONT);
11861}
11862
11863static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
11864{
11865 rule->action_ptr = ssl_action_wait_for_hs;
11866
11867 return ACT_RET_PRS_OK;
11868}
11869
11870static struct action_kw_list http_req_actions = {ILH, {
11871 { "wait-for-handshake", ssl_parse_wait_for_hs },
11872 { /* END */ }
11873}};
11874
Willy Tarreau0108d902018-11-25 19:14:37 +010011875INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
11876
Willy Tarreau5db847a2019-05-09 14:13:35 +020011877#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011878
11879static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11880{
11881 if (ptr) {
11882 chunk_destroy(ptr);
11883 free(ptr);
11884 }
11885}
11886
11887#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011888static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11889{
Willy Tarreaubafbe012017-11-24 17:34:44 +010011890 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011891}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011892
Emeric Brun46591952012-05-18 15:47:34 +020011893__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020011894static void __ssl_sock_init(void)
11895{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011896#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011897 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011898 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011899#endif
Emeric Brun46591952012-05-18 15:47:34 +020011900
Willy Tarreauef934602016-12-22 23:12:01 +010011901 if (global_ssl.listen_default_ciphers)
11902 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
11903 if (global_ssl.connect_default_ciphers)
11904 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011905#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011906 if (global_ssl.listen_default_ciphersuites)
11907 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
11908 if (global_ssl.connect_default_ciphersuites)
11909 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
11910#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010011911
Willy Tarreau13e14102016-12-22 20:25:26 +010011912 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011913#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020011914 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080011915#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011916#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011917 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011918 n = sk_SSL_COMP_num(cm);
11919 while (n--) {
11920 (void) sk_SSL_COMP_pop(cm);
11921 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011922#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011923
Willy Tarreau5db847a2019-05-09 14:13:35 +020011924#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011925 ssl_locking_init();
11926#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020011927#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011928 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
11929#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020011930 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020011931 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 +020011932#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011933 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011934 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011935#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010011936#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11937 hap_register_post_check(tlskeys_finalize_config);
11938#endif
Willy Tarreau80713382018-11-26 10:19:54 +010011939
11940 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
11941 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
11942
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010011943 hap_register_post_deinit(ssl_free_global_issuers);
11944
Willy Tarreau80713382018-11-26 10:19:54 +010011945#ifndef OPENSSL_NO_DH
11946 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
11947 hap_register_post_deinit(ssl_free_dh);
11948#endif
11949#ifndef OPENSSL_NO_ENGINE
11950 hap_register_post_deinit(ssl_free_engines);
11951#endif
11952 /* Load SSL string for the verbose & debug mode. */
11953 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020011954 ha_meth = BIO_meth_new(0x666, "ha methods");
11955 BIO_meth_set_write(ha_meth, ha_ssl_write);
11956 BIO_meth_set_read(ha_meth, ha_ssl_read);
11957 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
11958 BIO_meth_set_create(ha_meth, ha_ssl_new);
11959 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
11960 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
11961 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020011962
11963 HA_SPIN_INIT(&ckch_lock);
Willy Tarreau80713382018-11-26 10:19:54 +010011964}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010011965
Willy Tarreau80713382018-11-26 10:19:54 +010011966/* Compute and register the version string */
11967static void ssl_register_build_options()
11968{
11969 char *ptr = NULL;
11970 int i;
11971
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011972 memprintf(&ptr, "Built with OpenSSL version : "
11973#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011974 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011975#else /* OPENSSL_IS_BORINGSSL */
11976 OPENSSL_VERSION_TEXT
11977 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080011978 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020011979 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011980#endif
11981 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011982#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011983 "no (library version too old)"
11984#elif defined(OPENSSL_NO_TLSEXT)
11985 "no (disabled via OPENSSL_NO_TLSEXT)"
11986#else
11987 "yes"
11988#endif
11989 "", ptr);
11990
11991 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
11992#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11993 "yes"
11994#else
11995#ifdef OPENSSL_NO_TLSEXT
11996 "no (because of OPENSSL_NO_TLSEXT)"
11997#else
11998 "no (version might be too old, 0.9.8f min needed)"
11999#endif
12000#endif
12001 "", ptr);
12002
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020012003 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
12004 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
12005 if (methodVersions[i].option)
12006 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010012007
Willy Tarreauc2c0b612016-12-21 19:23:20 +010012008 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010012009}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010012010
Willy Tarreau80713382018-11-26 10:19:54 +010012011INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020012012
Emeric Brun46591952012-05-18 15:47:34 +020012013
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012014#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000012015void ssl_free_engines(void) {
12016 struct ssl_engine_list *wl, *wlb;
12017 /* free up engine list */
12018 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
12019 ENGINE_finish(wl->e);
12020 ENGINE_free(wl->e);
12021 LIST_DEL(&wl->list);
12022 free(wl);
12023 }
12024}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012025#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020012026
Remi Gacogned3a23c32015-05-28 16:39:47 +020012027#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000012028void ssl_free_dh(void) {
12029 if (local_dh_1024) {
12030 DH_free(local_dh_1024);
12031 local_dh_1024 = NULL;
12032 }
12033 if (local_dh_2048) {
12034 DH_free(local_dh_2048);
12035 local_dh_2048 = NULL;
12036 }
12037 if (local_dh_4096) {
12038 DH_free(local_dh_4096);
12039 local_dh_4096 = NULL;
12040 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020012041 if (global_dh) {
12042 DH_free(global_dh);
12043 global_dh = NULL;
12044 }
Grant Zhang872f9c22017-01-21 01:10:18 +000012045}
12046#endif
12047
12048__attribute__((destructor))
12049static void __ssl_sock_deinit(void)
12050{
12051#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020012052 if (ssl_ctx_lru_tree) {
12053 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010012054 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020012055 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020012056#endif
12057
Willy Tarreau5db847a2019-05-09 14:13:35 +020012058#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020012059 ERR_remove_state(0);
12060 ERR_free_strings();
12061
12062 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080012063#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020012064
Willy Tarreau5db847a2019-05-09 14:13:35 +020012065#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020012066 CRYPTO_cleanup_all_ex_data();
12067#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020012068 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020012069}
12070
12071
Emeric Brun46591952012-05-18 15:47:34 +020012072/*
12073 * Local variables:
12074 * c-indent-level: 8
12075 * c-basic-offset: 8
12076 * End:
12077 */