blob: efa8dfaaff8b8e009ade285471b0eba43f88ba10 [file] [log] [blame]
Emeric Brunfc0421f2012-09-07 17:30:07 +02001/*
2 * include/types/ssl_sock.h
3 * SSL settings for listeners and servers
4 *
5 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef _TYPES_SSL_SOCK_H
23#define _TYPES_SSL_SOCK_H
Willy Tarreauc125cef2019-05-10 09:58:43 +020024#ifdef USE_OPENSSL
Emeric Brunfc0421f2012-09-07 17:30:07 +020025
William Lallemand2954c472020-03-06 21:54:13 +010026#include <ebpttree.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020027#include <ebmbtree.h>
William Lallemande0f3fd52020-02-25 14:53:06 +010028#include <eb64tree.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020029
Christopher Faulet16f45c82018-02-16 11:23:49 +010030#include <common/hathreads.h>
Willy Tarreau8dd0d552020-03-06 18:43:08 +010031#include <common/mini-clist.h>
Willy Tarreau55994562019-05-09 14:52:44 +020032#include <common/openssl-compat.h>
Christopher Faulet16f45c82018-02-16 11:23:49 +010033
William Lallemand7fd8b452020-05-07 15:20:43 +020034/* ***** READ THIS before adding code here! *****
35 *
36 * Due to API incompatibilities between multiple OpenSSL versions and their
37 * derivatives, it's often tempting to add macros to (re-)define certain
38 * symbols. Please do not do this here, and do it in common/openssl-compat.h
39 * exclusively so that the whole code consistently uses the same macros.
40 *
41 * Whenever possible if a macro is missing in certain versions, it's better
42 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
43 */
44
45/* Warning, these are bits, not integers! */
46#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
47#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
48#define SSL_SOCK_SEND_UNLIMITED 0x00000004
49#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
50
51/* bits 0xFFFF0000 are reserved to store verify errors */
52
53/* Verify errors macros */
54#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
55#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
56#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
57
58#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
59#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
60#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
61
62/* ssl_methods flags for ssl options */
63#define MC_SSL_O_ALL 0x0000
64#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
65#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
66#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
67#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
68#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
69
70/* file to guess during file loading */
71#define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */
72#define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */
73#define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */
74#define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */
75#define SSL_GF_OCSP_ISSUER 0x00000008 /* try to open the .issuer file if an OCSP file was loaded */
76#define SSL_GF_KEY 0x00000010 /* try to open the .key file to load a private key */
77
78#define SSL_GF_ALL (SSL_GF_BUNDLE|SSL_GF_SCTL|SSL_GF_OCSP|SSL_GF_OCSP_ISSUER|SSL_GF_KEY)
79
80/* ssl_methods versions */
81enum {
82 CONF_TLSV_NONE = 0,
83 CONF_TLSV_MIN = 1,
84 CONF_SSLV3 = 1,
85 CONF_TLSV10 = 2,
86 CONF_TLSV11 = 3,
87 CONF_TLSV12 = 4,
88 CONF_TLSV13 = 5,
89 CONF_TLSV_MAX = 5,
90};
91
92#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
93typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
94#else /* openssl >= 1.1.0 */
95typedef enum { SET_MIN, SET_MAX } set_context_func;
96#endif
97
98struct methodVersions {
99 int option;
100 uint16_t flag;
101 void (*ctx_set_version)(SSL_CTX *, set_context_func);
102 void (*ssl_set_version)(SSL *, set_context_func);
103 const char *name;
104};
105
106/* server and bind verify method, it uses a global value as default */
107enum {
108 SSL_SOCK_VERIFY_DEFAULT = 0,
109 SSL_SOCK_VERIFY_REQUIRED = 1,
110 SSL_SOCK_VERIFY_OPTIONAL = 2,
111 SSL_SOCK_VERIFY_NONE = 3,
112};
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200113
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +0200114struct pkey_info {
115 uint8_t sig; /* TLSEXT_signature_[rsa,ecdsa,...] */
116 uint16_t bits; /* key size in bits */
117};
118
Emeric Brunfc0421f2012-09-07 17:30:07 +0200119struct sni_ctx {
120 SSL_CTX *ctx; /* context associated to the certificate */
121 int order; /* load order for the certificate */
Tim Duesterhus6a0dd732020-01-18 01:32:49 +0100122 unsigned int neg:1; /* reject if match */
123 unsigned int wild:1; /* wildcard sni */
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +0200124 struct pkey_info kinfo; /* pkey info */
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100125 struct ssl_bind_conf *conf; /* ssl "bind" conf for the certificate */
William Lallemand9117de92019-10-04 00:29:42 +0200126 struct list by_ckch_inst; /* chained in ckch_inst's list of sni_ctx */
William Lallemandcfca1422020-03-05 10:17:47 +0100127 struct ckch_inst *ckch_inst; /* instance used to create this sni_ctx */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200128 struct ebmb_node name; /* node holding the servername value */
129};
130
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200131struct tls_version_filter {
132 uint16_t flags; /* ssl options */
133 uint8_t min; /* min TLS version */
134 uint8_t max; /* max TLS version */
135};
136
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200137extern struct list tlskeys_reference;
138
Emeric Brun9e754772019-01-10 17:51:55 +0100139struct tls_sess_key_128 {
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100140 unsigned char name[16];
141 unsigned char aes_key[16];
142 unsigned char hmac_key[16];
143} __attribute__((packed));
144
Emeric Brun9e754772019-01-10 17:51:55 +0100145struct tls_sess_key_256 {
146 unsigned char name[16];
147 unsigned char aes_key[32];
148 unsigned char hmac_key[32];
149} __attribute__((packed));
150
151union tls_sess_key{
152 unsigned char name[16];
Nenad Merdanovic8ef70652019-04-14 16:06:46 +0200153 struct tls_sess_key_128 key_128;
Emeric Brun9e754772019-01-10 17:51:55 +0100154 struct tls_sess_key_256 key_256;
155} __attribute__((packed));
156
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200157struct tls_keys_ref {
158 struct list list; /* Used to chain refs. */
159 char *filename;
160 int unique_id; /* Each pattern reference have unique id. */
Willy Tarreau17b4aa12018-07-17 10:05:32 +0200161 int refcount; /* number of users of this tls_keys_ref. */
Emeric Brun9e754772019-01-10 17:51:55 +0100162 union tls_sess_key *tlskeys;
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200163 int tls_ticket_enc_index;
Emeric Brun9e754772019-01-10 17:51:55 +0100164 int key_size_bits;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100165 __decl_hathreads(HA_RWLOCK_T lock); /* lock used to protect the ref */
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200166};
167
William Lallemand4f45bb92017-10-30 20:08:51 +0100168/* shared ssl session */
169struct sh_ssl_sess_hdr {
170 struct ebmb_node key;
171 unsigned char key_data[SSL_MAX_SSL_SESSION_ID_LENGTH];
172};
173
William Lallemandad3c37b2020-03-25 20:20:26 +0100174/* states of the CLI IO handler for 'set ssl cert' */
175enum {
176 SETCERT_ST_INIT = 0,
177 SETCERT_ST_GEN,
178 SETCERT_ST_INSERT,
179 SETCERT_ST_FIN,
180};
181
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500182/* This is used to preload the certificate, private key
William Lallemandf6adbe92019-09-11 16:33:52 +0200183 * and Cert Chain of a file passed in via the crt
184 * argument
185 *
186 * This way, we do not have to read the file multiple times
187 */
188struct cert_key_and_chain {
189 X509 *cert;
190 EVP_PKEY *key;
191 STACK_OF(X509) *chain;
192 DH *dh;
William Lallemanda17f4112019-10-10 15:16:44 +0200193 struct buffer *sctl;
William Lallemand246c0242019-10-11 08:59:13 +0200194 struct buffer *ocsp_response;
195 X509 *ocsp_issuer;
William Lallemandf6adbe92019-09-11 16:33:52 +0200196};
197
198/*
199 * this is used to store 1 to SSL_SOCK_NUM_KEYTYPES cert_key_and_chain and
200 * metadata.
201 */
202struct ckch_store {
203 struct cert_key_and_chain *ckch;
Tim Duesterhus6a0dd732020-01-18 01:32:49 +0100204 unsigned int multi:1; /* is it a multi-cert bundle ? */
William Lallemand9117de92019-10-04 00:29:42 +0200205 struct list ckch_inst; /* list of ckch_inst which uses this ckch_node */
William Lallemand23d61c02020-03-30 18:27:58 +0200206 struct list crtlist_entry; /* list of entries which use this store */
William Lallemandf6adbe92019-09-11 16:33:52 +0200207 struct ebmb_node node;
208 char path[0];
209};
210
William Lallemand9117de92019-10-04 00:29:42 +0200211/*
212 * This structure describe a ckch instance. An instance is generated for each
213 * bind_conf. The instance contains a linked list of the sni ctx which uses
214 * the ckch in this bind_conf.
William Lallemand9117de92019-10-04 00:29:42 +0200215 */
216struct ckch_inst {
217 struct bind_conf *bind_conf; /* pointer to the bind_conf that uses this ckch_inst */
William Lallemand150bfa82019-09-19 17:12:49 +0200218 struct ssl_bind_conf *ssl_conf; /* pointer to the ssl_conf which is used by every sni_ctx of this inst */
William Lallemandcfca1422020-03-05 10:17:47 +0100219 struct ckch_store *ckch_store; /* pointer to the store used to generate this inst */
William Lallemandcaa16192020-04-08 16:29:15 +0200220 struct crtlist_entry *crtlist_entry; /* pointer to the crtlist_entry used, or NULL */
William Lallemand0a528462020-03-09 16:53:42 +0100221 unsigned int is_default:1; /* This instance is used as the default ctx for this bind_conf */
222 /* space for more flag there */
William Lallemand9117de92019-10-04 00:29:42 +0200223 struct list sni_ctx; /* list of sni_ctx using this ckch_inst */
224 struct list by_ckchs; /* chained in ckch_store's list of ckch_inst */
William Lallemand2954c472020-03-06 21:54:13 +0100225 struct list by_crtlist_entry; /* chained in crtlist_entry list of inst */
226};
227
William Lallemand79d31ec2020-03-25 15:10:49 +0100228/* list of bind conf used by struct crtlist */
229struct bind_conf_list {
230 struct bind_conf *bind_conf;
231 struct bind_conf_list *next;
232};
233
William Lallemand2954c472020-03-06 21:54:13 +0100234/* This structure is basically a crt-list or a directory */
235struct crtlist {
William Lallemand79d31ec2020-03-25 15:10:49 +0100236 struct bind_conf_list *bind_conf; /* list of bind_conf which use this crtlist */
William Lallemandc69f02d2020-04-06 19:07:03 +0200237 unsigned int linecount; /* number of lines */
William Lallemand2954c472020-03-06 21:54:13 +0100238 struct eb_root entries;
239 struct list ord_entries; /* list to keep the line order of the crt-list file */
240 struct ebmb_node node; /* key is the filename or directory */
241};
242
243/* a file in a directory or a line in a crt-list */
244struct crtlist_entry {
245 struct ssl_bind_conf *ssl_conf; /* SSL conf in crt-list */
246 unsigned int linenum;
247 unsigned int fcount; /* filters count */
248 char **filters;
William Lallemandfa8cf0c2020-03-30 19:59:57 +0200249 struct crtlist *crtlist; /* ptr to the parent crtlist */
William Lallemand49398312020-03-30 17:01:33 +0200250 struct list ckch_inst; /* list of instances of this entry, there is 1 ckch_inst per instance of the crt-list */
William Lallemand2954c472020-03-06 21:54:13 +0100251 struct list by_crtlist; /* ordered entries */
William Lallemand23d61c02020-03-30 18:27:58 +0200252 struct list by_ckch_store; /* linked in ckch_store list of crtlist_entries */
William Lallemand2954c472020-03-06 21:54:13 +0100253 struct ebpt_node node; /* key is a ptr to a ckch_store */
William Lallemand9117de92019-10-04 00:29:42 +0200254};
William Lallemandf6adbe92019-09-11 16:33:52 +0200255
256#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
257
258#define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES))
259
260struct key_combo_ctx {
261 SSL_CTX *ctx;
262 int order;
263};
264
265/* Map used for processing multiple keypairs for a single purpose
266 *
267 * This maps CN/SNI name to certificate type
268 */
269struct sni_keytype {
270 int keytypes; /* BITMASK for keytypes */
271 struct ebmb_node name; /* node holding the servername value */
272};
273
274#endif
275
William Lallemande0f3fd52020-02-25 14:53:06 +0100276/* issuer chain store with hash of Subject Key Identifier
277 certificate/issuer matching is verify with X509_check_issued
278*/
279struct issuer_chain {
280 struct eb64_node node;
281 STACK_OF(X509) *chain;
282 char *path;
283};
284
William Lallemand7fd8b452020-05-07 15:20:43 +0200285struct connection;
286
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200287typedef void (*ssl_sock_msg_callback_func)(struct connection *conn,
288 int write_p, int version, int content_type,
289 const void *buf, size_t len, SSL *ssl);
290
291/* This structure contains a function pointer <func> that is called
292 * when observing received or sent SSL/TLS protocol messages, such as
293 * handshake messages or other events that can occur during processing.
294 */
295struct ssl_sock_msg_callback {
296 ssl_sock_msg_callback_func func;
297 struct list list; /* list of registered callbacks */
298};
William Lallemande0f3fd52020-02-25 14:53:06 +0100299
William Lallemand7fd8b452020-05-07 15:20:43 +0200300/* This memory pool is used for capturing clienthello parameters. */
301struct ssl_capture {
302 unsigned long long int xxh64;
303 unsigned char ciphersuite_len;
304 char ciphersuite[0];
305};
306
307struct global_ssl {
308 char *crt_base; /* base directory path for certificates */
309 char *ca_base; /* base directory path for CAs and CRLs */
310 char *issuers_chain_path; /* from "issuers-chain-path" */
311 int skip_self_issued_ca;
312
313 int async; /* whether we use ssl async mode */
314
315 char *listen_default_ciphers;
316 char *connect_default_ciphers;
317#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
318 char *listen_default_ciphersuites;
319 char *connect_default_ciphersuites;
320#endif
321#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
322 char *listen_default_curves;
323#endif
324 int listen_default_ssloptions;
325 int connect_default_ssloptions;
326 struct tls_version_filter listen_default_sslmethods;
327 struct tls_version_filter connect_default_sslmethods;
328
329 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
330 unsigned int life_time; /* SSL session lifetime in seconds */
331 unsigned int max_record; /* SSL max record size */
332 unsigned int default_dh_param; /* SSL maximum DH parameter size */
333 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
334 int capture_cipherlist; /* Size of the cipherlist buffer. */
335 int extra_files; /* which files not defined in the configuration file are we looking for */
336};
337
Willy Tarreauc125cef2019-05-10 09:58:43 +0200338#endif /* USE_OPENSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200339#endif /* _TYPES_SSL_SOCK_H */