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 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
| 27 | #include <import/ebmbtree.h> |
| 28 | #include <import/eb64tree.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 29 | |
William Lallemand | c0cdaff | 2020-05-15 00:20:53 +0200 | [diff] [blame] | 30 | #include <types/connection.h> /* struct wait_event */ |
William Lallemand | d4632b2 | 2020-05-12 14:46:24 +0200 | [diff] [blame] | 31 | #include <types/ssl_ckch.h> |
William Lallemand | be21b66 | 2020-05-12 14:17:23 +0200 | [diff] [blame] | 32 | #include <types/ssl_crtlist.h> |
| 33 | |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame^] | 34 | #include <haproxy/buf-t.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 35 | #include <haproxy/thread.h> |
Willy Tarreau | 853b297 | 2020-05-27 18:01:47 +0200 | [diff] [blame] | 36 | #include <haproxy/list-t.h> |
Willy Tarreau | 6019fab | 2020-05-27 16:26:00 +0200 | [diff] [blame] | 37 | #include <haproxy/openssl-compat.h> |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 38 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 39 | /* ***** READ THIS before adding code here! ***** |
| 40 | * |
| 41 | * Due to API incompatibilities between multiple OpenSSL versions and their |
| 42 | * derivatives, it's often tempting to add macros to (re-)define certain |
| 43 | * symbols. Please do not do this here, and do it in common/openssl-compat.h |
| 44 | * exclusively so that the whole code consistently uses the same macros. |
| 45 | * |
| 46 | * Whenever possible if a macro is missing in certain versions, it's better |
| 47 | * to conditionally define it in openssl-compat.h than using lots of ifdefs. |
| 48 | */ |
| 49 | |
| 50 | /* Warning, these are bits, not integers! */ |
| 51 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
| 52 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
| 53 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
| 54 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 55 | |
| 56 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 57 | |
| 58 | /* Verify errors macros */ |
| 59 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 60 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 61 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 62 | |
| 63 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 64 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 65 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
| 66 | |
| 67 | /* ssl_methods flags for ssl options */ |
| 68 | #define MC_SSL_O_ALL 0x0000 |
| 69 | #define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */ |
| 70 | #define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */ |
| 71 | #define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */ |
| 72 | #define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */ |
| 73 | #define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */ |
| 74 | |
| 75 | /* file to guess during file loading */ |
| 76 | #define SSL_GF_NONE 0x00000000 /* Don't guess any file, only open the files specified in the configuration files */ |
| 77 | #define SSL_GF_BUNDLE 0x00000001 /* try to open the bundles */ |
| 78 | #define SSL_GF_SCTL 0x00000002 /* try to open the .sctl file */ |
| 79 | #define SSL_GF_OCSP 0x00000004 /* try to open the .ocsp file */ |
| 80 | #define SSL_GF_OCSP_ISSUER 0x00000008 /* try to open the .issuer file if an OCSP file was loaded */ |
| 81 | #define SSL_GF_KEY 0x00000010 /* try to open the .key file to load a private key */ |
| 82 | |
| 83 | #define SSL_GF_ALL (SSL_GF_BUNDLE|SSL_GF_SCTL|SSL_GF_OCSP|SSL_GF_OCSP_ISSUER|SSL_GF_KEY) |
| 84 | |
| 85 | /* ssl_methods versions */ |
| 86 | enum { |
| 87 | CONF_TLSV_NONE = 0, |
| 88 | CONF_TLSV_MIN = 1, |
| 89 | CONF_SSLV3 = 1, |
| 90 | CONF_TLSV10 = 2, |
| 91 | CONF_TLSV11 = 3, |
| 92 | CONF_TLSV12 = 4, |
| 93 | CONF_TLSV13 = 5, |
| 94 | CONF_TLSV_MAX = 5, |
| 95 | }; |
| 96 | |
| 97 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
| 98 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 99 | #else /* openssl >= 1.1.0 */ |
| 100 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 101 | #endif |
| 102 | |
| 103 | struct methodVersions { |
| 104 | int option; |
| 105 | uint16_t flag; |
| 106 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 107 | void (*ssl_set_version)(SSL *, set_context_func); |
| 108 | const char *name; |
| 109 | }; |
| 110 | |
| 111 | /* server and bind verify method, it uses a global value as default */ |
| 112 | enum { |
| 113 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 114 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 115 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 116 | SSL_SOCK_VERIFY_NONE = 3, |
| 117 | }; |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 118 | |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 119 | struct pkey_info { |
| 120 | uint8_t sig; /* TLSEXT_signature_[rsa,ecdsa,...] */ |
| 121 | uint16_t bits; /* key size in bits */ |
| 122 | }; |
| 123 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 124 | struct sni_ctx { |
| 125 | SSL_CTX *ctx; /* context associated to the certificate */ |
| 126 | int order; /* load order for the certificate */ |
Tim Duesterhus | 6a0dd73 | 2020-01-18 01:32:49 +0100 | [diff] [blame] | 127 | unsigned int neg:1; /* reject if match */ |
| 128 | unsigned int wild:1; /* wildcard sni */ |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 129 | struct pkey_info kinfo; /* pkey info */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 130 | struct ssl_bind_conf *conf; /* ssl "bind" conf for the certificate */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 131 | 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] | 132 | struct ckch_inst *ckch_inst; /* instance used to create this sni_ctx */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 133 | struct ebmb_node name; /* node holding the servername value */ |
| 134 | }; |
| 135 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 136 | extern struct list tlskeys_reference; |
| 137 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 138 | struct tls_sess_key_128 { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 139 | unsigned char name[16]; |
| 140 | unsigned char aes_key[16]; |
| 141 | unsigned char hmac_key[16]; |
| 142 | } __attribute__((packed)); |
| 143 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 144 | struct tls_sess_key_256 { |
| 145 | unsigned char name[16]; |
| 146 | unsigned char aes_key[32]; |
| 147 | unsigned char hmac_key[32]; |
| 148 | } __attribute__((packed)); |
| 149 | |
| 150 | union tls_sess_key{ |
| 151 | unsigned char name[16]; |
Nenad Merdanovic | 8ef7065 | 2019-04-14 16:06:46 +0200 | [diff] [blame] | 152 | struct tls_sess_key_128 key_128; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 153 | struct tls_sess_key_256 key_256; |
| 154 | } __attribute__((packed)); |
| 155 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 156 | struct tls_keys_ref { |
| 157 | struct list list; /* Used to chain refs. */ |
| 158 | char *filename; |
| 159 | int unique_id; /* Each pattern reference have unique id. */ |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 160 | int refcount; /* number of users of this tls_keys_ref. */ |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 161 | union tls_sess_key *tlskeys; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 162 | int tls_ticket_enc_index; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 163 | int key_size_bits; |
Willy Tarreau | af613e8 | 2020-06-05 08:40:51 +0200 | [diff] [blame] | 164 | __decl_thread(HA_RWLOCK_T lock); /* lock used to protect the ref */ |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 165 | }; |
| 166 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 167 | /* shared ssl session */ |
| 168 | struct sh_ssl_sess_hdr { |
| 169 | struct ebmb_node key; |
| 170 | unsigned char key_data[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 171 | }; |
| 172 | |
William Lallemand | ad3c37b | 2020-03-25 20:20:26 +0100 | [diff] [blame] | 173 | /* states of the CLI IO handler for 'set ssl cert' */ |
| 174 | enum { |
| 175 | SETCERT_ST_INIT = 0, |
| 176 | SETCERT_ST_GEN, |
| 177 | SETCERT_ST_INSERT, |
| 178 | SETCERT_ST_FIN, |
| 179 | }; |
| 180 | |
William Lallemand | f6adbe9 | 2019-09-11 16:33:52 +0200 | [diff] [blame] | 181 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 182 | |
| 183 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 184 | |
| 185 | struct key_combo_ctx { |
| 186 | SSL_CTX *ctx; |
| 187 | int order; |
| 188 | }; |
| 189 | |
| 190 | /* Map used for processing multiple keypairs for a single purpose |
| 191 | * |
| 192 | * This maps CN/SNI name to certificate type |
| 193 | */ |
| 194 | struct sni_keytype { |
| 195 | int keytypes; /* BITMASK for keytypes */ |
| 196 | struct ebmb_node name; /* node holding the servername value */ |
| 197 | }; |
| 198 | |
| 199 | #endif |
| 200 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 201 | /* issuer chain store with hash of Subject Key Identifier |
| 202 | certificate/issuer matching is verify with X509_check_issued |
| 203 | */ |
| 204 | struct issuer_chain { |
| 205 | struct eb64_node node; |
| 206 | STACK_OF(X509) *chain; |
| 207 | char *path; |
| 208 | }; |
| 209 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 210 | struct connection; |
| 211 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 212 | typedef void (*ssl_sock_msg_callback_func)(struct connection *conn, |
| 213 | int write_p, int version, int content_type, |
| 214 | const void *buf, size_t len, SSL *ssl); |
| 215 | |
| 216 | /* This structure contains a function pointer <func> that is called |
| 217 | * when observing received or sent SSL/TLS protocol messages, such as |
| 218 | * handshake messages or other events that can occur during processing. |
| 219 | */ |
| 220 | struct ssl_sock_msg_callback { |
| 221 | ssl_sock_msg_callback_func func; |
| 222 | struct list list; /* list of registered callbacks */ |
| 223 | }; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 224 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 225 | /* This memory pool is used for capturing clienthello parameters. */ |
| 226 | struct ssl_capture { |
| 227 | unsigned long long int xxh64; |
| 228 | unsigned char ciphersuite_len; |
| 229 | char ciphersuite[0]; |
| 230 | }; |
| 231 | |
William Lallemand | c0cdaff | 2020-05-15 00:20:53 +0200 | [diff] [blame] | 232 | struct ssl_sock_ctx { |
| 233 | struct connection *conn; |
| 234 | SSL *ssl; |
| 235 | BIO *bio; |
| 236 | const struct xprt_ops *xprt; |
| 237 | void *xprt_ctx; |
| 238 | struct wait_event wait_event; |
| 239 | struct wait_event *subs; |
| 240 | int xprt_st; /* transport layer state, initialized to zero */ |
| 241 | struct buffer early_buf; /* buffer to store the early data received */ |
| 242 | int sent_early_data; /* Amount of early data we sent so far */ |
| 243 | |
| 244 | }; |
| 245 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 246 | struct global_ssl { |
| 247 | char *crt_base; /* base directory path for certificates */ |
| 248 | char *ca_base; /* base directory path for CAs and CRLs */ |
| 249 | char *issuers_chain_path; /* from "issuers-chain-path" */ |
| 250 | int skip_self_issued_ca; |
| 251 | |
| 252 | int async; /* whether we use ssl async mode */ |
| 253 | |
| 254 | char *listen_default_ciphers; |
| 255 | char *connect_default_ciphers; |
| 256 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 257 | char *listen_default_ciphersuites; |
| 258 | char *connect_default_ciphersuites; |
| 259 | #endif |
| 260 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
| 261 | char *listen_default_curves; |
| 262 | #endif |
| 263 | int listen_default_ssloptions; |
| 264 | int connect_default_ssloptions; |
| 265 | struct tls_version_filter listen_default_sslmethods; |
| 266 | struct tls_version_filter connect_default_sslmethods; |
| 267 | |
| 268 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 269 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 270 | unsigned int max_record; /* SSL max record size */ |
| 271 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 272 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
| 273 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
| 274 | int extra_files; /* which files not defined in the configuration file are we looking for */ |
| 275 | }; |
| 276 | |
William Lallemand | d4632b2 | 2020-05-12 14:46:24 +0200 | [diff] [blame] | 277 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 278 | /* The order here matters for picking a default context, |
| 279 | * keep the most common keytype at the bottom of the list |
| 280 | */ |
| 281 | extern const char *SSL_SOCK_KEYTYPE_NAMES[]; |
| 282 | |
| 283 | #define SSL_SOCK_NUM_KEYTYPES 3 |
| 284 | #else |
| 285 | #define SSL_SOCK_NUM_KEYTYPES 1 |
| 286 | #endif |
| 287 | |
Willy Tarreau | c125cef | 2019-05-10 09:58:43 +0200 | [diff] [blame] | 288 | #endif /* USE_OPENSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 289 | #endif /* _TYPES_SSL_SOCK_H */ |