blob: fa815715541783348d8bccf0afc41e62ffa8414d [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
26#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020027#include <ctype.h>
28#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020029#include <errno.h>
30#include <fcntl.h>
31#include <stdio.h>
32#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020033#include <string.h>
34#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020035
36#include <sys/socket.h>
37#include <sys/stat.h>
38#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020039#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020040#include <netinet/tcp.h>
41
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020042#include <openssl/crypto.h>
Emeric Brun46591952012-05-18 15:47:34 +020043#include <openssl/ssl.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020044#include <openssl/x509.h>
45#include <openssl/x509v3.h>
46#include <openssl/x509.h>
47#include <openssl/err.h>
Thierry Fournier383085f2013-01-24 14:15:43 +010048#include <openssl/rand.h>
Lukas Tribuse4e30f72014-12-09 16:32:51 +010049#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +020050#include <openssl/ocsp.h>
51#endif
Remi Gacogne4f902b82015-05-28 16:23:00 +020052#ifndef OPENSSL_NO_DH
53#include <openssl/dh.h>
54#endif
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020055#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000056#include <openssl/engine.h>
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020057#endif
Emeric Brun46591952012-05-18 15:47:34 +020058
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000059#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
60#include <openssl/async.h>
61#endif
62
Christopher Faulet31af49d2015-06-09 17:29:50 +020063#include <import/lru.h>
64#include <import/xxhash.h>
65
Emeric Brun46591952012-05-18 15:47:34 +020066#include <common/buffer.h>
67#include <common/compat.h>
68#include <common/config.h>
69#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020070#include <common/errors.h>
Emeric Brun46591952012-05-18 15:47:34 +020071#include <common/standard.h>
72#include <common/ticks.h>
73#include <common/time.h>
Emeric Brun2c86cbf2014-10-30 15:56:50 +010074#include <common/cfgparse.h>
Nenad Merdanovic05552d42015-02-27 19:56:49 +010075#include <common/base64.h>
Emeric Brun46591952012-05-18 15:47:34 +020076
Emeric Brunfc0421f2012-09-07 17:30:07 +020077#include <ebsttree.h>
78
William Lallemand32af2032016-10-29 18:09:35 +020079#include <types/applet.h>
80#include <types/cli.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020081#include <types/global.h>
82#include <types/ssl_sock.h>
William Lallemand32af2032016-10-29 18:09:35 +020083#include <types/stats.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020084
Willy Tarreau7875d092012-09-10 08:20:03 +020085#include <proto/acl.h>
86#include <proto/arg.h>
William Lallemand32af2032016-10-29 18:09:35 +020087#include <proto/channel.h>
Emeric Brun46591952012-05-18 15:47:34 +020088#include <proto/connection.h>
William Lallemand32af2032016-10-29 18:09:35 +020089#include <proto/cli.h>
Emeric Brun46591952012-05-18 15:47:34 +020090#include <proto/fd.h>
91#include <proto/freq_ctr.h>
92#include <proto/frontend.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020093#include <proto/listener.h>
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +020094#include <proto/openssl-compat.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010095#include <proto/pattern.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020096#include <proto/proto_tcp.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020097#include <proto/server.h>
William Lallemand32af2032016-10-29 18:09:35 +020098#include <proto/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020099#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +0200100#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +0200101#include <proto/shctx.h>
Emeric Brun46591952012-05-18 15:47:34 +0200102#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200103#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +0200104#include <proto/task.h>
105
Willy Tarreau518cedd2014-02-17 15:43:01 +0100106/* Warning, these are bits, not integers! */
Emeric Brune64aef12012-09-21 13:15:06 +0200107#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
Emeric Brund8b2bb52014-01-28 15:43:53 +0100108#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
Willy Tarreau518cedd2014-02-17 15:43:01 +0100109#define SSL_SOCK_SEND_UNLIMITED 0x00000004
Emeric Brun29f037d2014-04-25 19:05:36 +0200110#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
111
Emeric Brunf282a812012-09-21 15:27:54 +0200112/* bits 0xFFFF0000 are reserved to store verify errors */
113
114/* Verify errors macros */
115#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
116#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
117#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
118
119#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
120#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
121#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
Emeric Brune64aef12012-09-21 13:15:06 +0200122
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100123/* Supported hash function for TLS tickets */
124#ifdef OPENSSL_NO_SHA256
125#define HASH_FUNCT EVP_sha1
126#else
127#define HASH_FUNCT EVP_sha256
128#endif /* OPENSSL_NO_SHA256 */
129
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200130/* ssl_methods flags for ssl options */
131#define MC_SSL_O_ALL 0x0000
132#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
133#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
134#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
135#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200136#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200137
138/* ssl_methods versions */
139enum {
140 CONF_TLSV_NONE = 0,
141 CONF_TLSV_MIN = 1,
142 CONF_SSLV3 = 1,
143 CONF_TLSV10 = 2,
144 CONF_TLSV11 = 3,
145 CONF_TLSV12 = 4,
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +0200146 CONF_TLSV13 = 5,
147 CONF_TLSV_MAX = 5,
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200148};
149
Emeric Brun850efd52014-01-29 12:24:34 +0100150/* server and bind verify method, it uses a global value as default */
151enum {
152 SSL_SOCK_VERIFY_DEFAULT = 0,
153 SSL_SOCK_VERIFY_REQUIRED = 1,
154 SSL_SOCK_VERIFY_OPTIONAL = 2,
155 SSL_SOCK_VERIFY_NONE = 3,
156};
157
Willy Tarreau71b734c2014-01-28 15:19:44 +0100158int sslconns = 0;
159int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100160static struct xprt_ops ssl_sock;
Emeric Brune1f38db2012-09-03 20:36:47 +0200161
Willy Tarreauef934602016-12-22 23:12:01 +0100162static struct {
163 char *crt_base; /* base directory path for certificates */
164 char *ca_base; /* base directory path for CAs and CRLs */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000165 int async; /* whether we use ssl async mode */
Willy Tarreauef934602016-12-22 23:12:01 +0100166
167 char *listen_default_ciphers;
168 char *connect_default_ciphers;
169 int listen_default_ssloptions;
170 int connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200171 struct tls_version_filter listen_default_sslmethods;
172 struct tls_version_filter connect_default_sslmethods;
Willy Tarreauef934602016-12-22 23:12:01 +0100173
174 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
175 unsigned int life_time; /* SSL session lifetime in seconds */
176 unsigned int max_record; /* SSL max record size */
177 unsigned int default_dh_param; /* SSL maximum DH parameter size */
178 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100179 int capture_cipherlist; /* Size of the cipherlist buffer. */
Willy Tarreauef934602016-12-22 23:12:01 +0100180} global_ssl = {
181#ifdef LISTEN_DEFAULT_CIPHERS
182 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
183#endif
184#ifdef CONNECT_DEFAULT_CIPHERS
185 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
186#endif
187 .listen_default_ssloptions = BC_SSL_O_NONE,
188 .connect_default_ssloptions = SRV_SSL_O_NONE,
189
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200190 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
191 .listen_default_sslmethods.min = CONF_TLSV_NONE,
192 .listen_default_sslmethods.max = CONF_TLSV_NONE,
193 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
194 .connect_default_sslmethods.min = CONF_TLSV_NONE,
195 .connect_default_sslmethods.max = CONF_TLSV_NONE,
196
Willy Tarreauef934602016-12-22 23:12:01 +0100197#ifdef DEFAULT_SSL_MAX_RECORD
198 .max_record = DEFAULT_SSL_MAX_RECORD,
199#endif
200 .default_dh_param = SSL_DEFAULT_DH_PARAM,
201 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100202 .capture_cipherlist = 0,
Willy Tarreauef934602016-12-22 23:12:01 +0100203};
204
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100205/* This memory pool is used for capturing clienthello parameters. */
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100206struct ssl_capture {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100207 unsigned long long int xxh64;
208 unsigned char ciphersuite_len;
209 char ciphersuite[0];
210};
211struct pool_head *pool2_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100212static int ssl_capture_ptr_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100213
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200214#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
215struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
216#endif
217
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200218#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000219static unsigned int openssl_engines_initialized;
220struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
221struct ssl_engine_list {
222 struct list list;
223 ENGINE *e;
224};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200225#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000226
Remi Gacogne8de54152014-07-15 11:36:40 +0200227#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200228static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200229static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200230static DH *local_dh_1024 = NULL;
231static DH *local_dh_2048 = NULL;
232static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100233static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200234#endif /* OPENSSL_NO_DH */
235
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100236#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200237/* X509V3 Extensions that will be added on generated certificates */
238#define X509V3_EXT_SIZE 5
239static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
240 "basicConstraints",
241 "nsComment",
242 "subjectKeyIdentifier",
243 "authorityKeyIdentifier",
244 "keyUsage",
245};
246static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
247 "CA:FALSE",
248 "\"OpenSSL Generated Certificate\"",
249 "hash",
250 "keyid,issuer:always",
251 "nonRepudiation,digitalSignature,keyEncipherment"
252};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200253/* LRU cache to store generated certificate */
254static struct lru64_head *ssl_ctx_lru_tree = NULL;
255static unsigned int ssl_ctx_lru_seed = 0;
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200256#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
257
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100258static struct ssl_bind_kw ssl_bind_kws[];
259
yanbzhube2774d2015-12-10 15:07:30 -0500260#if OPENSSL_VERSION_NUMBER >= 0x1000200fL
261/* The order here matters for picking a default context,
262 * keep the most common keytype at the bottom of the list
263 */
264const char *SSL_SOCK_KEYTYPE_NAMES[] = {
265 "dsa",
266 "ecdsa",
267 "rsa"
268};
269#define SSL_SOCK_NUM_KEYTYPES 3
Willy Tarreau30da7ad2015-12-14 11:28:33 +0100270#else
271#define SSL_SOCK_NUM_KEYTYPES 1
yanbzhube2774d2015-12-10 15:07:30 -0500272#endif
273
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100274/*
275 * This function gives the detail of the SSL error. It is used only
276 * if the debug mode and the verbose mode are activated. It dump all
277 * the SSL error until the stack was empty.
278 */
279static forceinline void ssl_sock_dump_errors(struct connection *conn)
280{
281 unsigned long ret;
282
283 if (unlikely(global.mode & MODE_DEBUG)) {
284 while(1) {
285 ret = ERR_get_error();
286 if (ret == 0)
287 return;
288 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
289 (unsigned short)conn->t.sock.fd, ret,
290 ERR_func_error_string(ret), ERR_reason_error_string(ret));
291 }
292 }
293}
294
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200295#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
yanbzhube2774d2015-12-10 15:07:30 -0500296/*
297 * struct alignment works here such that the key.key is the same as key_data
298 * Do not change the placement of key_data
299 */
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200300struct certificate_ocsp {
301 struct ebmb_node key;
302 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
303 struct chunk response;
304 long expire;
305};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200306
yanbzhube2774d2015-12-10 15:07:30 -0500307struct ocsp_cbk_arg {
308 int is_single;
309 int single_kt;
310 union {
311 struct certificate_ocsp *s_ocsp;
312 /*
313 * m_ocsp will have multiple entries dependent on key type
314 * Entry 0 - DSA
315 * Entry 1 - ECDSA
316 * Entry 2 - RSA
317 */
318 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
319 };
320};
321
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200322#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000323static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
324{
325 int err_code = ERR_ABORT;
326 ENGINE *engine;
327 struct ssl_engine_list *el;
328
329 /* grab the structural reference to the engine */
330 engine = ENGINE_by_id(engine_id);
331 if (engine == NULL) {
332 Alert("ssl-engine %s: failed to get structural reference\n", engine_id);
333 goto fail_get;
334 }
335
336 if (!ENGINE_init(engine)) {
337 /* the engine couldn't initialise, release it */
338 Alert("ssl-engine %s: failed to initialize\n", engine_id);
339 goto fail_init;
340 }
341
342 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
343 Alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
344 goto fail_set_method;
345 }
346
347 el = calloc(1, sizeof(*el));
348 el->e = engine;
349 LIST_ADD(&openssl_engines, &el->list);
350 return 0;
351
352fail_set_method:
353 /* release the functional reference from ENGINE_init() */
354 ENGINE_finish(engine);
355
356fail_init:
357 /* release the structural reference from ENGINE_by_id() */
358 ENGINE_free(engine);
359
360fail_get:
361 return err_code;
362}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200363#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000364
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000365#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
Emeric Brun3854e012017-05-17 20:42:48 +0200366/*
367 * openssl async fd handler
368 */
369static void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000370{
371 struct connection *conn = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000372
Emeric Brun3854e012017-05-17 20:42:48 +0200373 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000374 * to poll this fd until it is requested
375 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000376 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000377 fd_cant_recv(fd);
378
379 /* crypto engine is available, let's notify the associated
380 * connection that it can pursue its processing.
381 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000382 __conn_sock_want_recv(conn);
383 __conn_sock_want_send(conn);
384 conn_update_sock_polling(conn);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000385}
386
Emeric Brun3854e012017-05-17 20:42:48 +0200387/*
388 * openssl async delayed SSL_free handler
389 */
390static void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000391{
392 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200393 OSSL_ASYNC_FD all_fd[32];
394 size_t num_all_fds = 0;
395 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000396
Emeric Brun3854e012017-05-17 20:42:48 +0200397 /* We suppose that the async job for a same SSL *
398 * are serialized. So if we are awake it is
399 * because the running job has just finished
400 * and we can remove all async fds safely
401 */
402 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
403 if (num_all_fds > 32) {
404 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
405 return;
406 }
407
408 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
409 for (i=0 ; i < num_all_fds ; i++)
410 fd_remove(all_fd[i]);
411
412 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000413 SSL_free(ssl);
414 sslconns--;
415 jobs--;
416}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000417/*
Emeric Brun3854e012017-05-17 20:42:48 +0200418 * function used to manage a returned SSL_ERROR_WANT_ASYNC
419 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000420 */
Emeric Brun3854e012017-05-17 20:42:48 +0200421static void inline ssl_async_process_fds(struct connection *conn, SSL *ssl)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000422{
Emeric Brun3854e012017-05-17 20:42:48 +0200423 OSSL_ASYNC_FD add_fd[32], afd;
424 OSSL_ASYNC_FD del_fd[32];
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000425 size_t num_add_fds = 0;
426 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200427 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000428
429 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
430 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200431 if (num_add_fds > 32 || num_del_fds > 32) {
432 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 +0000433 return;
434 }
435
Emeric Brun3854e012017-05-17 20:42:48 +0200436 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000437
Emeric Brun3854e012017-05-17 20:42:48 +0200438 /* We remove unused fds from the fdtab */
439 for (i=0 ; i < num_del_fds ; i++)
440 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000441
Emeric Brun3854e012017-05-17 20:42:48 +0200442 /* We add new fds to the fdtab */
443 for (i=0 ; i < num_add_fds ; i++) {
444 afd = add_fd[i];
445 fdtab[afd].owner = conn;
446 fdtab[afd].iocb = ssl_async_fd_handler;
447 fd_insert(afd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000448 }
449
Emeric Brun3854e012017-05-17 20:42:48 +0200450 num_add_fds = 0;
451 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
452 if (num_add_fds > 32) {
453 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
454 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000455 }
Emeric Brun3854e012017-05-17 20:42:48 +0200456
457 /* We activate the polling for all known async fds */
458 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000459 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200460 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000461 /* To ensure that the fd cache won't be used
462 * We'll prefer to catch a real RD event
463 * because handling an EAGAIN on this fd will
464 * result in a context switch and also
465 * some engines uses a fd in blocking mode.
466 */
467 fd_cant_recv(add_fd[i]);
468 }
Emeric Brun3854e012017-05-17 20:42:48 +0200469
470 /* We must also prevent the conn_handler
471 * to be called until a read event was
472 * polled on an async fd
473 */
474 __conn_sock_stop_both(conn);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000475}
476#endif
477
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200478/*
479 * This function returns the number of seconds elapsed
480 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
481 * date presented un ASN1_GENERALIZEDTIME.
482 *
483 * In parsing error case, it returns -1.
484 */
485static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
486{
487 long epoch;
488 char *p, *end;
489 const unsigned short month_offset[12] = {
490 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
491 };
492 int year, month;
493
494 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
495
496 p = (char *)d->data;
497 end = p + d->length;
498
499 if (end - p < 4) return -1;
500 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
501 p += 4;
502 if (end - p < 2) return -1;
503 month = 10 * (p[0] - '0') + p[1] - '0';
504 if (month < 1 || month > 12) return -1;
505 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
506 We consider leap years and the current month (<marsh or not) */
507 epoch = ( ((year - 1970) * 365)
508 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
509 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
510 + month_offset[month-1]
511 ) * 24 * 60 * 60;
512 p += 2;
513 if (end - p < 2) return -1;
514 /* Add the number of seconds of completed days of current month */
515 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
516 p += 2;
517 if (end - p < 2) return -1;
518 /* Add the completed hours of the current day */
519 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
520 p += 2;
521 if (end - p < 2) return -1;
522 /* Add the completed minutes of the current hour */
523 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
524 p += 2;
525 if (p == end) return -1;
526 /* Test if there is available seconds */
527 if (p[0] < '0' || p[0] > '9')
528 goto nosec;
529 if (end - p < 2) return -1;
530 /* Add the seconds of the current minute */
531 epoch += 10 * (p[0] - '0') + p[1] - '0';
532 p += 2;
533 if (p == end) return -1;
534 /* Ignore seconds float part if present */
535 if (p[0] == '.') {
536 do {
537 if (++p == end) return -1;
538 } while (p[0] >= '0' && p[0] <= '9');
539 }
540
541nosec:
542 if (p[0] == 'Z') {
543 if (end - p != 1) return -1;
544 return epoch;
545 }
546 else if (p[0] == '+') {
547 if (end - p != 5) return -1;
548 /* Apply timezone offset */
549 return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
550 }
551 else if (p[0] == '-') {
552 if (end - p != 5) return -1;
553 /* Apply timezone offset */
554 return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
555 }
556
557 return -1;
558}
559
Emeric Brun1d3865b2014-06-20 15:37:32 +0200560static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200561
562/* This function starts to check if the OCSP response (in DER format) contained
563 * in chunk 'ocsp_response' is valid (else exits on error).
564 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
565 * contained in the OCSP Response and exits on error if no match.
566 * If it's a valid OCSP Response:
567 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
568 * pointed by 'ocsp'.
569 * If 'ocsp' is NULL, the function looks up into the OCSP response's
570 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
571 * from the response) and exits on error if not found. Finally, If an OCSP response is
572 * already present in the container, it will be overwritten.
573 *
574 * Note: OCSP response containing more than one OCSP Single response is not
575 * considered valid.
576 *
577 * Returns 0 on success, 1 in error case.
578 */
579static int ssl_sock_load_ocsp_response(struct chunk *ocsp_response, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err)
580{
581 OCSP_RESPONSE *resp;
582 OCSP_BASICRESP *bs = NULL;
583 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200584 OCSP_CERTID *id;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200585 unsigned char *p = (unsigned char *)ocsp_response->str;
586 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200587 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200588 int reason;
589 int ret = 1;
590
591 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, ocsp_response->len);
592 if (!resp) {
593 memprintf(err, "Unable to parse OCSP response");
594 goto out;
595 }
596
597 rc = OCSP_response_status(resp);
598 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
599 memprintf(err, "OCSP response status not successful");
600 goto out;
601 }
602
603 bs = OCSP_response_get1_basic(resp);
604 if (!bs) {
605 memprintf(err, "Failed to get basic response from OCSP Response");
606 goto out;
607 }
608
609 count_sr = OCSP_resp_count(bs);
610 if (count_sr > 1) {
611 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
612 goto out;
613 }
614
615 sr = OCSP_resp_get0(bs, 0);
616 if (!sr) {
617 memprintf(err, "Failed to get OCSP single response");
618 goto out;
619 }
620
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200621 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
622
Emeric Brun4147b2e2014-06-16 18:36:30 +0200623 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
624 if (rc != V_OCSP_CERTSTATUS_GOOD) {
625 memprintf(err, "OCSP single response: certificate status not good");
626 goto out;
627 }
628
Emeric Brun13a6b482014-06-20 15:44:34 +0200629 if (!nextupd) {
630 memprintf(err, "OCSP single response: missing nextupdate");
631 goto out;
632 }
633
Emeric Brunc8b27b62014-06-19 14:16:17 +0200634 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200635 if (!rc) {
636 memprintf(err, "OCSP single response: no longer valid.");
637 goto out;
638 }
639
640 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200641 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200642 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
643 goto out;
644 }
645 }
646
647 if (!ocsp) {
648 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
649 unsigned char *p;
650
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200651 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200652 if (!rc) {
653 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
654 goto out;
655 }
656
657 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
658 memprintf(err, "OCSP single response: Certificate ID too long");
659 goto out;
660 }
661
662 p = key;
663 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200664 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200665 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
666 if (!ocsp) {
667 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
668 goto out;
669 }
670 }
671
672 /* According to comments on "chunk_dup", the
673 previous chunk buffer will be freed */
674 if (!chunk_dup(&ocsp->response, ocsp_response)) {
675 memprintf(err, "OCSP response: Memory allocation error");
676 goto out;
677 }
678
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200679 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
680
Emeric Brun4147b2e2014-06-16 18:36:30 +0200681 ret = 0;
682out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +0100683 ERR_clear_error();
684
Emeric Brun4147b2e2014-06-16 18:36:30 +0200685 if (bs)
686 OCSP_BASICRESP_free(bs);
687
688 if (resp)
689 OCSP_RESPONSE_free(resp);
690
691 return ret;
692}
693/*
694 * External function use to update the OCSP response in the OCSP response's
695 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
696 * to update in DER format.
697 *
698 * Returns 0 on success, 1 in error case.
699 */
700int ssl_sock_update_ocsp_response(struct chunk *ocsp_response, char **err)
701{
702 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
703}
704
705/*
706 * This function load the OCSP Resonse in DER format contained in file at
707 * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response'
708 *
709 * Returns 0 on success, 1 in error case.
710 */
711static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err)
712{
713 int fd = -1;
714 int r = 0;
715 int ret = 1;
716
717 fd = open(ocsp_path, O_RDONLY);
718 if (fd == -1) {
719 memprintf(err, "Error opening OCSP response file");
720 goto end;
721 }
722
723 trash.len = 0;
724 while (trash.len < trash.size) {
725 r = read(fd, trash.str + trash.len, trash.size - trash.len);
726 if (r < 0) {
727 if (errno == EINTR)
728 continue;
729
730 memprintf(err, "Error reading OCSP response from file");
731 goto end;
732 }
733 else if (r == 0) {
734 break;
735 }
736 trash.len += r;
737 }
738
739 close(fd);
740 fd = -1;
741
742 ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err);
743end:
744 if (fd != -1)
745 close(fd);
746
747 return ret;
748}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100749#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +0200750
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100751#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
752static 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)
753{
754 struct tls_sess_key *keys;
755 struct connection *conn;
756 int head;
757 int i;
758
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200759 conn = SSL_get_app_data(s);
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200760 keys = objt_listener(conn->target)->bind_conf->keys_ref->tlskeys;
761 head = objt_listener(conn->target)->bind_conf->keys_ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100762
763 if (enc) {
764 memcpy(key_name, keys[head].name, 16);
765
766 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
767 return -1;
768
769 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].aes_key, iv))
770 return -1;
771
772 HMAC_Init_ex(hctx, keys[head].hmac_key, 16, HASH_FUNCT(), NULL);
773
774 return 1;
775 } else {
776 for (i = 0; i < TLS_TICKETS_NO; i++) {
777 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
778 goto found;
779 }
780 return 0;
781
782 found:
783 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].hmac_key, 16, HASH_FUNCT(), NULL);
784 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].aes_key, iv))
785 return -1;
786 /* 2 for key renewal, 1 if current key is still valid */
787 return i ? 2 : 1;
788 }
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200789}
790
791struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
792{
793 struct tls_keys_ref *ref;
794
795 list_for_each_entry(ref, &tlskeys_reference, list)
796 if (ref->filename && strcmp(filename, ref->filename) == 0)
797 return ref;
798 return NULL;
799}
800
801struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
802{
803 struct tls_keys_ref *ref;
804
805 list_for_each_entry(ref, &tlskeys_reference, list)
806 if (ref->unique_id == unique_id)
807 return ref;
808 return NULL;
809}
810
811int ssl_sock_update_tlskey(char *filename, struct chunk *tlskey, char **err) {
812 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
813
814 if(!ref) {
815 memprintf(err, "Unable to locate the referenced filename: %s", filename);
816 return 1;
817 }
818
Pradeep Jindalcc79b002015-08-20 18:25:17 +0530819 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), tlskey->str, tlskey->len);
820 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200821
822 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100823}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200824
825/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +0100826 * automatic ids. It's called just after the basic checks. It returns
827 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200828 */
Willy Tarreaud1c57502016-12-22 22:46:15 +0100829static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200830{
831 int i = 0;
832 struct tls_keys_ref *ref, *ref2, *ref3;
833 struct list tkr = LIST_HEAD_INIT(tkr);
834
835 list_for_each_entry(ref, &tlskeys_reference, list) {
836 if (ref->unique_id == -1) {
837 /* Look for the first free id. */
838 while (1) {
839 list_for_each_entry(ref2, &tlskeys_reference, list) {
840 if (ref2->unique_id == i) {
841 i++;
842 break;
843 }
844 }
845 if (&ref2->list == &tlskeys_reference)
846 break;
847 }
848
849 /* Uses the unique id and increment it for the next entry. */
850 ref->unique_id = i;
851 i++;
852 }
853 }
854
855 /* This sort the reference list by id. */
856 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
857 LIST_DEL(&ref->list);
858 list_for_each_entry(ref3, &tkr, list) {
859 if (ref->unique_id < ref3->unique_id) {
860 LIST_ADDQ(&ref3->list, &ref->list);
861 break;
862 }
863 }
864 if (&ref3->list == &tkr)
865 LIST_ADDQ(&tkr, &ref->list);
866 }
867
868 /* swap root */
869 LIST_ADD(&tkr, &tlskeys_reference);
870 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +0100871 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200872}
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100873#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
874
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100875#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -0500876int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
877{
878 switch (evp_keytype) {
879 case EVP_PKEY_RSA:
880 return 2;
881 case EVP_PKEY_DSA:
882 return 0;
883 case EVP_PKEY_EC:
884 return 1;
885 }
886
887 return -1;
888}
889
Emeric Brun4147b2e2014-06-16 18:36:30 +0200890/*
891 * Callback used to set OCSP status extension content in server hello.
892 */
893int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
894{
yanbzhube2774d2015-12-10 15:07:30 -0500895 struct certificate_ocsp *ocsp;
896 struct ocsp_cbk_arg *ocsp_arg;
897 char *ssl_buf;
898 EVP_PKEY *ssl_pkey;
899 int key_type;
900 int index;
901
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200902 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -0500903
904 ssl_pkey = SSL_get_privatekey(ssl);
905 if (!ssl_pkey)
906 return SSL_TLSEXT_ERR_NOACK;
907
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200908 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -0500909
910 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
911 ocsp = ocsp_arg->s_ocsp;
912 else {
913 /* For multiple certs per context, we have to find the correct OCSP response based on
914 * the certificate type
915 */
916 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
917
918 if (index < 0)
919 return SSL_TLSEXT_ERR_NOACK;
920
921 ocsp = ocsp_arg->m_ocsp[index];
922
923 }
Emeric Brun4147b2e2014-06-16 18:36:30 +0200924
925 if (!ocsp ||
926 !ocsp->response.str ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200927 !ocsp->response.len ||
928 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +0200929 return SSL_TLSEXT_ERR_NOACK;
930
931 ssl_buf = OPENSSL_malloc(ocsp->response.len);
932 if (!ssl_buf)
933 return SSL_TLSEXT_ERR_NOACK;
934
935 memcpy(ssl_buf, ocsp->response.str, ocsp->response.len);
936 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.len);
937
938 return SSL_TLSEXT_ERR_OK;
939}
940
941/*
942 * This function enables the handling of OCSP status extension on 'ctx' if a
943 * file name 'cert_path' suffixed using ".ocsp" is present.
944 * To enable OCSP status extension, the issuer's certificate is mandatory.
945 * It should be present in the certificate's extra chain builded from file
946 * 'cert_path'. If not found, the issuer certificate is loaded from a file
947 * named 'cert_path' suffixed using '.issuer'.
948 *
949 * In addition, ".ocsp" file content is loaded as a DER format of an OCSP
950 * response. If file is empty or content is not a valid OCSP response,
951 * OCSP status extension is enabled but OCSP response is ignored (a warning
952 * is displayed).
953 *
954 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
955 * succesfully enabled, or -1 in other error case.
956 */
957static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path)
958{
959
960 BIO *in = NULL;
961 X509 *x, *xi = NULL, *issuer = NULL;
962 STACK_OF(X509) *chain = NULL;
963 OCSP_CERTID *cid = NULL;
964 SSL *ssl;
965 char ocsp_path[MAXPATHLEN+1];
966 int i, ret = -1;
967 struct stat st;
968 struct certificate_ocsp *ocsp = NULL, *iocsp;
969 char *warn = NULL;
970 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200971 pem_password_cb *passwd_cb;
972 void *passwd_cb_userdata;
973 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200974
975 snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path);
976
977 if (stat(ocsp_path, &st))
978 return 1;
979
980 ssl = SSL_new(ctx);
981 if (!ssl)
982 goto out;
983
984 x = SSL_get_certificate(ssl);
985 if (!x)
986 goto out;
987
988 /* Try to lookup for issuer in certificate extra chain */
989#ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS
990 SSL_CTX_get_extra_chain_certs(ctx, &chain);
991#else
992 chain = ctx->extra_certs;
993#endif
994 for (i = 0; i < sk_X509_num(chain); i++) {
995 issuer = sk_X509_value(chain, i);
996 if (X509_check_issued(issuer, x) == X509_V_OK)
997 break;
998 else
999 issuer = NULL;
1000 }
1001
1002 /* If not found try to load issuer from a suffixed file */
1003 if (!issuer) {
1004 char issuer_path[MAXPATHLEN+1];
1005
1006 in = BIO_new(BIO_s_file());
1007 if (!in)
1008 goto out;
1009
1010 snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path);
1011 if (BIO_read_filename(in, issuer_path) <= 0)
1012 goto out;
1013
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001014 passwd_cb = SSL_CTX_get_default_passwd_cb(ctx);
1015 passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx);
1016
1017 xi = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001018 if (!xi)
1019 goto out;
1020
1021 if (X509_check_issued(xi, x) != X509_V_OK)
1022 goto out;
1023
1024 issuer = xi;
1025 }
1026
1027 cid = OCSP_cert_to_id(0, x, issuer);
1028 if (!cid)
1029 goto out;
1030
1031 i = i2d_OCSP_CERTID(cid, NULL);
1032 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1033 goto out;
1034
Vincent Bernat02779b62016-04-03 13:48:43 +02001035 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001036 if (!ocsp)
1037 goto out;
1038
1039 p = ocsp->key_data;
1040 i2d_OCSP_CERTID(cid, &p);
1041
1042 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1043 if (iocsp == ocsp)
1044 ocsp = NULL;
1045
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001046#ifndef SSL_CTX_get_tlsext_status_cb
1047# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1048 *cb = (void (*) (void))ctx->tlsext_status_cb;
1049#endif
1050 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1051
1052 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001053 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001054 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001055
1056 cb_arg->is_single = 1;
1057 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001058
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001059 pkey = X509_get_pubkey(x);
1060 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1061 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001062
1063 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1064 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1065 } else {
1066 /*
1067 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1068 * Update that cb_arg with the new cert's staple
1069 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001070 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001071 struct certificate_ocsp *tmp_ocsp;
1072 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001073 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001074 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001075
1076#ifdef SSL_CTX_get_tlsext_status_arg
1077 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1078#else
1079 cb_arg = ctx->tlsext_status_arg;
1080#endif
yanbzhube2774d2015-12-10 15:07:30 -05001081
1082 /*
1083 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1084 * the order of operations below matter, take care when changing it
1085 */
1086 tmp_ocsp = cb_arg->s_ocsp;
1087 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1088 cb_arg->s_ocsp = NULL;
1089 cb_arg->m_ocsp[index] = tmp_ocsp;
1090 cb_arg->is_single = 0;
1091 cb_arg->single_kt = 0;
1092
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001093 pkey = X509_get_pubkey(x);
1094 key_type = EVP_PKEY_base_id(pkey);
1095 EVP_PKEY_free(pkey);
1096
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001097 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001098 if (index >= 0 && !cb_arg->m_ocsp[index])
1099 cb_arg->m_ocsp[index] = iocsp;
1100
1101 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001102
1103 ret = 0;
1104
1105 warn = NULL;
1106 if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) {
1107 memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure");
1108 Warning("%s.\n", warn);
1109 }
1110
1111out:
1112 if (ssl)
1113 SSL_free(ssl);
1114
1115 if (in)
1116 BIO_free(in);
1117
1118 if (xi)
1119 X509_free(xi);
1120
1121 if (cid)
1122 OCSP_CERTID_free(cid);
1123
1124 if (ocsp)
1125 free(ocsp);
1126
1127 if (warn)
1128 free(warn);
1129
1130
1131 return ret;
1132}
1133
1134#endif
1135
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001136#ifdef OPENSSL_IS_BORINGSSL
1137static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path)
1138{
1139 char ocsp_path[MAXPATHLEN+1];
1140 struct stat st;
1141 int fd = -1, r = 0;
1142
1143 snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path);
1144 if (stat(ocsp_path, &st))
1145 return 0;
1146
1147 fd = open(ocsp_path, O_RDONLY);
1148 if (fd == -1) {
1149 Warning("Error opening OCSP response file %s.\n", ocsp_path);
1150 return -1;
1151 }
1152
1153 trash.len = 0;
1154 while (trash.len < trash.size) {
1155 r = read(fd, trash.str + trash.len, trash.size - trash.len);
1156 if (r < 0) {
1157 if (errno == EINTR)
1158 continue;
1159 Warning("Error reading OCSP response from file %s.\n", ocsp_path);
1160 close(fd);
1161 return -1;
1162 }
1163 else if (r == 0) {
1164 break;
1165 }
1166 trash.len += r;
1167 }
1168 close(fd);
1169 return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)trash.str, trash.len);
1170}
1171#endif
1172
Daniel Jakots54ffb912015-11-06 20:02:41 +01001173#if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001174
1175#define CT_EXTENSION_TYPE 18
1176
1177static int sctl_ex_index = -1;
1178
1179/*
1180 * Try to parse Signed Certificate Timestamp List structure. This function
1181 * makes only basic test if the data seems like SCTL. No signature validation
1182 * is performed.
1183 */
1184static int ssl_sock_parse_sctl(struct chunk *sctl)
1185{
1186 int ret = 1;
1187 int len, pos, sct_len;
1188 unsigned char *data;
1189
1190 if (sctl->len < 2)
1191 goto out;
1192
1193 data = (unsigned char *)sctl->str;
1194 len = (data[0] << 8) | data[1];
1195
1196 if (len + 2 != sctl->len)
1197 goto out;
1198
1199 data = data + 2;
1200 pos = 0;
1201 while (pos < len) {
1202 if (len - pos < 2)
1203 goto out;
1204
1205 sct_len = (data[pos] << 8) | data[pos + 1];
1206 if (pos + sct_len + 2 > len)
1207 goto out;
1208
1209 pos += sct_len + 2;
1210 }
1211
1212 ret = 0;
1213
1214out:
1215 return ret;
1216}
1217
1218static int ssl_sock_load_sctl_from_file(const char *sctl_path, struct chunk **sctl)
1219{
1220 int fd = -1;
1221 int r = 0;
1222 int ret = 1;
1223
1224 *sctl = NULL;
1225
1226 fd = open(sctl_path, O_RDONLY);
1227 if (fd == -1)
1228 goto end;
1229
1230 trash.len = 0;
1231 while (trash.len < trash.size) {
1232 r = read(fd, trash.str + trash.len, trash.size - trash.len);
1233 if (r < 0) {
1234 if (errno == EINTR)
1235 continue;
1236
1237 goto end;
1238 }
1239 else if (r == 0) {
1240 break;
1241 }
1242 trash.len += r;
1243 }
1244
1245 ret = ssl_sock_parse_sctl(&trash);
1246 if (ret)
1247 goto end;
1248
Vincent Bernat02779b62016-04-03 13:48:43 +02001249 *sctl = calloc(1, sizeof(**sctl));
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001250 if (!chunk_dup(*sctl, &trash)) {
1251 free(*sctl);
1252 *sctl = NULL;
1253 goto end;
1254 }
1255
1256end:
1257 if (fd != -1)
1258 close(fd);
1259
1260 return ret;
1261}
1262
1263int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1264{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001265 struct chunk *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001266
1267 *out = (unsigned char *)sctl->str;
1268 *outlen = sctl->len;
1269
1270 return 1;
1271}
1272
1273int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1274{
1275 return 1;
1276}
1277
1278static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path)
1279{
1280 char sctl_path[MAXPATHLEN+1];
1281 int ret = -1;
1282 struct stat st;
1283 struct chunk *sctl = NULL;
1284
1285 snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path);
1286
1287 if (stat(sctl_path, &st))
1288 return 1;
1289
1290 if (ssl_sock_load_sctl_from_file(sctl_path, &sctl))
1291 goto out;
1292
1293 if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) {
1294 free(sctl);
1295 goto out;
1296 }
1297
1298 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1299
1300 ret = 0;
1301
1302out:
1303 return ret;
1304}
1305
1306#endif
1307
Emeric Brune1f38db2012-09-03 20:36:47 +02001308void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1309{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001310 struct connection *conn = SSL_get_app_data(ssl);
Emeric Brund8b2bb52014-01-28 15:43:53 +01001311 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001312 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001313
1314 if (where & SSL_CB_HANDSHAKE_START) {
1315 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreau20879a02012-12-03 16:32:10 +01001316 if (conn->flags & CO_FL_CONNECTED) {
Emeric Brune1f38db2012-09-03 20:36:47 +02001317 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001318 conn->err_code = CO_ER_SSL_RENEG;
1319 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001320 }
Emeric Brund8b2bb52014-01-28 15:43:53 +01001321
1322 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
1323 if (!(conn->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
1324 /* Long certificate chains optimz
1325 If write and read bios are differents, we
1326 consider that the buffering was activated,
1327 so we rise the output buffer size from 4k
1328 to 16k */
1329 write_bio = SSL_get_wbio(ssl);
1330 if (write_bio != SSL_get_rbio(ssl)) {
1331 BIO_set_write_buffer_size(write_bio, 16384);
1332 conn->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
1333 }
1334 }
1335 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001336}
1337
Emeric Brune64aef12012-09-21 13:15:06 +02001338/* Callback is called for each certificate of the chain during a verify
1339 ok is set to 1 if preverify detect no error on current certificate.
1340 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001341int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001342{
1343 SSL *ssl;
1344 struct connection *conn;
Emeric Brun81c00f02012-09-21 14:31:21 +02001345 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001346
1347 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001348 conn = SSL_get_app_data(ssl);
Emeric Brune64aef12012-09-21 13:15:06 +02001349
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001350 conn->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001351
Emeric Brun81c00f02012-09-21 14:31:21 +02001352 if (ok) /* no errors */
1353 return ok;
1354
1355 depth = X509_STORE_CTX_get_error_depth(x_store);
1356 err = X509_STORE_CTX_get_error(x_store);
1357
1358 /* check if CA error needs to be ignored */
1359 if (depth > 0) {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001360 if (!SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st)) {
1361 conn->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1362 conn->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001363 }
1364
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001365 if (objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001366 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001367 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001368 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001369 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001370
Willy Tarreau20879a02012-12-03 16:32:10 +01001371 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001372 return 0;
1373 }
1374
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02001375 if (!SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st))
1376 conn->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001377
Emeric Brun81c00f02012-09-21 14:31:21 +02001378 /* check if certificate error needs to be ignored */
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001379 if (objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001380 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001381 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001382 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001383 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001384
Willy Tarreau20879a02012-12-03 16:32:10 +01001385 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001386 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001387}
1388
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001389static inline
1390void ssl_sock_parse_clienthello(int write_p, int version, int content_type,
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001391 const void *buf, size_t len, SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001392{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001393 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001394 unsigned char *msg;
1395 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001396 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001397
1398 /* This function is called for "from client" and "to server"
1399 * connections. The combination of write_p == 0 and content_type == 22
1400 * is only avalaible during "from client" connection.
1401 */
1402
1403 /* "write_p" is set to 0 is the bytes are received messages,
1404 * otherwise it is set to 1.
1405 */
1406 if (write_p != 0)
1407 return;
1408
1409 /* content_type contains the type of message received or sent
1410 * according with the SSL/TLS protocol spec. This message is
1411 * encoded with one byte. The value 256 (two bytes) is used
1412 * for designing the SSL/TLS record layer. According with the
1413 * rfc6101, the expected message (other than 256) are:
1414 * - change_cipher_spec(20)
1415 * - alert(21)
1416 * - handshake(22)
1417 * - application_data(23)
1418 * - (255)
1419 * We are interessed by the handshake and specially the client
1420 * hello.
1421 */
1422 if (content_type != 22)
1423 return;
1424
1425 /* The message length is at least 4 bytes, containing the
1426 * message type and the message length.
1427 */
1428 if (len < 4)
1429 return;
1430
1431 /* First byte of the handshake message id the type of
1432 * message. The konwn types are:
1433 * - hello_request(0)
1434 * - client_hello(1)
1435 * - server_hello(2)
1436 * - certificate(11)
1437 * - server_key_exchange (12)
1438 * - certificate_request(13)
1439 * - server_hello_done(14)
1440 * We are interested by the client hello.
1441 */
1442 msg = (unsigned char *)buf;
1443 if (msg[0] != 1)
1444 return;
1445
1446 /* Next three bytes are the length of the message. The total length
1447 * must be this decoded length + 4. If the length given as argument
1448 * is not the same, we abort the protocol dissector.
1449 */
1450 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1451 if (len < rec_len + 4)
1452 return;
1453 msg += 4;
1454 end = msg + rec_len;
1455 if (end < msg)
1456 return;
1457
1458 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1459 * for minor, the random, composed by 4 bytes for the unix time and
1460 * 28 bytes for unix payload, and them 1 byte for the session id. So
1461 * we jump 1 + 1 + 4 + 28 + 1 bytes.
1462 */
1463 msg += 1 + 1 + 4 + 28 + 1;
1464 if (msg > end)
1465 return;
1466
1467 /* Next two bytes are the ciphersuite length. */
1468 if (msg + 2 > end)
1469 return;
1470 rec_len = (msg[0] << 8) + msg[1];
1471 msg += 2;
1472 if (msg + rec_len > end || msg + rec_len < msg)
1473 return;
1474
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001475 capture = pool_alloc_dirty(pool2_ssl_capture);
1476 if (!capture)
1477 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001478 /* Compute the xxh64 of the ciphersuite. */
1479 capture->xxh64 = XXH64(msg, rec_len, 0);
1480
1481 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001482 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1483 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001484 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001485
1486 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001487}
1488
Emeric Brun29f037d2014-04-25 19:05:36 +02001489/* Callback is called for ssl protocol analyse */
1490void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1491{
Emeric Brun29f037d2014-04-25 19:05:36 +02001492#ifdef TLS1_RT_HEARTBEAT
1493 /* test heartbeat received (write_p is set to 0
1494 for a received record) */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001495 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001496 struct connection *conn = SSL_get_app_data(ssl);
Willy Tarreauf51c6982014-04-25 20:02:39 +02001497 const unsigned char *p = buf;
1498 unsigned int payload;
1499
Emeric Brun29f037d2014-04-25 19:05:36 +02001500 conn->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001501
1502 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1503 if (*p != TLS1_HB_REQUEST)
1504 return;
1505
Willy Tarreauaeed6722014-04-25 23:59:58 +02001506 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
Willy Tarreauf51c6982014-04-25 20:02:39 +02001507 goto kill_it;
1508
1509 payload = (p[1] * 256) + p[2];
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001510 if (3 + payload + 16 <= len)
Willy Tarreauf51c6982014-04-25 20:02:39 +02001511 return; /* OK no problem */
Willy Tarreauaeed6722014-04-25 23:59:58 +02001512 kill_it:
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001513 /* We have a clear heartbleed attack (CVE-2014-0160), the
1514 * advertised payload is larger than the advertised packet
1515 * length, so we have garbage in the buffer between the
1516 * payload and the end of the buffer (p+len). We can't know
1517 * if the SSL stack is patched, and we don't know if we can
1518 * safely wipe out the area between p+3+len and payload.
1519 * So instead, we prevent the response from being sent by
1520 * setting the max_send_fragment to 0 and we report an SSL
1521 * error, which will kill this connection. It will be reported
1522 * above as SSL_ERROR_SSL while an other handshake failure with
Willy Tarreauf51c6982014-04-25 20:02:39 +02001523 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1524 */
Willy Tarreau3b2fdb62014-04-25 23:44:22 +02001525 ssl->max_send_fragment = 0;
Willy Tarreauf51c6982014-04-25 20:02:39 +02001526 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1527 return;
1528 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001529#endif
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001530 if (global_ssl.capture_cipherlist > 0)
1531 ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl);
Emeric Brun29f037d2014-04-25 19:05:36 +02001532}
1533
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001534#ifdef OPENSSL_NPN_NEGOTIATED
1535/* This callback is used so that the server advertises the list of
1536 * negociable protocols for NPN.
1537 */
1538static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1539 unsigned int *len, void *arg)
1540{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001541 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001542
1543 *data = (const unsigned char *)conf->npn_str;
1544 *len = conf->npn_len;
1545 return SSL_TLSEXT_ERR_OK;
1546}
1547#endif
1548
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001549#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001550/* This callback is used so that the server advertises the list of
1551 * negociable protocols for ALPN.
1552 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001553static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1554 unsigned char *outlen,
1555 const unsigned char *server,
1556 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001557{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001558 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001559
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001560 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1561 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1562 return SSL_TLSEXT_ERR_NOACK;
1563 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001564 return SSL_TLSEXT_ERR_OK;
1565}
1566#endif
1567
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001568#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001569#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001570
Christopher Faulet30548802015-06-11 13:39:32 +02001571/* Create a X509 certificate with the specified servername and serial. This
1572 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001573static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001574ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001575{
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001576 static unsigned int serial = 0;
1577
Christopher Faulet7969a332015-10-09 11:15:03 +02001578 X509 *cacert = bind_conf->ca_sign_cert;
1579 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001580 SSL_CTX *ssl_ctx = NULL;
1581 X509 *newcrt = NULL;
1582 EVP_PKEY *pkey = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001583 X509_NAME *name;
1584 const EVP_MD *digest;
1585 X509V3_CTX ctx;
1586 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001587 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001588
Christopher Faulet7969a332015-10-09 11:15:03 +02001589 /* Get the private key of the defautl certificate and use it */
1590 if (!(pkey = SSL_get_privatekey(ssl)))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001591 goto mkcert_error;
1592
1593 /* Create the certificate */
1594 if (!(newcrt = X509_new()))
1595 goto mkcert_error;
1596
1597 /* Set version number for the certificate (X509v3) and the serial
1598 * number */
1599 if (X509_set_version(newcrt, 2L) != 1)
1600 goto mkcert_error;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001601 if (!serial)
1602 serial = now_ms;
1603 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), serial++);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001604
1605 /* Set duration for the certificate */
1606 if (!X509_gmtime_adj(X509_get_notBefore(newcrt), (long)-60*60*24) ||
1607 !X509_gmtime_adj(X509_get_notAfter(newcrt),(long)60*60*24*365))
1608 goto mkcert_error;
1609
1610 /* set public key in the certificate */
1611 if (X509_set_pubkey(newcrt, pkey) != 1)
1612 goto mkcert_error;
1613
1614 /* Set issuer name from the CA */
1615 if (!(name = X509_get_subject_name(cacert)))
1616 goto mkcert_error;
1617 if (X509_set_issuer_name(newcrt, name) != 1)
1618 goto mkcert_error;
1619
1620 /* Set the subject name using the same, but the CN */
1621 name = X509_NAME_dup(name);
1622 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
1623 (const unsigned char *)servername,
1624 -1, -1, 0) != 1) {
1625 X509_NAME_free(name);
1626 goto mkcert_error;
1627 }
1628 if (X509_set_subject_name(newcrt, name) != 1) {
1629 X509_NAME_free(name);
1630 goto mkcert_error;
1631 }
1632 X509_NAME_free(name);
1633
1634 /* Add x509v3 extensions as specified */
1635 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
1636 for (i = 0; i < X509V3_EXT_SIZE; i++) {
1637 X509_EXTENSION *ext;
1638
1639 if (!(ext = X509V3_EXT_conf(NULL, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
1640 goto mkcert_error;
1641 if (!X509_add_ext(newcrt, ext, -1)) {
1642 X509_EXTENSION_free(ext);
1643 goto mkcert_error;
1644 }
1645 X509_EXTENSION_free(ext);
1646 }
1647
1648 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001649
1650 key_type = EVP_PKEY_base_id(capkey);
1651
1652 if (key_type == EVP_PKEY_DSA)
1653 digest = EVP_sha1();
1654 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001655 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001656 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02001657 digest = EVP_sha256();
1658 else {
Christopher Faulete7db2162015-10-19 13:59:24 +02001659#if (OPENSSL_VERSION_NUMBER >= 0x1000000fL)
Christopher Faulet7969a332015-10-09 11:15:03 +02001660 int nid;
1661
1662 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
1663 goto mkcert_error;
1664 if (!(digest = EVP_get_digestbynid(nid)))
1665 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02001666#else
1667 goto mkcert_error;
1668#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02001669 }
1670
Christopher Faulet31af49d2015-06-09 17:29:50 +02001671 if (!(X509_sign(newcrt, capkey, digest)))
1672 goto mkcert_error;
1673
1674 /* Create and set the new SSL_CTX */
1675 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
1676 goto mkcert_error;
1677 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1678 goto mkcert_error;
1679 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
1680 goto mkcert_error;
1681 if (!SSL_CTX_check_private_key(ssl_ctx))
1682 goto mkcert_error;
1683
1684 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02001685
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001686#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001687 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001688#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001689#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
1690 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001691 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001692 EC_KEY *ecc;
1693 int nid;
1694
1695 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
1696 goto end;
1697 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
1698 goto end;
1699 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
1700 EC_KEY_free(ecc);
1701 }
1702#endif
1703 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02001704 return ssl_ctx;
1705
1706 mkcert_error:
1707 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
1708 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001709 return NULL;
1710}
1711
Christopher Faulet7969a332015-10-09 11:15:03 +02001712SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001713ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02001714{
1715 struct bind_conf *bind_conf = objt_listener(conn->target)->bind_conf;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001716
1717 return ssl_sock_do_create_cert(servername, bind_conf, conn->xprt_ctx);
Christopher Faulet7969a332015-10-09 11:15:03 +02001718}
1719
Christopher Faulet30548802015-06-11 13:39:32 +02001720/* Do a lookup for a certificate in the LRU cache used to store generated
1721 * certificates. */
1722SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001723ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02001724{
1725 struct lru64 *lru = NULL;
1726
1727 if (ssl_ctx_lru_tree) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001728 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Christopher Faulet30548802015-06-11 13:39:32 +02001729 if (lru && lru->domain)
1730 return (SSL_CTX *)lru->data;
1731 }
1732 return NULL;
1733}
1734
Christopher Fauletd2cab922015-07-28 16:03:47 +02001735/* Set a certificate int the LRU cache used to store generated
1736 * certificate. Return 0 on success, otherwise -1 */
1737int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001738ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02001739{
1740 struct lru64 *lru = NULL;
1741
1742 if (ssl_ctx_lru_tree) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001743 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Christopher Faulet30548802015-06-11 13:39:32 +02001744 if (!lru)
Christopher Fauletd2cab922015-07-28 16:03:47 +02001745 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02001746 if (lru->domain && lru->data)
1747 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02001748 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02001749 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02001750 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02001751 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02001752}
1753
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001754/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02001755unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001756ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02001757{
1758 return XXH32(data, len, ssl_ctx_lru_seed);
1759}
1760
Willy Tarreau2f63ef42015-10-20 15:16:01 +02001761/* Generate a cert and immediately assign it to the SSL session so that the cert's
1762 * refcount is maintained regardless of the cert's presence in the LRU cache.
1763 */
Christopher Faulet31af49d2015-06-09 17:29:50 +02001764static SSL_CTX *
Christopher Faulet7969a332015-10-09 11:15:03 +02001765ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001766{
1767 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001768 SSL_CTX *ssl_ctx = NULL;
1769 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001770 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001771
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001772 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001773 if (ssl_ctx_lru_tree) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001774 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001775 if (lru && lru->domain)
1776 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02001777 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001778 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001779 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02001780 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02001781 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001782 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02001783 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001784 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02001785 SSL_set_SSL_CTX(ssl, ssl_ctx);
1786 /* No LRU cache, this CTX will be released as soon as the session dies */
1787 SSL_CTX_free(ssl_ctx);
1788 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02001789 return ssl_ctx;
1790}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001791#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02001792
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001793
1794#ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */
1795#define SSL_OP_CIPHER_SERVER_PREFERENCE 0
1796#endif
1797
1798#ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */
1799#define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0
1800#define SSL_renegotiate_pending(arg) 0
1801#endif
1802#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */
1803#define SSL_OP_SINGLE_ECDH_USE 0
1804#endif
1805#ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */
1806#define SSL_OP_NO_TICKET 0
1807#endif
1808#ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */
1809#define SSL_OP_NO_COMPRESSION 0
1810#endif
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02001811#ifdef OPENSSL_NO_SSL3 /* SSLv3 support removed */
1812#undef SSL_OP_NO_SSLv3
1813#define SSL_OP_NO_SSLv3 0
1814#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001815#ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */
1816#define SSL_OP_NO_TLSv1_1 0
1817#endif
1818#ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */
1819#define SSL_OP_NO_TLSv1_2 0
1820#endif
1821#ifndef SSL_OP_NO_TLSv1_3 /* dev */
1822#define SSL_OP_NO_TLSv1_3 0
1823#endif
1824#ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */
1825#define SSL_OP_SINGLE_DH_USE 0
1826#endif
1827#ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */
1828#define SSL_OP_SINGLE_ECDH_USE 0
1829#endif
1830#ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */
1831#define SSL_MODE_RELEASE_BUFFERS 0
1832#endif
1833#ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */
1834#define SSL_MODE_SMALL_BUFFERS 0
1835#endif
1836
1837#if (OPENSSL_VERSION_NUMBER < 0x1010000fL) && !defined(OPENSSL_IS_BORINGSSL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001838typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
1839
1840static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001841{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02001842#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001843 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001844 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
1845#endif
1846}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001847static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
1848 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001849 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
1850}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001851static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001852#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001853 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001854 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
1855#endif
1856}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001857static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001858#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001859 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001860 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
1861#endif
1862}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001863/* TLS 1.2 is the last supported version in this context. */
1864static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
1865/* Unusable in this context. */
1866static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
1867static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
1868static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
1869static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
1870static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001871#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001872typedef enum { SET_MIN, SET_MAX } set_context_func;
1873
1874static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
1875 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001876 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
1877}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001878static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
1879 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
1880 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
1881}
1882static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
1883 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001884 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
1885}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001886static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
1887 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
1888 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
1889}
1890static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
1891 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001892 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
1893}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001894static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
1895 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
1896 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
1897}
1898static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
1899 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001900 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
1901}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001902static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
1903 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
1904 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
1905}
1906static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001907#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001908 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001909 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
1910#endif
1911}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001912static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
1913#if SSL_OP_NO_TLSv1_3
1914 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
1915 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001916#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001917}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001918#endif
1919static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
1920static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001921
1922static struct {
1923 int option;
1924 uint16_t flag;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001925 void (*ctx_set_version)(SSL_CTX *, set_context_func);
1926 void (*ssl_set_version)(SSL *, set_context_func);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02001927 const char *name;
1928} methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02001929 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
1930 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
1931 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
1932 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
1933 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
1934 {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 +02001935};
1936
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01001937static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
1938{
1939 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
1940 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
1941 SSL_set_SSL_CTX(ssl, ctx);
1942}
1943
Emmanuel Hocdet05942112017-02-20 16:11:50 +01001944#ifdef OPENSSL_IS_BORINGSSL
1945
1946static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
1947{
1948 (void)al; /* shut gcc stupid warning */
1949 (void)priv;
1950
1951 if (!SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name))
1952 return SSL_TLSEXT_ERR_NOACK;
1953 return SSL_TLSEXT_ERR_OK;
1954}
1955
1956static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
1957{
1958 struct connection *conn;
1959 struct bind_conf *s;
1960 const uint8_t *extension_data;
1961 size_t extension_len;
1962 CBS extension, cipher_suites, server_name_list, host_name, sig_algs;
1963 const SSL_CIPHER *cipher;
1964 uint16_t cipher_suite;
1965 uint8_t name_type, hash, sign;
1966 int has_rsa = 0, has_ecdsa = 0, has_ecdsa_sig = 0;
1967
1968 char *wildp = NULL;
1969 const uint8_t *servername;
1970 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
1971 int i;
1972
1973 conn = SSL_get_app_data(ctx->ssl);
1974 s = objt_listener(conn->target)->bind_conf;
1975
1976 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
1977 &extension_data, &extension_len)) {
1978 CBS_init(&extension, extension_data, extension_len);
1979
1980 if (!CBS_get_u16_length_prefixed(&extension, &server_name_list)
1981 || !CBS_get_u8(&server_name_list, &name_type)
1982 /* Although the server_name extension was intended to be extensible to
1983 * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
1984 * different name types will cause an error. Further, RFC 4366 originally
1985 * defined syntax inextensibly. RFC 6066 corrected this mistake, but
1986 * adding new name types is no longer feasible.
1987 *
1988 * Act as if the extensibility does not exist to simplify parsing. */
1989 || !CBS_get_u16_length_prefixed(&server_name_list, &host_name)
1990 || CBS_len(&server_name_list) != 0
1991 || CBS_len(&extension) != 0
1992 || name_type != TLSEXT_NAMETYPE_host_name
1993 || CBS_len(&host_name) == 0
1994 || CBS_len(&host_name) > TLSEXT_MAXLEN_host_name
1995 || CBS_contains_zero_byte(&host_name)) {
1996 goto abort;
1997 }
1998 } else {
1999 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002000 if (!s->strict_sni) {
2001 ssl_sock_switchctx_set(ctx->ssl, s->default_ctx);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002002 return 1;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002003 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002004 goto abort;
2005 }
2006
2007 /* extract/check clientHello informations */
2008 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2009 CBS_init(&extension, extension_data, extension_len);
2010
2011 if (!CBS_get_u16_length_prefixed(&extension, &sig_algs)
2012 || CBS_len(&sig_algs) == 0
2013 || CBS_len(&extension) != 0) {
2014 goto abort;
2015 }
2016 if (CBS_len(&sig_algs) % 2 != 0) {
2017 goto abort;
2018 }
2019 while (CBS_len(&sig_algs) != 0) {
2020 if (!CBS_get_u8(&sig_algs, &hash)
2021 || !CBS_get_u8(&sig_algs, &sign)) {
2022 goto abort;
2023 }
2024 switch (sign) {
2025 case TLSEXT_signature_rsa:
2026 has_rsa = 1;
2027 break;
2028 case TLSEXT_signature_ecdsa:
2029 has_ecdsa_sig = 1;
2030 break;
2031 default:
2032 continue;
2033 }
2034 if (has_ecdsa_sig && has_rsa)
2035 break;
2036 }
2037 } else {
2038 /* without TLSEXT_TYPE_signature_algorithms extension (< TLS 1.2) */
2039 has_rsa = 1;
2040 }
2041 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
2042 CBS_init(&cipher_suites, ctx->cipher_suites, ctx->cipher_suites_len);
2043
2044 while (CBS_len(&cipher_suites) != 0) {
2045 if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
2046 goto abort;
2047 }
2048 cipher = SSL_get_cipher_by_value(cipher_suite);
2049 if (cipher && SSL_CIPHER_is_ECDSA(cipher)) {
2050 has_ecdsa = 1;
2051 break;
2052 }
2053 }
2054 }
2055
2056 servername = CBS_data(&host_name);
2057 for (i = 0; i < trash.size && i < CBS_len(&host_name); i++) {
2058 trash.str[i] = tolower(servername[i]);
2059 if (!wildp && (trash.str[i] == '.'))
2060 wildp = &trash.str[i];
2061 }
2062 trash.str[i] = 0;
2063
2064 /* lookup in full qualified names */
2065 node = ebst_lookup(&s->sni_ctx, trash.str);
2066
2067 /* lookup a not neg filter */
2068 for (n = node; n; n = ebmb_next_dup(n)) {
2069 if (!container_of(n, struct sni_ctx, name)->neg) {
2070 switch(container_of(n, struct sni_ctx, name)->key_sig) {
2071 case TLSEXT_signature_ecdsa:
2072 if (has_ecdsa) {
2073 node_ecdsa = n;
2074 goto find_one;
2075 }
2076 break;
2077 case TLSEXT_signature_rsa:
2078 if (has_rsa && !node_rsa) {
2079 node_rsa = n;
2080 if (!has_ecdsa)
2081 goto find_one;
2082 }
2083 break;
2084 default: /* TLSEXT_signature_anonymous */
2085 if (!node_anonymous)
2086 node_anonymous = n;
2087 break;
2088 }
2089 }
2090 }
2091 if (wildp) {
2092 /* lookup in wildcards names */
2093 node = ebst_lookup(&s->sni_w_ctx, wildp);
2094 for (n = node; n; n = ebmb_next_dup(n)) {
2095 if (!container_of(n, struct sni_ctx, name)->neg) {
2096 switch(container_of(n, struct sni_ctx, name)->key_sig) {
2097 case TLSEXT_signature_ecdsa:
2098 if (has_ecdsa) {
2099 node_ecdsa = n;
2100 goto find_one;
2101 }
2102 break;
2103 case TLSEXT_signature_rsa:
2104 if (has_rsa && !node_rsa) {
2105 node_rsa = n;
2106 if (!has_ecdsa)
2107 goto find_one;
2108 }
2109 break;
2110 default: /* TLSEXT_signature_anonymous */
2111 if (!node_anonymous)
2112 node_anonymous = n;
2113 break;
2114 }
2115 }
2116 }
2117 }
2118 find_one:
2119 /* select by key_signature priority order */
2120 node = node_ecdsa ? node_ecdsa : (node_rsa ? node_rsa : node_anonymous);
2121
2122 if (node) {
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02002123 int min, max;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002124 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002125 ssl_sock_switchctx_set(ctx->ssl, container_of(node, struct sni_ctx, name)->ctx);
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02002126 min = container_of(node, struct sni_ctx, name)->conf->ssl_methods.min;
2127 if (min != s->ssl_methods.min)
2128 methodVersions[min].ssl_set_version(ctx->ssl, SET_MIN);
2129 max = container_of(node, struct sni_ctx, name)->conf->ssl_methods.max;
2130 if (max != s->ssl_methods.max)
2131 methodVersions[max].ssl_set_version(ctx->ssl, SET_MAX);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002132 return 1;
2133 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002134 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002135 /* no certificate match, is the default_ctx */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002136 ssl_sock_switchctx_set(ctx->ssl, s->default_ctx);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002137 return 1;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002138 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002139 abort:
2140 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2141 conn->err_code = CO_ER_SSL_HANDSHAKE;
2142 return -1;
2143}
2144
2145#else /* OPENSSL_IS_BORINGSSL */
2146
Emeric Brunfc0421f2012-09-07 17:30:07 +02002147/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2148 * warning when no match is found, which implies the default (first) cert
2149 * will keep being used.
2150 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002151static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002152{
2153 const char *servername;
2154 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002155 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002156 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002157 int i;
2158 (void)al; /* shut gcc stupid warning */
2159
2160 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002161 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002162#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreauf6721452015-07-07 18:04:38 +02002163 if (s->generate_certs) {
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002164 struct connection *conn = SSL_get_app_data(ssl);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002165 unsigned int key;
Willy Tarreauf6721452015-07-07 18:04:38 +02002166 SSL_CTX *ctx;
Christopher Faulet30548802015-06-11 13:39:32 +02002167
Willy Tarreauf6721452015-07-07 18:04:38 +02002168 conn_get_to_addr(conn);
2169 if (conn->flags & CO_FL_ADDR_TO_SET) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002170 key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to));
2171 ctx = ssl_sock_get_generated_cert(key, s);
Willy Tarreauf6721452015-07-07 18:04:38 +02002172 if (ctx) {
2173 /* switch ctx */
2174 SSL_set_SSL_CTX(ssl, ctx);
2175 return SSL_TLSEXT_ERR_OK;
2176 }
Christopher Faulet30548802015-06-11 13:39:32 +02002177 }
2178 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002179#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002180 if (s->strict_sni)
2181 return SSL_TLSEXT_ERR_ALERT_FATAL;
2182 ssl_sock_switchctx_set(ssl, s->default_ctx);
2183 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002184 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002185
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002186 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002187 if (!servername[i])
2188 break;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002189 trash.str[i] = tolower(servername[i]);
2190 if (!wildp && (trash.str[i] == '.'))
2191 wildp = &trash.str[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002192 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002193 trash.str[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002194
2195 /* lookup in full qualified names */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002196 node = ebst_lookup(&s->sni_ctx, trash.str);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002197
2198 /* lookup a not neg filter */
2199 for (n = node; n; n = ebmb_next_dup(n)) {
2200 if (!container_of(n, struct sni_ctx, name)->neg) {
2201 node = n;
2202 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002203 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002204 }
2205 if (!node && wildp) {
2206 /* lookup in wildcards names */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002207 node = ebst_lookup(&s->sni_w_ctx, wildp);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002208 }
2209 if (!node || container_of(node, struct sni_ctx, name)->neg) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002210#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002211 SSL_CTX *ctx;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002212 if (s->generate_certs &&
Christopher Faulet7969a332015-10-09 11:15:03 +02002213 (ctx = ssl_sock_generate_certificate(servername, s, ssl))) {
Christopher Faulet31af49d2015-06-09 17:29:50 +02002214 /* switch ctx */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002215 return SSL_TLSEXT_ERR_OK;
2216 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002217#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002218 if (s->strict_sni)
2219 return SSL_TLSEXT_ERR_ALERT_FATAL;
2220 ssl_sock_switchctx_set(ssl, s->default_ctx);
2221 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002222 }
2223
2224 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002225 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002226 return SSL_TLSEXT_ERR_OK;
2227}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002228#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002229#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2230
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002231#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002232
2233static DH * ssl_get_dh_1024(void)
2234{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002235 static unsigned char dh1024_p[]={
2236 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2237 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2238 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2239 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2240 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2241 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2242 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2243 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2244 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2245 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2246 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2247 };
2248 static unsigned char dh1024_g[]={
2249 0x02,
2250 };
2251
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002252 BIGNUM *p;
2253 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002254 DH *dh = DH_new();
2255 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002256 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2257 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002258
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002259 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002260 DH_free(dh);
2261 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002262 } else {
2263 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002264 }
2265 }
2266 return dh;
2267}
2268
2269static DH *ssl_get_dh_2048(void)
2270{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002271 static unsigned char dh2048_p[]={
2272 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2273 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2274 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2275 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2276 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2277 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2278 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2279 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2280 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2281 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2282 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2283 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2284 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2285 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2286 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2287 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2288 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2289 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2290 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2291 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2292 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2293 0xB7,0x1F,0x77,0xF3,
2294 };
2295 static unsigned char dh2048_g[]={
2296 0x02,
2297 };
2298
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002299 BIGNUM *p;
2300 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002301 DH *dh = DH_new();
2302 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002303 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2304 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002305
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002306 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002307 DH_free(dh);
2308 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002309 } else {
2310 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002311 }
2312 }
2313 return dh;
2314}
2315
2316static DH *ssl_get_dh_4096(void)
2317{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002318 static unsigned char dh4096_p[]={
2319 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2320 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2321 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2322 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2323 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2324 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2325 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2326 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2327 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2328 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2329 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2330 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2331 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2332 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2333 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2334 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2335 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2336 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2337 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2338 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2339 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2340 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2341 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2342 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2343 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2344 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2345 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2346 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2347 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2348 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2349 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2350 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2351 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2352 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2353 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2354 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2355 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2356 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2357 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2358 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2359 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2360 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2361 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002362 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002363 static unsigned char dh4096_g[]={
2364 0x02,
2365 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002366
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002367 BIGNUM *p;
2368 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002369 DH *dh = DH_new();
2370 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002371 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2372 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002373
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002374 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002375 DH_free(dh);
2376 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002377 } else {
2378 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002379 }
2380 }
2381 return dh;
2382}
2383
2384/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002385 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002386static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2387{
2388 DH *dh = NULL;
2389 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002390 int type;
2391
2392 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002393
2394 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2395 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2396 */
2397 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2398 keylen = EVP_PKEY_bits(pkey);
2399 }
2400
Willy Tarreauef934602016-12-22 23:12:01 +01002401 if (keylen > global_ssl.default_dh_param) {
2402 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002403 }
2404
Remi Gacogned3a341a2015-05-29 16:26:17 +02002405 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002406 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002407 }
2408 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002409 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002410 }
2411 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002412 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002413 }
2414
2415 return dh;
2416}
2417
Remi Gacogne47783ef2015-05-29 15:53:22 +02002418static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002419{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002420 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002421 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002422
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002423 if (in == NULL)
2424 goto end;
2425
Remi Gacogne47783ef2015-05-29 15:53:22 +02002426 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002427 goto end;
2428
Remi Gacogne47783ef2015-05-29 15:53:22 +02002429 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2430
2431end:
2432 if (in)
2433 BIO_free(in);
2434
2435 return dh;
2436}
2437
2438int ssl_sock_load_global_dh_param_from_file(const char *filename)
2439{
2440 global_dh = ssl_sock_get_dh_from_file(filename);
2441
2442 if (global_dh) {
2443 return 0;
2444 }
2445
2446 return -1;
2447}
2448
2449/* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1
2450 if an error occured, and 0 if parameter not found. */
2451int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file)
2452{
2453 int ret = -1;
2454 DH *dh = ssl_sock_get_dh_from_file(file);
2455
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002456 if (dh) {
2457 ret = 1;
2458 SSL_CTX_set_tmp_dh(ctx, dh);
Remi Gacogne4f902b82015-05-28 16:23:00 +02002459
2460 if (ssl_dh_ptr_index >= 0) {
2461 /* store a pointer to the DH params to avoid complaining about
2462 ssl-default-dh-param not being set for this SSL_CTX */
2463 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
2464 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002465 }
Remi Gacogne47783ef2015-05-29 15:53:22 +02002466 else if (global_dh) {
2467 SSL_CTX_set_tmp_dh(ctx, global_dh);
2468 ret = 0; /* DH params not found */
2469 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002470 else {
Emeric Brun41fdb3c2013-04-26 11:05:44 +02002471 /* Clear openssl global errors stack */
2472 ERR_clear_error();
2473
Willy Tarreauef934602016-12-22 23:12:01 +01002474 if (global_ssl.default_dh_param <= 1024) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002475 /* we are limited to DH parameter of 1024 bits anyway */
Remi Gacognec7e12632016-07-02 16:26:10 +02002476 if (local_dh_1024 == NULL)
2477 local_dh_1024 = ssl_get_dh_1024();
2478
Remi Gacogne8de54152014-07-15 11:36:40 +02002479 if (local_dh_1024 == NULL)
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002480 goto end;
Willy Tarreau6e774b42014-04-25 21:35:23 +02002481
Remi Gacogne8de54152014-07-15 11:36:40 +02002482 SSL_CTX_set_tmp_dh(ctx, local_dh_1024);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002483 }
2484 else {
2485 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
2486 }
Willy Tarreau6e774b42014-04-25 21:35:23 +02002487
Emeric Brun41fdb3c2013-04-26 11:05:44 +02002488 ret = 0; /* DH params not found */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002489 }
Emeric Brun644cde02012-12-14 11:21:13 +01002490
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002491end:
2492 if (dh)
2493 DH_free(dh);
2494
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002495 return ret;
2496}
2497#endif
2498
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002499static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf,
2500 uint8_t key_sig, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002501{
2502 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002503 int wild = 0, neg = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002504 struct ebmb_node *node;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002505
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002506 if (*name == '!') {
2507 neg = 1;
2508 name++;
2509 }
2510 if (*name == '*') {
2511 wild = 1;
2512 name++;
2513 }
2514 /* !* filter is a nop */
2515 if (neg && wild)
2516 return order;
2517 if (*name) {
2518 int j, len;
2519 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002520 for (j = 0; j < len && j < trash.size; j++)
2521 trash.str[j] = tolower(name[j]);
2522 if (j >= trash.size)
2523 return order;
2524 trash.str[j] = 0;
2525
2526 /* Check for duplicates. */
2527 if (wild)
2528 node = ebst_lookup(&s->sni_w_ctx, trash.str);
2529 else
2530 node = ebst_lookup(&s->sni_ctx, trash.str);
2531 for (; node; node = ebmb_next_dup(node)) {
2532 sc = ebmb_entry(node, struct sni_ctx, name);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002533 if (sc->ctx == ctx && sc->conf == conf &&
2534 sc->key_sig == key_sig && sc->neg == neg)
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002535 return order;
2536 }
2537
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002538 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002539 if (!sc)
2540 return order;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002541 memcpy(sc->name.key, trash.str, len + 1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002542 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002543 sc->conf = conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002544 sc->key_sig = key_sig;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002545 sc->order = order++;
2546 sc->neg = neg;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002547 if (wild)
2548 ebst_insert(&s->sni_w_ctx, &sc->name);
2549 else
2550 ebst_insert(&s->sni_ctx, &sc->name);
2551 }
2552 return order;
2553}
2554
yanbzhu488a4d22015-12-01 15:16:07 -05002555
2556/* The following code is used for loading multiple crt files into
2557 * SSL_CTX's based on CN/SAN
2558 */
Luca Pizzamiglio578b1692016-12-12 10:56:56 +01002559#if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(LIBRESSL_VERSION_NUMBER)
yanbzhu488a4d22015-12-01 15:16:07 -05002560/* This is used to preload the certifcate, private key
2561 * and Cert Chain of a file passed in via the crt
2562 * argument
2563 *
2564 * This way, we do not have to read the file multiple times
2565 */
2566struct cert_key_and_chain {
2567 X509 *cert;
2568 EVP_PKEY *key;
2569 unsigned int num_chain_certs;
2570 /* This is an array of X509 pointers */
2571 X509 **chain_certs;
2572};
2573
yanbzhu08ce6ab2015-12-02 13:01:29 -05002574#define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES))
2575
2576struct key_combo_ctx {
2577 SSL_CTX *ctx;
2578 int order;
2579};
2580
2581/* Map used for processing multiple keypairs for a single purpose
2582 *
2583 * This maps CN/SNI name to certificate type
2584 */
2585struct sni_keytype {
2586 int keytypes; /* BITMASK for keytypes */
2587 struct ebmb_node name; /* node holding the servername value */
2588};
2589
2590
yanbzhu488a4d22015-12-01 15:16:07 -05002591/* Frees the contents of a cert_key_and_chain
2592 */
2593static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
2594{
2595 int i;
2596
2597 if (!ckch)
2598 return;
2599
2600 /* Free the certificate and set pointer to NULL */
2601 if (ckch->cert)
2602 X509_free(ckch->cert);
2603 ckch->cert = NULL;
2604
2605 /* Free the key and set pointer to NULL */
2606 if (ckch->key)
2607 EVP_PKEY_free(ckch->key);
2608 ckch->key = NULL;
2609
2610 /* Free each certificate in the chain */
2611 for (i = 0; i < ckch->num_chain_certs; i++) {
2612 if (ckch->chain_certs[i])
2613 X509_free(ckch->chain_certs[i]);
2614 }
2615
2616 /* Free the chain obj itself and set to NULL */
2617 if (ckch->num_chain_certs > 0) {
2618 free(ckch->chain_certs);
2619 ckch->num_chain_certs = 0;
2620 ckch->chain_certs = NULL;
2621 }
2622
2623}
2624
2625/* checks if a key and cert exists in the ckch
2626 */
2627static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
2628{
2629 return (ckch->cert != NULL && ckch->key != NULL);
2630}
2631
2632
2633/* Loads the contents of a crt file (path) into a cert_key_and_chain
2634 * This allows us to carry the contents of the file without having to
2635 * read the file multiple times.
2636 *
2637 * returns:
2638 * 0 on Success
2639 * 1 on SSL Failure
2640 * 2 on file not found
2641 */
2642static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
2643{
2644
2645 BIO *in;
2646 X509 *ca = NULL;
2647 int ret = 1;
2648
2649 ssl_sock_free_cert_key_and_chain_contents(ckch);
2650
2651 in = BIO_new(BIO_s_file());
2652 if (in == NULL)
2653 goto end;
2654
2655 if (BIO_read_filename(in, path) <= 0)
2656 goto end;
2657
yanbzhu488a4d22015-12-01 15:16:07 -05002658 /* Read Private Key */
2659 ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
2660 if (ckch->key == NULL) {
2661 memprintf(err, "%sunable to load private key from file '%s'.\n",
2662 err && *err ? *err : "", path);
2663 goto end;
2664 }
2665
Willy Tarreaubb137a82016-04-06 19:02:38 +02002666 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02002667 if (BIO_reset(in) == -1) {
2668 memprintf(err, "%san error occurred while reading the file '%s'.\n",
2669 err && *err ? *err : "", path);
2670 goto end;
2671 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02002672
2673 /* Read Certificate */
2674 ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
2675 if (ckch->cert == NULL) {
2676 memprintf(err, "%sunable to load certificate from file '%s'.\n",
2677 err && *err ? *err : "", path);
2678 goto end;
2679 }
2680
yanbzhu488a4d22015-12-01 15:16:07 -05002681 /* Read Certificate Chain */
2682 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
2683 /* Grow the chain certs */
2684 ckch->num_chain_certs++;
2685 ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *)));
2686
2687 /* use - 1 here since we just incremented it above */
2688 ckch->chain_certs[ckch->num_chain_certs - 1] = ca;
2689 }
2690 ret = ERR_get_error();
2691 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
2692 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
2693 err && *err ? *err : "", path);
2694 ret = 1;
2695 goto end;
2696 }
2697
2698 ret = 0;
2699
2700end:
2701
2702 ERR_clear_error();
2703 if (in)
2704 BIO_free(in);
2705
2706 /* Something went wrong in one of the reads */
2707 if (ret != 0)
2708 ssl_sock_free_cert_key_and_chain_contents(ckch);
2709
2710 return ret;
2711}
2712
2713/* Loads the info in ckch into ctx
2714 * Currently, this does not process any information about ocsp, dhparams or
2715 * sctl
2716 * Returns
2717 * 0 on success
2718 * 1 on failure
2719 */
2720static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
2721{
2722 int i = 0;
2723
2724 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
2725 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
2726 err && *err ? *err : "", path);
2727 return 1;
2728 }
2729
2730 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
2731 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
2732 err && *err ? *err : "", path);
2733 return 1;
2734 }
2735
yanbzhu488a4d22015-12-01 15:16:07 -05002736 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
2737 for (i = 0; i < ckch->num_chain_certs; i++) {
2738 if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05002739 memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
2740 err && *err ? *err : "", (i+1), path);
yanbzhu488a4d22015-12-01 15:16:07 -05002741 return 1;
2742 }
2743 }
2744
2745 if (SSL_CTX_check_private_key(ctx) <= 0) {
2746 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
2747 err && *err ? *err : "", path);
2748 return 1;
2749 }
2750
2751 return 0;
2752}
2753
yanbzhu08ce6ab2015-12-02 13:01:29 -05002754
2755static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
2756{
2757 struct sni_keytype *s_kt = NULL;
2758 struct ebmb_node *node;
2759 int i;
2760
2761 for (i = 0; i < trash.size; i++) {
2762 if (!str[i])
2763 break;
2764 trash.str[i] = tolower(str[i]);
2765 }
2766 trash.str[i] = 0;
2767 node = ebst_lookup(sni_keytypes, trash.str);
2768 if (!node) {
2769 /* CN not found in tree */
2770 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
2771 /* Using memcpy here instead of strncpy.
2772 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
2773 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
2774 */
2775 memcpy(s_kt->name.key, trash.str, i+1);
2776 s_kt->keytypes = 0;
2777 ebst_insert(sni_keytypes, &s_kt->name);
2778 } else {
2779 /* CN found in tree */
2780 s_kt = container_of(node, struct sni_keytype, name);
2781 }
2782
2783 /* Mark that this CN has the keytype of key_index via keytypes mask */
2784 s_kt->keytypes |= 1<<key_index;
2785
2786}
2787
2788
2789/* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files.
2790 * If any are found, group these files into a set of SSL_CTX*
2791 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
2792 *
2793 * This will allow the user to explictly group multiple cert/keys for a single purpose
2794 *
2795 * Returns
2796 * 0 on success
2797 * 1 on failure
2798 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002799static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
2800 char **sni_filter, int fcount, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05002801{
2802 char fp[MAXPATHLEN+1] = {0};
2803 int n = 0;
2804 int i = 0;
2805 struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} };
2806 struct eb_root sni_keytypes_map = { {0} };
2807 struct ebmb_node *node;
2808 struct ebmb_node *next;
2809 /* Array of SSL_CTX pointers corresponding to each possible combo
2810 * of keytypes
2811 */
2812 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
2813 int rv = 0;
2814 X509_NAME *xname = NULL;
2815 char *str = NULL;
2816#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2817 STACK_OF(GENERAL_NAME) *names = NULL;
2818#endif
2819
2820 /* Load all possible certs and keys */
2821 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
2822 struct stat buf;
2823
2824 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
2825 if (stat(fp, &buf) == 0) {
2826 if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) {
2827 rv = 1;
2828 goto end;
2829 }
2830 }
2831 }
2832
2833 /* Process each ckch and update keytypes for each CN/SAN
2834 * for example, if CN/SAN www.a.com is associated with
2835 * certs with keytype 0 and 2, then at the end of the loop,
2836 * www.a.com will have:
2837 * keyindex = 0 | 1 | 4 = 5
2838 */
2839 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
2840
2841 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
2842 continue;
2843
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02002844 if (fcount) {
Willy Tarreau24b892f2016-06-20 23:01:57 +02002845 for (i = 0; i < fcount; i++)
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02002846 ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
2847 } else {
2848 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
2849 * so the line that contains logic is marked via comments
2850 */
2851 xname = X509_get_subject_name(certs_and_keys[n].cert);
2852 i = -1;
2853 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
2854 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002855 ASN1_STRING *value;
2856 value = X509_NAME_ENTRY_get_data(entry);
2857 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02002858 /* Important line is here */
2859 ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05002860
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02002861 OPENSSL_free(str);
2862 str = NULL;
2863 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05002864 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05002865
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02002866 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05002867#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02002868 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
2869 if (names) {
2870 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
2871 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05002872
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02002873 if (name->type == GEN_DNS) {
2874 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
2875 /* Important line is here */
2876 ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05002877
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02002878 OPENSSL_free(str);
2879 str = NULL;
2880 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05002881 }
2882 }
2883 }
2884 }
2885#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2886 }
2887
2888 /* If no files found, return error */
2889 if (eb_is_empty(&sni_keytypes_map)) {
2890 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
2891 err && *err ? *err : "", path);
2892 rv = 1;
2893 goto end;
2894 }
2895
2896 /* We now have a map of CN/SAN to keytypes that are loaded in
2897 * Iterate through the map to create the SSL_CTX's (if needed)
2898 * and add each CTX to the SNI tree
2899 *
2900 * Some math here:
2901 * There are 2^n - 1 possibile combinations, each unique
2902 * combination is denoted by the key in the map. Each key
2903 * has a value between 1 and 2^n - 1. Conveniently, the array
2904 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
2905 * entry in the array to correspond to the unique combo (key)
2906 * associated with i. This unique key combo (i) will be associated
2907 * with combos[i-1]
2908 */
2909
2910 node = ebmb_first(&sni_keytypes_map);
2911 while (node) {
2912 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00002913 char cur_file[MAXPATHLEN+1];
yanbzhu08ce6ab2015-12-02 13:01:29 -05002914
2915 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
2916 i = container_of(node, struct sni_keytype, name)->keytypes;
2917 cur_ctx = key_combos[i-1].ctx;
2918
2919 if (cur_ctx == NULL) {
2920 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002921 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05002922 if (cur_ctx == NULL) {
2923 memprintf(err, "%sunable to allocate SSL context.\n",
2924 err && *err ? *err : "");
2925 rv = 1;
2926 goto end;
2927 }
2928
yanbzhube2774d2015-12-10 15:07:30 -05002929 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05002930 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
2931 if (i & (1<<n)) {
2932 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00002933 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
2934 if (ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err) != 0) {
yanbzhu08ce6ab2015-12-02 13:01:29 -05002935 SSL_CTX_free(cur_ctx);
2936 rv = 1;
2937 goto end;
2938 }
yanbzhube2774d2015-12-10 15:07:30 -05002939
2940#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
2941 /* Load OCSP Info into context */
Bertrand Jacquin33423092016-11-13 16:37:13 +00002942 if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) {
yanbzhube2774d2015-12-10 15:07:30 -05002943 if (err)
2944 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",
Bertrand Jacquin5424ee02016-11-13 16:37:14 +00002945 *err ? *err : "", cur_file);
yanbzhube2774d2015-12-10 15:07:30 -05002946 SSL_CTX_free(cur_ctx);
2947 rv = 1;
2948 goto end;
2949 }
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02002950#elif (defined OPENSSL_IS_BORINGSSL)
2951 ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file);
yanbzhube2774d2015-12-10 15:07:30 -05002952#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05002953 }
2954 }
2955
2956 /* Load DH params into the ctx to support DHE keys */
2957#ifndef OPENSSL_NO_DH
2958 if (ssl_dh_ptr_index >= 0)
2959 SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL);
2960
2961 rv = ssl_sock_load_dh_params(cur_ctx, NULL);
2962 if (rv < 0) {
2963 if (err)
2964 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
2965 *err ? *err : "", path);
2966 rv = 1;
2967 goto end;
2968 }
2969#endif
2970
2971 /* Update key_combos */
2972 key_combos[i-1].ctx = cur_ctx;
2973 }
2974
2975 /* Update SNI Tree */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002976 key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf,
2977 TLSEXT_signature_anonymous, str, key_combos[i-1].order);
yanbzhu08ce6ab2015-12-02 13:01:29 -05002978 node = ebmb_next(node);
2979 }
2980
2981
2982 /* Mark a default context if none exists, using the ctx that has the most shared keys */
2983 if (!bind_conf->default_ctx) {
2984 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
2985 if (key_combos[i].ctx) {
2986 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002987 bind_conf->default_ssl_conf = ssl_conf;
yanbzhu08ce6ab2015-12-02 13:01:29 -05002988 break;
2989 }
2990 }
2991 }
2992
2993end:
2994
2995 if (names)
2996 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
2997
2998 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
2999 ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]);
3000
3001 node = ebmb_first(&sni_keytypes_map);
3002 while (node) {
3003 next = ebmb_next(node);
3004 ebmb_delete(node);
3005 node = next;
3006 }
3007
3008 return rv;
3009}
3010#else
3011/* This is a dummy, that just logs an error and returns error */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003012static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3013 char **sni_filter, int fcount, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003014{
3015 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3016 err && *err ? *err : "", path, strerror(errno));
3017 return 1;
3018}
3019
yanbzhu488a4d22015-12-01 15:16:07 -05003020#endif /* #if OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
3021
Emeric Brunfc0421f2012-09-07 17:30:07 +02003022/* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if
3023 * an early error happens and the caller must call SSL_CTX_free() by itelf.
3024 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003025static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s,
3026 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003027{
3028 BIO *in;
3029 X509 *x = NULL, *ca;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003030 int i, err;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003031 int ret = -1;
3032 int order = 0;
3033 X509_NAME *xname;
3034 char *str;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003035 pem_password_cb *passwd_cb;
3036 void *passwd_cb_userdata;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003037 EVP_PKEY *pkey;
3038 uint8_t key_sig = TLSEXT_signature_anonymous;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003039
Emeric Brunfc0421f2012-09-07 17:30:07 +02003040#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3041 STACK_OF(GENERAL_NAME) *names;
3042#endif
3043
3044 in = BIO_new(BIO_s_file());
3045 if (in == NULL)
3046 goto end;
3047
3048 if (BIO_read_filename(in, file) <= 0)
3049 goto end;
3050
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003051
3052 passwd_cb = SSL_CTX_get_default_passwd_cb(ctx);
3053 passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx);
3054
3055 x = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003056 if (x == NULL)
3057 goto end;
3058
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003059 pkey = X509_get_pubkey(x);
3060 if (pkey) {
3061 switch(EVP_PKEY_base_id(pkey)) {
3062 case EVP_PKEY_RSA:
3063 key_sig = TLSEXT_signature_rsa;
3064 break;
3065 case EVP_PKEY_EC:
3066 key_sig = TLSEXT_signature_ecdsa;
3067 break;
3068 }
3069 EVP_PKEY_free(pkey);
3070 }
3071
Emeric Brun50bcecc2013-04-22 13:05:23 +02003072 if (fcount) {
3073 while (fcount--)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003074 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, sni_filter[fcount], order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003075 }
3076 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003077#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003078 names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
3079 if (names) {
3080 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3081 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
3082 if (name->type == GEN_DNS) {
3083 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003084 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003085 OPENSSL_free(str);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003086 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003087 }
3088 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003089 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003090 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003091#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003092 xname = X509_get_subject_name(x);
3093 i = -1;
3094 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3095 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003096 ASN1_STRING *value;
3097
3098 value = X509_NAME_ENTRY_get_data(entry);
3099 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003100 order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, key_sig, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003101 OPENSSL_free(str);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003102 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003103 }
3104 }
3105
3106 ret = 0; /* the caller must not free the SSL_CTX argument anymore */
3107 if (!SSL_CTX_use_certificate(ctx, x))
3108 goto end;
3109
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003110#ifdef SSL_CTX_clear_extra_chain_certs
3111 SSL_CTX_clear_extra_chain_certs(ctx);
3112#else
Emeric Brunfc0421f2012-09-07 17:30:07 +02003113 if (ctx->extra_certs != NULL) {
3114 sk_X509_pop_free(ctx->extra_certs, X509_free);
3115 ctx->extra_certs = NULL;
3116 }
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003117#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02003118
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003119 while ((ca = PEM_read_bio_X509(in, NULL, passwd_cb, passwd_cb_userdata))) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003120 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3121 X509_free(ca);
3122 goto end;
3123 }
3124 }
3125
3126 err = ERR_get_error();
3127 if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
3128 /* we successfully reached the last cert in the file */
3129 ret = 1;
3130 }
3131 ERR_clear_error();
3132
3133end:
3134 if (x)
3135 X509_free(x);
3136
3137 if (in)
3138 BIO_free(in);
3139
3140 return ret;
3141}
3142
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003143static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3144 char **sni_filter, int fcount, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003145{
3146 int ret;
3147 SSL_CTX *ctx;
3148
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003149 ctx = SSL_CTX_new(SSLv23_server_method());
Emeric Brunfc0421f2012-09-07 17:30:07 +02003150 if (!ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003151 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3152 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003153 return 1;
3154 }
3155
3156 if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003157 memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n",
3158 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003159 SSL_CTX_free(ctx);
3160 return 1;
3161 }
3162
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003163 ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf, ssl_conf, sni_filter, fcount);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003164 if (ret <= 0) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003165 memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n",
3166 err && *err ? *err : "", path);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003167 if (ret < 0) /* serious error, must do that ourselves */
3168 SSL_CTX_free(ctx);
3169 return 1;
3170 }
Emeric Brun61694ab2012-10-26 13:35:33 +02003171
3172 if (SSL_CTX_check_private_key(ctx) <= 0) {
3173 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
3174 err && *err ? *err : "", path);
3175 return 1;
3176 }
3177
Emeric Brunfc0421f2012-09-07 17:30:07 +02003178 /* we must not free the SSL_CTX anymore below, since it's already in
3179 * the tree, so it will be discovered and cleaned in time.
3180 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003181#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +02003182 /* store a NULL pointer to indicate we have not yet loaded
3183 a custom DH param file */
3184 if (ssl_dh_ptr_index >= 0) {
3185 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3186 }
3187
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003188 ret = ssl_sock_load_dh_params(ctx, path);
3189 if (ret < 0) {
3190 if (err)
3191 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3192 *err ? *err : "", path);
3193 return 1;
3194 }
3195#endif
3196
Lukas Tribuse4e30f72014-12-09 16:32:51 +01003197#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4147b2e2014-06-16 18:36:30 +02003198 ret = ssl_sock_load_ocsp(ctx, path);
3199 if (ret < 0) {
3200 if (err)
3201 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",
3202 *err ? *err : "", path);
3203 return 1;
3204 }
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02003205#elif (defined OPENSSL_IS_BORINGSSL)
3206 ssl_sock_set_ocsp_response_from_file(ctx, path);
Emeric Brun4147b2e2014-06-16 18:36:30 +02003207#endif
3208
Daniel Jakots54ffb912015-11-06 20:02:41 +01003209#if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01003210 if (sctl_ex_index >= 0) {
3211 ret = ssl_sock_load_sctl(ctx, path);
3212 if (ret < 0) {
3213 if (err)
3214 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
3215 *err ? *err : "", path);
3216 return 1;
3217 }
3218 }
3219#endif
3220
Emeric Brunfc0421f2012-09-07 17:30:07 +02003221#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003222 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003223 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
3224 err && *err ? *err : "");
Emeric Brunfc0421f2012-09-07 17:30:07 +02003225 return 1;
3226 }
3227#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003228 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003229 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003230 bind_conf->default_ssl_conf = ssl_conf;
3231 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003232
3233 return 0;
3234}
3235
Willy Tarreau03209342016-12-22 17:08:28 +01003236int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003237{
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003238 struct dirent **de_list;
3239 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003240 DIR *dir;
3241 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01003242 char *end;
3243 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02003244 int cfgerr = 0;
yanbzhu63ea8462015-12-09 13:35:14 -05003245#if OPENSSL_VERSION_NUMBER >= 0x1000200fL
3246 int is_bundle;
3247 int j;
3248#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02003249
yanbzhu08ce6ab2015-12-02 13:01:29 -05003250 if (stat(path, &buf) == 0) {
3251 dir = opendir(path);
3252 if (!dir)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003253 return ssl_sock_load_cert_file(path, bind_conf, NULL, NULL, 0, err);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003254
yanbzhu08ce6ab2015-12-02 13:01:29 -05003255 /* strip trailing slashes, including first one */
3256 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
3257 *end = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003258
yanbzhu08ce6ab2015-12-02 13:01:29 -05003259 n = scandir(path, &de_list, 0, alphasort);
3260 if (n < 0) {
3261 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
3262 err && *err ? *err : "", path, strerror(errno));
3263 cfgerr++;
3264 }
3265 else {
3266 for (i = 0; i < n; i++) {
3267 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02003268
yanbzhu08ce6ab2015-12-02 13:01:29 -05003269 end = strrchr(de->d_name, '.');
3270 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl")))
3271 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003272
yanbzhu08ce6ab2015-12-02 13:01:29 -05003273 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
3274 if (stat(fp, &buf) != 0) {
3275 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3276 err && *err ? *err : "", fp, strerror(errno));
3277 cfgerr++;
3278 goto ignore_entry;
3279 }
3280 if (!S_ISREG(buf.st_mode))
3281 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05003282
3283#if OPENSSL_VERSION_NUMBER >= 0x1000200fL
3284 is_bundle = 0;
3285 /* Check if current entry in directory is part of a multi-cert bundle */
3286
3287 if (end) {
3288 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
3289 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
3290 is_bundle = 1;
3291 break;
3292 }
3293 }
3294
3295 if (is_bundle) {
3296 char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */
3297 int dp_len;
3298
3299 dp_len = end - de->d_name;
3300 snprintf(dp, dp_len + 1, "%s", de->d_name);
3301
3302 /* increment i and free de until we get to a non-bundle cert
3303 * Note here that we look at de_list[i + 1] before freeing de
3304 * this is important since ignore_entry will free de
3305 */
3306 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) {
3307 free(de);
3308 i++;
3309 de = de_list[i];
3310 }
3311
3312 snprintf(fp, sizeof(fp), "%s/%s", path, dp);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003313 ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err);
yanbzhu63ea8462015-12-09 13:35:14 -05003314
3315 /* Successfully processed the bundle */
3316 goto ignore_entry;
3317 }
3318 }
3319
3320#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003321 cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003322ignore_entry:
3323 free(de);
Cyril Bonté3180f7b2015-01-25 00:16:08 +01003324 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003325 free(de_list);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003326 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003327 closedir(dir);
3328 return cfgerr;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003329 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003330
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003331 cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003332
Emeric Brunfc0421f2012-09-07 17:30:07 +02003333 return cfgerr;
3334}
3335
Thierry Fournier383085f2013-01-24 14:15:43 +01003336/* Make sure openssl opens /dev/urandom before the chroot. The work is only
3337 * done once. Zero is returned if the operation fails. No error is returned
3338 * if the random is said as not implemented, because we expect that openssl
3339 * will use another method once needed.
3340 */
3341static int ssl_initialize_random()
3342{
3343 unsigned char random;
3344 static int random_initialized = 0;
3345
3346 if (!random_initialized && RAND_bytes(&random, 1) != 0)
3347 random_initialized = 1;
3348
3349 return random_initialized;
3350}
3351
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003352/* release ssl bind conf */
3353void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003354{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003355 if (conf) {
3356#ifdef OPENSSL_NPN_NEGOTIATED
3357 free(conf->npn_str);
3358 conf->npn_str = NULL;
3359#endif
3360#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
3361 free(conf->alpn_str);
3362 conf->alpn_str = NULL;
3363#endif
3364 free(conf->ca_file);
3365 conf->ca_file = NULL;
3366 free(conf->crl_file);
3367 conf->crl_file = NULL;
3368 free(conf->ciphers);
3369 conf->ciphers = NULL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01003370 free(conf->curves);
3371 conf->curves = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003372 free(conf->ecdhe);
3373 conf->ecdhe = NULL;
3374 }
3375}
3376
3377int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
3378{
3379 char thisline[CRT_LINESIZE];
3380 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003381 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05003382 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003383 int linenum = 0;
3384 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003385
Willy Tarreauad1731d2013-04-02 17:35:58 +02003386 if ((f = fopen(file, "r")) == NULL) {
3387 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003388 return 1;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003389 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003390
3391 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003392 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003393 char *end;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003394 char *args[MAX_CRT_ARGS + 1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003395 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003396 char *crt_path;
3397 struct ssl_bind_conf *ssl_conf = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003398
3399 linenum++;
3400 end = line + strlen(line);
3401 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
3402 /* Check if we reached the limit and the last char is not \n.
3403 * Watch out for the last line without the terminating '\n'!
3404 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02003405 memprintf(err, "line %d too long in file '%s', limit is %d characters",
3406 linenum, file, (int)sizeof(thisline)-1);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003407 cfgerr = 1;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003408 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003409 }
3410
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003411 arg = 0;
Emeric Brun50bcecc2013-04-22 13:05:23 +02003412 newarg = 1;
3413 while (*line) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003414 if (*line == '#' || *line == '\n' || *line == '\r') {
3415 /* end of string, end of loop */
3416 *line = 0;
3417 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003418 } else if (isspace(*line)) {
Emeric Brun50bcecc2013-04-22 13:05:23 +02003419 newarg = 1;
3420 *line = 0;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003421 } else if (*line == '[') {
3422 if (ssl_b) {
3423 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
3424 cfgerr = 1;
3425 break;
3426 }
3427 if (!arg) {
3428 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
3429 cfgerr = 1;
3430 break;
3431 }
3432 ssl_b = arg;
3433 newarg = 1;
3434 *line = 0;
3435 } else if (*line == ']') {
3436 if (ssl_e) {
3437 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
Emeric Brun50bcecc2013-04-22 13:05:23 +02003438 cfgerr = 1;
3439 break;
3440 }
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003441 if (!ssl_b) {
3442 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
3443 cfgerr = 1;
3444 break;
3445 }
3446 ssl_e = arg;
3447 newarg = 1;
3448 *line = 0;
3449 } else if (newarg) {
3450 if (arg == MAX_CRT_ARGS) {
3451 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
3452 cfgerr = 1;
3453 break;
3454 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02003455 newarg = 0;
3456 args[arg++] = line;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003457 }
Emeric Brun50bcecc2013-04-22 13:05:23 +02003458 line++;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003459 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003460 if (cfgerr)
3461 break;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003462 args[arg++] = line;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003463
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003464 /* empty line */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003465 if (!*args[0])
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003466 continue;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003467
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003468 crt_path = args[0];
3469 if (*crt_path != '/' && global_ssl.crt_base) {
3470 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
3471 memprintf(err, "'%s' : path too long on line %d in file '%s'",
3472 crt_path, linenum, file);
3473 cfgerr = 1;
3474 break;
3475 }
3476 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
3477 crt_path = path;
3478 }
3479
3480 ssl_conf = calloc(1, sizeof *ssl_conf);
3481 cur_arg = ssl_b ? ssl_b : 1;
3482 while (cur_arg < ssl_e) {
3483 newarg = 0;
3484 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
3485 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
3486 newarg = 1;
3487 cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err);
3488 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
3489 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
3490 args[cur_arg], linenum, file);
3491 cfgerr = 1;
3492 }
3493 cur_arg += 1 + ssl_bind_kws[i].skip;
3494 break;
3495 }
3496 }
3497 if (!cfgerr && !newarg) {
3498 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
3499 args[cur_arg], linenum, file);
3500 cfgerr = 1;
3501 break;
3502 }
3503 }
3504 if (cfgerr) {
3505 ssl_sock_free_ssl_conf(ssl_conf);
3506 free(ssl_conf);
3507 ssl_conf = NULL;
3508 break;
3509 }
3510
3511 if (stat(crt_path, &buf) == 0) {
3512 cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf,
3513 &args[cur_arg], arg - cur_arg - 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05003514 } else {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003515 cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf,
3516 &args[cur_arg], arg - cur_arg - 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05003517 }
3518
Willy Tarreauad1731d2013-04-02 17:35:58 +02003519 if (cfgerr) {
3520 memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003521 break;
Willy Tarreauad1731d2013-04-02 17:35:58 +02003522 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003523 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003524 fclose(f);
3525 return cfgerr;
3526}
3527
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003528/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003529static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003530ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003531{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003532 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003533 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003534 SSL_OP_ALL | /* all known workarounds for bugs */
3535 SSL_OP_NO_SSLv2 |
3536 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003537 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02003538 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003539 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
3540 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003541 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003542 SSL_MODE_ENABLE_PARTIAL_WRITE |
3543 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01003544 SSL_MODE_RELEASE_BUFFERS |
3545 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003546 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003547 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003548 int flags = MC_SSL_O_ALL;
3549 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003550
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003551 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003552 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003553
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003554 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
3555 Warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
3556 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3557 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
3558 else
3559 flags = conf_ssl_methods->flags;
3560
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003561 min = conf_ssl_methods->min;
3562 max = conf_ssl_methods->max;
3563 /* start with TLSv10 to remove SSLv3 per default */
3564 if (!min && (!max || max >= CONF_TLSV10))
3565 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003566 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003567 if (min)
3568 flags |= (methodVersions[min].flag - 1);
3569 if (max)
3570 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003571 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003572 min = max = CONF_TLSV_NONE;
3573 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003574 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003575 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003576 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003577 if (min) {
3578 if (hole) {
3579 Warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02003580 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003581 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
3582 methodVersions[hole].name);
3583 hole = 0;
3584 }
3585 max = i;
3586 }
3587 else {
3588 min = max = i;
3589 }
3590 }
3591 else {
3592 if (min)
3593 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003594 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003595 if (!min) {
3596 Alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003597 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003598 cfgerr += 1;
3599 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02003600 /* save real min/max in bind_conf */
3601 conf_ssl_methods->min = min;
3602 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003603
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003604#if (OPENSSL_VERSION_NUMBER < 0x1010000fL) && !defined(OPENSSL_IS_BORINGSSL)
3605 /* Keep force-xxx implementation as it is in older haproxy. It's a
3606 precautionary measure to avoid any suprise with older openssl version. */
3607 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003608 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003609 else
3610 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
3611 if (flags & methodVersions[i].flag)
3612 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003613#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003614 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003615 methodVersions[min].ctx_set_version(ctx, SET_MIN);
3616 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02003617#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003618
3619 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
3620 options |= SSL_OP_NO_TICKET;
3621 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
3622 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
3623 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00003624
3625#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
3626 if (global_ssl.async)
3627 mode |= SSL_MODE_ASYNC;
3628#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003629 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003630 if (global_ssl.life_time)
3631 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003632
3633#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3634#ifdef OPENSSL_IS_BORINGSSL
3635 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
3636 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
3637#else
3638 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
3639 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
3640#endif
3641#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003642 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003643}
3644
3645int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx)
3646{
3647 struct proxy *curproxy = bind_conf->frontend;
3648 int cfgerr = 0;
3649 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003650 struct ssl_bind_conf *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003651 const char *conf_ciphers;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01003652 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003653
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02003654 if (ssl_conf) {
3655 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
3656 int i, min, max;
3657 int flags = MC_SSL_O_ALL;
3658
3659 /* Real min and max should be determinate with configuration and openssl's capabilities */
3660 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_methods.min;
3661 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_methods.max;
3662 if (min)
3663 flags |= (methodVersions[min].flag - 1);
3664 if (max)
3665 flags |= ~((methodVersions[max].flag << 1) - 1);
3666 min = max = CONF_TLSV_NONE;
3667 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
3668 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
3669 if (min)
3670 max = i;
3671 else
3672 min = max = i;
3673 }
3674 /* save real min/max */
3675 conf_ssl_methods->min = min;
3676 conf_ssl_methods->max = max;
3677 if (!min) {
3678 Alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
3679 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
3680 cfgerr += 1;
3681 }
3682 }
3683
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003684 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01003685 case SSL_SOCK_VERIFY_NONE:
3686 verify = SSL_VERIFY_NONE;
3687 break;
3688 case SSL_SOCK_VERIFY_OPTIONAL:
3689 verify = SSL_VERIFY_PEER;
3690 break;
3691 case SSL_SOCK_VERIFY_REQUIRED:
3692 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
3693 break;
3694 }
3695 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
3696 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003697 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
3698 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
3699 if (ca_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02003700 /* load CAfile to verify */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003701 if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02003702 Alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n",
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003703 curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02003704 cfgerr++;
3705 }
3706 /* set CA names fo client cert request, function returns void */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003707 SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file));
Emeric Brund94b3fe2012-09-20 18:23:56 +02003708 }
Emeric Brun850efd52014-01-29 12:24:34 +01003709 else {
3710 Alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
3711 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
3712 cfgerr++;
3713 }
Emeric Brun051cdab2012-10-02 19:25:50 +02003714#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003715 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02003716 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
3717
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003718 if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02003719 Alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003720 curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brund94b3fe2012-09-20 18:23:56 +02003721 cfgerr++;
3722 }
Emeric Brun561e5742012-10-02 15:20:55 +02003723 else {
3724 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
3725 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02003726 }
Emeric Brun051cdab2012-10-02 19:25:50 +02003727#endif
Emeric Brun644cde02012-12-14 11:21:13 +01003728 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02003729 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01003730#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02003731 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01003732 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
3733 Alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
3734 curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
3735 cfgerr++;
3736 }
3737 }
3738#endif
3739
Emeric Brunfc0421f2012-09-07 17:30:07 +02003740 shared_context_set_cache(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003741 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
3742 if (conf_ciphers &&
3743 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003744 Alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003745 curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003746 cfgerr++;
3747 }
3748
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01003749#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02003750 /* If tune.ssl.default-dh-param has not been set,
3751 neither has ssl-default-dh-file and no static DH
3752 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01003753 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02003754 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02003755 (ssl_dh_ptr_index == -1 ||
3756 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01003757 STACK_OF(SSL_CIPHER) * ciphers = NULL;
3758 const SSL_CIPHER * cipher = NULL;
3759 char cipher_description[128];
3760 /* The description of ciphers using an Ephemeral Diffie Hellman key exchange
3761 contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/",
3762 which is not ephemeral DH. */
3763 const char dhe_description[] = " Kx=DH ";
3764 const char dhe_export_description[] = " Kx=DH(";
3765 int idx = 0;
3766 int dhe_found = 0;
3767 SSL *ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02003768
Remi Gacogne23d5d372014-10-10 17:04:26 +02003769 ssl = SSL_new(ctx);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003770
Remi Gacogne23d5d372014-10-10 17:04:26 +02003771 if (ssl) {
3772 ciphers = SSL_get_ciphers(ssl);
3773
3774 if (ciphers) {
3775 for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) {
3776 cipher = sk_SSL_CIPHER_value(ciphers, idx);
3777 if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) {
3778 if (strstr(cipher_description, dhe_description) != NULL ||
3779 strstr(cipher_description, dhe_export_description) != NULL) {
3780 dhe_found = 1;
3781 break;
3782 }
Remi Gacognec1eab8c2014-06-12 18:20:11 +02003783 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003784 }
3785 }
Remi Gacogne23d5d372014-10-10 17:04:26 +02003786 SSL_free(ssl);
3787 ssl = NULL;
Lukas Tribus90132722014-08-18 00:56:33 +02003788 }
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003789
Lukas Tribus90132722014-08-18 00:56:33 +02003790 if (dhe_found) {
3791 Warning("Setting 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");
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003792 }
3793
Willy Tarreauef934602016-12-22 23:12:01 +01003794 global_ssl.default_dh_param = 1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003795 }
Remi Gacogne8de54152014-07-15 11:36:40 +02003796
Willy Tarreauef934602016-12-22 23:12:01 +01003797 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02003798 if (local_dh_1024 == NULL) {
3799 local_dh_1024 = ssl_get_dh_1024();
3800 }
Willy Tarreauef934602016-12-22 23:12:01 +01003801 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02003802 if (local_dh_2048 == NULL) {
3803 local_dh_2048 = ssl_get_dh_2048();
3804 }
Willy Tarreauef934602016-12-22 23:12:01 +01003805 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02003806 if (local_dh_4096 == NULL) {
3807 local_dh_4096 = ssl_get_dh_4096();
3808 }
Remi Gacogne8de54152014-07-15 11:36:40 +02003809 }
3810 }
3811 }
3812#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003813
Emeric Brunfc0421f2012-09-07 17:30:07 +02003814 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02003815#if OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02003816 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02003817#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02003818
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02003819#ifdef OPENSSL_NPN_NEGOTIATED
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003820 ssl_conf_cur = NULL;
3821 if (ssl_conf && ssl_conf->npn_str)
3822 ssl_conf_cur = ssl_conf;
3823 else if (bind_conf->ssl_conf.npn_str)
3824 ssl_conf_cur = &bind_conf->ssl_conf;
3825 if (ssl_conf_cur)
3826 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02003827#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01003828#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003829 ssl_conf_cur = NULL;
3830 if (ssl_conf && ssl_conf->alpn_str)
3831 ssl_conf_cur = ssl_conf;
3832 else if (bind_conf->ssl_conf.alpn_str)
3833 ssl_conf_cur = &bind_conf->ssl_conf;
3834 if (ssl_conf_cur)
3835 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02003836#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01003837#if OPENSSL_VERSION_NUMBER >= 0x1000200fL
3838 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
3839 if (conf_curves) {
3840 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
3841 Alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
3842 curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
3843 cfgerr++;
3844 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01003845#if defined(SSL_CTX_set_ecdh_auto)
3846 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
3847#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01003848 }
3849#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02003850#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01003851 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02003852 int i;
3853 EC_KEY *ecdh;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003854 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
3855 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Emeric Brun2b58d042012-09-20 17:10:03 +02003856
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003857 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02003858 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
3859 Alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003860 curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
Emeric Brun2b58d042012-09-20 17:10:03 +02003861 cfgerr++;
3862 }
3863 else {
3864 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
3865 EC_KEY_free(ecdh);
3866 }
3867 }
3868#endif
3869
Emeric Brunfc0421f2012-09-07 17:30:07 +02003870 return cfgerr;
3871}
3872
Evan Broderbe554312013-06-27 00:05:25 -07003873static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
3874{
3875 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
3876 size_t prefixlen, suffixlen;
3877
3878 /* Trivial case */
3879 if (strcmp(pattern, hostname) == 0)
3880 return 1;
3881
Evan Broderbe554312013-06-27 00:05:25 -07003882 /* The rest of this logic is based on RFC 6125, section 6.4.3
3883 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
3884
Emeric Bruna848dae2013-10-08 11:27:28 +02003885 pattern_wildcard = NULL;
3886 pattern_left_label_end = pattern;
3887 while (*pattern_left_label_end != '.') {
3888 switch (*pattern_left_label_end) {
3889 case 0:
3890 /* End of label not found */
3891 return 0;
3892 case '*':
3893 /* If there is more than one wildcards */
3894 if (pattern_wildcard)
3895 return 0;
3896 pattern_wildcard = pattern_left_label_end;
3897 break;
3898 }
3899 pattern_left_label_end++;
3900 }
3901
3902 /* If it's not trivial and there is no wildcard, it can't
3903 * match */
3904 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07003905 return 0;
3906
3907 /* Make sure all labels match except the leftmost */
3908 hostname_left_label_end = strchr(hostname, '.');
3909 if (!hostname_left_label_end
3910 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
3911 return 0;
3912
3913 /* Make sure the leftmost label of the hostname is long enough
3914 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02003915 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07003916 return 0;
3917
3918 /* Finally compare the string on either side of the
3919 * wildcard */
3920 prefixlen = pattern_wildcard - pattern;
3921 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02003922 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
3923 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07003924 return 0;
3925
3926 return 1;
3927}
3928
3929static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
3930{
3931 SSL *ssl;
3932 struct connection *conn;
Willy Tarreau2ab88672017-07-05 18:23:03 +02003933 const char *servername;
Evan Broderbe554312013-06-27 00:05:25 -07003934
3935 int depth;
3936 X509 *cert;
3937 STACK_OF(GENERAL_NAME) *alt_names;
3938 int i;
3939 X509_NAME *cert_subject;
3940 char *str;
3941
3942 if (ok == 0)
3943 return ok;
3944
3945 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003946 conn = SSL_get_app_data(ssl);
Evan Broderbe554312013-06-27 00:05:25 -07003947
Willy Tarreau2ab88672017-07-05 18:23:03 +02003948 /* we're checking against the configured "verifyhost" directive if
3949 * present, or against the SNI used on this connection if present.
3950 * If neither is set, the verification is OK.
3951 */
Evan Broderbe554312013-06-27 00:05:25 -07003952 servername = objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02003953 if (!servername) {
3954 SSL_SESSION *ssl_sess = SSL_get_session(conn->xprt_ctx);
3955 if (!ssl_sess)
3956 return ok;
3957
3958 servername = SSL_SESSION_get0_hostname(ssl_sess);
3959 if (!servername)
3960 return ok;
3961 }
Evan Broderbe554312013-06-27 00:05:25 -07003962
3963 /* We only need to verify the CN on the actual server cert,
3964 * not the indirect CAs */
3965 depth = X509_STORE_CTX_get_error_depth(ctx);
3966 if (depth != 0)
3967 return ok;
3968
3969 /* At this point, the cert is *not* OK unless we can find a
3970 * hostname match */
3971 ok = 0;
3972
3973 cert = X509_STORE_CTX_get_current_cert(ctx);
3974 /* It seems like this might happen if verify peer isn't set */
3975 if (!cert)
3976 return ok;
3977
3978 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
3979 if (alt_names) {
3980 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
3981 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
3982 if (name->type == GEN_DNS) {
Emeric Bruna33410c2013-09-17 15:47:48 +02003983#if OPENSSL_VERSION_NUMBER < 0x00907000L
3984 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
3985#else
Evan Broderbe554312013-06-27 00:05:25 -07003986 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02003987#endif
Evan Broderbe554312013-06-27 00:05:25 -07003988 ok = ssl_sock_srv_hostcheck(str, servername);
3989 OPENSSL_free(str);
3990 }
3991 }
3992 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02003993 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07003994 }
3995
3996 cert_subject = X509_get_subject_name(cert);
3997 i = -1;
3998 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
3999 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004000 ASN1_STRING *value;
4001 value = X509_NAME_ENTRY_get_data(entry);
4002 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07004003 ok = ssl_sock_srv_hostcheck(str, servername);
4004 OPENSSL_free(str);
4005 }
4006 }
4007
4008 return ok;
4009}
4010
Emeric Brun94324a42012-10-11 14:00:19 +02004011/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01004012int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02004013{
Willy Tarreau03209342016-12-22 17:08:28 +01004014 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02004015 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004016 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02004017 SSL_OP_ALL | /* all known workarounds for bugs */
4018 SSL_OP_NO_SSLv2 |
4019 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004020 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02004021 SSL_MODE_ENABLE_PARTIAL_WRITE |
4022 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004023 SSL_MODE_RELEASE_BUFFERS |
4024 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01004025 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004026 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004027 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004028 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004029 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02004030
Thierry Fournier383085f2013-01-24 14:15:43 +01004031 /* Make sure openssl opens /dev/urandom before the chroot */
4032 if (!ssl_initialize_random()) {
4033 Alert("OpenSSL random data generator initialization failed.\n");
4034 cfgerr++;
4035 }
4036
Willy Tarreaufce03112015-01-15 21:32:40 +01004037 /* Automatic memory computations need to know we use SSL there */
4038 global.ssl_used_backend = 1;
4039
4040 /* Initiate SSL context for current server */
Emeric Brun94324a42012-10-11 14:00:19 +02004041 srv->ssl_ctx.reused_sess = NULL;
4042 if (srv->use_ssl)
4043 srv->xprt = &ssl_sock;
4044 if (srv->check.use_ssl)
Cyril Bonté9ce13112014-11-15 22:41:27 +01004045 srv->check.xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02004046
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004047 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004048 if (!ctx) {
Emeric Brun94324a42012-10-11 14:00:19 +02004049 Alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
4050 proxy_type_str(curproxy), curproxy->id,
4051 srv->id);
4052 cfgerr++;
4053 return cfgerr;
4054 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004055
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004056 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
4057 Warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
4058 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4059 proxy_type_str(curproxy), curproxy->id, srv->id);
4060 else
4061 flags = conf_ssl_methods->flags;
4062
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004063 /* Real min and max should be determinate with configuration and openssl's capabilities */
4064 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004065 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004066 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004067 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004068
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004069 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004070 min = max = CONF_TLSV_NONE;
4071 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004072 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004073 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004074 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004075 if (min) {
4076 if (hole) {
4077 Warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02004078 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004079 proxy_type_str(curproxy), curproxy->id, srv->id,
4080 methodVersions[hole].name);
4081 hole = 0;
4082 }
4083 max = i;
4084 }
4085 else {
4086 min = max = i;
4087 }
4088 }
4089 else {
4090 if (min)
4091 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004092 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004093 if (!min) {
4094 Alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
4095 proxy_type_str(curproxy), curproxy->id, srv->id);
4096 cfgerr += 1;
4097 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004098
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004099#if (OPENSSL_VERSION_NUMBER < 0x1010000fL) && !defined(OPENSSL_IS_BORINGSSL)
4100 /* Keep force-xxx implementation as it is in older haproxy. It's a
4101 precautionary measure to avoid any suprise with older openssl version. */
4102 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004103 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004104 else
4105 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4106 if (flags & methodVersions[i].flag)
4107 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004108#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004109 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004110 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4111 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004112#endif
4113
4114 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
4115 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004116 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004117
4118#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
4119 if (global_ssl.async)
4120 mode |= SSL_MODE_ASYNC;
4121#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004122 SSL_CTX_set_mode(ctx, mode);
4123 srv->ssl_ctx.ctx = ctx;
4124
Emeric Bruna7aa3092012-10-26 12:58:00 +02004125 if (srv->ssl_ctx.client_crt) {
4126 if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) {
4127 Alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
4128 proxy_type_str(curproxy), curproxy->id,
4129 srv->id, srv->ssl_ctx.client_crt);
4130 cfgerr++;
4131 }
4132 else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) {
4133 Alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
4134 proxy_type_str(curproxy), curproxy->id,
4135 srv->id, srv->ssl_ctx.client_crt);
4136 cfgerr++;
4137 }
4138 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
4139 Alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
4140 proxy_type_str(curproxy), curproxy->id,
4141 srv->id, srv->ssl_ctx.client_crt);
4142 cfgerr++;
4143 }
4144 }
Emeric Brun94324a42012-10-11 14:00:19 +02004145
Emeric Brun850efd52014-01-29 12:24:34 +01004146 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
4147 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01004148 switch (srv->ssl_ctx.verify) {
4149 case SSL_SOCK_VERIFY_NONE:
4150 verify = SSL_VERIFY_NONE;
4151 break;
4152 case SSL_SOCK_VERIFY_REQUIRED:
4153 verify = SSL_VERIFY_PEER;
4154 break;
4155 }
Evan Broderbe554312013-06-27 00:05:25 -07004156 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01004157 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02004158 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01004159 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02004160 if (srv->ssl_ctx.ca_file) {
4161 /* load CAfile to verify */
4162 if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) {
Willy Tarreau07ba08b2014-02-16 19:22:08 +01004163 Alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n",
Emeric Brunef42d922012-10-11 16:11:36 +02004164 curproxy->id, srv->id,
4165 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
4166 cfgerr++;
4167 }
4168 }
Emeric Brun850efd52014-01-29 12:24:34 +01004169 else {
4170 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Willy Tarreau07ba08b2014-02-16 19:22:08 +01004171 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",
Emeric Brun850efd52014-01-29 12:24:34 +01004172 curproxy->id, srv->id,
4173 srv->conf.file, srv->conf.line);
4174 else
Willy Tarreau07ba08b2014-02-16 19:22:08 +01004175 Alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
Emeric Brun850efd52014-01-29 12:24:34 +01004176 curproxy->id, srv->id,
4177 srv->conf.file, srv->conf.line);
4178 cfgerr++;
4179 }
Emeric Brunef42d922012-10-11 16:11:36 +02004180#ifdef X509_V_FLAG_CRL_CHECK
4181 if (srv->ssl_ctx.crl_file) {
4182 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
4183
4184 if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) {
Willy Tarreau07ba08b2014-02-16 19:22:08 +01004185 Alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
Emeric Brunef42d922012-10-11 16:11:36 +02004186 curproxy->id, srv->id,
4187 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
4188 cfgerr++;
4189 }
4190 else {
4191 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4192 }
4193 }
4194#endif
4195 }
4196
Emeric Brun94324a42012-10-11 14:00:19 +02004197 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_OFF);
4198 if (srv->ssl_ctx.ciphers &&
4199 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
4200 Alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
4201 curproxy->id, srv->id,
4202 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
4203 cfgerr++;
4204 }
4205
4206 return cfgerr;
4207}
4208
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004209/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004210 * be NULL, in which case nothing is done. Returns the number of errors
4211 * encountered.
4212 */
Willy Tarreau03209342016-12-22 17:08:28 +01004213int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004214{
4215 struct ebmb_node *node;
4216 struct sni_ctx *sni;
4217 int err = 0;
4218
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004219 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004220 return 0;
4221
Willy Tarreaufce03112015-01-15 21:32:40 +01004222 /* Automatic memory computations need to know we use SSL there */
4223 global.ssl_used_frontend = 1;
4224
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004225 /* Make sure openssl opens /dev/urandom before the chroot */
4226 if (!ssl_initialize_random()) {
4227 Alert("OpenSSL random data generator initialization failed.\n");
4228 err++;
4229 }
4230 /* Create initial_ctx used to start the ssl connection before do switchctx */
4231 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004232 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004233 /* It should not be necessary to call this function, but it's
4234 necessary first to check and move all initialisation related
4235 to initial_ctx in ssl_sock_initial_ctx. */
4236 err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx);
4237 }
Emeric Brun0bed9942014-10-30 19:25:24 +01004238 if (bind_conf->default_ctx)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004239 err += ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx);
Emeric Brun0bed9942014-10-30 19:25:24 +01004240
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004241 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004242 while (node) {
4243 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004244 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4245 /* only initialize the CTX on its first occurrence and
4246 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004247 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004248 node = ebmb_next(node);
4249 }
4250
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004251 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004252 while (node) {
4253 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004254 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4255 /* only initialize the CTX on its first occurrence and
4256 if it is not the default_ctx */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004257 err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004258 node = ebmb_next(node);
4259 }
4260 return err;
4261}
4262
Willy Tarreau55d37912016-12-21 23:38:39 +01004263/* Prepares all the contexts for a bind_conf and allocates the shared SSL
4264 * context if needed. Returns < 0 on error, 0 on success. The warnings and
4265 * alerts are directly emitted since the rest of the stack does it below.
4266 */
4267int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
4268{
4269 struct proxy *px = bind_conf->frontend;
4270 int alloc_ctx;
4271 int err;
4272
4273 if (!bind_conf->is_ssl) {
4274 if (bind_conf->default_ctx) {
4275 Warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
4276 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
4277 }
4278 return 0;
4279 }
4280 if (!bind_conf->default_ctx) {
4281 Alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
4282 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
4283 return -1;
4284 }
4285
Willy Tarreauef934602016-12-22 23:12:01 +01004286 alloc_ctx = shared_context_init(global.tune.sslcachesize, (!global_ssl.private_cache && (global.nbproc > 1)) ? 1 : 0);
Willy Tarreau55d37912016-12-21 23:38:39 +01004287 if (alloc_ctx < 0) {
4288 if (alloc_ctx == SHCTX_E_INIT_LOCK)
4289 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");
4290 else
4291 Alert("Unable to allocate SSL session cache.\n");
4292 return -1;
4293 }
4294
4295 err = 0;
4296 /* initialize all certificate contexts */
4297 err += ssl_sock_prepare_all_ctx(bind_conf);
4298
4299 /* initialize CA variables if the certificates generation is enabled */
4300 err += ssl_sock_load_ca(bind_conf);
4301
4302 return -err;
4303}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02004304
4305/* release ssl context allocated for servers. */
4306void ssl_sock_free_srv_ctx(struct server *srv)
4307{
4308 if (srv->ssl_ctx.ctx)
4309 SSL_CTX_free(srv->ssl_ctx.ctx);
4310}
4311
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004312/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004313 * be NULL, in which case nothing is done. The default_ctx is nullified too.
4314 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004315void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004316{
4317 struct ebmb_node *node, *back;
4318 struct sni_ctx *sni;
4319
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004320 if (!bind_conf || !bind_conf->is_ssl)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004321 return;
4322
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004323 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004324 while (node) {
4325 sni = ebmb_entry(node, struct sni_ctx, name);
4326 back = ebmb_next(node);
4327 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004328 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02004329 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004330 ssl_sock_free_ssl_conf(sni->conf);
4331 free(sni->conf);
4332 sni->conf = NULL;
4333 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004334 free(sni);
4335 node = back;
4336 }
4337
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004338 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004339 while (node) {
4340 sni = ebmb_entry(node, struct sni_ctx, name);
4341 back = ebmb_next(node);
4342 ebmb_delete(node);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004343 if (!sni->order) { /* only free the CTX on its first occurrence */
Emeric Brunfc0421f2012-09-07 17:30:07 +02004344 SSL_CTX_free(sni->ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004345 ssl_sock_free_ssl_conf(sni->conf);
4346 free(sni->conf);
4347 sni->conf = NULL;
4348 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004349 free(sni);
4350 node = back;
4351 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004352 SSL_CTX_free(bind_conf->initial_ctx);
4353 bind_conf->initial_ctx = NULL;
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004354 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004355 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02004356}
4357
Willy Tarreau795cdab2016-12-22 17:30:54 +01004358/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
4359void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
4360{
4361 ssl_sock_free_ca(bind_conf);
4362 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004363 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01004364 free(bind_conf->ca_sign_file);
4365 free(bind_conf->ca_sign_pass);
Willy Tarreau795cdab2016-12-22 17:30:54 +01004366 if (bind_conf->keys_ref) {
4367 free(bind_conf->keys_ref->filename);
4368 free(bind_conf->keys_ref->tlskeys);
4369 LIST_DEL(&bind_conf->keys_ref->list);
4370 free(bind_conf->keys_ref);
4371 }
4372 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01004373 bind_conf->ca_sign_pass = NULL;
4374 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01004375}
4376
Christopher Faulet31af49d2015-06-09 17:29:50 +02004377/* Load CA cert file and private key used to generate certificates */
4378int
Willy Tarreau03209342016-12-22 17:08:28 +01004379ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02004380{
Willy Tarreau03209342016-12-22 17:08:28 +01004381 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004382 FILE *fp;
4383 X509 *cacert = NULL;
4384 EVP_PKEY *capkey = NULL;
4385 int err = 0;
4386
4387 if (!bind_conf || !bind_conf->generate_certs)
4388 return err;
4389
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004390#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreauef934602016-12-22 23:12:01 +01004391 if (global_ssl.ctx_cache)
4392 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Christopher Fauletd2cab922015-07-28 16:03:47 +02004393 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Willy Tarreaua84c2672015-10-09 12:10:13 +02004394#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02004395
Christopher Faulet31af49d2015-06-09 17:29:50 +02004396 if (!bind_conf->ca_sign_file) {
4397 Alert("Proxy '%s': cannot enable certificate generation, "
4398 "no CA certificate File configured at [%s:%d].\n",
4399 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004400 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004401 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02004402
4403 /* read in the CA certificate */
4404 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
4405 Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
4406 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004407 goto load_error;
4408 }
4409 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
4410 Alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
4411 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004412 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004413 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004414 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004415 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
4416 Alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
4417 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004418 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004419 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02004420
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004421 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004422 bind_conf->ca_sign_cert = cacert;
4423 bind_conf->ca_sign_pkey = capkey;
4424 return err;
4425
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004426 read_error:
4427 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004428 if (capkey) EVP_PKEY_free(capkey);
4429 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004430 load_error:
4431 bind_conf->generate_certs = 0;
4432 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004433 return err;
4434}
4435
4436/* Release CA cert and private key used to generate certificated */
4437void
4438ssl_sock_free_ca(struct bind_conf *bind_conf)
4439{
4440 if (!bind_conf)
4441 return;
4442
4443 if (bind_conf->ca_sign_pkey)
4444 EVP_PKEY_free(bind_conf->ca_sign_pkey);
4445 if (bind_conf->ca_sign_cert)
4446 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01004447 bind_conf->ca_sign_pkey = NULL;
4448 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004449}
4450
Emeric Brun46591952012-05-18 15:47:34 +02004451/*
4452 * This function is called if SSL * context is not yet allocated. The function
4453 * is designed to be called before any other data-layer operation and sets the
4454 * handshake flag on the connection. It is safe to call it multiple times.
4455 * It returns 0 on success and -1 in error case.
4456 */
4457static int ssl_sock_init(struct connection *conn)
4458{
4459 /* already initialized */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004460 if (conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02004461 return 0;
4462
Willy Tarreau3c728722014-01-23 13:50:42 +01004463 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02004464 return 0;
4465
Willy Tarreau20879a02012-12-03 16:32:10 +01004466 if (global.maxsslconn && sslconns >= global.maxsslconn) {
4467 conn->err_code = CO_ER_SSL_TOO_MANY;
Willy Tarreau403edff2012-09-06 11:58:37 +02004468 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +01004469 }
Willy Tarreau403edff2012-09-06 11:58:37 +02004470
Emeric Brun46591952012-05-18 15:47:34 +02004471 /* If it is in client mode initiate SSL session
4472 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004473 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004474 int may_retry = 1;
4475
4476 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02004477 /* Alloc a new SSL session ctx */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004478 conn->xprt_ctx = SSL_new(objt_server(conn->target)->ssl_ctx.ctx);
Willy Tarreau20879a02012-12-03 16:32:10 +01004479 if (!conn->xprt_ctx) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004480 if (may_retry--) {
4481 pool_gc2();
4482 goto retry_connect;
4483 }
Willy Tarreau20879a02012-12-03 16:32:10 +01004484 conn->err_code = CO_ER_SSL_NO_MEM;
Emeric Brun46591952012-05-18 15:47:34 +02004485 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +01004486 }
Emeric Brun46591952012-05-18 15:47:34 +02004487
Emeric Brun46591952012-05-18 15:47:34 +02004488 /* set fd on SSL session context */
Emeric Brun55476152014-11-12 17:35:37 +01004489 if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) {
4490 SSL_free(conn->xprt_ctx);
4491 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004492 if (may_retry--) {
4493 pool_gc2();
4494 goto retry_connect;
4495 }
Emeric Brun55476152014-11-12 17:35:37 +01004496 conn->err_code = CO_ER_SSL_NO_MEM;
4497 return -1;
4498 }
Emeric Brun46591952012-05-18 15:47:34 +02004499
Evan Broderbe554312013-06-27 00:05:25 -07004500 /* set connection pointer */
Emeric Brun55476152014-11-12 17:35:37 +01004501 if (!SSL_set_app_data(conn->xprt_ctx, conn)) {
4502 SSL_free(conn->xprt_ctx);
4503 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004504 if (may_retry--) {
4505 pool_gc2();
4506 goto retry_connect;
4507 }
Emeric Brun55476152014-11-12 17:35:37 +01004508 conn->err_code = CO_ER_SSL_NO_MEM;
4509 return -1;
4510 }
4511
4512 SSL_set_connect_state(conn->xprt_ctx);
4513 if (objt_server(conn->target)->ssl_ctx.reused_sess) {
4514 if(!SSL_set_session(conn->xprt_ctx, objt_server(conn->target)->ssl_ctx.reused_sess)) {
4515 SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess);
4516 objt_server(conn->target)->ssl_ctx.reused_sess = NULL;
4517 }
4518 }
Evan Broderbe554312013-06-27 00:05:25 -07004519
Emeric Brun46591952012-05-18 15:47:34 +02004520 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02004521 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02004522
4523 sslconns++;
Willy Tarreau71b734c2014-01-28 15:19:44 +01004524 totalsslconns++;
Emeric Brun46591952012-05-18 15:47:34 +02004525 return 0;
4526 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004527 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004528 int may_retry = 1;
4529
4530 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02004531 /* Alloc a new SSL session ctx */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004532 conn->xprt_ctx = SSL_new(objt_listener(conn->target)->bind_conf->initial_ctx);
Willy Tarreau20879a02012-12-03 16:32:10 +01004533 if (!conn->xprt_ctx) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004534 if (may_retry--) {
4535 pool_gc2();
4536 goto retry_accept;
4537 }
Willy Tarreau20879a02012-12-03 16:32:10 +01004538 conn->err_code = CO_ER_SSL_NO_MEM;
Emeric Brun46591952012-05-18 15:47:34 +02004539 return -1;
Willy Tarreau20879a02012-12-03 16:32:10 +01004540 }
Emeric Brun46591952012-05-18 15:47:34 +02004541
Emeric Brun46591952012-05-18 15:47:34 +02004542 /* set fd on SSL session context */
Emeric Brun55476152014-11-12 17:35:37 +01004543 if (!SSL_set_fd(conn->xprt_ctx, conn->t.sock.fd)) {
4544 SSL_free(conn->xprt_ctx);
4545 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004546 if (may_retry--) {
4547 pool_gc2();
4548 goto retry_accept;
4549 }
Emeric Brun55476152014-11-12 17:35:37 +01004550 conn->err_code = CO_ER_SSL_NO_MEM;
4551 return -1;
4552 }
Emeric Brun46591952012-05-18 15:47:34 +02004553
Emeric Brune1f38db2012-09-03 20:36:47 +02004554 /* set connection pointer */
Emeric Brun55476152014-11-12 17:35:37 +01004555 if (!SSL_set_app_data(conn->xprt_ctx, conn)) {
4556 SSL_free(conn->xprt_ctx);
4557 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004558 if (may_retry--) {
4559 pool_gc2();
4560 goto retry_accept;
4561 }
Emeric Brun55476152014-11-12 17:35:37 +01004562 conn->err_code = CO_ER_SSL_NO_MEM;
4563 return -1;
4564 }
4565
4566 SSL_set_accept_state(conn->xprt_ctx);
Emeric Brune1f38db2012-09-03 20:36:47 +02004567
Emeric Brun46591952012-05-18 15:47:34 +02004568 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02004569 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02004570
4571 sslconns++;
Willy Tarreau71b734c2014-01-28 15:19:44 +01004572 totalsslconns++;
Emeric Brun46591952012-05-18 15:47:34 +02004573 return 0;
4574 }
4575 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01004576 conn->err_code = CO_ER_SSL_NO_TARGET;
Emeric Brun46591952012-05-18 15:47:34 +02004577 return -1;
4578}
4579
4580
4581/* This is the callback which is used when an SSL handshake is pending. It
4582 * updates the FD status if it wants some polling before being called again.
4583 * It returns 0 if it fails in a fatal way or needs to poll to go further,
4584 * otherwise it returns non-zero and removes itself from the connection's
4585 * flags (the bit is provided in <flag> by the caller).
4586 */
4587int ssl_sock_handshake(struct connection *conn, unsigned int flag)
4588{
4589 int ret;
4590
Willy Tarreau3c728722014-01-23 13:50:42 +01004591 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02004592 return 0;
4593
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004594 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02004595 goto out_error;
4596
Emeric Brun674b7432012-11-08 19:21:55 +01004597 /* If we use SSL_do_handshake to process a reneg initiated by
4598 * the remote peer, it sometimes returns SSL_ERROR_SSL.
4599 * Usually SSL_write and SSL_read are used and process implicitly
4600 * the reneg handshake.
4601 * Here we use SSL_peek as a workaround for reneg.
4602 */
4603 if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(conn->xprt_ctx)) {
4604 char c;
4605
4606 ret = SSL_peek(conn->xprt_ctx, &c, 1);
4607 if (ret <= 0) {
4608 /* handshake may have not been completed, let's find why */
4609 ret = SSL_get_error(conn->xprt_ctx, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004610
Emeric Brun674b7432012-11-08 19:21:55 +01004611 if (ret == SSL_ERROR_WANT_WRITE) {
4612 /* SSL handshake needs to write, L4 connection may not be ready */
4613 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +01004614 __conn_sock_want_send(conn);
4615 fd_cant_send(conn->t.sock.fd);
Emeric Brun674b7432012-11-08 19:21:55 +01004616 return 0;
4617 }
4618 else if (ret == SSL_ERROR_WANT_READ) {
4619 /* handshake may have been completed but we have
4620 * no more data to read.
4621 */
4622 if (!SSL_renegotiate_pending(conn->xprt_ctx)) {
4623 ret = 1;
4624 goto reneg_ok;
4625 }
4626 /* SSL handshake needs to read, L4 connection is ready */
4627 if (conn->flags & CO_FL_WAIT_L4_CONN)
4628 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4629 __conn_sock_stop_send(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +01004630 __conn_sock_want_recv(conn);
4631 fd_cant_recv(conn->t.sock.fd);
Emeric Brun674b7432012-11-08 19:21:55 +01004632 return 0;
4633 }
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004634#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
4635 else if (ret == SSL_ERROR_WANT_ASYNC) {
Emeric Brun3854e012017-05-17 20:42:48 +02004636 ssl_async_process_fds(conn, conn->xprt_ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004637 return 0;
4638 }
4639#endif
Emeric Brun674b7432012-11-08 19:21:55 +01004640 else if (ret == SSL_ERROR_SYSCALL) {
4641 /* if errno is null, then connection was successfully established */
4642 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
4643 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01004644 if (!conn->err_code) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004645#ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */
4646 conn->err_code = CO_ER_SSL_HANDSHAKE;
4647#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004648 int empty_handshake;
Luca Pizzamiglio578b1692016-12-12 10:56:56 +01004649#if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004650 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx);
4651 empty_handshake = state == TLS_ST_BEFORE;
4652#else
4653 empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length;
4654#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004655 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02004656 if (!errno) {
4657 if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
4658 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
4659 else
4660 conn->err_code = CO_ER_SSL_EMPTY;
4661 }
4662 else {
4663 if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
4664 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
4665 else
4666 conn->err_code = CO_ER_SSL_ABORT;
4667 }
4668 }
4669 else {
4670 if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
4671 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01004672 else
Emeric Brun29f037d2014-04-25 19:05:36 +02004673 conn->err_code = CO_ER_SSL_HANDSHAKE;
4674 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004675#endif
Willy Tarreau20879a02012-12-03 16:32:10 +01004676 }
Emeric Brun674b7432012-11-08 19:21:55 +01004677 goto out_error;
4678 }
4679 else {
4680 /* Fail on all other handshake errors */
4681 /* Note: OpenSSL may leave unread bytes in the socket's
4682 * buffer, causing an RST to be emitted upon close() on
4683 * TCP sockets. We first try to drain possibly pending
4684 * data to avoid this as much as possible.
4685 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01004686 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01004687 if (!conn->err_code)
Willy Tarreauf51c6982014-04-25 20:02:39 +02004688 conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
4689 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01004690 goto out_error;
4691 }
4692 }
4693 /* read some data: consider handshake completed */
4694 goto reneg_ok;
4695 }
4696
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004697 ret = SSL_do_handshake(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +02004698 if (ret != 1) {
4699 /* handshake did not complete, let's find why */
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004700 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +02004701
4702 if (ret == SSL_ERROR_WANT_WRITE) {
4703 /* SSL handshake needs to write, L4 connection may not be ready */
4704 __conn_sock_stop_recv(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +01004705 __conn_sock_want_send(conn);
4706 fd_cant_send(conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +02004707 return 0;
4708 }
4709 else if (ret == SSL_ERROR_WANT_READ) {
4710 /* SSL handshake needs to read, L4 connection is ready */
4711 if (conn->flags & CO_FL_WAIT_L4_CONN)
4712 conn->flags &= ~CO_FL_WAIT_L4_CONN;
4713 __conn_sock_stop_send(conn);
Willy Tarreaue1f50c42014-01-22 20:02:06 +01004714 __conn_sock_want_recv(conn);
4715 fd_cant_recv(conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +02004716 return 0;
4717 }
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004718#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
4719 else if (ret == SSL_ERROR_WANT_ASYNC) {
Emeric Brun3854e012017-05-17 20:42:48 +02004720 ssl_async_process_fds(conn, conn->xprt_ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004721 return 0;
4722 }
4723#endif
Willy Tarreau89230192012-09-28 20:22:13 +02004724 else if (ret == SSL_ERROR_SYSCALL) {
4725 /* if errno is null, then connection was successfully established */
4726 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
4727 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004728 if (!conn->err_code) {
4729#ifdef OPENSSL_NO_HEARTBEATS /* BoringSSL */
4730 conn->err_code = CO_ER_SSL_HANDSHAKE;
4731#else
4732 int empty_handshake;
Luca Pizzamiglio578b1692016-12-12 10:56:56 +01004733#if (OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(LIBRESSL_VERSION_NUMBER)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004734 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)conn->xprt_ctx);
4735 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004736#else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004737 empty_handshake = !((SSL *)conn->xprt_ctx)->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004738#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004739 if (empty_handshake) {
4740 if (!errno) {
4741 if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
4742 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
4743 else
4744 conn->err_code = CO_ER_SSL_EMPTY;
4745 }
4746 else {
4747 if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
4748 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
4749 else
4750 conn->err_code = CO_ER_SSL_ABORT;
4751 }
Emeric Brun29f037d2014-04-25 19:05:36 +02004752 }
4753 else {
4754 if (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
4755 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
4756 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004757 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02004758 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004759#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02004760 }
Willy Tarreau89230192012-09-28 20:22:13 +02004761 goto out_error;
4762 }
Emeric Brun46591952012-05-18 15:47:34 +02004763 else {
4764 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02004765 /* Note: OpenSSL may leave unread bytes in the socket's
4766 * buffer, causing an RST to be emitted upon close() on
4767 * TCP sockets. We first try to drain possibly pending
4768 * data to avoid this as much as possible.
4769 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01004770 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01004771 if (!conn->err_code)
Willy Tarreauf51c6982014-04-25 20:02:39 +02004772 conn->err_code = (conn->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
4773 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02004774 goto out_error;
4775 }
4776 }
4777
Emeric Brun674b7432012-11-08 19:21:55 +01004778reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00004779
4780#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
4781 /* ASYNC engine API doesn't support moving read/write
4782 * buffers. So we disable ASYNC mode right after
4783 * the handshake to avoid buffer oveflows.
4784 */
4785 if (global_ssl.async)
4786 SSL_clear_mode(conn->xprt_ctx, SSL_MODE_ASYNC);
4787#endif
Emeric Brun46591952012-05-18 15:47:34 +02004788 /* Handshake succeeded */
Willy Tarreau0c9c2722014-05-28 12:28:58 +02004789 if (!SSL_session_reused(conn->xprt_ctx)) {
4790 if (objt_server(conn->target)) {
4791 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
4792 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
4793 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
4794
Emeric Brun46591952012-05-18 15:47:34 +02004795 /* check if session was reused, if not store current session on server for reuse */
Willy Tarreau30fd4bd2016-12-22 21:54:21 +01004796 if (objt_server(conn->target)->ssl_ctx.reused_sess) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004797 SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess);
Willy Tarreau30fd4bd2016-12-22 21:54:21 +01004798 objt_server(conn->target)->ssl_ctx.reused_sess = NULL;
4799 }
Emeric Brun46591952012-05-18 15:47:34 +02004800
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01004801 if (!(objt_server(conn->target)->ssl_ctx.options & SRV_SSL_O_NO_REUSE))
4802 objt_server(conn->target)->ssl_ctx.reused_sess = SSL_get1_session(conn->xprt_ctx);
Emeric Brun46591952012-05-18 15:47:34 +02004803 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02004804 else {
4805 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
4806 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
4807 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
4808 }
Emeric Brun46591952012-05-18 15:47:34 +02004809 }
4810
4811 /* The connection is now established at both layers, it's time to leave */
4812 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
4813 return 1;
4814
4815 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01004816 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02004817 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01004818 ERR_clear_error();
4819
Emeric Brun9fa89732012-10-04 17:09:56 +02004820 /* free resumed session if exists */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004821 if (objt_server(conn->target) && objt_server(conn->target)->ssl_ctx.reused_sess) {
4822 SSL_SESSION_free(objt_server(conn->target)->ssl_ctx.reused_sess);
4823 objt_server(conn->target)->ssl_ctx.reused_sess = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02004824 }
4825
Emeric Brun46591952012-05-18 15:47:34 +02004826 /* Fail on all other handshake errors */
4827 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01004828 if (!conn->err_code)
4829 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02004830 return 0;
4831}
4832
4833/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01004834 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02004835 * buffer wraps, in which case a second call may be performed. The connection's
4836 * flags are updated with whatever special event is detected (error, read0,
4837 * empty). The caller is responsible for taking care of those events and
4838 * avoiding the call if inappropriate. The function does not call the
4839 * connection's polling update function, so the caller is responsible for this.
4840 */
4841static int ssl_sock_to_buf(struct connection *conn, struct buffer *buf, int count)
4842{
4843 int ret, done = 0;
Willy Tarreauabf08d92014-01-14 11:31:27 +01004844 int try;
Emeric Brun46591952012-05-18 15:47:34 +02004845
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004846 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02004847 goto out_error;
4848
4849 if (conn->flags & CO_FL_HANDSHAKE)
4850 /* a handshake was requested */
4851 return 0;
4852
Willy Tarreauabf08d92014-01-14 11:31:27 +01004853 /* let's realign the buffer to optimize I/O */
4854 if (buffer_empty(buf))
Emeric Brun46591952012-05-18 15:47:34 +02004855 buf->p = buf->data;
Emeric Brun46591952012-05-18 15:47:34 +02004856
4857 /* read the largest possible block. For this, we perform only one call
4858 * to recv() unless the buffer wraps and we exactly fill the first hunk,
4859 * in which case we accept to do it once again. A new attempt is made on
4860 * EINTR too.
4861 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01004862 while (count > 0) {
Willy Tarreauabf08d92014-01-14 11:31:27 +01004863 /* first check if we have some room after p+i */
4864 try = buf->data + buf->size - (buf->p + buf->i);
4865 /* otherwise continue between data and p-o */
4866 if (try <= 0) {
4867 try = buf->p - (buf->data + buf->o);
4868 if (try <= 0)
4869 break;
4870 }
4871 if (try > count)
4872 try = count;
4873
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004874 ret = SSL_read(conn->xprt_ctx, bi_end(buf), try);
Emeric Brune1f38db2012-09-03 20:36:47 +02004875 if (conn->flags & CO_FL_ERROR) {
4876 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01004877 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02004878 }
Emeric Brun46591952012-05-18 15:47:34 +02004879 if (ret > 0) {
4880 buf->i += ret;
4881 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02004882 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02004883 }
4884 else if (ret == 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01004885 ret = SSL_get_error(conn->xprt_ctx, ret);
4886 if (ret != SSL_ERROR_ZERO_RETURN) {
Emeric Brun1c646862012-12-14 12:33:41 +01004887 /* error on protocol or underlying transport */
4888 if ((ret != SSL_ERROR_SYSCALL)
4889 || (errno && (errno != EAGAIN)))
4890 conn->flags |= CO_FL_ERROR;
4891
Emeric Brun644cde02012-12-14 11:21:13 +01004892 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02004893 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01004894 ERR_clear_error();
4895 }
Emeric Brun46591952012-05-18 15:47:34 +02004896 goto read0;
4897 }
4898 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004899 ret = SSL_get_error(conn->xprt_ctx, ret);
Emeric Brun46591952012-05-18 15:47:34 +02004900 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01004901 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02004902 conn->flags |= CO_FL_SSL_WAIT_HS;
Emeric Brun8af8dd12012-11-08 17:56:20 +01004903 __conn_sock_want_send(conn);
Emeric Brunb5e42a82017-06-06 12:35:14 +00004904#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
4905 /* Async mode can be re-enabled, because we're leaving data state.*/
4906 if (global_ssl.async)
4907 SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC);
4908#endif
Emeric Brun46591952012-05-18 15:47:34 +02004909 break;
4910 }
4911 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun282a76a2012-11-08 18:02:56 +01004912 if (SSL_renegotiate_pending(conn->xprt_ctx)) {
4913 /* handshake is running, and it may need to re-enable read */
4914 conn->flags |= CO_FL_SSL_WAIT_HS;
4915 __conn_sock_want_recv(conn);
Emeric Brunb5e42a82017-06-06 12:35:14 +00004916#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
4917 /* Async mode can be re-enabled, because we're leaving data state.*/
4918 if (global_ssl.async)
4919 SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC);
4920#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01004921 break;
4922 }
Emeric Brun46591952012-05-18 15:47:34 +02004923 /* we need to poll for retry a read later */
Willy Tarreaue1f50c42014-01-22 20:02:06 +01004924 fd_cant_recv(conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +02004925 break;
4926 }
4927 /* otherwise it's a real error */
4928 goto out_error;
4929 }
4930 }
4931 return done;
4932
4933 read0:
4934 conn_sock_read0(conn);
4935 return done;
4936 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01004937 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02004938 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01004939 ERR_clear_error();
4940
Emeric Brun46591952012-05-18 15:47:34 +02004941 conn->flags |= CO_FL_ERROR;
4942 return done;
4943}
4944
4945
4946/* Send all pending bytes from buffer <buf> to connection <conn>'s socket.
Willy Tarreau1049b1f2014-02-02 01:51:17 +01004947 * <flags> may contain some CO_SFL_* flags to hint the system about other
4948 * pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02004949 * Only one call to send() is performed, unless the buffer wraps, in which case
4950 * a second call may be performed. The connection's flags are updated with
4951 * whatever special event is detected (error, empty). The caller is responsible
4952 * for taking care of those events and avoiding the call if inappropriate. The
4953 * function does not call the connection's polling update function, so the caller
4954 * is responsible for this.
4955 */
4956static int ssl_sock_from_buf(struct connection *conn, struct buffer *buf, int flags)
4957{
4958 int ret, try, done;
4959
4960 done = 0;
4961
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004962 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02004963 goto out_error;
4964
4965 if (conn->flags & CO_FL_HANDSHAKE)
4966 /* a handshake was requested */
4967 return 0;
4968
4969 /* send the largest possible block. For this we perform only one call
4970 * to send() unless the buffer wraps and we exactly fill the first hunk,
4971 * in which case we accept to do it once again.
4972 */
4973 while (buf->o) {
Kevin Hestercad82342013-05-30 15:12:41 -07004974 try = bo_contig_data(buf);
Willy Tarreaubfd59462013-02-21 07:46:09 +01004975
Willy Tarreau7bed9452014-02-02 02:00:24 +01004976 if (!(flags & CO_SFL_STREAMER) &&
Willy Tarreau518cedd2014-02-17 15:43:01 +01004977 !(conn->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01004978 global_ssl.max_record && try > global_ssl.max_record) {
4979 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01004980 }
4981 else {
4982 /* we need to keep the information about the fact that
4983 * we're not limiting the upcoming send(), because if it
4984 * fails, we'll have to retry with at least as many data.
4985 */
4986 conn->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
4987 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01004988
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02004989 ret = SSL_write(conn->xprt_ctx, bo_ptr(buf), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01004990
Emeric Brune1f38db2012-09-03 20:36:47 +02004991 if (conn->flags & CO_FL_ERROR) {
4992 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01004993 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02004994 }
Emeric Brun46591952012-05-18 15:47:34 +02004995 if (ret > 0) {
Willy Tarreau518cedd2014-02-17 15:43:01 +01004996 conn->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
4997
Emeric Brun46591952012-05-18 15:47:34 +02004998 buf->o -= ret;
4999 done += ret;
5000
Willy Tarreau5fb38032012-12-16 19:39:09 +01005001 if (likely(buffer_empty(buf)))
Emeric Brun46591952012-05-18 15:47:34 +02005002 /* optimize data alignment in the buffer */
5003 buf->p = buf->data;
5004
5005 /* if the system buffer is full, don't insist */
5006 if (ret < try)
5007 break;
5008 }
5009 else {
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005010 ret = SSL_get_error(conn->xprt_ctx, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005011
Emeric Brun46591952012-05-18 15:47:34 +02005012 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun282a76a2012-11-08 18:02:56 +01005013 if (SSL_renegotiate_pending(conn->xprt_ctx)) {
5014 /* handshake is running, and it may need to re-enable write */
5015 conn->flags |= CO_FL_SSL_WAIT_HS;
5016 __conn_sock_want_send(conn);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005017#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
5018 /* Async mode can be re-enabled, because we're leaving data state.*/
5019 if (global_ssl.async)
5020 SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC);
5021#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01005022 break;
5023 }
Emeric Brun46591952012-05-18 15:47:34 +02005024 /* we need to poll to retry a write later */
Willy Tarreaue1f50c42014-01-22 20:02:06 +01005025 fd_cant_send(conn->t.sock.fd);
Emeric Brun46591952012-05-18 15:47:34 +02005026 break;
5027 }
5028 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01005029 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02005030 conn->flags |= CO_FL_SSL_WAIT_HS;
Emeric Brun8af8dd12012-11-08 17:56:20 +01005031 __conn_sock_want_recv(conn);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005032#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
Emeric Brunb5e42a82017-06-06 12:35:14 +00005033 /* Async mode can be re-enabled, because we're leaving data state.*/
5034 if (global_ssl.async)
5035 SSL_set_mode(conn->xprt_ctx, SSL_MODE_ASYNC);
5036#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005037 break;
5038 }
Emeric Brun46591952012-05-18 15:47:34 +02005039 goto out_error;
5040 }
5041 }
5042 return done;
5043
5044 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01005045 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005046 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005047 ERR_clear_error();
5048
Emeric Brun46591952012-05-18 15:47:34 +02005049 conn->flags |= CO_FL_ERROR;
5050 return done;
5051}
5052
Emeric Brun46591952012-05-18 15:47:34 +02005053static void ssl_sock_close(struct connection *conn) {
5054
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005055 if (conn->xprt_ctx) {
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005056#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
Emeric Brun3854e012017-05-17 20:42:48 +02005057 if (global_ssl.async) {
5058 OSSL_ASYNC_FD all_fd[32], afd;
5059 size_t num_all_fds = 0;
5060 int i;
5061
5062 SSL_get_all_async_fds(conn->xprt_ctx, NULL, &num_all_fds);
5063 if (num_all_fds > 32) {
5064 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
5065 return;
5066 }
5067
5068 SSL_get_all_async_fds(conn->xprt_ctx, all_fd, &num_all_fds);
5069
5070 /* If an async job is pending, we must try to
5071 to catch the end using polling before calling
5072 SSL_free */
5073 if (num_all_fds && SSL_waiting_for_async(conn->xprt_ctx)) {
5074 for (i=0 ; i < num_all_fds ; i++) {
5075 /* switch on an handler designed to
5076 * handle the SSL_free
5077 */
5078 afd = all_fd[i];
5079 fdtab[afd].iocb = ssl_async_fd_free;
5080 fdtab[afd].owner = conn->xprt_ctx;
5081 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00005082 /* To ensure that the fd cache won't be used
5083 * and we'll catch a real RD event.
5084 */
5085 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02005086 }
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005087 conn->xprt_ctx = NULL;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005088 jobs++;
5089 return;
5090 }
Emeric Brun3854e012017-05-17 20:42:48 +02005091 /* Else we can remove the fds from the fdtab
5092 * and call SSL_free.
5093 * note: we do a fd_remove and not a delete
5094 * because the fd is owned by the engine.
5095 * the engine is responsible to close
5096 */
5097 for (i=0 ; i < num_all_fds ; i++)
5098 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005099 }
5100#endif
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005101 SSL_free(conn->xprt_ctx);
5102 conn->xprt_ctx = NULL;
Willy Tarreau403edff2012-09-06 11:58:37 +02005103 sslconns--;
Emeric Brun46591952012-05-18 15:47:34 +02005104 }
Emeric Brun46591952012-05-18 15:47:34 +02005105}
5106
5107/* This function tries to perform a clean shutdown on an SSL connection, and in
5108 * any case, flags the connection as reusable if no handshake was in progress.
5109 */
5110static void ssl_sock_shutw(struct connection *conn, int clean)
5111{
5112 if (conn->flags & CO_FL_HANDSHAKE)
5113 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01005114 if (!clean)
5115 /* don't sent notify on SSL_shutdown */
Willy Tarreaue3cc3a32017-02-13 11:12:29 +01005116 SSL_set_quiet_shutdown(conn->xprt_ctx, 1);
Emeric Brun46591952012-05-18 15:47:34 +02005117 /* no handshake was in progress, try a clean ssl shutdown */
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01005118 if (SSL_shutdown(conn->xprt_ctx) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01005119 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005120 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005121 ERR_clear_error();
5122 }
Emeric Brun46591952012-05-18 15:47:34 +02005123}
5124
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005125/* used for logging, may be changed for a sample fetch later */
5126const char *ssl_sock_get_cipher_name(struct connection *conn)
5127{
5128 if (!conn->xprt && !conn->xprt_ctx)
5129 return NULL;
5130 return SSL_get_cipher_name(conn->xprt_ctx);
5131}
5132
5133/* used for logging, may be changed for a sample fetch later */
5134const char *ssl_sock_get_proto_version(struct connection *conn)
5135{
5136 if (!conn->xprt && !conn->xprt_ctx)
5137 return NULL;
5138 return SSL_get_version(conn->xprt_ctx);
5139}
5140
Willy Tarreau8d598402012-10-22 17:58:39 +02005141/* Extract a serial from a cert, and copy it to a chunk.
5142 * Returns 1 if serial is found and copied, 0 if no serial found and
5143 * -1 if output is not large enough.
5144 */
5145static int
5146ssl_sock_get_serial(X509 *crt, struct chunk *out)
5147{
5148 ASN1_INTEGER *serial;
5149
5150 serial = X509_get_serialNumber(crt);
5151 if (!serial)
5152 return 0;
5153
5154 if (out->size < serial->length)
5155 return -1;
5156
5157 memcpy(out->str, serial->data, serial->length);
5158 out->len = serial->length;
5159 return 1;
5160}
5161
Emeric Brun43e79582014-10-29 19:03:26 +01005162/* Extract a cert to der, and copy it to a chunk.
5163 * Returns 1 if cert is found and copied, 0 on der convertion failure and
5164 * -1 if output is not large enough.
5165 */
5166static int
5167ssl_sock_crt2der(X509 *crt, struct chunk *out)
5168{
5169 int len;
5170 unsigned char *p = (unsigned char *)out->str;;
5171
5172 len =i2d_X509(crt, NULL);
5173 if (len <= 0)
5174 return 1;
5175
5176 if (out->size < len)
5177 return -1;
5178
5179 i2d_X509(crt,&p);
5180 out->len = len;
5181 return 1;
5182}
5183
Emeric Brunce5ad802012-10-22 14:11:22 +02005184
5185/* Copy Date in ASN1_UTCTIME format in struct chunk out.
5186 * Returns 1 if serial is found and copied, 0 if no valid time found
5187 * and -1 if output is not large enough.
5188 */
5189static int
5190ssl_sock_get_time(ASN1_TIME *tm, struct chunk *out)
5191{
5192 if (tm->type == V_ASN1_GENERALIZEDTIME) {
5193 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
5194
5195 if (gentm->length < 12)
5196 return 0;
5197 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
5198 return 0;
5199 if (out->size < gentm->length-2)
5200 return -1;
5201
5202 memcpy(out->str, gentm->data+2, gentm->length-2);
5203 out->len = gentm->length-2;
5204 return 1;
5205 }
5206 else if (tm->type == V_ASN1_UTCTIME) {
5207 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
5208
5209 if (utctm->length < 10)
5210 return 0;
5211 if (utctm->data[0] >= 0x35)
5212 return 0;
5213 if (out->size < utctm->length)
5214 return -1;
5215
5216 memcpy(out->str, utctm->data, utctm->length);
5217 out->len = utctm->length;
5218 return 1;
5219 }
5220
5221 return 0;
5222}
5223
Emeric Brun87855892012-10-17 17:39:35 +02005224/* Extract an entry from a X509_NAME and copy its value to an output chunk.
5225 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
5226 */
5227static int
5228ssl_sock_get_dn_entry(X509_NAME *a, const struct chunk *entry, int pos, struct chunk *out)
5229{
5230 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005231 ASN1_OBJECT *obj;
5232 ASN1_STRING *data;
5233 const unsigned char *data_ptr;
5234 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02005235 int i, j, n;
5236 int cur = 0;
5237 const char *s;
5238 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005239 int name_count;
5240
5241 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02005242
5243 out->len = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005244 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02005245 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005246 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02005247 else
5248 j = i;
5249
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005250 ne = X509_NAME_get_entry(a, j);
5251 obj = X509_NAME_ENTRY_get_object(ne);
5252 data = X509_NAME_ENTRY_get_data(ne);
5253 data_ptr = ASN1_STRING_get0_data(data);
5254 data_len = ASN1_STRING_length(data);
5255 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02005256 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005257 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02005258 s = tmp;
5259 }
5260
5261 if (chunk_strcasecmp(entry, s) != 0)
5262 continue;
5263
5264 if (pos < 0)
5265 cur--;
5266 else
5267 cur++;
5268
5269 if (cur != pos)
5270 continue;
5271
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005272 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02005273 return -1;
5274
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005275 memcpy(out->str, data_ptr, data_len);
5276 out->len = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02005277 return 1;
5278 }
5279
5280 return 0;
5281
5282}
5283
5284/* Extract and format full DN from a X509_NAME and copy result into a chunk
5285 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
5286 */
5287static int
5288ssl_sock_get_dn_oneline(X509_NAME *a, struct chunk *out)
5289{
5290 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005291 ASN1_OBJECT *obj;
5292 ASN1_STRING *data;
5293 const unsigned char *data_ptr;
5294 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02005295 int i, n, ln;
5296 int l = 0;
5297 const char *s;
5298 char *p;
5299 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005300 int name_count;
5301
5302
5303 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02005304
5305 out->len = 0;
5306 p = out->str;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005307 for (i = 0; i < name_count; i++) {
5308 ne = X509_NAME_get_entry(a, i);
5309 obj = X509_NAME_ENTRY_get_object(ne);
5310 data = X509_NAME_ENTRY_get_data(ne);
5311 data_ptr = ASN1_STRING_get0_data(data);
5312 data_len = ASN1_STRING_length(data);
5313 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02005314 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005315 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02005316 s = tmp;
5317 }
5318 ln = strlen(s);
5319
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005320 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02005321 if (l > out->size)
5322 return -1;
5323 out->len = l;
5324
5325 *(p++)='/';
5326 memcpy(p, s, ln);
5327 p += ln;
5328 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005329 memcpy(p, data_ptr, data_len);
5330 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02005331 }
5332
5333 if (!out->len)
5334 return 0;
5335
5336 return 1;
5337}
5338
David Safb76832014-05-08 23:42:08 -04005339char *ssl_sock_get_version(struct connection *conn)
5340{
5341 if (!ssl_sock_is_ssl(conn))
5342 return NULL;
5343
5344 return (char *)SSL_get_version(conn->xprt_ctx);
5345}
5346
Willy Tarreau119a4082016-12-22 21:58:38 +01005347/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
5348 * to disable SNI.
5349 */
Willy Tarreau63076412015-07-10 11:33:32 +02005350void ssl_sock_set_servername(struct connection *conn, const char *hostname)
5351{
5352#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau119a4082016-12-22 21:58:38 +01005353 char *prev_name;
5354
Willy Tarreau63076412015-07-10 11:33:32 +02005355 if (!ssl_sock_is_ssl(conn))
5356 return;
5357
Willy Tarreau119a4082016-12-22 21:58:38 +01005358 /* if the SNI changes, we must destroy the reusable context so that a
5359 * new connection will present a new SNI. As an optimization we could
5360 * later imagine having a small cache of ssl_ctx to hold a few SNI per
5361 * server.
5362 */
5363 prev_name = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name);
5364 if ((!prev_name && hostname) ||
5365 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
5366 SSL_set_session(conn->xprt_ctx, NULL);
5367
Willy Tarreau63076412015-07-10 11:33:32 +02005368 SSL_set_tlsext_host_name(conn->xprt_ctx, hostname);
5369#endif
5370}
5371
Emeric Brun0abf8362014-06-24 18:26:41 +02005372/* Extract peer certificate's common name into the chunk dest
5373 * Returns
5374 * the len of the extracted common name
5375 * or 0 if no CN found in DN
5376 * or -1 on error case (i.e. no peer certificate)
5377 */
5378int ssl_sock_get_remote_common_name(struct connection *conn, struct chunk *dest)
David Safb76832014-05-08 23:42:08 -04005379{
5380 X509 *crt = NULL;
5381 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04005382 const char find_cn[] = "CN";
5383 const struct chunk find_cn_chunk = {
5384 .str = (char *)&find_cn,
5385 .len = sizeof(find_cn)-1
5386 };
Emeric Brun0abf8362014-06-24 18:26:41 +02005387 int result = -1;
David Safb76832014-05-08 23:42:08 -04005388
5389 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02005390 goto out;
David Safb76832014-05-08 23:42:08 -04005391
5392 /* SSL_get_peer_certificate, it increase X509 * ref count */
5393 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5394 if (!crt)
5395 goto out;
5396
5397 name = X509_get_subject_name(crt);
5398 if (!name)
5399 goto out;
David Safb76832014-05-08 23:42:08 -04005400
Emeric Brun0abf8362014-06-24 18:26:41 +02005401 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
5402out:
David Safb76832014-05-08 23:42:08 -04005403 if (crt)
5404 X509_free(crt);
5405
5406 return result;
5407}
5408
Dave McCowan328fb582014-07-30 10:39:13 -04005409/* returns 1 if client passed a certificate for this session, 0 if not */
5410int ssl_sock_get_cert_used_sess(struct connection *conn)
5411{
5412 X509 *crt = NULL;
5413
5414 if (!ssl_sock_is_ssl(conn))
5415 return 0;
5416
5417 /* SSL_get_peer_certificate, it increase X509 * ref count */
5418 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5419 if (!crt)
5420 return 0;
5421
5422 X509_free(crt);
5423 return 1;
5424}
5425
5426/* returns 1 if client passed a certificate for this connection, 0 if not */
5427int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04005428{
5429 if (!ssl_sock_is_ssl(conn))
5430 return 0;
5431
5432 return SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0;
5433}
5434
5435/* returns result from SSL verify */
5436unsigned int ssl_sock_get_verify_result(struct connection *conn)
5437{
5438 if (!ssl_sock_is_ssl(conn))
5439 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
5440
5441 return (unsigned int)SSL_get_verify_result(conn->xprt_ctx);
5442}
5443
Willy Tarreau8743f7e2016-12-04 18:44:29 +01005444/* Returns the application layer protocol name in <str> and <len> when known.
5445 * Zero is returned if the protocol name was not found, otherwise non-zero is
5446 * returned. The string is allocated in the SSL context and doesn't have to be
5447 * freed by the caller. NPN is also checked if available since older versions
5448 * of openssl (1.0.1) which are more common in field only support this one.
5449 */
5450static int ssl_sock_get_alpn(const struct connection *conn, const char **str, int *len)
5451{
5452 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
5453 return 0;
5454
5455 *str = NULL;
5456
5457#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
5458 SSL_get0_alpn_selected(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len);
5459 if (*str)
5460 return 1;
5461#endif
5462#ifdef OPENSSL_NPN_NEGOTIATED
5463 SSL_get0_next_proto_negotiated(conn->xprt_ctx, (const unsigned char **)str, (unsigned *)len);
5464 if (*str)
5465 return 1;
5466#endif
5467 return 0;
5468}
5469
Willy Tarreau7875d092012-09-10 08:20:03 +02005470/***** Below are some sample fetching functions for ACL/patterns *****/
5471
Emeric Brune64aef12012-09-21 13:15:06 +02005472/* boolean, returns true if client cert was present */
5473static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005474smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02005475{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005476 struct connection *conn;
5477
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005478 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005479 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02005480 return 0;
5481
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005482 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brune64aef12012-09-21 13:15:06 +02005483 smp->flags |= SMP_F_MAY_CHANGE;
5484 return 0;
5485 }
5486
5487 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005488 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005489 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & conn->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02005490
5491 return 1;
5492}
5493
Emeric Brun43e79582014-10-29 19:03:26 +01005494/* binary, returns a certificate in a binary chunk (der/raw).
5495 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
5496 * should be use.
5497 */
5498static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005499smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01005500{
5501 int cert_peer = (kw[4] == 'c') ? 1 : 0;
5502 X509 *crt = NULL;
5503 int ret = 0;
5504 struct chunk *smp_trash;
5505 struct connection *conn;
5506
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005507 conn = objt_conn(smp->sess->origin);
Emeric Brun43e79582014-10-29 19:03:26 +01005508 if (!conn || conn->xprt != &ssl_sock)
5509 return 0;
5510
5511 if (!(conn->flags & CO_FL_CONNECTED)) {
5512 smp->flags |= SMP_F_MAY_CHANGE;
5513 return 0;
5514 }
5515
5516 if (cert_peer)
5517 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5518 else
5519 crt = SSL_get_certificate(conn->xprt_ctx);
5520
5521 if (!crt)
5522 goto out;
5523
5524 smp_trash = get_trash_chunk();
5525 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
5526 goto out;
5527
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005528 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005529 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01005530 ret = 1;
5531out:
5532 /* SSL_get_peer_certificate, it increase X509 * ref count */
5533 if (cert_peer && crt)
5534 X509_free(crt);
5535 return ret;
5536}
5537
Emeric Brunba841a12014-04-30 17:05:08 +02005538/* binary, returns serial of certificate in a binary chunk.
5539 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
5540 * should be use.
5541 */
Willy Tarreau8d598402012-10-22 17:58:39 +02005542static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005543smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02005544{
Emeric Brunba841a12014-04-30 17:05:08 +02005545 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02005546 X509 *crt = NULL;
5547 int ret = 0;
5548 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005549 struct connection *conn;
5550
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005551 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005552 if (!conn || conn->xprt != &ssl_sock)
Willy Tarreau8d598402012-10-22 17:58:39 +02005553 return 0;
5554
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005555 if (!(conn->flags & CO_FL_CONNECTED)) {
Willy Tarreau8d598402012-10-22 17:58:39 +02005556 smp->flags |= SMP_F_MAY_CHANGE;
5557 return 0;
5558 }
5559
Emeric Brunba841a12014-04-30 17:05:08 +02005560 if (cert_peer)
5561 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5562 else
5563 crt = SSL_get_certificate(conn->xprt_ctx);
5564
Willy Tarreau8d598402012-10-22 17:58:39 +02005565 if (!crt)
5566 goto out;
5567
Willy Tarreau47ca5452012-12-23 20:22:19 +01005568 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02005569 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
5570 goto out;
5571
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005572 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005573 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02005574 ret = 1;
5575out:
Emeric Brunba841a12014-04-30 17:05:08 +02005576 /* SSL_get_peer_certificate, it increase X509 * ref count */
5577 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02005578 X509_free(crt);
5579 return ret;
5580}
Emeric Brune64aef12012-09-21 13:15:06 +02005581
Emeric Brunba841a12014-04-30 17:05:08 +02005582/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
5583 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
5584 * should be use.
5585 */
James Votha051b4a2013-05-14 20:37:59 +02005586static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005587smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02005588{
Emeric Brunba841a12014-04-30 17:05:08 +02005589 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02005590 X509 *crt = NULL;
5591 const EVP_MD *digest;
5592 int ret = 0;
5593 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005594 struct connection *conn;
James Votha051b4a2013-05-14 20:37:59 +02005595
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005596 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005597 if (!conn || conn->xprt != &ssl_sock)
5598 return 0;
5599
5600 if (!(conn->flags & CO_FL_CONNECTED)) {
James Votha051b4a2013-05-14 20:37:59 +02005601 smp->flags |= SMP_F_MAY_CHANGE;
5602 return 0;
5603 }
5604
Emeric Brunba841a12014-04-30 17:05:08 +02005605 if (cert_peer)
5606 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5607 else
5608 crt = SSL_get_certificate(conn->xprt_ctx);
James Votha051b4a2013-05-14 20:37:59 +02005609 if (!crt)
5610 goto out;
5611
5612 smp_trash = get_trash_chunk();
5613 digest = EVP_sha1();
5614 X509_digest(crt, digest, (unsigned char *)smp_trash->str, (unsigned int *)&smp_trash->len);
5615
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005616 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005617 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02005618 ret = 1;
5619out:
Emeric Brunba841a12014-04-30 17:05:08 +02005620 /* SSL_get_peer_certificate, it increase X509 * ref count */
5621 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02005622 X509_free(crt);
5623 return ret;
5624}
5625
Emeric Brunba841a12014-04-30 17:05:08 +02005626/* string, returns certificate's notafter date in ASN1_UTCTIME format.
5627 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
5628 * should be use.
5629 */
Emeric Brunce5ad802012-10-22 14:11:22 +02005630static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005631smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02005632{
Emeric Brunba841a12014-04-30 17:05:08 +02005633 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02005634 X509 *crt = NULL;
5635 int ret = 0;
5636 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005637 struct connection *conn;
Emeric Brunce5ad802012-10-22 14:11:22 +02005638
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005639 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005640 if (!conn || conn->xprt != &ssl_sock)
5641 return 0;
5642
5643 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02005644 smp->flags |= SMP_F_MAY_CHANGE;
5645 return 0;
5646 }
5647
Emeric Brunba841a12014-04-30 17:05:08 +02005648 if (cert_peer)
5649 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5650 else
5651 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02005652 if (!crt)
5653 goto out;
5654
Willy Tarreau47ca5452012-12-23 20:22:19 +01005655 smp_trash = get_trash_chunk();
Emeric Brunce5ad802012-10-22 14:11:22 +02005656 if (ssl_sock_get_time(X509_get_notAfter(crt), smp_trash) <= 0)
5657 goto out;
5658
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005659 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005660 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02005661 ret = 1;
5662out:
Emeric Brunba841a12014-04-30 17:05:08 +02005663 /* SSL_get_peer_certificate, it increase X509 * ref count */
5664 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02005665 X509_free(crt);
5666 return ret;
5667}
5668
Emeric Brunba841a12014-04-30 17:05:08 +02005669/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
5670 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
5671 * should be use.
5672 */
Emeric Brun87855892012-10-17 17:39:35 +02005673static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005674smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02005675{
Emeric Brunba841a12014-04-30 17:05:08 +02005676 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02005677 X509 *crt = NULL;
5678 X509_NAME *name;
5679 int ret = 0;
5680 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005681 struct connection *conn;
Emeric Brun87855892012-10-17 17:39:35 +02005682
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005683 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005684 if (!conn || conn->xprt != &ssl_sock)
5685 return 0;
5686
5687 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02005688 smp->flags |= SMP_F_MAY_CHANGE;
5689 return 0;
5690 }
5691
Emeric Brunba841a12014-04-30 17:05:08 +02005692 if (cert_peer)
5693 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5694 else
5695 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02005696 if (!crt)
5697 goto out;
5698
5699 name = X509_get_issuer_name(crt);
5700 if (!name)
5701 goto out;
5702
Willy Tarreau47ca5452012-12-23 20:22:19 +01005703 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02005704 if (args && args[0].type == ARGT_STR) {
5705 int pos = 1;
5706
5707 if (args[1].type == ARGT_SINT)
5708 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02005709
5710 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
5711 goto out;
5712 }
5713 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
5714 goto out;
5715
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005716 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005717 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02005718 ret = 1;
5719out:
Emeric Brunba841a12014-04-30 17:05:08 +02005720 /* SSL_get_peer_certificate, it increase X509 * ref count */
5721 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02005722 X509_free(crt);
5723 return ret;
5724}
5725
Emeric Brunba841a12014-04-30 17:05:08 +02005726/* string, returns notbefore date in ASN1_UTCTIME format.
5727 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
5728 * should be use.
5729 */
Emeric Brunce5ad802012-10-22 14:11:22 +02005730static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005731smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02005732{
Emeric Brunba841a12014-04-30 17:05:08 +02005733 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02005734 X509 *crt = NULL;
5735 int ret = 0;
5736 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005737 struct connection *conn;
5738
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005739 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005740 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunce5ad802012-10-22 14:11:22 +02005741 return 0;
5742
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005743 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunce5ad802012-10-22 14:11:22 +02005744 smp->flags |= SMP_F_MAY_CHANGE;
5745 return 0;
5746 }
5747
Emeric Brunba841a12014-04-30 17:05:08 +02005748 if (cert_peer)
5749 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5750 else
5751 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brunce5ad802012-10-22 14:11:22 +02005752 if (!crt)
5753 goto out;
5754
Willy Tarreau47ca5452012-12-23 20:22:19 +01005755 smp_trash = get_trash_chunk();
Emeric Brunce5ad802012-10-22 14:11:22 +02005756 if (ssl_sock_get_time(X509_get_notBefore(crt), smp_trash) <= 0)
5757 goto out;
5758
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005759 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005760 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02005761 ret = 1;
5762out:
Emeric Brunba841a12014-04-30 17:05:08 +02005763 /* SSL_get_peer_certificate, it increase X509 * ref count */
5764 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02005765 X509_free(crt);
5766 return ret;
5767}
5768
Emeric Brunba841a12014-04-30 17:05:08 +02005769/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
5770 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
5771 * should be use.
5772 */
Emeric Brun87855892012-10-17 17:39:35 +02005773static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005774smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02005775{
Emeric Brunba841a12014-04-30 17:05:08 +02005776 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02005777 X509 *crt = NULL;
5778 X509_NAME *name;
5779 int ret = 0;
5780 struct chunk *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005781 struct connection *conn;
Emeric Brun87855892012-10-17 17:39:35 +02005782
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005783 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005784 if (!conn || conn->xprt != &ssl_sock)
5785 return 0;
5786
5787 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun87855892012-10-17 17:39:35 +02005788 smp->flags |= SMP_F_MAY_CHANGE;
5789 return 0;
5790 }
5791
Emeric Brunba841a12014-04-30 17:05:08 +02005792 if (cert_peer)
5793 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5794 else
5795 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brun87855892012-10-17 17:39:35 +02005796 if (!crt)
5797 goto out;
5798
5799 name = X509_get_subject_name(crt);
5800 if (!name)
5801 goto out;
5802
Willy Tarreau47ca5452012-12-23 20:22:19 +01005803 smp_trash = get_trash_chunk();
Emeric Brun87855892012-10-17 17:39:35 +02005804 if (args && args[0].type == ARGT_STR) {
5805 int pos = 1;
5806
5807 if (args[1].type == ARGT_SINT)
5808 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02005809
5810 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
5811 goto out;
5812 }
5813 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
5814 goto out;
5815
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005816 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005817 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02005818 ret = 1;
5819out:
Emeric Brunba841a12014-04-30 17:05:08 +02005820 /* SSL_get_peer_certificate, it increase X509 * ref count */
5821 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02005822 X509_free(crt);
5823 return ret;
5824}
Emeric Brun9143d372012-12-20 15:44:16 +01005825
5826/* integer, returns true if current session use a client certificate */
5827static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005828smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01005829{
5830 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005831 struct connection *conn;
Emeric Brun9143d372012-12-20 15:44:16 +01005832
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005833 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005834 if (!conn || conn->xprt != &ssl_sock)
5835 return 0;
5836
5837 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun9143d372012-12-20 15:44:16 +01005838 smp->flags |= SMP_F_MAY_CHANGE;
5839 return 0;
5840 }
5841
5842 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005843 crt = SSL_get_peer_certificate(conn->xprt_ctx);
Emeric Brun9143d372012-12-20 15:44:16 +01005844 if (crt) {
5845 X509_free(crt);
5846 }
5847
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005848 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005849 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01005850 return 1;
5851}
5852
Emeric Brunba841a12014-04-30 17:05:08 +02005853/* integer, returns the certificate version
5854 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
5855 * should be use.
5856 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02005857static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005858smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02005859{
Emeric Brunba841a12014-04-30 17:05:08 +02005860 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02005861 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005862 struct connection *conn;
5863
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005864 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005865 if (!conn || conn->xprt != &ssl_sock)
Emeric Bruna7359fd2012-10-17 15:03:11 +02005866 return 0;
5867
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005868 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02005869 smp->flags |= SMP_F_MAY_CHANGE;
5870 return 0;
5871 }
5872
Emeric Brunba841a12014-04-30 17:05:08 +02005873 if (cert_peer)
5874 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5875 else
5876 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Bruna7359fd2012-10-17 15:03:11 +02005877 if (!crt)
5878 return 0;
5879
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005880 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02005881 /* SSL_get_peer_certificate increase X509 * ref count */
5882 if (cert_peer)
5883 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005884 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02005885
5886 return 1;
5887}
5888
Emeric Brunba841a12014-04-30 17:05:08 +02005889/* string, returns the certificate's signature algorithm.
5890 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
5891 * should be use.
5892 */
Emeric Brun7f56e742012-10-19 18:15:40 +02005893static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005894smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02005895{
Emeric Brunba841a12014-04-30 17:05:08 +02005896 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02005897 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005898 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02005899 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005900 struct connection *conn;
Emeric Brun7f56e742012-10-19 18:15:40 +02005901
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005902 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005903 if (!conn || conn->xprt != &ssl_sock)
5904 return 0;
5905
5906 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun7f56e742012-10-19 18:15:40 +02005907 smp->flags |= SMP_F_MAY_CHANGE;
5908 return 0;
5909 }
5910
Emeric Brunba841a12014-04-30 17:05:08 +02005911 if (cert_peer)
5912 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5913 else
5914 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brun7f56e742012-10-19 18:15:40 +02005915 if (!crt)
5916 return 0;
5917
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005918 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
5919 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02005920
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005921 smp->data.u.str.str = (char *)OBJ_nid2sn(nid);
5922 if (!smp->data.u.str.str) {
Emeric Brunba841a12014-04-30 17:05:08 +02005923 /* SSL_get_peer_certificate increase X509 * ref count */
5924 if (cert_peer)
5925 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02005926 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02005927 }
Emeric Brun7f56e742012-10-19 18:15:40 +02005928
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005929 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005930 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005931 smp->data.u.str.len = strlen(smp->data.u.str.str);
Emeric Brunba841a12014-04-30 17:05:08 +02005932 /* SSL_get_peer_certificate increase X509 * ref count */
5933 if (cert_peer)
5934 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02005935
5936 return 1;
5937}
5938
Emeric Brunba841a12014-04-30 17:05:08 +02005939/* string, returns the certificate's key algorithm.
5940 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
5941 * should be use.
5942 */
Emeric Brun521a0112012-10-22 12:22:55 +02005943static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005944smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02005945{
Emeric Brunba841a12014-04-30 17:05:08 +02005946 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02005947 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005948 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02005949 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005950 struct connection *conn;
Emeric Brun521a0112012-10-22 12:22:55 +02005951
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02005952 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005953 if (!conn || conn->xprt != &ssl_sock)
5954 return 0;
5955
5956 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brun521a0112012-10-22 12:22:55 +02005957 smp->flags |= SMP_F_MAY_CHANGE;
5958 return 0;
5959 }
5960
Emeric Brunba841a12014-04-30 17:05:08 +02005961 if (cert_peer)
5962 crt = SSL_get_peer_certificate(conn->xprt_ctx);
5963 else
5964 crt = SSL_get_certificate(conn->xprt_ctx);
Emeric Brun521a0112012-10-22 12:22:55 +02005965 if (!crt)
5966 return 0;
5967
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005968 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
5969 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02005970
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005971 smp->data.u.str.str = (char *)OBJ_nid2sn(nid);
5972 if (!smp->data.u.str.str) {
Emeric Brunba841a12014-04-30 17:05:08 +02005973 /* SSL_get_peer_certificate increase X509 * ref count */
5974 if (cert_peer)
5975 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02005976 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02005977 }
Emeric Brun521a0112012-10-22 12:22:55 +02005978
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005979 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01005980 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005981 smp->data.u.str.len = strlen(smp->data.u.str.str);
Emeric Brunba841a12014-04-30 17:05:08 +02005982 if (cert_peer)
5983 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02005984
5985 return 1;
5986}
5987
Emeric Brun645ae792014-04-30 14:21:06 +02005988/* boolean, returns true if front conn. transport layer is SSL.
5989 * This function is also usable on backend conn if the fetch keyword 5th
5990 * char is 'b'.
5991 */
Willy Tarreau7875d092012-09-10 08:20:03 +02005992static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02005993smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02005994{
Willy Tarreaue237fe12016-03-10 17:05:28 +01005995 struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin :
5996 smp->strm ? smp->strm->si[1].end : NULL);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02005997
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02005998 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02005999 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02006000 return 1;
6001}
6002
Emeric Brun2525b6b2012-10-18 15:59:43 +02006003/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02006004static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006005smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02006006{
6007#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006008 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006009
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006010 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006011 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006012 conn->xprt_ctx &&
6013 SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02006014 return 1;
6015#else
6016 return 0;
6017#endif
6018}
6019
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02006020/* boolean, returns true if client session has been resumed */
6021static int
6022smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
6023{
6024 struct connection *conn = objt_conn(smp->sess->origin);
6025
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006026 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006027 smp->data.u.sint = (conn && conn->xprt == &ssl_sock) &&
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02006028 conn->xprt_ctx &&
6029 SSL_session_reused(conn->xprt_ctx);
6030 return 1;
6031}
6032
Emeric Brun645ae792014-04-30 14:21:06 +02006033/* string, returns the used cipher if front conn. transport layer is SSL.
6034 * This function is also usable on backend conn if the fetch keyword 5th
6035 * char is 'b'.
6036 */
Emeric Brun589fcad2012-10-16 14:13:26 +02006037static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006038smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02006039{
Willy Tarreaue237fe12016-03-10 17:05:28 +01006040 struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin :
6041 smp->strm ? smp->strm->si[1].end : NULL);
Emeric Brun589fcad2012-10-16 14:13:26 +02006042
Willy Tarreaube508f12016-03-10 11:47:01 +01006043 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006044 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02006045 return 0;
6046
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006047 smp->data.u.str.str = (char *)SSL_get_cipher_name(conn->xprt_ctx);
6048 if (!smp->data.u.str.str)
Emeric Brun589fcad2012-10-16 14:13:26 +02006049 return 0;
6050
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006051 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01006052 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006053 smp->data.u.str.len = strlen(smp->data.u.str.str);
Emeric Brun589fcad2012-10-16 14:13:26 +02006054
6055 return 1;
6056}
6057
Emeric Brun645ae792014-04-30 14:21:06 +02006058/* integer, returns the algoritm's keysize if front conn. transport layer
6059 * is SSL.
6060 * This function is also usable on backend conn if the fetch keyword 5th
6061 * char is 'b'.
6062 */
Emeric Brun589fcad2012-10-16 14:13:26 +02006063static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006064smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02006065{
Willy Tarreaue237fe12016-03-10 17:05:28 +01006066 struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin :
6067 smp->strm ? smp->strm->si[1].end : NULL);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006068
Willy Tarreaue237fe12016-03-10 17:05:28 +01006069 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01006070
Emeric Brun589fcad2012-10-16 14:13:26 +02006071 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006072 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Emeric Brun589fcad2012-10-16 14:13:26 +02006073 return 0;
6074
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02006075 if (!SSL_get_cipher_bits(conn->xprt_ctx, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006076 return 0;
6077
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006078 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006079 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02006080
6081 return 1;
6082}
6083
Emeric Brun645ae792014-04-30 14:21:06 +02006084/* integer, returns the used keysize if front conn. transport layer is SSL.
6085 * This function is also usable on backend conn if the fetch keyword 5th
6086 * char is 'b'.
6087 */
Emeric Brun589fcad2012-10-16 14:13:26 +02006088static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006089smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02006090{
Willy Tarreaue237fe12016-03-10 17:05:28 +01006091 struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin :
6092 smp->strm ? smp->strm->si[1].end : NULL);
Willy Tarreaube508f12016-03-10 11:47:01 +01006093
Emeric Brun589fcad2012-10-16 14:13:26 +02006094 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006095 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
6096 return 0;
6097
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006098 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(conn->xprt_ctx, NULL);
6099 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02006100 return 0;
6101
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006102 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02006103
6104 return 1;
6105}
6106
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02006107#ifdef OPENSSL_NPN_NEGOTIATED
Willy Tarreau7875d092012-09-10 08:20:03 +02006108static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006109smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02006110{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006111 struct connection *conn;
6112
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01006113 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006114 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02006115
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006116 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006117 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
6118 return 0;
6119
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006120 smp->data.u.str.str = NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006121 SSL_get0_next_proto_negotiated(conn->xprt_ctx,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006122 (const unsigned char **)&smp->data.u.str.str, (unsigned *)&smp->data.u.str.len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02006123
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006124 if (!smp->data.u.str.str)
Willy Tarreaua33c6542012-10-15 13:19:06 +02006125 return 0;
6126
6127 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02006128}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02006129#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02006130
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01006131#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02006132static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006133smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02006134{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006135 struct connection *conn;
6136
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01006137 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006138 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02006139
Willy Tarreaue26bf052015-05-12 10:30:12 +02006140 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006141 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
Willy Tarreauab861d32013-04-02 02:30:41 +02006142 return 0;
6143
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006144 smp->data.u.str.str = NULL;
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01006145 SSL_get0_alpn_selected(conn->xprt_ctx,
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006146 (const unsigned char **)&smp->data.u.str.str, (unsigned *)&smp->data.u.str.len);
Willy Tarreauab861d32013-04-02 02:30:41 +02006147
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006148 if (!smp->data.u.str.str)
Willy Tarreauab861d32013-04-02 02:30:41 +02006149 return 0;
6150
6151 return 1;
6152}
6153#endif
6154
Emeric Brun645ae792014-04-30 14:21:06 +02006155/* string, returns the used protocol if front conn. transport layer is SSL.
6156 * This function is also usable on backend conn if the fetch keyword 5th
6157 * char is 'b'.
6158 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02006159static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006160smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02006161{
Willy Tarreaue237fe12016-03-10 17:05:28 +01006162 struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin :
6163 smp->strm ? smp->strm->si[1].end : NULL);
Willy Tarreaube508f12016-03-10 11:47:01 +01006164
Emeric Brun589fcad2012-10-16 14:13:26 +02006165 smp->flags = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006166 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
6167 return 0;
6168
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006169 smp->data.u.str.str = (char *)SSL_get_version(conn->xprt_ctx);
6170 if (!smp->data.u.str.str)
Emeric Brun589fcad2012-10-16 14:13:26 +02006171 return 0;
6172
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006173 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01006174 smp->flags = SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006175 smp->data.u.str.len = strlen(smp->data.u.str.str);
Emeric Brun589fcad2012-10-16 14:13:26 +02006176
6177 return 1;
6178}
6179
Willy Tarreau87b09662015-04-03 00:22:06 +02006180/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02006181 * This function is also usable on backend conn if the fetch keyword 5th
6182 * char is 'b'.
6183 */
Emeric Brun589fcad2012-10-16 14:13:26 +02006184static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006185smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02006186{
6187#if OPENSSL_VERSION_NUMBER > 0x0090800fL
Willy Tarreaue237fe12016-03-10 17:05:28 +01006188 struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin :
6189 smp->strm ? smp->strm->si[1].end : NULL);
Emeric Brunfe68f682012-10-16 14:59:28 +02006190
Willy Tarreaue237fe12016-03-10 17:05:28 +01006191 SSL_SESSION *ssl_sess;
Willy Tarreaube508f12016-03-10 11:47:01 +01006192
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01006193 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006194 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02006195
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006196 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
6197 return 0;
6198
Willy Tarreau192252e2015-04-04 01:47:55 +02006199 ssl_sess = SSL_get_session(conn->xprt_ctx);
6200 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02006201 return 0;
6202
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006203 smp->data.u.str.str = (char *)SSL_SESSION_get_id(ssl_sess, (unsigned int *)&smp->data.u.str.len);
6204 if (!smp->data.u.str.str || !smp->data.u.str.len)
Emeric Brunfe68f682012-10-16 14:59:28 +02006205 return 0;
6206
6207 return 1;
6208#else
6209 return 0;
6210#endif
6211}
6212
6213static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006214smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02006215{
6216#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006217 struct connection *conn;
6218
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01006219 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006220 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02006221
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006222 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006223 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
6224 return 0;
6225
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006226 smp->data.u.str.str = (char *)SSL_get_servername(conn->xprt_ctx, TLSEXT_NAMETYPE_host_name);
6227 if (!smp->data.u.str.str)
Willy Tarreau3e394c92012-09-14 23:56:58 +02006228 return 0;
6229
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006230 smp->data.u.str.len = strlen(smp->data.u.str.str);
Willy Tarreau7875d092012-09-10 08:20:03 +02006231 return 1;
6232#else
6233 return 0;
6234#endif
6235}
6236
David Sc1ad52e2014-04-08 18:48:47 -04006237static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01006238smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
6239{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01006240 struct connection *conn;
6241 struct ssl_capture *capture;
6242
6243 conn = objt_conn(smp->sess->origin);
6244 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
6245 return 0;
6246
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01006247 capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01006248 if (!capture)
6249 return 0;
6250
6251 smp->flags = SMP_F_CONST;
6252 smp->data.type = SMP_T_BIN;
6253 smp->data.u.str.str = capture->ciphersuite;
6254 smp->data.u.str.len = capture->ciphersuite_len;
6255 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01006256}
6257
6258static int
6259smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
6260{
6261 struct chunk *data;
6262
6263 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
6264 return 0;
6265
6266 data = get_trash_chunk();
6267 dump_binary(data, smp->data.u.str.str, smp->data.u.str.len);
6268 smp->data.type = SMP_T_BIN;
6269 smp->data.u.str = *data;
6270 return 1;
6271}
6272
6273static int
6274smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
6275{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01006276 struct connection *conn;
6277 struct ssl_capture *capture;
6278
6279 conn = objt_conn(smp->sess->origin);
6280 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
6281 return 0;
6282
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01006283 capture = SSL_get_ex_data(conn->xprt_ctx, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01006284 if (!capture)
6285 return 0;
6286
6287 smp->data.type = SMP_T_SINT;
6288 smp->data.u.sint = capture->xxh64;
6289 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01006290}
6291
6292static int
6293smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
6294{
6295#if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && !defined(OPENSSL_NO_SSL_TRACE)
6296 struct chunk *data;
6297 SSL_CIPHER cipher;
6298 int i;
6299 const char *str;
6300 unsigned char *bin;
6301
6302 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
6303 return 0;
6304
6305 /* The cipher algorith must not be SSL_SSLV2, because this
6306 * SSL version seems to not have the same cipher encoding,
6307 * and it is not supported by OpenSSL. Unfortunately, the
6308 * #define SSL_SSLV2, SSL_SSLV3 and others are not available
6309 * with standard defines. We just set the variable to 0,
6310 * ensure that the match with SSL_SSLV2 fails.
6311 */
6312 cipher.algorithm_ssl = 0;
6313
6314 data = get_trash_chunk();
6315 for (i = 0; i + 1 < smp->data.u.str.len; i += 2) {
6316 bin = (unsigned char *)smp->data.u.str.str + i;
6317 cipher.id = (unsigned int)(bin[0] << 8) | bin[1];
6318 str = SSL_CIPHER_standard_name(&cipher);
6319 if (!str || strcmp(str, "UNKNOWN") == 0)
6320 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", (unsigned int)cipher.id);
6321 else
6322 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
6323 }
6324 smp->data.type = SMP_T_STR;
6325 smp->data.u.str = *data;
6326 return 1;
6327#else
6328 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
6329#endif
6330}
6331
6332static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006333smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04006334{
6335#if OPENSSL_VERSION_NUMBER > 0x0090800fL
Willy Tarreaue237fe12016-03-10 17:05:28 +01006336 struct connection *conn = objt_conn((kw[4] != 'b') ? smp->sess->origin :
6337 smp->strm ? smp->strm->si[1].end : NULL);
6338
David Sc1ad52e2014-04-08 18:48:47 -04006339 int finished_len;
David Sc1ad52e2014-04-08 18:48:47 -04006340 struct chunk *finished_trash;
David Sc1ad52e2014-04-08 18:48:47 -04006341
6342 smp->flags = 0;
David Sc1ad52e2014-04-08 18:48:47 -04006343 if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock)
6344 return 0;
6345
6346 if (!(conn->flags & CO_FL_CONNECTED)) {
6347 smp->flags |= SMP_F_MAY_CHANGE;
6348 return 0;
6349 }
6350
6351 finished_trash = get_trash_chunk();
6352 if (!SSL_session_reused(conn->xprt_ctx))
6353 finished_len = SSL_get_peer_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size);
6354 else
6355 finished_len = SSL_get_finished(conn->xprt_ctx, finished_trash->str, finished_trash->size);
6356
6357 if (!finished_len)
6358 return 0;
6359
Emeric Brunb73a9b02014-04-30 18:49:19 +02006360 finished_trash->len = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006361 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006362 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04006363
6364 return 1;
6365#else
6366 return 0;
6367#endif
6368}
6369
Emeric Brun2525b6b2012-10-18 15:59:43 +02006370/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02006371static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006372smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02006373{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006374 struct connection *conn;
6375
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006376 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006377 if (!conn || conn->xprt != &ssl_sock)
6378 return 0;
6379
6380 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02006381 smp->flags = SMP_F_MAY_CHANGE;
6382 return 0;
6383 }
6384
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006385 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006386 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02006387 smp->flags = 0;
6388
6389 return 1;
6390}
6391
Emeric Brun2525b6b2012-10-18 15:59:43 +02006392/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02006393static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006394smp_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 +02006395{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006396 struct connection *conn;
6397
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006398 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006399 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02006400 return 0;
6401
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006402 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02006403 smp->flags = SMP_F_MAY_CHANGE;
6404 return 0;
6405 }
6406
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006407 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006408 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02006409 smp->flags = 0;
6410
6411 return 1;
6412}
6413
Emeric Brun2525b6b2012-10-18 15:59:43 +02006414/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02006415static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006416smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02006417{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006418 struct connection *conn;
6419
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006420 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006421 if (!conn || conn->xprt != &ssl_sock)
6422 return 0;
6423
6424 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunf282a812012-09-21 15:27:54 +02006425 smp->flags = SMP_F_MAY_CHANGE;
6426 return 0;
6427 }
6428
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006429 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006430 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(conn->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02006431 smp->flags = 0;
6432
6433 return 1;
6434}
6435
Emeric Brun2525b6b2012-10-18 15:59:43 +02006436/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02006437static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006438smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02006439{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006440 struct connection *conn;
6441
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006442 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006443 if (!conn || conn->xprt != &ssl_sock)
6444 return 0;
6445
6446 if (!(conn->flags & CO_FL_CONNECTED)) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02006447 smp->flags = SMP_F_MAY_CHANGE;
6448 return 0;
6449 }
6450
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006451 if (!conn->xprt_ctx)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02006452 return 0;
6453
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006454 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006455 smp->data.u.sint = (long long int)SSL_get_verify_result(conn->xprt_ctx);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02006456 smp->flags = 0;
6457
6458 return 1;
6459}
6460
Emeric Brunfb510ea2012-10-05 12:00:26 +02006461/* parse the "ca-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006462static 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 +02006463{
6464 if (!*args[cur_arg + 1]) {
6465 if (err)
6466 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
6467 return ERR_ALERT | ERR_FATAL;
6468 }
6469
Willy Tarreauef934602016-12-22 23:12:01 +01006470 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
6471 memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02006472 else
6473 memprintf(&conf->ca_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02006474
Emeric Brund94b3fe2012-09-20 18:23:56 +02006475 return 0;
6476}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006477static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6478{
6479 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
6480}
Emeric Brund94b3fe2012-09-20 18:23:56 +02006481
Christopher Faulet31af49d2015-06-09 17:29:50 +02006482/* parse the "ca-sign-file" bind keyword */
6483static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6484{
6485 if (!*args[cur_arg + 1]) {
6486 if (err)
6487 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
6488 return ERR_ALERT | ERR_FATAL;
6489 }
6490
Willy Tarreauef934602016-12-22 23:12:01 +01006491 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
6492 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006493 else
6494 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
6495
6496 return 0;
6497}
6498
Bertrand Jacquinff13c062016-11-13 16:37:11 +00006499/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02006500static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6501{
6502 if (!*args[cur_arg + 1]) {
6503 if (err)
6504 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
6505 return ERR_ALERT | ERR_FATAL;
6506 }
6507 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
6508 return 0;
6509}
6510
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006511/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006512static 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 +02006513{
6514 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02006515 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006516 return ERR_ALERT | ERR_FATAL;
6517 }
6518
Emeric Brun76d88952012-10-05 15:47:31 +02006519 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02006520 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006521 return 0;
6522}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006523static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6524{
6525 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
6526}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006527/* parse the "crt" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02006528static 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 +02006529{
Willy Tarreau38011032013-08-13 16:59:39 +02006530 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02006531
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006532 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02006533 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006534 return ERR_ALERT | ERR_FATAL;
6535 }
6536
Willy Tarreauef934602016-12-22 23:12:01 +01006537 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
6538 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02006539 memprintf(err, "'%s' : path too long", args[cur_arg]);
6540 return ERR_ALERT | ERR_FATAL;
6541 }
Willy Tarreauef934602016-12-22 23:12:01 +01006542 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreau03209342016-12-22 17:08:28 +01006543 if (ssl_sock_load_cert(path, conf, err) > 0)
Emeric Brunc8e8d122012-10-02 18:42:10 +02006544 return ERR_ALERT | ERR_FATAL;
6545
6546 return 0;
6547 }
6548
Willy Tarreau03209342016-12-22 17:08:28 +01006549 if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006550 return ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02006551
6552 return 0;
6553}
6554
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01006555/* parse the "crt-list" bind keyword */
6556static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6557{
6558 if (!*args[cur_arg + 1]) {
6559 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
6560 return ERR_ALERT | ERR_FATAL;
6561 }
6562
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006563 if (ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err) > 0) {
Willy Tarreauad1731d2013-04-02 17:35:58 +02006564 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01006565 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02006566 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01006567
6568 return 0;
6569}
6570
Emeric Brunfb510ea2012-10-05 12:00:26 +02006571/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006572static 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 +02006573{
Emeric Brun051cdab2012-10-02 19:25:50 +02006574#ifndef X509_V_FLAG_CRL_CHECK
6575 if (err)
6576 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
6577 return ERR_ALERT | ERR_FATAL;
6578#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02006579 if (!*args[cur_arg + 1]) {
6580 if (err)
6581 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
6582 return ERR_ALERT | ERR_FATAL;
6583 }
Emeric Brun2b58d042012-09-20 17:10:03 +02006584
Willy Tarreauef934602016-12-22 23:12:01 +01006585 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
6586 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02006587 else
6588 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02006589
Emeric Brun2b58d042012-09-20 17:10:03 +02006590 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02006591#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02006592}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006593static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6594{
6595 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
6596}
Emeric Brun2b58d042012-09-20 17:10:03 +02006597
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01006598/* parse the "curves" bind keyword keyword */
6599static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
6600{
6601#if OPENSSL_VERSION_NUMBER >= 0x1000200fL
6602 if (!*args[cur_arg + 1]) {
6603 if (err)
6604 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
6605 return ERR_ALERT | ERR_FATAL;
6606 }
6607 conf->curves = strdup(args[cur_arg + 1]);
6608 return 0;
6609#else
6610 if (err)
6611 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
6612 return ERR_ALERT | ERR_FATAL;
6613#endif
6614}
6615static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6616{
6617 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
6618}
6619
Bertrand Jacquinff13c062016-11-13 16:37:11 +00006620/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006621static 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 +02006622{
6623#if OPENSSL_VERSION_NUMBER < 0x0090800fL
6624 if (err)
6625 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
6626 return ERR_ALERT | ERR_FATAL;
6627#elif defined(OPENSSL_NO_ECDH)
6628 if (err)
6629 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
6630 return ERR_ALERT | ERR_FATAL;
6631#else
6632 if (!*args[cur_arg + 1]) {
6633 if (err)
6634 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
6635 return ERR_ALERT | ERR_FATAL;
6636 }
6637
6638 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006639
6640 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02006641#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006642}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006643static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6644{
6645 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
6646}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006647
Bertrand Jacquinff13c062016-11-13 16:37:11 +00006648/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02006649static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6650{
6651 int code;
6652 char *p = args[cur_arg + 1];
6653 unsigned long long *ignerr = &conf->crt_ignerr;
6654
6655 if (!*p) {
6656 if (err)
6657 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
6658 return ERR_ALERT | ERR_FATAL;
6659 }
6660
6661 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
6662 ignerr = &conf->ca_ignerr;
6663
6664 if (strcmp(p, "all") == 0) {
6665 *ignerr = ~0ULL;
6666 return 0;
6667 }
6668
6669 while (p) {
6670 code = atoi(p);
6671 if ((code <= 0) || (code > 63)) {
6672 if (err)
6673 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
6674 args[cur_arg], code, args[cur_arg + 1]);
6675 return ERR_ALERT | ERR_FATAL;
6676 }
6677 *ignerr |= 1ULL << code;
6678 p = strchr(p, ',');
6679 if (p)
6680 p++;
6681 }
6682
Emeric Brun2cb7ae52012-10-05 14:14:21 +02006683 return 0;
6684}
6685
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006686/* parse tls_method_options "no-xxx" and "force-xxx" */
6687static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02006688{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006689 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006690 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006691 p = strchr(arg, '-');
6692 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006693 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006694 p++;
6695 if (!strcmp(p, "sslv3"))
6696 v = CONF_SSLV3;
6697 else if (!strcmp(p, "tlsv10"))
6698 v = CONF_TLSV10;
6699 else if (!strcmp(p, "tlsv11"))
6700 v = CONF_TLSV11;
6701 else if (!strcmp(p, "tlsv12"))
6702 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02006703 else if (!strcmp(p, "tlsv13"))
6704 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006705 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006706 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006707 if (!strncmp(arg, "no-", 3))
6708 methods->flags |= methodVersions[v].flag;
6709 else if (!strncmp(arg, "force-", 6))
6710 methods->min = methods->max = v;
6711 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006712 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02006713 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006714 fail:
6715 if (err)
6716 memprintf(err, "'%s' : option not implemented", arg);
6717 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02006718}
6719
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006720static 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 +02006721{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006722 return parse_tls_method_options(args[cur_arg], &conf->ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02006723}
6724
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006725static 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 +02006726{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006727 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
6728}
6729
6730/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
6731static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
6732{
6733 uint16_t i, v = 0;
6734 char *argv = args[cur_arg + 1];
6735 if (!*argv) {
6736 if (err)
6737 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
6738 return ERR_ALERT | ERR_FATAL;
6739 }
6740 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
6741 if (!strcmp(argv, methodVersions[i].name))
6742 v = i;
6743 if (!v) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006744 if (err)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006745 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006746 return ERR_ALERT | ERR_FATAL;
6747 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006748 if (!strcmp("ssl-min-ver", args[cur_arg]))
6749 methods->min = v;
6750 else if (!strcmp("ssl-max-ver", args[cur_arg]))
6751 methods->max = v;
6752 else {
6753 if (err)
6754 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
6755 return ERR_ALERT | ERR_FATAL;
6756 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02006757 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02006758}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02006759
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02006760static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
6761{
6762#if !defined(OPENSSL_IS_BORINGSSL)
6763 Warning("crt-list: ssl-min-ver and ssl-max-ver are not supported with this Openssl version (skipped).\n");
6764#endif
6765 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
6766}
6767
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02006768static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6769{
6770 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
6771}
6772
6773static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
6774{
6775 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
6776}
6777
Emeric Brun2d0c4822012-10-02 13:45:20 +02006778/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01006779static 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 +02006780{
Emeric Brun89675492012-10-05 13:48:26 +02006781 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02006782 return 0;
6783}
Emeric Brun2d0c4822012-10-02 13:45:20 +02006784
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02006785/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006786static 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 +02006787{
6788#ifdef OPENSSL_NPN_NEGOTIATED
6789 char *p1, *p2;
6790
6791 if (!*args[cur_arg + 1]) {
6792 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
6793 return ERR_ALERT | ERR_FATAL;
6794 }
6795
6796 free(conf->npn_str);
6797
Willy Tarreau3724da12016-02-12 17:11:12 +01006798 /* the NPN string is built as a suite of (<len> <name>)*,
6799 * so we reuse each comma to store the next <len> and need
6800 * one more for the end of the string.
6801 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02006802 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01006803 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02006804 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
6805
6806 /* replace commas with the name length */
6807 p1 = conf->npn_str;
6808 p2 = p1 + 1;
6809 while (1) {
6810 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
6811 if (!p2)
6812 p2 = p1 + 1 + strlen(p1 + 1);
6813
6814 if (p2 - (p1 + 1) > 255) {
6815 *p2 = '\0';
6816 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
6817 return ERR_ALERT | ERR_FATAL;
6818 }
6819
6820 *p1 = p2 - (p1 + 1);
6821 p1 = p2;
6822
6823 if (!*p2)
6824 break;
6825
6826 *(p2++) = '\0';
6827 }
6828 return 0;
6829#else
6830 if (err)
6831 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
6832 return ERR_ALERT | ERR_FATAL;
6833#endif
6834}
6835
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006836static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6837{
6838 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
6839}
6840
Willy Tarreauab861d32013-04-02 02:30:41 +02006841/* parse the "alpn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006842static 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 +02006843{
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01006844#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02006845 char *p1, *p2;
6846
6847 if (!*args[cur_arg + 1]) {
6848 memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]);
6849 return ERR_ALERT | ERR_FATAL;
6850 }
6851
6852 free(conf->alpn_str);
6853
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01006854 /* the ALPN string is built as a suite of (<len> <name>)*,
6855 * so we reuse each comma to store the next <len> and need
6856 * one more for the end of the string.
6857 */
Willy Tarreauab861d32013-04-02 02:30:41 +02006858 conf->alpn_len = strlen(args[cur_arg + 1]) + 1;
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01006859 conf->alpn_str = calloc(1, conf->alpn_len + 1);
Willy Tarreauab861d32013-04-02 02:30:41 +02006860 memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len);
6861
6862 /* replace commas with the name length */
6863 p1 = conf->alpn_str;
6864 p2 = p1 + 1;
6865 while (1) {
6866 p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1));
6867 if (!p2)
6868 p2 = p1 + 1 + strlen(p1 + 1);
6869
6870 if (p2 - (p1 + 1) > 255) {
6871 *p2 = '\0';
6872 memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
6873 return ERR_ALERT | ERR_FATAL;
6874 }
6875
6876 *p1 = p2 - (p1 + 1);
6877 p1 = p2;
6878
6879 if (!*p2)
6880 break;
6881
6882 *(p2++) = '\0';
6883 }
6884 return 0;
6885#else
6886 if (err)
6887 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
6888 return ERR_ALERT | ERR_FATAL;
6889#endif
6890}
6891
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006892static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6893{
6894 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
6895}
6896
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006897/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02006898static 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 +02006899{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01006900 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02006901 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02006902
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006903 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
6904 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01006905 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02006906 conf->ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
6907 if (!conf->ssl_methods.min)
6908 conf->ssl_methods.min = global_ssl.listen_default_sslmethods.min;
6909 if (!conf->ssl_methods.max)
6910 conf->ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02006911
Willy Tarreau79eeafa2012-09-14 07:53:05 +02006912 return 0;
6913}
6914
Lukas Tribus53ae85c2017-05-04 15:45:40 +00006915/* parse the "prefer-client-ciphers" bind keyword */
6916static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6917{
6918 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
6919 return 0;
6920}
6921
Christopher Faulet31af49d2015-06-09 17:29:50 +02006922/* parse the "generate-certificates" bind keyword */
6923static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6924{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006925#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006926 conf->generate_certs = 1;
6927#else
6928 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
6929 err && *err ? *err : "");
6930#endif
6931 return 0;
6932}
6933
Emmanuel Hocdet65623372013-01-24 17:17:15 +01006934/* parse the "strict-sni" bind keyword */
6935static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6936{
6937 conf->strict_sni = 1;
6938 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01006939}
6940
6941/* parse the "tls-ticket-keys" bind keyword */
6942static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
6943{
6944#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6945 FILE *f;
6946 int i = 0;
6947 char thisline[LINESIZE];
Nenad Merdanovic146defa2015-05-09 08:46:00 +02006948 struct tls_keys_ref *keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01006949
6950 if (!*args[cur_arg + 1]) {
6951 if (err)
6952 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
6953 return ERR_ALERT | ERR_FATAL;
6954 }
6955
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02006956 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
6957 if(keys_ref) {
6958 conf->keys_ref = keys_ref;
6959 return 0;
6960 }
6961
Vincent Bernat02779b62016-04-03 13:48:43 +02006962 keys_ref = malloc(sizeof(*keys_ref));
Nenad Merdanovic146defa2015-05-09 08:46:00 +02006963 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(struct tls_sess_key));
Nenad Merdanovic05552d42015-02-27 19:56:49 +01006964
6965 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
6966 if (err)
6967 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
6968 return ERR_ALERT | ERR_FATAL;
6969 }
6970
Nenad Merdanovic146defa2015-05-09 08:46:00 +02006971 keys_ref->filename = strdup(args[cur_arg + 1]);
6972
Nenad Merdanovic05552d42015-02-27 19:56:49 +01006973 while (fgets(thisline, sizeof(thisline), f) != NULL) {
6974 int len = strlen(thisline);
6975 /* Strip newline characters from the end */
6976 if(thisline[len - 1] == '\n')
6977 thisline[--len] = 0;
6978
6979 if(thisline[len - 1] == '\r')
6980 thisline[--len] = 0;
6981
Nenad Merdanovic146defa2015-05-09 08:46:00 +02006982 if (base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(struct tls_sess_key)) != sizeof(struct tls_sess_key)) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01006983 if (err)
6984 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
mildis16aa0152016-06-22 17:46:29 +02006985 fclose(f);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01006986 return ERR_ALERT | ERR_FATAL;
6987 }
6988 i++;
6989 }
6990
6991 if (i < TLS_TICKETS_NO) {
6992 if (err)
6993 memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO);
mildis16aa0152016-06-22 17:46:29 +02006994 fclose(f);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01006995 return ERR_ALERT | ERR_FATAL;
6996 }
6997
6998 fclose(f);
6999
7000 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01007001 i -= 2;
7002 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02007003 keys_ref->unique_id = -1;
Nenad Merdanovic146defa2015-05-09 08:46:00 +02007004 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01007005
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02007006 LIST_ADD(&tlskeys_reference, &keys_ref->list);
7007
Nenad Merdanovic05552d42015-02-27 19:56:49 +01007008 return 0;
7009#else
7010 if (err)
7011 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
7012 return ERR_ALERT | ERR_FATAL;
7013#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01007014}
7015
Emeric Brund94b3fe2012-09-20 18:23:56 +02007016/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007017static 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 +02007018{
7019 if (!*args[cur_arg + 1]) {
7020 if (err)
7021 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
7022 return ERR_ALERT | ERR_FATAL;
7023 }
7024
7025 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01007026 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02007027 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01007028 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02007029 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01007030 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02007031 else {
7032 if (err)
7033 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
7034 args[cur_arg], args[cur_arg + 1]);
7035 return ERR_ALERT | ERR_FATAL;
7036 }
7037
7038 return 0;
7039}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007040static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7041{
7042 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
7043}
Emeric Brund94b3fe2012-09-20 18:23:56 +02007044
Willy Tarreau92faadf2012-10-10 23:04:25 +02007045/************** "server" keywords ****************/
7046
Emeric Brunef42d922012-10-11 16:11:36 +02007047/* parse the "ca-file" server keyword */
7048static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7049{
7050 if (!*args[*cur_arg + 1]) {
7051 if (err)
7052 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
7053 return ERR_ALERT | ERR_FATAL;
7054 }
7055
Willy Tarreauef934602016-12-22 23:12:01 +01007056 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
7057 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007058 else
7059 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
7060
7061 return 0;
7062}
7063
Willy Tarreau92faadf2012-10-10 23:04:25 +02007064/* parse the "check-ssl" server keyword */
7065static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7066{
7067 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01007068 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
7069 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
7070 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007071 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
7072 if (!newsrv->ssl_ctx.methods.min)
7073 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
7074 if (!newsrv->ssl_ctx.methods.max)
7075 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
7076
Willy Tarreau92faadf2012-10-10 23:04:25 +02007077 return 0;
7078}
7079
7080/* parse the "ciphers" server keyword */
7081static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7082{
7083 if (!*args[*cur_arg + 1]) {
7084 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
7085 return ERR_ALERT | ERR_FATAL;
7086 }
7087
7088 free(newsrv->ssl_ctx.ciphers);
7089 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
7090 return 0;
7091}
7092
Emeric Brunef42d922012-10-11 16:11:36 +02007093/* parse the "crl-file" server keyword */
7094static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7095{
7096#ifndef X509_V_FLAG_CRL_CHECK
7097 if (err)
7098 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
7099 return ERR_ALERT | ERR_FATAL;
7100#else
7101 if (!*args[*cur_arg + 1]) {
7102 if (err)
7103 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
7104 return ERR_ALERT | ERR_FATAL;
7105 }
7106
Willy Tarreauef934602016-12-22 23:12:01 +01007107 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
7108 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007109 else
7110 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
7111
7112 return 0;
7113#endif
7114}
7115
Emeric Bruna7aa3092012-10-26 12:58:00 +02007116/* parse the "crt" server keyword */
7117static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7118{
7119 if (!*args[*cur_arg + 1]) {
7120 if (err)
7121 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
7122 return ERR_ALERT | ERR_FATAL;
7123 }
7124
Willy Tarreauef934602016-12-22 23:12:01 +01007125 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
7126 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02007127 else
7128 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
7129
7130 return 0;
7131}
Emeric Brunef42d922012-10-11 16:11:36 +02007132
Frédéric Lécaille340ae602017-03-13 10:38:04 +01007133/* parse the "no-check-ssl" server keyword */
7134static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7135{
7136 newsrv->check.use_ssl = 0;
7137 free(newsrv->ssl_ctx.ciphers);
7138 newsrv->ssl_ctx.ciphers = NULL;
7139 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
7140 return 0;
7141}
7142
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01007143/* parse the "no-send-proxy-v2-ssl" server keyword */
7144static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7145{
7146 newsrv->pp_opts &= ~SRV_PP_V2;
7147 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
7148 return 0;
7149}
7150
7151/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
7152static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7153{
7154 newsrv->pp_opts &= ~SRV_PP_V2;
7155 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
7156 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
7157 return 0;
7158}
7159
Frédéric Lécaillee381d762017-03-13 11:54:17 +01007160/* parse the "no-ssl" server keyword */
7161static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7162{
7163 newsrv->use_ssl = 0;
7164 free(newsrv->ssl_ctx.ciphers);
7165 newsrv->ssl_ctx.ciphers = NULL;
7166 return 0;
7167}
7168
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01007169/* parse the "no-ssl-reuse" server keyword */
7170static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7171{
7172 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
7173 return 0;
7174}
7175
Emeric Brunf9c5c472012-10-11 15:28:34 +02007176/* parse the "no-tls-tickets" server keyword */
7177static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7178{
7179 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
7180 return 0;
7181}
David Safb76832014-05-08 23:42:08 -04007182/* parse the "send-proxy-v2-ssl" server keyword */
7183static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7184{
7185 newsrv->pp_opts |= SRV_PP_V2;
7186 newsrv->pp_opts |= SRV_PP_V2_SSL;
7187 return 0;
7188}
7189
7190/* parse the "send-proxy-v2-ssl-cn" server keyword */
7191static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7192{
7193 newsrv->pp_opts |= SRV_PP_V2;
7194 newsrv->pp_opts |= SRV_PP_V2_SSL;
7195 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
7196 return 0;
7197}
Emeric Brunf9c5c472012-10-11 15:28:34 +02007198
Willy Tarreau732eac42015-07-09 11:40:25 +02007199/* parse the "sni" server keyword */
7200static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7201{
7202#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
7203 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
7204 return ERR_ALERT | ERR_FATAL;
7205#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01007206 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02007207
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01007208 arg = args[*cur_arg + 1];
7209 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02007210 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
7211 return ERR_ALERT | ERR_FATAL;
7212 }
7213
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01007214 free(newsrv->sni_expr);
7215 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02007216
Willy Tarreau732eac42015-07-09 11:40:25 +02007217 return 0;
7218#endif
7219}
7220
Willy Tarreau92faadf2012-10-10 23:04:25 +02007221/* parse the "ssl" server keyword */
7222static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7223{
7224 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01007225 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
7226 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Willy Tarreau92faadf2012-10-10 23:04:25 +02007227 return 0;
7228}
7229
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01007230/* parse the "ssl-reuse" server keyword */
7231static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7232{
7233 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
7234 return 0;
7235}
7236
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01007237/* parse the "tls-tickets" server keyword */
7238static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7239{
7240 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
7241 return 0;
7242}
7243
Emeric Brunef42d922012-10-11 16:11:36 +02007244/* parse the "verify" server keyword */
7245static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7246{
7247 if (!*args[*cur_arg + 1]) {
7248 if (err)
7249 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
7250 return ERR_ALERT | ERR_FATAL;
7251 }
7252
7253 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01007254 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02007255 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01007256 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02007257 else {
7258 if (err)
7259 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
7260 args[*cur_arg], args[*cur_arg + 1]);
7261 return ERR_ALERT | ERR_FATAL;
7262 }
7263
Evan Broderbe554312013-06-27 00:05:25 -07007264 return 0;
7265}
7266
7267/* parse the "verifyhost" server keyword */
7268static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7269{
7270 if (!*args[*cur_arg + 1]) {
7271 if (err)
7272 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
7273 return ERR_ALERT | ERR_FATAL;
7274 }
7275
Frédéric Lécaille273f3212017-03-13 15:52:01 +01007276 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07007277 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
7278
Emeric Brunef42d922012-10-11 16:11:36 +02007279 return 0;
7280}
7281
Emeric Brun2c86cbf2014-10-30 15:56:50 +01007282/* parse the "ssl-default-bind-options" keyword in global section */
7283static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
7284 struct proxy *defpx, const char *file, int line,
7285 char **err) {
7286 int i = 1;
7287
7288 if (*(args[i]) == 0) {
7289 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
7290 return -1;
7291 }
7292 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007293 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01007294 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00007295 else if (!strcmp(args[i], "prefer-client-ciphers"))
7296 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007297 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
7298 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
7299 i++;
7300 else {
7301 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
7302 return -1;
7303 }
7304 }
7305 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01007306 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
7307 return -1;
7308 }
7309 i++;
7310 }
7311 return 0;
7312}
7313
7314/* parse the "ssl-default-server-options" keyword in global section */
7315static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
7316 struct proxy *defpx, const char *file, int line,
7317 char **err) {
7318 int i = 1;
7319
7320 if (*(args[i]) == 0) {
7321 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
7322 return -1;
7323 }
7324 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007325 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01007326 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007327 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
7328 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
7329 i++;
7330 else {
7331 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
7332 return -1;
7333 }
7334 }
7335 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01007336 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
7337 return -1;
7338 }
7339 i++;
7340 }
7341 return 0;
7342}
7343
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01007344/* parse the "ca-base" / "crt-base" keywords in global section.
7345 * Returns <0 on alert, >0 on warning, 0 on success.
7346 */
7347static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
7348 struct proxy *defpx, const char *file, int line,
7349 char **err)
7350{
7351 char **target;
7352
Willy Tarreauef934602016-12-22 23:12:01 +01007353 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01007354
7355 if (too_many_args(1, args, err, NULL))
7356 return -1;
7357
7358 if (*target) {
7359 memprintf(err, "'%s' already specified.", args[0]);
7360 return -1;
7361 }
7362
7363 if (*(args[1]) == 0) {
7364 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
7365 return -1;
7366 }
7367 *target = strdup(args[1]);
7368 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007369}
7370
7371/* parse the "ssl-mode-async" keyword in global section.
7372 * Returns <0 on alert, >0 on warning, 0 on success.
7373 */
7374static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
7375 struct proxy *defpx, const char *file, int line,
7376 char **err)
7377{
7378#if OPENSSL_VERSION_NUMBER >= 0x1010000fL
7379 global_ssl.async = 1;
7380 return 0;
7381#else
7382 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
7383 return -1;
7384#endif
7385}
7386
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02007387#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007388static int ssl_check_async_engine_count(void) {
7389 int err_code = 0;
7390
Emeric Brun3854e012017-05-17 20:42:48 +02007391 if (global_ssl.async && (openssl_engines_initialized > 32)) {
7392 Alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007393 err_code = ERR_ABORT;
7394 }
7395 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01007396}
7397
Grant Zhang872f9c22017-01-21 01:10:18 +00007398/* parse the "ssl-engine" keyword in global section.
7399 * Returns <0 on alert, >0 on warning, 0 on success.
7400 */
7401static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
7402 struct proxy *defpx, const char *file, int line,
7403 char **err)
7404{
7405 char *algo;
7406 int ret = -1;
7407
7408 if (*(args[1]) == 0) {
7409 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
7410 return ret;
7411 }
7412
7413 if (*(args[2]) == 0) {
7414 /* if no list of algorithms is given, it defaults to ALL */
7415 algo = strdup("ALL");
7416 goto add_engine;
7417 }
7418
7419 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
7420 if (strcmp(args[2], "algo") != 0) {
7421 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
7422 return ret;
7423 }
7424
7425 if (*(args[3]) == 0) {
7426 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
7427 return ret;
7428 }
7429 algo = strdup(args[3]);
7430
7431add_engine:
7432 if (ssl_init_single_engine(args[1], algo)==0) {
7433 openssl_engines_initialized++;
7434 ret = 0;
7435 }
7436 free(algo);
7437 return ret;
7438}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02007439#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00007440
Willy Tarreauf22e9682016-12-21 23:23:19 +01007441/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
7442 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
7443 */
7444static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
7445 struct proxy *defpx, const char *file, int line,
7446 char **err)
7447{
7448 char **target;
7449
Willy Tarreauef934602016-12-22 23:12:01 +01007450 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01007451
7452 if (too_many_args(1, args, err, NULL))
7453 return -1;
7454
7455 if (*(args[1]) == 0) {
7456 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
7457 return -1;
7458 }
7459
7460 free(*target);
7461 *target = strdup(args[1]);
7462 return 0;
7463}
7464
Willy Tarreau9ceda382016-12-21 23:13:03 +01007465/* parse various global tune.ssl settings consisting in positive integers.
7466 * Returns <0 on alert, >0 on warning, 0 on success.
7467 */
7468static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
7469 struct proxy *defpx, const char *file, int line,
7470 char **err)
7471{
7472 int *target;
7473
7474 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
7475 target = &global.tune.sslcachesize;
7476 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01007477 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01007478 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01007479 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01007480 else if (strcmp(args[0], "maxsslconn") == 0)
7481 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007482 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
7483 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01007484 else {
7485 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
7486 return -1;
7487 }
7488
7489 if (too_many_args(1, args, err, NULL))
7490 return -1;
7491
7492 if (*(args[1]) == 0) {
7493 memprintf(err, "'%s' expects an integer argument.", args[0]);
7494 return -1;
7495 }
7496
7497 *target = atoi(args[1]);
7498 if (*target < 0) {
7499 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
7500 return -1;
7501 }
7502 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007503}
7504
7505static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
7506 struct proxy *defpx, const char *file, int line,
7507 char **err)
7508{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007509 int ret;
7510
7511 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
7512 if (ret != 0)
7513 return ret;
7514
7515 if (pool2_ssl_capture) {
7516 memprintf(err, "'%s' is already configured.", args[0]);
7517 return -1;
7518 }
7519
7520 pool2_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
7521 if (!pool2_ssl_capture) {
7522 memprintf(err, "Out of memory error.");
7523 return -1;
7524 }
7525 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01007526}
7527
7528/* parse "ssl.force-private-cache".
7529 * Returns <0 on alert, >0 on warning, 0 on success.
7530 */
7531static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
7532 struct proxy *defpx, const char *file, int line,
7533 char **err)
7534{
7535 if (too_many_args(0, args, err, NULL))
7536 return -1;
7537
Willy Tarreauef934602016-12-22 23:12:01 +01007538 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01007539 return 0;
7540}
7541
7542/* parse "ssl.lifetime".
7543 * Returns <0 on alert, >0 on warning, 0 on success.
7544 */
7545static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
7546 struct proxy *defpx, const char *file, int line,
7547 char **err)
7548{
7549 const char *res;
7550
7551 if (too_many_args(1, args, err, NULL))
7552 return -1;
7553
7554 if (*(args[1]) == 0) {
7555 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
7556 return -1;
7557 }
7558
Willy Tarreauef934602016-12-22 23:12:01 +01007559 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9ceda382016-12-21 23:13:03 +01007560 if (res) {
7561 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
7562 return -1;
7563 }
7564 return 0;
7565}
7566
7567#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01007568/* parse "ssl-dh-param-file".
7569 * Returns <0 on alert, >0 on warning, 0 on success.
7570 */
7571static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
7572 struct proxy *defpx, const char *file, int line,
7573 char **err)
7574{
7575 if (too_many_args(1, args, err, NULL))
7576 return -1;
7577
7578 if (*(args[1]) == 0) {
7579 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
7580 return -1;
7581 }
7582
7583 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
7584 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
7585 return -1;
7586 }
7587 return 0;
7588}
7589
Willy Tarreau9ceda382016-12-21 23:13:03 +01007590/* parse "ssl.default-dh-param".
7591 * Returns <0 on alert, >0 on warning, 0 on success.
7592 */
7593static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
7594 struct proxy *defpx, const char *file, int line,
7595 char **err)
7596{
7597 if (too_many_args(1, args, err, NULL))
7598 return -1;
7599
7600 if (*(args[1]) == 0) {
7601 memprintf(err, "'%s' expects an integer argument.", args[0]);
7602 return -1;
7603 }
7604
Willy Tarreauef934602016-12-22 23:12:01 +01007605 global_ssl.default_dh_param = atoi(args[1]);
7606 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01007607 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
7608 return -1;
7609 }
7610 return 0;
7611}
7612#endif
7613
7614
William Lallemand32af2032016-10-29 18:09:35 +02007615/* This function is used with TLS ticket keys management. It permits to browse
7616 * each reference. The variable <getnext> must contain the current node,
7617 * <end> point to the root node.
7618 */
7619#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
7620static inline
7621struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
7622{
7623 struct tls_keys_ref *ref = getnext;
7624
7625 while (1) {
7626
7627 /* Get next list entry. */
7628 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
7629
7630 /* If the entry is the last of the list, return NULL. */
7631 if (&ref->list == end)
7632 return NULL;
7633
7634 return ref;
7635 }
7636}
7637
7638static inline
7639struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
7640{
7641 int id;
7642 char *error;
7643
7644 /* If the reference starts by a '#', this is numeric id. */
7645 if (reference[0] == '#') {
7646 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
7647 id = strtol(reference + 1, &error, 10);
7648 if (*error != '\0')
7649 return NULL;
7650
7651 /* Perform the unique id lookup. */
7652 return tlskeys_ref_lookupid(id);
7653 }
7654
7655 /* Perform the string lookup. */
7656 return tlskeys_ref_lookup(reference);
7657}
7658#endif
7659
7660
7661#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
7662
7663static int cli_io_handler_tlskeys_files(struct appctx *appctx);
7664
7665static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
7666 return cli_io_handler_tlskeys_files(appctx);
7667}
7668
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007669/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
7670 * (next index to be dumped), and cli.p0 (next key reference).
7671 */
William Lallemand32af2032016-10-29 18:09:35 +02007672static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
7673
7674 struct stream_interface *si = appctx->owner;
7675
7676 switch (appctx->st2) {
7677 case STAT_ST_INIT:
7678 /* Display the column headers. If the message cannot be sent,
7679 * quit the fucntion with returning 0. The function is called
7680 * later and restart at the state "STAT_ST_INIT".
7681 */
7682 chunk_reset(&trash);
7683
7684 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
7685 chunk_appendf(&trash, "# id secret\n");
7686 else
7687 chunk_appendf(&trash, "# id (file)\n");
7688
7689 if (bi_putchk(si_ic(si), &trash) == -1) {
7690 si_applet_cant_put(si);
7691 return 0;
7692 }
7693
William Lallemand32af2032016-10-29 18:09:35 +02007694 /* Now, we start the browsing of the references lists.
7695 * Note that the following call to LIST_ELEM return bad pointer. The only
7696 * available field of this pointer is <list>. It is used with the function
7697 * tlskeys_list_get_next() for retruning the first available entry
7698 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007699 if (appctx->ctx.cli.p0 == NULL) {
7700 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
7701 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02007702 }
7703
7704 appctx->st2 = STAT_ST_LIST;
7705 /* fall through */
7706
7707 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007708 while (appctx->ctx.cli.p0) {
7709 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
7710 int head = ref->tls_ticket_enc_index;
William Lallemand32af2032016-10-29 18:09:35 +02007711
7712 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007713 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +02007714 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007715
7716 if (appctx->ctx.cli.i1 == 0)
7717 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
7718
William Lallemand32af2032016-10-29 18:09:35 +02007719 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007720 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
William Lallemand32af2032016-10-29 18:09:35 +02007721 struct chunk *t2 = get_trash_chunk();
7722
7723 chunk_reset(t2);
7724 /* should never fail here because we dump only a key in the t2 buffer */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007725 t2->len = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
William Lallemand32af2032016-10-29 18:09:35 +02007726 sizeof(struct tls_sess_key), t2->str, t2->size);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007727 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, t2->str);
William Lallemand32af2032016-10-29 18:09:35 +02007728
7729 if (bi_putchk(si_ic(si), &trash) == -1) {
7730 /* let's try again later from this stream. We add ourselves into
7731 * this stream's users so that it can remove us upon termination.
7732 */
7733 si_applet_cant_put(si);
7734 return 0;
7735 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007736 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +02007737 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007738 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +02007739 }
7740 if (bi_putchk(si_ic(si), &trash) == -1) {
7741 /* let's try again later from this stream. We add ourselves into
7742 * this stream's users so that it can remove us upon termination.
7743 */
7744 si_applet_cant_put(si);
7745 return 0;
7746 }
7747
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007748 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +02007749 break;
7750
7751 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007752 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02007753 }
7754
7755 appctx->st2 = STAT_ST_FIN;
7756 /* fall through */
7757
7758 default:
7759 appctx->st2 = STAT_ST_FIN;
7760 return 1;
7761 }
7762 return 0;
7763}
7764
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007765/* sets cli.i0 to non-zero if only file lists should be dumped */
William Lallemand32af2032016-10-29 18:09:35 +02007766static int cli_parse_show_tlskeys(char **args, struct appctx *appctx, void *private)
7767{
William Lallemand32af2032016-10-29 18:09:35 +02007768 /* no parameter, shows only file list */
7769 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007770 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02007771 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01007772 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02007773 }
7774
7775 if (args[2][0] == '*') {
7776 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007777 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02007778 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007779 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
7780 if (!appctx->ctx.cli.p0) {
William Lallemand32af2032016-10-29 18:09:35 +02007781 appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01007782 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02007783 return 1;
7784 }
7785 }
William Lallemand32af2032016-10-29 18:09:35 +02007786 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01007787 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02007788}
7789
William Lallemand32af2032016-10-29 18:09:35 +02007790static int cli_parse_set_tlskeys(char **args, struct appctx *appctx, void *private)
7791{
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007792 struct tls_keys_ref *ref;
7793
William Lallemand32af2032016-10-29 18:09:35 +02007794 /* Expect two parameters: the filename and the new new TLS key in encoding */
7795 if (!*args[3] || !*args[4]) {
7796 appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01007797 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02007798 return 1;
7799 }
7800
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007801 ref = tlskeys_ref_lookup_ref(args[3]);
7802 if (!ref) {
William Lallemand32af2032016-10-29 18:09:35 +02007803 appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01007804 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02007805 return 1;
7806 }
7807
7808 trash.len = base64dec(args[4], strlen(args[4]), trash.str, trash.size);
7809 if (trash.len != sizeof(struct tls_sess_key)) {
7810 appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01007811 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02007812 return 1;
7813 }
7814
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007815 memcpy(ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO), trash.str, trash.len);
7816 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
William Lallemand32af2032016-10-29 18:09:35 +02007817
7818 appctx->ctx.cli.msg = "TLS ticket key updated!";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01007819 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02007820 return 1;
7821
7822}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01007823#endif
William Lallemand32af2032016-10-29 18:09:35 +02007824
7825static int cli_parse_set_ocspresponse(char **args, struct appctx *appctx, void *private)
7826{
7827#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
7828 char *err = NULL;
7829
7830 /* Expect one parameter: the new response in base64 encoding */
7831 if (!*args[3]) {
7832 appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01007833 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02007834 return 1;
7835 }
7836
7837 trash.len = base64dec(args[3], strlen(args[3]), trash.str, trash.size);
7838 if (trash.len < 0) {
7839 appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01007840 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02007841 return 1;
7842 }
7843
7844 if (ssl_sock_update_ocsp_response(&trash, &err)) {
7845 if (err) {
7846 memprintf(&err, "%s.\n", err);
7847 appctx->ctx.cli.err = err;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01007848 appctx->st0 = CLI_ST_PRINT_FREE;
William Lallemand32af2032016-10-29 18:09:35 +02007849 }
7850 return 1;
7851 }
7852 appctx->ctx.cli.msg = "OCSP Response updated!";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01007853 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02007854 return 1;
7855#else
7856 appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01007857 appctx->st0 = CLI_ST_PRINT;
William Lallemand32af2032016-10-29 18:09:35 +02007858 return 1;
7859#endif
7860
7861}
7862
7863/* register cli keywords */
7864static struct cli_kw_list cli_kws = {{ },{
7865#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
7866 { { "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 },
7867 { { "set", "ssl", "tls-keys", NULL }, NULL, cli_parse_set_tlskeys, NULL },
William Lallemand32af2032016-10-29 18:09:35 +02007868#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01007869 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemand32af2032016-10-29 18:09:35 +02007870 { { NULL }, NULL, NULL, NULL }
7871}};
7872
7873
Willy Tarreau7875d092012-09-10 08:20:03 +02007874/* Note: must not be declared <const> as its list will be overwritten.
7875 * Please take care of keeping this list alphabetically sorted.
7876 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02007877static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +02007878 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02007879 { "ssl_bc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV },
Emeric Brun645ae792014-04-30 14:21:06 +02007880 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
7881 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +02007882 { "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 +02007883 { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV },
Emeric Brun645ae792014-04-30 14:21:06 +02007884 { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02007885 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
7886 { "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 +01007887 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02007888 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02007889 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
7890 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
7891 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
7892 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
7893 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
7894 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
7895 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
7896 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01007897 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02007898 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
7899 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +01007900 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +02007901 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
7902 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
7903 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
7904 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
7905 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
7906 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI },
7907 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +02007908 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02007909 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01007910 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02007911 { "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 +01007912 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +01007913 { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
7914 { "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 +02007915 { "ssl_fc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +02007916#ifdef OPENSSL_NPN_NEGOTIATED
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007917 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +02007918#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01007919#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007920 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +02007921#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007922 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunb73a9b02014-04-30 18:49:19 +02007923 { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02007924 { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007925 { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
7926 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007927 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
7928 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
7929 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
7930 { "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 +02007931 { NULL, NULL, 0, 0, 0 },
7932}};
7933
7934/* Note: must not be declared <const> as its list will be overwritten.
7935 * Please take care of keeping this list alphabetically sorted.
7936 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02007937static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +01007938 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
7939 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +01007940 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +02007941}};
7942
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007943/* Note: must not be declared <const> as its list will be overwritten.
7944 * Please take care of keeping this list alphabetically sorted, doing so helps
7945 * all code contributors.
7946 * Optional keywords are also declared with a NULL ->parse() function so that
7947 * the config parser can report an appropriate error when a known keyword was
7948 * not enabled.
7949 */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007950static struct ssl_bind_kw ssl_bind_kws[] = {
7951 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
7952 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
7953 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
7954 { "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 +01007955 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007956 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007957 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02007958 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
7959 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007960 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
7961 { NULL, NULL, 0 },
7962};
7963
Willy Tarreau51fb7652012-09-18 18:24:39 +02007964static struct bind_kw_list bind_kws = { "SSL", { }, {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007965 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
7966 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */
7967 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
7968 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
7969 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
7970 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
7971 { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */
7972 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
7973 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */
7974 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
7975 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
7976 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
7977 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
7978 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
7979 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
7980 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02007981 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007982 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
7983 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
7984 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
7985 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
7986 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02007987 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007988 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
7989 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007990 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
7991 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007992 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
7993 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
7994 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
7995 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
7996 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007997 { NULL, NULL, 0 },
7998}};
Emeric Brun46591952012-05-18 15:47:34 +02007999
Willy Tarreau92faadf2012-10-10 23:04:25 +02008000/* Note: must not be declared <const> as its list will be overwritten.
8001 * Please take care of keeping this list alphabetically sorted, doing so helps
8002 * all code contributors.
8003 * Optional keywords are also declared with a NULL ->parse() function so that
8004 * the config parser can report an appropriate error when a known keyword was
8005 * not enabled.
8006 */
8007static struct srv_kw_list srv_kws = { "SSL", { }, {
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008008 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
8009 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
8010 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
8011 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
8012 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
8013 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
8014 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
8015 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
8016 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
8017 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
8018 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
8019 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
8020 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
8021 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
8022 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
8023 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
8024 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
8025 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
8026 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
8027 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
8028 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
8029 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
8030 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
8031 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
8032 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
8033 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
8034 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
8035 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
8036 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
8037 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
8038 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +02008039 { NULL, NULL, 0, 0 },
8040}};
8041
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008042static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008043 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
8044 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Willy Tarreau0bea58d2016-12-21 23:17:25 +01008045 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008046 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
8047 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +01008048#ifndef OPENSSL_NO_DH
8049 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
8050#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008051 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008052#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00008053 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008054#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +01008055 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
8056#ifndef OPENSSL_NO_DH
8057 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
8058#endif
8059 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
8060 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
8061 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
8062 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008063 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +01008064 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
8065 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008066 { 0, NULL, NULL },
8067}};
8068
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02008069/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +01008070static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02008071 .snd_buf = ssl_sock_from_buf,
8072 .rcv_buf = ssl_sock_to_buf,
8073 .rcv_pipe = NULL,
8074 .snd_pipe = NULL,
8075 .shutr = NULL,
8076 .shutw = ssl_sock_shutw,
8077 .close = ssl_sock_close,
8078 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +01008079 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +01008080 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +01008081 .prepare_srv = ssl_sock_prepare_srv_ctx,
8082 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +01008083 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01008084 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +02008085};
8086
Daniel Jakots54ffb912015-11-06 20:02:41 +01008087#if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01008088
8089static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
8090{
8091 if (ptr) {
8092 chunk_destroy(ptr);
8093 free(ptr);
8094 }
8095}
8096
8097#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01008098static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
8099{
8100 pool_free2(pool2_ssl_capture, ptr);
8101}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01008102
Emeric Brun46591952012-05-18 15:47:34 +02008103__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +02008104static void __ssl_sock_init(void)
8105{
Willy Tarreauc2c0b612016-12-21 19:23:20 +01008106 char *ptr;
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +02008107 int i;
Willy Tarreauc2c0b612016-12-21 19:23:20 +01008108
Emeric Brun46591952012-05-18 15:47:34 +02008109 STACK_OF(SSL_COMP)* cm;
8110
Willy Tarreauef934602016-12-22 23:12:01 +01008111 if (global_ssl.listen_default_ciphers)
8112 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
8113 if (global_ssl.connect_default_ciphers)
8114 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Willy Tarreau610f04b2014-02-13 11:36:41 +01008115
Willy Tarreau13e14102016-12-22 20:25:26 +01008116 xprt_register(XPRT_SSL, &ssl_sock);
Emeric Brun46591952012-05-18 15:47:34 +02008117 SSL_library_init();
8118 cm = SSL_COMP_get_compression_methods();
8119 sk_SSL_COMP_zero(cm);
Daniel Jakots54ffb912015-11-06 20:02:41 +01008120#if (OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01008121 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
8122#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01008123 ssl_capture_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func);
Willy Tarreau7875d092012-09-10 08:20:03 +02008124 sample_register_fetches(&sample_fetch_keywords);
8125 acl_register_keywords(&acl_kws);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008126 bind_register_keywords(&bind_kws);
Willy Tarreau92faadf2012-10-10 23:04:25 +02008127 srv_register_keywords(&srv_kws);
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008128 cfg_register_keywords(&cfg_kws);
William Lallemand32af2032016-10-29 18:09:35 +02008129 cli_register_kw(&cli_kws);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008130#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00008131 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008132 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008133#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +01008134#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
8135 hap_register_post_check(tlskeys_finalize_config);
8136#endif
Willy Tarreaud92aa5c2015-01-15 21:34:39 +01008137
Willy Tarreauc2c0b612016-12-21 19:23:20 +01008138 ptr = NULL;
8139 memprintf(&ptr, "Built with OpenSSL version : "
8140#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +01008141 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +01008142#else /* OPENSSL_IS_BORINGSSL */
8143 OPENSSL_VERSION_TEXT
8144 "\nRunning on OpenSSL version : %s%s",
8145 SSLeay_version(SSLEAY_VERSION),
8146 ((OPENSSL_VERSION_NUMBER ^ SSLeay()) >> 8) ? " (VERSIONS DIFFER!)" : "");
8147#endif
8148 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
8149#if OPENSSL_VERSION_NUMBER < 0x00907000L
8150 "no (library version too old)"
8151#elif defined(OPENSSL_NO_TLSEXT)
8152 "no (disabled via OPENSSL_NO_TLSEXT)"
8153#else
8154 "yes"
8155#endif
8156 "", ptr);
8157
8158 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
8159#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
8160 "yes"
8161#else
8162#ifdef OPENSSL_NO_TLSEXT
8163 "no (because of OPENSSL_NO_TLSEXT)"
8164#else
8165 "no (version might be too old, 0.9.8f min needed)"
8166#endif
8167#endif
8168 "", ptr);
8169
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +02008170 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
8171 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
8172 if (methodVersions[i].option)
8173 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +01008174
Willy Tarreauc2c0b612016-12-21 19:23:20 +01008175 hap_register_build_opts(ptr, 1);
8176
Willy Tarreaud92aa5c2015-01-15 21:34:39 +01008177 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
8178 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
Remi Gacogne4f902b82015-05-28 16:23:00 +02008179
8180#ifndef OPENSSL_NO_DH
8181 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Grant Zhang872f9c22017-01-21 01:10:18 +00008182 hap_register_post_deinit(ssl_free_dh);
Remi Gacogne4f902b82015-05-28 16:23:00 +02008183#endif
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008184#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00008185 hap_register_post_deinit(ssl_free_engines);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008186#endif
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02008187 /* Load SSL string for the verbose & debug mode. */
8188 ERR_load_SSL_strings();
Emeric Brun46591952012-05-18 15:47:34 +02008189}
8190
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008191#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00008192void ssl_free_engines(void) {
8193 struct ssl_engine_list *wl, *wlb;
8194 /* free up engine list */
8195 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
8196 ENGINE_finish(wl->e);
8197 ENGINE_free(wl->e);
8198 LIST_DEL(&wl->list);
8199 free(wl);
8200 }
8201}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008202#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +02008203
Remi Gacogned3a23c32015-05-28 16:39:47 +02008204#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +00008205void ssl_free_dh(void) {
8206 if (local_dh_1024) {
8207 DH_free(local_dh_1024);
8208 local_dh_1024 = NULL;
8209 }
8210 if (local_dh_2048) {
8211 DH_free(local_dh_2048);
8212 local_dh_2048 = NULL;
8213 }
8214 if (local_dh_4096) {
8215 DH_free(local_dh_4096);
8216 local_dh_4096 = NULL;
8217 }
Remi Gacogne47783ef2015-05-29 15:53:22 +02008218 if (global_dh) {
8219 DH_free(global_dh);
8220 global_dh = NULL;
8221 }
Grant Zhang872f9c22017-01-21 01:10:18 +00008222}
8223#endif
8224
8225__attribute__((destructor))
8226static void __ssl_sock_deinit(void)
8227{
8228#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
8229 lru64_destroy(ssl_ctx_lru_tree);
Remi Gacogned3a23c32015-05-28 16:39:47 +02008230#endif
8231
8232 ERR_remove_state(0);
8233 ERR_free_strings();
8234
8235 EVP_cleanup();
8236
8237#if OPENSSL_VERSION_NUMBER >= 0x00907000L
8238 CRYPTO_cleanup_all_ex_data();
8239#endif
8240}
8241
8242
Emeric Brun46591952012-05-18 15:47:34 +02008243/*
8244 * Local variables:
8245 * c-indent-level: 8
8246 * c-basic-offset: 8
8247 * End:
8248 */