Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | c125cef | 2019-05-10 09:58:43 +0200 | [diff] [blame] | 24 | #ifdef USE_OPENSSL |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 25 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 26 | #include <ebpttree.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 27 | #include <ebmbtree.h> |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 28 | #include <eb64tree.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 29 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 30 | #include <common/hathreads.h> |
Willy Tarreau | 8dd0d55 | 2020-03-06 18:43:08 +0100 | [diff] [blame] | 31 | #include <common/mini-clist.h> |
Willy Tarreau | 5599456 | 2019-05-09 14:52:44 +0200 | [diff] [blame] | 32 | #include <common/openssl-compat.h> |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 33 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 34 | struct connection; |
| 35 | |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 36 | struct pkey_info { |
| 37 | uint8_t sig; /* TLSEXT_signature_[rsa,ecdsa,...] */ |
| 38 | uint16_t bits; /* key size in bits */ |
| 39 | }; |
| 40 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 41 | struct sni_ctx { |
| 42 | SSL_CTX *ctx; /* context associated to the certificate */ |
| 43 | int order; /* load order for the certificate */ |
Tim Duesterhus | 6a0dd73 | 2020-01-18 01:32:49 +0100 | [diff] [blame] | 44 | unsigned int neg:1; /* reject if match */ |
| 45 | unsigned int wild:1; /* wildcard sni */ |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 46 | struct pkey_info kinfo; /* pkey info */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 47 | struct ssl_bind_conf *conf; /* ssl "bind" conf for the certificate */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 48 | struct list by_ckch_inst; /* chained in ckch_inst's list of sni_ctx */ |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 49 | struct ckch_inst *ckch_inst; /* instance used to create this sni_ctx */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 50 | struct ebmb_node name; /* node holding the servername value */ |
| 51 | }; |
| 52 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 53 | struct tls_version_filter { |
| 54 | uint16_t flags; /* ssl options */ |
| 55 | uint8_t min; /* min TLS version */ |
| 56 | uint8_t max; /* max TLS version */ |
| 57 | }; |
| 58 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 59 | extern struct list tlskeys_reference; |
| 60 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 61 | struct tls_sess_key_128 { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 62 | unsigned char name[16]; |
| 63 | unsigned char aes_key[16]; |
| 64 | unsigned char hmac_key[16]; |
| 65 | } __attribute__((packed)); |
| 66 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 67 | struct tls_sess_key_256 { |
| 68 | unsigned char name[16]; |
| 69 | unsigned char aes_key[32]; |
| 70 | unsigned char hmac_key[32]; |
| 71 | } __attribute__((packed)); |
| 72 | |
| 73 | union tls_sess_key{ |
| 74 | unsigned char name[16]; |
Nenad Merdanovic | 8ef7065 | 2019-04-14 16:06:46 +0200 | [diff] [blame] | 75 | struct tls_sess_key_128 key_128; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 76 | struct tls_sess_key_256 key_256; |
| 77 | } __attribute__((packed)); |
| 78 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 79 | struct tls_keys_ref { |
| 80 | struct list list; /* Used to chain refs. */ |
| 81 | char *filename; |
| 82 | int unique_id; /* Each pattern reference have unique id. */ |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 83 | int refcount; /* number of users of this tls_keys_ref. */ |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 84 | union tls_sess_key *tlskeys; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 85 | int tls_ticket_enc_index; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 86 | int key_size_bits; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 87 | __decl_hathreads(HA_RWLOCK_T lock); /* lock used to protect the ref */ |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 88 | }; |
| 89 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 90 | /* shared ssl session */ |
| 91 | struct sh_ssl_sess_hdr { |
| 92 | struct ebmb_node key; |
| 93 | unsigned char key_data[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 94 | }; |
| 95 | |
William Lallemand | ad3c37b | 2020-03-25 20:20:26 +0100 | [diff] [blame] | 96 | /* states of the CLI IO handler for 'set ssl cert' */ |
| 97 | enum { |
| 98 | SETCERT_ST_INIT = 0, |
| 99 | SETCERT_ST_GEN, |
| 100 | SETCERT_ST_INSERT, |
| 101 | SETCERT_ST_FIN, |
| 102 | }; |
| 103 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 104 | /* This is used to preload the certificate, private key |
William Lallemand | f6adbe9 | 2019-09-11 16:33:52 +0200 | [diff] [blame] | 105 | * and Cert Chain of a file passed in via the crt |
| 106 | * argument |
| 107 | * |
| 108 | * This way, we do not have to read the file multiple times |
| 109 | */ |
| 110 | struct cert_key_and_chain { |
| 111 | X509 *cert; |
| 112 | EVP_PKEY *key; |
| 113 | STACK_OF(X509) *chain; |
| 114 | DH *dh; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 115 | struct buffer *sctl; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 116 | struct buffer *ocsp_response; |
| 117 | X509 *ocsp_issuer; |
William Lallemand | f6adbe9 | 2019-09-11 16:33:52 +0200 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /* |
| 121 | * this is used to store 1 to SSL_SOCK_NUM_KEYTYPES cert_key_and_chain and |
| 122 | * metadata. |
| 123 | */ |
| 124 | struct ckch_store { |
| 125 | struct cert_key_and_chain *ckch; |
Tim Duesterhus | 6a0dd73 | 2020-01-18 01:32:49 +0100 | [diff] [blame] | 126 | unsigned int multi:1; /* is it a multi-cert bundle ? */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 127 | struct list ckch_inst; /* list of ckch_inst which uses this ckch_node */ |
William Lallemand | 23d61c0 | 2020-03-30 18:27:58 +0200 | [diff] [blame] | 128 | struct list crtlist_entry; /* list of entries which use this store */ |
William Lallemand | f6adbe9 | 2019-09-11 16:33:52 +0200 | [diff] [blame] | 129 | struct ebmb_node node; |
| 130 | char path[0]; |
| 131 | }; |
| 132 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 133 | /* |
| 134 | * This structure describe a ckch instance. An instance is generated for each |
| 135 | * bind_conf. The instance contains a linked list of the sni ctx which uses |
| 136 | * the ckch in this bind_conf. |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 137 | */ |
| 138 | struct ckch_inst { |
| 139 | struct bind_conf *bind_conf; /* pointer to the bind_conf that uses this ckch_inst */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 140 | struct ssl_bind_conf *ssl_conf; /* pointer to the ssl_conf which is used by every sni_ctx of this inst */ |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 141 | struct ckch_store *ckch_store; /* pointer to the store used to generate this inst */ |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 142 | struct crtlist_entry *crtlist_entry; /* pointer to the crtlist_entry used, or NULL */ |
William Lallemand | 0a52846 | 2020-03-09 16:53:42 +0100 | [diff] [blame] | 143 | unsigned int is_default:1; /* This instance is used as the default ctx for this bind_conf */ |
| 144 | /* space for more flag there */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 145 | struct list sni_ctx; /* list of sni_ctx using this ckch_inst */ |
| 146 | struct list by_ckchs; /* chained in ckch_store's list of ckch_inst */ |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 147 | struct list by_crtlist_entry; /* chained in crtlist_entry list of inst */ |
| 148 | }; |
| 149 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 150 | /* list of bind conf used by struct crtlist */ |
| 151 | struct bind_conf_list { |
| 152 | struct bind_conf *bind_conf; |
| 153 | struct bind_conf_list *next; |
| 154 | }; |
| 155 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 156 | /* This structure is basically a crt-list or a directory */ |
| 157 | struct crtlist { |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 158 | struct bind_conf_list *bind_conf; /* list of bind_conf which use this crtlist */ |
William Lallemand | c69f02d | 2020-04-06 19:07:03 +0200 | [diff] [blame] | 159 | unsigned int linecount; /* number of lines */ |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 160 | struct eb_root entries; |
| 161 | struct list ord_entries; /* list to keep the line order of the crt-list file */ |
| 162 | struct ebmb_node node; /* key is the filename or directory */ |
| 163 | }; |
| 164 | |
| 165 | /* a file in a directory or a line in a crt-list */ |
| 166 | struct crtlist_entry { |
| 167 | struct ssl_bind_conf *ssl_conf; /* SSL conf in crt-list */ |
| 168 | unsigned int linenum; |
| 169 | unsigned int fcount; /* filters count */ |
| 170 | char **filters; |
William Lallemand | fa8cf0c | 2020-03-30 19:59:57 +0200 | [diff] [blame] | 171 | struct crtlist *crtlist; /* ptr to the parent crtlist */ |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 172 | struct list ckch_inst; /* list of instances of this entry, there is 1 ckch_inst per instance of the crt-list */ |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 173 | struct list by_crtlist; /* ordered entries */ |
William Lallemand | 23d61c0 | 2020-03-30 18:27:58 +0200 | [diff] [blame] | 174 | struct list by_ckch_store; /* linked in ckch_store list of crtlist_entries */ |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 175 | struct ebpt_node node; /* key is a ptr to a ckch_store */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 176 | }; |
William Lallemand | f6adbe9 | 2019-09-11 16:33:52 +0200 | [diff] [blame] | 177 | |
| 178 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 179 | |
| 180 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 181 | |
| 182 | struct key_combo_ctx { |
| 183 | SSL_CTX *ctx; |
| 184 | int order; |
| 185 | }; |
| 186 | |
| 187 | /* Map used for processing multiple keypairs for a single purpose |
| 188 | * |
| 189 | * This maps CN/SNI name to certificate type |
| 190 | */ |
| 191 | struct sni_keytype { |
| 192 | int keytypes; /* BITMASK for keytypes */ |
| 193 | struct ebmb_node name; /* node holding the servername value */ |
| 194 | }; |
| 195 | |
| 196 | #endif |
| 197 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 198 | /* issuer chain store with hash of Subject Key Identifier |
| 199 | certificate/issuer matching is verify with X509_check_issued |
| 200 | */ |
| 201 | struct issuer_chain { |
| 202 | struct eb64_node node; |
| 203 | STACK_OF(X509) *chain; |
| 204 | char *path; |
| 205 | }; |
| 206 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 207 | typedef void (*ssl_sock_msg_callback_func)(struct connection *conn, |
| 208 | int write_p, int version, int content_type, |
| 209 | const void *buf, size_t len, SSL *ssl); |
| 210 | |
| 211 | /* This structure contains a function pointer <func> that is called |
| 212 | * when observing received or sent SSL/TLS protocol messages, such as |
| 213 | * handshake messages or other events that can occur during processing. |
| 214 | */ |
| 215 | struct ssl_sock_msg_callback { |
| 216 | ssl_sock_msg_callback_func func; |
| 217 | struct list list; /* list of registered callbacks */ |
| 218 | }; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 219 | |
Willy Tarreau | c125cef | 2019-05-10 09:58:43 +0200 | [diff] [blame] | 220 | #endif /* USE_OPENSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 221 | #endif /* _TYPES_SSL_SOCK_H */ |