blob: e6b092e663981896d04ef689c05b9731ed7e4668 [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 Lallemandd4632b22020-05-12 14:46:24 +020030#include <types/ssl_ckch.h>
William Lallemandbe21b662020-05-12 14:17:23 +020031#include <types/ssl_crtlist.h>
32
Christopher Faulet16f45c82018-02-16 11:23:49 +010033#include <common/hathreads.h>
Willy Tarreau8dd0d552020-03-06 18:43:08 +010034#include <common/mini-clist.h>
Willy Tarreau55994562019-05-09 14:52:44 +020035#include <common/openssl-compat.h>
Christopher Faulet16f45c82018-02-16 11:23:49 +010036
William Lallemand7fd8b452020-05-07 15:20:43 +020037/* ***** READ THIS before adding code here! *****
38 *
39 * Due to API incompatibilities between multiple OpenSSL versions and their
40 * derivatives, it's often tempting to add macros to (re-)define certain
41 * symbols. Please do not do this here, and do it in common/openssl-compat.h
42 * exclusively so that the whole code consistently uses the same macros.
43 *
44 * Whenever possible if a macro is missing in certain versions, it's better
45 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
46 */
47
48/* Warning, these are bits, not integers! */
49#define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001
50#define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002
51#define SSL_SOCK_SEND_UNLIMITED 0x00000004
52#define SSL_SOCK_RECV_HEARTBEAT 0x00000008
53
54/* bits 0xFFFF0000 are reserved to store verify errors */
55
56/* Verify errors macros */
57#define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16))
58#define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16))
59#define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16))
60
61#define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63)
62#define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15)
63#define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63)
64
65/* ssl_methods flags for ssl options */
66#define MC_SSL_O_ALL 0x0000
67#define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */
68#define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */
69#define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */
70#define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */
71#define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */
72
73/* file to guess during file loading */
74#define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */
75#define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */
76#define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */
77#define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */
78#define SSL_GF_OCSP_ISSUER 0x00000008 /* try to open the .issuer file if an OCSP file was loaded */
79#define SSL_GF_KEY 0x00000010 /* try to open the .key file to load a private key */
80
81#define SSL_GF_ALL (SSL_GF_BUNDLE|SSL_GF_SCTL|SSL_GF_OCSP|SSL_GF_OCSP_ISSUER|SSL_GF_KEY)
82
83/* ssl_methods versions */
84enum {
85 CONF_TLSV_NONE = 0,
86 CONF_TLSV_MIN = 1,
87 CONF_SSLV3 = 1,
88 CONF_TLSV10 = 2,
89 CONF_TLSV11 = 3,
90 CONF_TLSV12 = 4,
91 CONF_TLSV13 = 5,
92 CONF_TLSV_MAX = 5,
93};
94
95#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
96typedef enum { SET_CLIENT, SET_SERVER } set_context_func;
97#else /* openssl >= 1.1.0 */
98typedef enum { SET_MIN, SET_MAX } set_context_func;
99#endif
100
101struct methodVersions {
102 int option;
103 uint16_t flag;
104 void (*ctx_set_version)(SSL_CTX *, set_context_func);
105 void (*ssl_set_version)(SSL *, set_context_func);
106 const char *name;
107};
108
109/* server and bind verify method, it uses a global value as default */
110enum {
111 SSL_SOCK_VERIFY_DEFAULT = 0,
112 SSL_SOCK_VERIFY_REQUIRED = 1,
113 SSL_SOCK_VERIFY_OPTIONAL = 2,
114 SSL_SOCK_VERIFY_NONE = 3,
115};
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200116
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +0200117struct pkey_info {
118 uint8_t sig; /* TLSEXT_signature_[rsa,ecdsa,...] */
119 uint16_t bits; /* key size in bits */
120};
121
Emeric Brunfc0421f2012-09-07 17:30:07 +0200122struct sni_ctx {
123 SSL_CTX *ctx; /* context associated to the certificate */
124 int order; /* load order for the certificate */
Tim Duesterhus6a0dd732020-01-18 01:32:49 +0100125 unsigned int neg:1; /* reject if match */
126 unsigned int wild:1; /* wildcard sni */
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +0200127 struct pkey_info kinfo; /* pkey info */
Emmanuel Hocdet98263292016-12-29 18:26:15 +0100128 struct ssl_bind_conf *conf; /* ssl "bind" conf for the certificate */
William Lallemand9117de92019-10-04 00:29:42 +0200129 struct list by_ckch_inst; /* chained in ckch_inst's list of sni_ctx */
William Lallemandcfca1422020-03-05 10:17:47 +0100130 struct ckch_inst *ckch_inst; /* instance used to create this sni_ctx */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200131 struct ebmb_node name; /* node holding the servername value */
132};
133
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200134struct tls_version_filter {
135 uint16_t flags; /* ssl options */
136 uint8_t min; /* min TLS version */
137 uint8_t max; /* max TLS version */
138};
139
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200140extern struct list tlskeys_reference;
141
Emeric Brun9e754772019-01-10 17:51:55 +0100142struct tls_sess_key_128 {
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100143 unsigned char name[16];
144 unsigned char aes_key[16];
145 unsigned char hmac_key[16];
146} __attribute__((packed));
147
Emeric Brun9e754772019-01-10 17:51:55 +0100148struct tls_sess_key_256 {
149 unsigned char name[16];
150 unsigned char aes_key[32];
151 unsigned char hmac_key[32];
152} __attribute__((packed));
153
154union tls_sess_key{
155 unsigned char name[16];
Nenad Merdanovic8ef70652019-04-14 16:06:46 +0200156 struct tls_sess_key_128 key_128;
Emeric Brun9e754772019-01-10 17:51:55 +0100157 struct tls_sess_key_256 key_256;
158} __attribute__((packed));
159
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200160struct tls_keys_ref {
161 struct list list; /* Used to chain refs. */
162 char *filename;
163 int unique_id; /* Each pattern reference have unique id. */
Willy Tarreau17b4aa12018-07-17 10:05:32 +0200164 int refcount; /* number of users of this tls_keys_ref. */
Emeric Brun9e754772019-01-10 17:51:55 +0100165 union tls_sess_key *tlskeys;
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200166 int tls_ticket_enc_index;
Emeric Brun9e754772019-01-10 17:51:55 +0100167 int key_size_bits;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100168 __decl_hathreads(HA_RWLOCK_T lock); /* lock used to protect the ref */
Nenad Merdanovic146defa2015-05-09 08:46:00 +0200169};
170
William Lallemand4f45bb92017-10-30 20:08:51 +0100171/* shared ssl session */
172struct sh_ssl_sess_hdr {
173 struct ebmb_node key;
174 unsigned char key_data[SSL_MAX_SSL_SESSION_ID_LENGTH];
175};
176
William Lallemandad3c37b2020-03-25 20:20:26 +0100177/* states of the CLI IO handler for 'set ssl cert' */
178enum {
179 SETCERT_ST_INIT = 0,
180 SETCERT_ST_GEN,
181 SETCERT_ST_INSERT,
182 SETCERT_ST_FIN,
183};
184
William Lallemandf6adbe92019-09-11 16:33:52 +0200185#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
186
187#define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES))
188
189struct key_combo_ctx {
190 SSL_CTX *ctx;
191 int order;
192};
193
194/* Map used for processing multiple keypairs for a single purpose
195 *
196 * This maps CN/SNI name to certificate type
197 */
198struct sni_keytype {
199 int keytypes; /* BITMASK for keytypes */
200 struct ebmb_node name; /* node holding the servername value */
201};
202
203#endif
204
William Lallemande0f3fd52020-02-25 14:53:06 +0100205/* issuer chain store with hash of Subject Key Identifier
206 certificate/issuer matching is verify with X509_check_issued
207*/
208struct issuer_chain {
209 struct eb64_node node;
210 STACK_OF(X509) *chain;
211 char *path;
212};
213
William Lallemand7fd8b452020-05-07 15:20:43 +0200214struct connection;
215
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200216typedef void (*ssl_sock_msg_callback_func)(struct connection *conn,
217 int write_p, int version, int content_type,
218 const void *buf, size_t len, SSL *ssl);
219
220/* This structure contains a function pointer <func> that is called
221 * when observing received or sent SSL/TLS protocol messages, such as
222 * handshake messages or other events that can occur during processing.
223 */
224struct ssl_sock_msg_callback {
225 ssl_sock_msg_callback_func func;
226 struct list list; /* list of registered callbacks */
227};
William Lallemande0f3fd52020-02-25 14:53:06 +0100228
William Lallemand7fd8b452020-05-07 15:20:43 +0200229/* This memory pool is used for capturing clienthello parameters. */
230struct ssl_capture {
231 unsigned long long int xxh64;
232 unsigned char ciphersuite_len;
233 char ciphersuite[0];
234};
235
236struct global_ssl {
237 char *crt_base; /* base directory path for certificates */
238 char *ca_base; /* base directory path for CAs and CRLs */
239 char *issuers_chain_path; /* from "issuers-chain-path" */
240 int skip_self_issued_ca;
241
242 int async; /* whether we use ssl async mode */
243
244 char *listen_default_ciphers;
245 char *connect_default_ciphers;
246#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
247 char *listen_default_ciphersuites;
248 char *connect_default_ciphersuites;
249#endif
250#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
251 char *listen_default_curves;
252#endif
253 int listen_default_ssloptions;
254 int connect_default_ssloptions;
255 struct tls_version_filter listen_default_sslmethods;
256 struct tls_version_filter connect_default_sslmethods;
257
258 int private_cache; /* Force to use a private session cache even if nbproc > 1 */
259 unsigned int life_time; /* SSL session lifetime in seconds */
260 unsigned int max_record; /* SSL max record size */
261 unsigned int default_dh_param; /* SSL maximum DH parameter size */
262 int ctx_cache; /* max number of entries in the ssl_ctx cache. */
263 int capture_cipherlist; /* Size of the cipherlist buffer. */
264 int extra_files; /* which files not defined in the configuration file are we looking for */
265};
266
William Lallemandd4632b22020-05-12 14:46:24 +0200267#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
268/* The order here matters for picking a default context,
269 * keep the most common keytype at the bottom of the list
270 */
271extern const char *SSL_SOCK_KEYTYPE_NAMES[];
272
273#define SSL_SOCK_NUM_KEYTYPES 3
274#else
275#define SSL_SOCK_NUM_KEYTYPES 1
276#endif
277
Willy Tarreauc125cef2019-05-10 09:58:43 +0200278#endif /* USE_OPENSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200279#endif /* _TYPES_SSL_SOCK_H */