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 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 26 | #include <ebmbtree.h> |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 27 | #include <eb64tree.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 28 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 29 | #include <common/hathreads.h> |
Willy Tarreau | 8dd0d55 | 2020-03-06 18:43:08 +0100 | [diff] [blame] | 30 | #include <common/mini-clist.h> |
Willy Tarreau | 5599456 | 2019-05-09 14:52:44 +0200 | [diff] [blame] | 31 | #include <common/openssl-compat.h> |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 32 | |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 33 | struct pkey_info { |
| 34 | uint8_t sig; /* TLSEXT_signature_[rsa,ecdsa,...] */ |
| 35 | uint16_t bits; /* key size in bits */ |
| 36 | }; |
| 37 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 38 | struct sni_ctx { |
| 39 | SSL_CTX *ctx; /* context associated to the certificate */ |
| 40 | int order; /* load order for the certificate */ |
Tim Duesterhus | 6a0dd73 | 2020-01-18 01:32:49 +0100 | [diff] [blame] | 41 | unsigned int neg:1; /* reject if match */ |
| 42 | unsigned int wild:1; /* wildcard sni */ |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 43 | struct pkey_info kinfo; /* pkey info */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 44 | struct ssl_bind_conf *conf; /* ssl "bind" conf for the certificate */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 45 | 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] | 46 | struct ckch_inst *ckch_inst; /* instance used to create this sni_ctx */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 47 | struct ebmb_node name; /* node holding the servername value */ |
| 48 | }; |
| 49 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 50 | struct tls_version_filter { |
| 51 | uint16_t flags; /* ssl options */ |
| 52 | uint8_t min; /* min TLS version */ |
| 53 | uint8_t max; /* max TLS version */ |
| 54 | }; |
| 55 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 56 | extern struct list tlskeys_reference; |
| 57 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 58 | struct tls_sess_key_128 { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 59 | unsigned char name[16]; |
| 60 | unsigned char aes_key[16]; |
| 61 | unsigned char hmac_key[16]; |
| 62 | } __attribute__((packed)); |
| 63 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 64 | struct tls_sess_key_256 { |
| 65 | unsigned char name[16]; |
| 66 | unsigned char aes_key[32]; |
| 67 | unsigned char hmac_key[32]; |
| 68 | } __attribute__((packed)); |
| 69 | |
| 70 | union tls_sess_key{ |
| 71 | unsigned char name[16]; |
Nenad Merdanovic | 8ef7065 | 2019-04-14 16:06:46 +0200 | [diff] [blame] | 72 | struct tls_sess_key_128 key_128; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 73 | struct tls_sess_key_256 key_256; |
| 74 | } __attribute__((packed)); |
| 75 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 76 | struct tls_keys_ref { |
| 77 | struct list list; /* Used to chain refs. */ |
| 78 | char *filename; |
| 79 | int unique_id; /* Each pattern reference have unique id. */ |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 80 | int refcount; /* number of users of this tls_keys_ref. */ |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 81 | union tls_sess_key *tlskeys; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 82 | int tls_ticket_enc_index; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 83 | int key_size_bits; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 84 | __decl_hathreads(HA_RWLOCK_T lock); /* lock used to protect the ref */ |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 87 | /* shared ssl session */ |
| 88 | struct sh_ssl_sess_hdr { |
| 89 | struct ebmb_node key; |
| 90 | unsigned char key_data[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 91 | }; |
| 92 | |
William Lallemand | f6adbe9 | 2019-09-11 16:33:52 +0200 | [diff] [blame] | 93 | /* This is used to preload the certifcate, private key |
| 94 | * and Cert Chain of a file passed in via the crt |
| 95 | * argument |
| 96 | * |
| 97 | * This way, we do not have to read the file multiple times |
| 98 | */ |
| 99 | struct cert_key_and_chain { |
| 100 | X509 *cert; |
| 101 | EVP_PKEY *key; |
| 102 | STACK_OF(X509) *chain; |
| 103 | DH *dh; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 104 | struct buffer *sctl; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 105 | struct buffer *ocsp_response; |
| 106 | X509 *ocsp_issuer; |
William Lallemand | f6adbe9 | 2019-09-11 16:33:52 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /* |
| 110 | * this is used to store 1 to SSL_SOCK_NUM_KEYTYPES cert_key_and_chain and |
| 111 | * metadata. |
| 112 | */ |
| 113 | struct ckch_store { |
| 114 | struct cert_key_and_chain *ckch; |
Tim Duesterhus | 6a0dd73 | 2020-01-18 01:32:49 +0100 | [diff] [blame] | 115 | unsigned int multi:1; /* is it a multi-cert bundle ? */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 116 | struct list ckch_inst; /* list of ckch_inst which uses this ckch_node */ |
William Lallemand | f6adbe9 | 2019-09-11 16:33:52 +0200 | [diff] [blame] | 117 | struct ebmb_node node; |
| 118 | char path[0]; |
| 119 | }; |
| 120 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 121 | /* |
| 122 | * This structure describe a ckch instance. An instance is generated for each |
| 123 | * bind_conf. The instance contains a linked list of the sni ctx which uses |
| 124 | * the ckch in this bind_conf. |
| 125 | * |
| 126 | * XXX: the instance may evolve to handle ssl_bind_conf instead of bind_conf. |
| 127 | */ |
| 128 | struct ckch_inst { |
| 129 | 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] | 130 | 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] | 131 | struct ckch_store *ckch_store; /* pointer to the store used to generate this inst */ |
William Lallemand | 6763016 | 2020-03-09 16:56:39 +0100 | [diff] [blame^] | 132 | unsigned int filters:1; /* using sni filters ? */ |
William Lallemand | 0a52846 | 2020-03-09 16:53:42 +0100 | [diff] [blame] | 133 | unsigned int is_default:1; /* This instance is used as the default ctx for this bind_conf */ |
| 134 | /* space for more flag there */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 135 | struct list sni_ctx; /* list of sni_ctx using this ckch_inst */ |
| 136 | struct list by_ckchs; /* chained in ckch_store's list of ckch_inst */ |
| 137 | }; |
William Lallemand | f6adbe9 | 2019-09-11 16:33:52 +0200 | [diff] [blame] | 138 | |
| 139 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 140 | |
| 141 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 142 | |
| 143 | struct key_combo_ctx { |
| 144 | SSL_CTX *ctx; |
| 145 | int order; |
| 146 | }; |
| 147 | |
| 148 | /* Map used for processing multiple keypairs for a single purpose |
| 149 | * |
| 150 | * This maps CN/SNI name to certificate type |
| 151 | */ |
| 152 | struct sni_keytype { |
| 153 | int keytypes; /* BITMASK for keytypes */ |
| 154 | struct ebmb_node name; /* node holding the servername value */ |
| 155 | }; |
| 156 | |
| 157 | #endif |
| 158 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 159 | /* issuer chain store with hash of Subject Key Identifier |
| 160 | certificate/issuer matching is verify with X509_check_issued |
| 161 | */ |
| 162 | struct issuer_chain { |
| 163 | struct eb64_node node; |
| 164 | STACK_OF(X509) *chain; |
| 165 | char *path; |
| 166 | }; |
| 167 | |
| 168 | |
Willy Tarreau | c125cef | 2019-05-10 09:58:43 +0200 | [diff] [blame] | 169 | #endif /* USE_OPENSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 170 | #endif /* _TYPES_SSL_SOCK_H */ |