blob: dbfa3d72616b61d70000cb390af0723913aacd9f [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
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +020034struct pkey_info {
35 uint8_t sig; /* TLSEXT_signature_[rsa,ecdsa,...] */
36 uint16_t bits; /* key size in bits */
37};
38
Emeric Brunfc0421f2012-09-07 17:30:07 +020039struct sni_ctx {
40 SSL_CTX *ctx; /* context associated to the certificate */
41 int order; /* load order for the certificate */
Tim Duesterhus6a0dd732020-01-18 01:32:49 +010042 unsigned int neg:1; /* reject if match */
43 unsigned int wild:1; /* wildcard sni */
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +020044 struct pkey_info kinfo; /* pkey info */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010045 struct ssl_bind_conf *conf; /* ssl "bind" conf for the certificate */
William Lallemand9117de92019-10-04 00:29:42 +020046 struct list by_ckch_inst; /* chained in ckch_inst's list of sni_ctx */
William Lallemandcfca1422020-03-05 10:17:47 +010047 struct ckch_inst *ckch_inst; /* instance used to create this sni_ctx */
Emeric Brunfc0421f2012-09-07 17:30:07 +020048 struct ebmb_node name; /* node holding the servername value */
49};
50
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020051struct tls_version_filter {
52 uint16_t flags; /* ssl options */
53 uint8_t min; /* min TLS version */
54 uint8_t max; /* max TLS version */
55};
56
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +020057extern struct list tlskeys_reference;
58
Emeric Brun9e754772019-01-10 17:51:55 +010059struct tls_sess_key_128 {
Nenad Merdanovic05552d42015-02-27 19:56:49 +010060 unsigned char name[16];
61 unsigned char aes_key[16];
62 unsigned char hmac_key[16];
63} __attribute__((packed));
64
Emeric Brun9e754772019-01-10 17:51:55 +010065struct tls_sess_key_256 {
66 unsigned char name[16];
67 unsigned char aes_key[32];
68 unsigned char hmac_key[32];
69} __attribute__((packed));
70
71union tls_sess_key{
72 unsigned char name[16];
Nenad Merdanovic8ef70652019-04-14 16:06:46 +020073 struct tls_sess_key_128 key_128;
Emeric Brun9e754772019-01-10 17:51:55 +010074 struct tls_sess_key_256 key_256;
75} __attribute__((packed));
76
Nenad Merdanovic146defa2015-05-09 08:46:00 +020077struct tls_keys_ref {
78 struct list list; /* Used to chain refs. */
79 char *filename;
80 int unique_id; /* Each pattern reference have unique id. */
Willy Tarreau17b4aa12018-07-17 10:05:32 +020081 int refcount; /* number of users of this tls_keys_ref. */
Emeric Brun9e754772019-01-10 17:51:55 +010082 union tls_sess_key *tlskeys;
Nenad Merdanovic146defa2015-05-09 08:46:00 +020083 int tls_ticket_enc_index;
Emeric Brun9e754772019-01-10 17:51:55 +010084 int key_size_bits;
Christopher Faulet16f45c82018-02-16 11:23:49 +010085 __decl_hathreads(HA_RWLOCK_T lock); /* lock used to protect the ref */
Nenad Merdanovic146defa2015-05-09 08:46:00 +020086};
87
William Lallemand4f45bb92017-10-30 20:08:51 +010088/* shared ssl session */
89struct sh_ssl_sess_hdr {
90 struct ebmb_node key;
91 unsigned char key_data[SSL_MAX_SSL_SESSION_ID_LENGTH];
92};
93
William Lallemandad3c37b2020-03-25 20:20:26 +010094/* states of the CLI IO handler for 'set ssl cert' */
95enum {
96 SETCERT_ST_INIT = 0,
97 SETCERT_ST_GEN,
98 SETCERT_ST_INSERT,
99 SETCERT_ST_FIN,
100};
101
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500102/* This is used to preload the certificate, private key
William Lallemandf6adbe92019-09-11 16:33:52 +0200103 * and Cert Chain of a file passed in via the crt
104 * argument
105 *
106 * This way, we do not have to read the file multiple times
107 */
108struct cert_key_and_chain {
109 X509 *cert;
110 EVP_PKEY *key;
111 STACK_OF(X509) *chain;
112 DH *dh;
William Lallemanda17f4112019-10-10 15:16:44 +0200113 struct buffer *sctl;
William Lallemand246c0242019-10-11 08:59:13 +0200114 struct buffer *ocsp_response;
115 X509 *ocsp_issuer;
William Lallemandf6adbe92019-09-11 16:33:52 +0200116};
117
118/*
119 * this is used to store 1 to SSL_SOCK_NUM_KEYTYPES cert_key_and_chain and
120 * metadata.
121 */
122struct ckch_store {
123 struct cert_key_and_chain *ckch;
Tim Duesterhus6a0dd732020-01-18 01:32:49 +0100124 unsigned int multi:1; /* is it a multi-cert bundle ? */
William Lallemand9117de92019-10-04 00:29:42 +0200125 struct list ckch_inst; /* list of ckch_inst which uses this ckch_node */
William Lallemand23d61c02020-03-30 18:27:58 +0200126 struct list crtlist_entry; /* list of entries which use this store */
William Lallemandf6adbe92019-09-11 16:33:52 +0200127 struct ebmb_node node;
128 char path[0];
129};
130
William Lallemand9117de92019-10-04 00:29:42 +0200131/*
132 * This structure describe a ckch instance. An instance is generated for each
133 * bind_conf. The instance contains a linked list of the sni ctx which uses
134 * the ckch in this bind_conf.
William Lallemand9117de92019-10-04 00:29:42 +0200135 */
136struct ckch_inst {
137 struct bind_conf *bind_conf; /* pointer to the bind_conf that uses this ckch_inst */
William Lallemand150bfa82019-09-19 17:12:49 +0200138 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 +0100139 struct ckch_store *ckch_store; /* pointer to the store used to generate this inst */
William Lallemandcaa16192020-04-08 16:29:15 +0200140 struct crtlist_entry *crtlist_entry; /* pointer to the crtlist_entry used, or NULL */
William Lallemand0a528462020-03-09 16:53:42 +0100141 unsigned int is_default:1; /* This instance is used as the default ctx for this bind_conf */
142 /* space for more flag there */
William Lallemand9117de92019-10-04 00:29:42 +0200143 struct list sni_ctx; /* list of sni_ctx using this ckch_inst */
144 struct list by_ckchs; /* chained in ckch_store's list of ckch_inst */
William Lallemand2954c472020-03-06 21:54:13 +0100145 struct list by_crtlist_entry; /* chained in crtlist_entry list of inst */
146};
147
William Lallemand79d31ec2020-03-25 15:10:49 +0100148/* list of bind conf used by struct crtlist */
149struct bind_conf_list {
150 struct bind_conf *bind_conf;
151 struct bind_conf_list *next;
152};
153
William Lallemand2954c472020-03-06 21:54:13 +0100154/* This structure is basically a crt-list or a directory */
155struct crtlist {
William Lallemand79d31ec2020-03-25 15:10:49 +0100156 struct bind_conf_list *bind_conf; /* list of bind_conf which use this crtlist */
William Lallemandc69f02d2020-04-06 19:07:03 +0200157 unsigned int linecount; /* number of lines */
William Lallemand2954c472020-03-06 21:54:13 +0100158 struct eb_root entries;
159 struct list ord_entries; /* list to keep the line order of the crt-list file */
160 struct ebmb_node node; /* key is the filename or directory */
161};
162
163/* a file in a directory or a line in a crt-list */
164struct crtlist_entry {
165 struct ssl_bind_conf *ssl_conf; /* SSL conf in crt-list */
166 unsigned int linenum;
167 unsigned int fcount; /* filters count */
168 char **filters;
William Lallemandfa8cf0c2020-03-30 19:59:57 +0200169 struct crtlist *crtlist; /* ptr to the parent crtlist */
William Lallemand49398312020-03-30 17:01:33 +0200170 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 +0100171 struct list by_crtlist; /* ordered entries */
William Lallemand23d61c02020-03-30 18:27:58 +0200172 struct list by_ckch_store; /* linked in ckch_store list of crtlist_entries */
William Lallemand2954c472020-03-06 21:54:13 +0100173 struct ebpt_node node; /* key is a ptr to a ckch_store */
William Lallemand9117de92019-10-04 00:29:42 +0200174};
William Lallemandf6adbe92019-09-11 16:33:52 +0200175
176#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
177
178#define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES))
179
180struct key_combo_ctx {
181 SSL_CTX *ctx;
182 int order;
183};
184
185/* Map used for processing multiple keypairs for a single purpose
186 *
187 * This maps CN/SNI name to certificate type
188 */
189struct sni_keytype {
190 int keytypes; /* BITMASK for keytypes */
191 struct ebmb_node name; /* node holding the servername value */
192};
193
194#endif
195
William Lallemande0f3fd52020-02-25 14:53:06 +0100196/* issuer chain store with hash of Subject Key Identifier
197 certificate/issuer matching is verify with X509_check_issued
198*/
199struct issuer_chain {
200 struct eb64_node node;
201 STACK_OF(X509) *chain;
202 char *path;
203};
204
205
Willy Tarreauc125cef2019-05-10 09:58:43 +0200206#endif /* USE_OPENSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +0200207#endif /* _TYPES_SSL_SOCK_H */