blob: 1c95eb7e55e0282a194483688c77bbc0077e5191 [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
William Lallemandbe21b662020-05-12 14:17:23 +020030#include <types/ssl_crtlist.h>
31
Christopher Faulet16f45c82018-02-16 11:23:49 +010032#include <common/hathreads.h>
Willy Tarreau8dd0d552020-03-06 18:43:08 +010033#include <common/mini-clist.h>
Willy Tarreau55994562019-05-09 14:52:44 +020034#include <common/openssl-compat.h>
Christopher Faulet16f45c82018-02-16 11:23:49 +010035
William Lallemand7fd8b452020-05-07 15:20:43 +020036/* ***** READ THIS before adding code here! *****
37 *
38 * Due to API incompatibilities between multiple OpenSSL versions and their
39 * derivatives, it's often tempting to add macros to (re-)define certain
40 * symbols. Please do not do this here, and do it in common/openssl-compat.h
41 * exclusively so that the whole code consistently uses the same macros.
42 *
43 * Whenever possible if a macro is missing in certain versions, it's better
44 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
45 */
46
47/* Warning, these are bits, not integers! */
48#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
49#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
50#define SSL_SOCK_SEND_UNLIMITED 0x00000004
51#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
52
53/* bits 0xFFFF0000 are reserved to store verify errors */
54
55/* Verify errors macros */
56#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
57#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
58#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
59
60#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
61#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
62#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
63
64/* ssl_methods flags for ssl options */
65#define MC_SSL_O_ALL 0x0000
66#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
67#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
68#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
69#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
70#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
71
72/* file to guess during file loading */
73#define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */
74#define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */
75#define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */
76#define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */
77#define SSL_GF_OCSP_ISSUER 0x00000008 /* try to open the .issuer file if an OCSP file was loaded */
78#define SSL_GF_KEY 0x00000010 /* try to open the .key file to load a private key */
79
80#define SSL_GF_ALL (SSL_GF_BUNDLE|SSL_GF_SCTL|SSL_GF_OCSP|SSL_GF_OCSP_ISSUER|SSL_GF_KEY)
81
82/* ssl_methods versions */
83enum {
84 CONF_TLSV_NONE = 0,
85 CONF_TLSV_MIN = 1,
86 CONF_SSLV3 = 1,
87 CONF_TLSV10 = 2,
88 CONF_TLSV11 = 3,
89 CONF_TLSV12 = 4,
90 CONF_TLSV13 = 5,
91 CONF_TLSV_MAX = 5,
92};
93
94#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
95typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
96#else /* openssl >= 1.1.0 */
97typedef enum { SET_MIN, SET_MAX } set_context_func;
98#endif
99
100struct methodVersions {
101 int option;
102 uint16_t flag;
103 void (*ctx_set_version)(SSL_CTX *, set_context_func);
104 void (*ssl_set_version)(SSL *, set_context_func);
105 const char *name;
106};
107
108/* server and bind verify method, it uses a global value as default */
109enum {
110 SSL_SOCK_VERIFY_DEFAULT = 0,
111 SSL_SOCK_VERIFY_REQUIRED = 1,
112 SSL_SOCK_VERIFY_OPTIONAL = 2,
113 SSL_SOCK_VERIFY_NONE = 3,
114};
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200115
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +0200116struct pkey_info {
117 uint8_t sig; /* TLSEXT_signature_[rsa,ecdsa,...] */
118 uint16_t bits; /* key size in bits */
119};
120
Emeric Brunfc0421f2012-09-07 17:30:07 +0200121struct sni_ctx {
122 SSL_CTX *ctx; /* context associated to the certificate */
123 int order; /* load order for the certificate */
Tim Duesterhus6a0dd732020-01-18 01:32:49 +0100124 unsigned int neg:1; /* reject if match */
125 unsigned int wild:1; /* wildcard sni */
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +0200126 struct pkey_info kinfo; /* pkey info */
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100127 struct ssl_bind_conf *conf; /* ssl "bind" conf for the certificate */
William Lallemand9117de92019-10-04 00:29:42 +0200128 struct list by_ckch_inst; /* chained in ckch_inst's list of sni_ctx */
William Lallemandcfca1422020-03-05 10:17:47 +0100129 struct ckch_inst *ckch_inst; /* instance used to create this sni_ctx */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200130 struct ebmb_node name; /* node holding the servername value */
131};
132
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200133struct tls_version_filter {
134 uint16_t flags; /* ssl options */
135 uint8_t min; /* min TLS version */
136 uint8_t max; /* max TLS version */
137};
138
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200139extern struct list tlskeys_reference;
140
Emeric Brun9e754772019-01-10 17:51:55 +0100141struct tls_sess_key_128 {
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100142 unsigned char name[16];
143 unsigned char aes_key[16];
144 unsigned char hmac_key[16];
145} __attribute__((packed));
146
Emeric Brun9e754772019-01-10 17:51:55 +0100147struct tls_sess_key_256 {
148 unsigned char name[16];
149 unsigned char aes_key[32];
150 unsigned char hmac_key[32];
151} __attribute__((packed));
152
153union tls_sess_key{
154 unsigned char name[16];
Nenad Merdanovic8ef70652019-04-14 16:06:46 +0200155 struct tls_sess_key_128 key_128;
Emeric Brun9e754772019-01-10 17:51:55 +0100156 struct tls_sess_key_256 key_256;
157} __attribute__((packed));
158
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200159struct tls_keys_ref {
160 struct list list; /* Used to chain refs. */
161 char *filename;
162 int unique_id; /* Each pattern reference have unique id. */
Willy Tarreau17b4aa12018-07-17 10:05:32 +0200163 int refcount; /* number of users of this tls_keys_ref. */
Emeric Brun9e754772019-01-10 17:51:55 +0100164 union tls_sess_key *tlskeys;
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200165 int tls_ticket_enc_index;
Emeric Brun9e754772019-01-10 17:51:55 +0100166 int key_size_bits;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100167 __decl_hathreads(HA_RWLOCK_T lock); /* lock used to protect the ref */
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200168};
169
William Lallemand4f45bb92017-10-30 20:08:51 +0100170/* shared ssl session */
171struct sh_ssl_sess_hdr {
172 struct ebmb_node key;
173 unsigned char key_data[SSL_MAX_SSL_SESSION_ID_LENGTH];
174};
175
William Lallemandad3c37b2020-03-25 20:20:26 +0100176/* states of the CLI IO handler for 'set ssl cert' */
177enum {
178 SETCERT_ST_INIT = 0,
179 SETCERT_ST_GEN,
180 SETCERT_ST_INSERT,
181 SETCERT_ST_FIN,
182};
183
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500184/* This is used to preload the certificate, private key
William Lallemandf6adbe92019-09-11 16:33:52 +0200185 * and Cert Chain of a file passed in via the crt
186 * argument
187 *
188 * This way, we do not have to read the file multiple times
189 */
190struct cert_key_and_chain {
191 X509 *cert;
192 EVP_PKEY *key;
193 STACK_OF(X509) *chain;
194 DH *dh;
William Lallemanda17f4112019-10-10 15:16:44 +0200195 struct buffer *sctl;
William Lallemand246c0242019-10-11 08:59:13 +0200196 struct buffer *ocsp_response;
197 X509 *ocsp_issuer;
William Lallemandf6adbe92019-09-11 16:33:52 +0200198};
199
200/*
201 * this is used to store 1 to SSL_SOCK_NUM_KEYTYPES cert_key_and_chain and
202 * metadata.
203 */
204struct ckch_store {
205 struct cert_key_and_chain *ckch;
Tim Duesterhus6a0dd732020-01-18 01:32:49 +0100206 unsigned int multi:1; /* is it a multi-cert bundle ? */
William Lallemand9117de92019-10-04 00:29:42 +0200207 struct list ckch_inst; /* list of ckch_inst which uses this ckch_node */
William Lallemand23d61c02020-03-30 18:27:58 +0200208 struct list crtlist_entry; /* list of entries which use this store */
William Lallemandf6adbe92019-09-11 16:33:52 +0200209 struct ebmb_node node;
210 char path[0];
211};
212
William Lallemand9117de92019-10-04 00:29:42 +0200213/*
214 * This structure describe a ckch instance. An instance is generated for each
215 * bind_conf. The instance contains a linked list of the sni ctx which uses
216 * the ckch in this bind_conf.
William Lallemand9117de92019-10-04 00:29:42 +0200217 */
218struct ckch_inst {
219 struct bind_conf *bind_conf; /* pointer to the bind_conf that uses this ckch_inst */
William Lallemand150bfa82019-09-19 17:12:49 +0200220 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 +0100221 struct ckch_store *ckch_store; /* pointer to the store used to generate this inst */
William Lallemandcaa16192020-04-08 16:29:15 +0200222 struct crtlist_entry *crtlist_entry; /* pointer to the crtlist_entry used, or NULL */
William Lallemand0a528462020-03-09 16:53:42 +0100223 unsigned int is_default:1; /* This instance is used as the default ctx for this bind_conf */
224 /* space for more flag there */
William Lallemand9117de92019-10-04 00:29:42 +0200225 struct list sni_ctx; /* list of sni_ctx using this ckch_inst */
226 struct list by_ckchs; /* chained in ckch_store's list of ckch_inst */
William Lallemand2954c472020-03-06 21:54:13 +0100227 struct list by_crtlist_entry; /* chained in crtlist_entry list of inst */
228};
229
William Lallemandf6adbe92019-09-11 16:33:52 +0200230
231#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
232
233#define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES))
234
235struct key_combo_ctx {
236 SSL_CTX *ctx;
237 int order;
238};
239
240/* Map used for processing multiple keypairs for a single purpose
241 *
242 * This maps CN/SNI name to certificate type
243 */
244struct sni_keytype {
245 int keytypes; /* BITMASK for keytypes */
246 struct ebmb_node name; /* node holding the servername value */
247};
248
249#endif
250
William Lallemande0f3fd52020-02-25 14:53:06 +0100251/* issuer chain store with hash of Subject Key Identifier
252 certificate/issuer matching is verify with X509_check_issued
253*/
254struct issuer_chain {
255 struct eb64_node node;
256 STACK_OF(X509) *chain;
257 char *path;
258};
259
William Lallemand7fd8b452020-05-07 15:20:43 +0200260struct connection;
261
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200262typedef void (*ssl_sock_msg_callback_func)(struct connection *conn,
263 int write_p, int version, int content_type,
264 const void *buf, size_t len, SSL *ssl);
265
266/* This structure contains a function pointer <func> that is called
267 * when observing received or sent SSL/TLS protocol messages, such as
268 * handshake messages or other events that can occur during processing.
269 */
270struct ssl_sock_msg_callback {
271 ssl_sock_msg_callback_func func;
272 struct list list; /* list of registered callbacks */
273};
William Lallemande0f3fd52020-02-25 14:53:06 +0100274
William Lallemand7fd8b452020-05-07 15:20:43 +0200275/* This memory pool is used for capturing clienthello parameters. */
276struct ssl_capture {
277 unsigned long long int xxh64;
278 unsigned char ciphersuite_len;
279 char ciphersuite[0];
280};
281
282struct global_ssl {
283 char *crt_base; /* base directory path for certificates */
284 char *ca_base; /* base directory path for CAs and CRLs */
285 char *issuers_chain_path; /* from "issuers-chain-path" */
286 int skip_self_issued_ca;
287
288 int async; /* whether we use ssl async mode */
289
290 char *listen_default_ciphers;
291 char *connect_default_ciphers;
292#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
293 char *listen_default_ciphersuites;
294 char *connect_default_ciphersuites;
295#endif
296#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
297 char *listen_default_curves;
298#endif
299 int listen_default_ssloptions;
300 int connect_default_ssloptions;
301 struct tls_version_filter listen_default_sslmethods;
302 struct tls_version_filter connect_default_sslmethods;
303
304 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
305 unsigned int life_time; /* SSL session lifetime in seconds */
306 unsigned int max_record; /* SSL max record size */
307 unsigned int default_dh_param; /* SSL maximum DH parameter size */
308 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
309 int capture_cipherlist; /* Size of the cipherlist buffer. */
310 int extra_files; /* which files not defined in the configuration file are we looking for */
311};
312
Willy Tarreauc125cef2019-05-10 09:58:43 +0200313#endif /* USE_OPENSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200314#endif /* _TYPES_SSL_SOCK_H */