yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2 | /* |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3 | * SSL/TLS transport layer over SOCK_STREAM sockets |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
Willy Tarreau | 69845df | 2012-09-10 09:43:09 +0200 | [diff] [blame] | 12 | * Acknowledgement: |
| 13 | * We'd like to specially thank the Stud project authors for a very clean |
| 14 | * and well documented code which helped us understand how the OpenSSL API |
| 15 | * ought to be used in non-blocking mode. This is one difficult part which |
| 16 | * is not easy to get from the OpenSSL doc, and reading the Stud code made |
| 17 | * it much more obvious than the examples in the OpenSSL package. Keep up |
| 18 | * the good works, guys ! |
| 19 | * |
| 20 | * Stud is an extremely efficient and scalable SSL/TLS proxy which combines |
| 21 | * particularly well with haproxy. For more info about this project, visit : |
| 22 | * https://github.com/bumptech/stud |
| 23 | * |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
Willy Tarreau | 8d164dc | 2019-05-10 09:35:00 +0200 | [diff] [blame] | 26 | /* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 27 | #define _GNU_SOURCE |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 28 | #include <ctype.h> |
| 29 | #include <dirent.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 34 | #include <string.h> |
| 35 | #include <unistd.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 36 | |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 40 | #include <netdb.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 41 | #include <netinet/tcp.h> |
| 42 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 43 | #include <haproxy/api.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 44 | #include <import/lru.h> |
| 45 | #include <import/xxhash.h> |
| 46 | |
Willy Tarreau | 2741c8c | 2020-06-02 11:28:02 +0200 | [diff] [blame^] | 47 | #include <haproxy/dynbuf.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 48 | #include <haproxy/chunk.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 49 | #include <haproxy/errors.h> |
Willy Tarreau | 6019fab | 2020-05-27 16:26:00 +0200 | [diff] [blame] | 50 | #include <haproxy/openssl-compat.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 51 | #include <common/standard.h> |
| 52 | #include <common/ticks.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 53 | #include <haproxy/time.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 54 | #include <haproxy/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 55 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 56 | #include <import/ebpttree.h> |
| 57 | #include <import/ebsttree.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 58 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 59 | #include <types/applet.h> |
| 60 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 61 | #include <types/global.h> |
| 62 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 63 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 64 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 65 | #include <proto/acl.h> |
| 66 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 67 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 68 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 69 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 70 | #include <proto/fd.h> |
Willy Tarreau | 6634794 | 2020-06-01 12:18:08 +0200 | [diff] [blame] | 71 | #include <haproxy/freq_ctr.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 72 | #include <proto/frontend.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 73 | #include <proto/http_rules.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 74 | #include <proto/listener.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 75 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 76 | #include <proto/proto_tcp.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 77 | #include <proto/http_ana.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 78 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 79 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 80 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 81 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 82 | #include <proto/shctx.h> |
William Lallemand | c69973f | 2020-05-12 17:42:42 +0200 | [diff] [blame] | 83 | #include <proto/ssl_ckch.h> |
William Lallemand | 6e9556b | 2020-05-12 17:52:44 +0200 | [diff] [blame] | 84 | #include <proto/ssl_crtlist.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 85 | #include <proto/ssl_sock.h> |
William Lallemand | 6a66a5e | 2020-05-15 12:01:17 +0200 | [diff] [blame] | 86 | #include <proto/ssl_utils.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 87 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 88 | #include <proto/task.h> |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 89 | #include <proto/vars.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 90 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 91 | /* ***** READ THIS before adding code here! ***** |
| 92 | * |
| 93 | * Due to API incompatibilities between multiple OpenSSL versions and their |
| 94 | * derivatives, it's often tempting to add macros to (re-)define certain |
| 95 | * symbols. Please do not do this here, and do it in common/openssl-compat.h |
| 96 | * exclusively so that the whole code consistently uses the same macros. |
| 97 | * |
| 98 | * Whenever possible if a macro is missing in certain versions, it's better |
| 99 | * to conditionally define it in openssl-compat.h than using lots of ifdefs. |
| 100 | */ |
| 101 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 102 | int sslconns = 0; |
| 103 | int totalsslconns = 0; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 104 | int nb_engines = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 105 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 106 | static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */ |
| 107 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 108 | struct global_ssl global_ssl = { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 109 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 110 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 111 | #endif |
| 112 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 113 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 114 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 115 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 116 | #ifdef LISTEN_DEFAULT_CIPHERSUITES |
| 117 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
| 118 | #endif |
| 119 | #ifdef CONNECT_DEFAULT_CIPHERSUITES |
| 120 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 121 | #endif |
| 122 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 123 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 124 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 125 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 126 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 127 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 128 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 129 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 130 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 131 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 132 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 133 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 134 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 135 | #endif |
| 136 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 137 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 138 | .capture_cipherlist = 0, |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 139 | .extra_files = SSL_GF_ALL, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 140 | }; |
| 141 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 142 | static BIO_METHOD *ha_meth; |
| 143 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 144 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx)); |
| 145 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 146 | static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short); |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 147 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 148 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 149 | /* Methods to implement OpenSSL BIO */ |
| 150 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 151 | { |
| 152 | struct buffer tmpbuf; |
| 153 | struct ssl_sock_ctx *ctx; |
| 154 | int ret; |
| 155 | |
| 156 | ctx = BIO_get_data(h); |
| 157 | tmpbuf.size = num; |
| 158 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 159 | tmpbuf.data = num; |
| 160 | tmpbuf.head = 0; |
| 161 | ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0); |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 162 | if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 163 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 164 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 165 | } else if (ret == 0) |
| 166 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 171 | { |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int ha_ssl_puts(BIO *h, const char *str) |
| 177 | { |
| 178 | |
| 179 | return ha_ssl_write(h, str, strlen(str)); |
| 180 | } |
| 181 | |
| 182 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 183 | { |
| 184 | struct buffer tmpbuf; |
| 185 | struct ssl_sock_ctx *ctx; |
| 186 | int ret; |
| 187 | |
| 188 | ctx = BIO_get_data(h); |
| 189 | tmpbuf.size = size; |
| 190 | tmpbuf.area = buf; |
| 191 | tmpbuf.data = 0; |
| 192 | tmpbuf.head = 0; |
| 193 | ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0); |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 194 | if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 195 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 196 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 197 | } else if (ret == 0) |
| 198 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 204 | { |
| 205 | int ret = 0; |
| 206 | switch (cmd) { |
| 207 | case BIO_CTRL_DUP: |
| 208 | case BIO_CTRL_FLUSH: |
| 209 | ret = 1; |
| 210 | break; |
| 211 | } |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | static int ha_ssl_new(BIO *h) |
| 216 | { |
| 217 | BIO_set_init(h, 1); |
| 218 | BIO_set_data(h, NULL); |
| 219 | BIO_clear_flags(h, ~0); |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | static int ha_ssl_free(BIO *data) |
| 224 | { |
| 225 | |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 230 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 231 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 232 | static HA_RWLOCK_T *ssl_rwlocks; |
| 233 | |
| 234 | |
| 235 | unsigned long ssl_id_function(void) |
| 236 | { |
| 237 | return (unsigned long)tid; |
| 238 | } |
| 239 | |
| 240 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 241 | { |
| 242 | if (mode & CRYPTO_LOCK) { |
| 243 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 244 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 245 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 246 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 247 | } |
| 248 | else { |
| 249 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 250 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 251 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 252 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | static int ssl_locking_init(void) |
| 257 | { |
| 258 | int i; |
| 259 | |
| 260 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 261 | if (!ssl_rwlocks) |
| 262 | return -1; |
| 263 | |
| 264 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 265 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 266 | |
| 267 | CRYPTO_set_id_callback(ssl_id_function); |
| 268 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 269 | |
| 270 | return 0; |
| 271 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 272 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 273 | #endif |
| 274 | |
Willy Tarreau | af613e8 | 2020-06-05 08:40:51 +0200 | [diff] [blame] | 275 | __decl_thread(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 276 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 277 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 278 | /* |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 279 | * deduplicate cafile (and crlfile) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 280 | */ |
| 281 | struct cafile_entry { |
| 282 | X509_STORE *ca_store; |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 283 | STACK_OF(X509_NAME) *ca_list; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 284 | struct ebmb_node node; |
| 285 | char path[0]; |
| 286 | }; |
| 287 | |
| 288 | static struct eb_root cafile_tree = EB_ROOT_UNIQUE; |
| 289 | |
| 290 | static X509_STORE* ssl_store_get0_locations_file(char *path) |
| 291 | { |
| 292 | struct ebmb_node *eb; |
| 293 | |
| 294 | eb = ebst_lookup(&cafile_tree, path); |
| 295 | if (eb) { |
| 296 | struct cafile_entry *ca_e; |
| 297 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 298 | return ca_e->ca_store; |
| 299 | } |
| 300 | return NULL; |
| 301 | } |
| 302 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 303 | int ssl_store_load_locations_file(char *path) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 304 | { |
| 305 | if (ssl_store_get0_locations_file(path) == NULL) { |
| 306 | struct cafile_entry *ca_e; |
| 307 | X509_STORE *store = X509_STORE_new(); |
| 308 | if (X509_STORE_load_locations(store, path, NULL)) { |
| 309 | int pathlen; |
| 310 | pathlen = strlen(path); |
| 311 | ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1); |
| 312 | if (ca_e) { |
| 313 | memcpy(ca_e->path, path, pathlen + 1); |
| 314 | ca_e->ca_store = store; |
| 315 | ebst_insert(&cafile_tree, &ca_e->node); |
| 316 | return 1; |
| 317 | } |
| 318 | } |
| 319 | X509_STORE_free(store); |
| 320 | return 0; |
| 321 | } |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 326 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 327 | { |
| 328 | X509_STORE *store; |
| 329 | store = ssl_store_get0_locations_file(path); |
| 330 | if (store_ctx && store) { |
| 331 | int i; |
| 332 | X509_OBJECT *obj; |
| 333 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 334 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 335 | obj = sk_X509_OBJECT_value(objs, i); |
| 336 | switch (X509_OBJECT_get_type(obj)) { |
| 337 | case X509_LU_X509: |
| 338 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 339 | break; |
| 340 | case X509_LU_CRL: |
| 341 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 342 | break; |
| 343 | default: |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | return 1; |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 352 | /* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */ |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 353 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 354 | { |
| 355 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 356 | return ssl_set_cert_crl_file(store_ctx, path); |
| 357 | } |
| 358 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 359 | /* |
| 360 | Extract CA_list from CA_file already in tree. |
| 361 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 362 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 363 | */ |
| 364 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 365 | { |
| 366 | struct ebmb_node *eb; |
| 367 | struct cafile_entry *ca_e; |
| 368 | |
| 369 | eb = ebst_lookup(&cafile_tree, path); |
| 370 | if (!eb) |
| 371 | return NULL; |
| 372 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 373 | |
| 374 | if (ca_e->ca_list == NULL) { |
| 375 | int i; |
| 376 | unsigned long key; |
| 377 | struct eb_root ca_name_tree = EB_ROOT; |
| 378 | struct eb64_node *node, *back; |
| 379 | struct { |
| 380 | struct eb64_node node; |
| 381 | X509_NAME *xname; |
| 382 | } *ca_name; |
| 383 | STACK_OF(X509_OBJECT) *objs; |
| 384 | STACK_OF(X509_NAME) *skn; |
| 385 | X509 *x; |
| 386 | X509_NAME *xn; |
| 387 | |
| 388 | skn = sk_X509_NAME_new_null(); |
| 389 | /* take x509 from cafile_tree */ |
| 390 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 391 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 392 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 393 | if (!x) |
| 394 | continue; |
| 395 | xn = X509_get_subject_name(x); |
| 396 | if (!xn) |
| 397 | continue; |
| 398 | /* Check for duplicates. */ |
| 399 | key = X509_NAME_hash(xn); |
| 400 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 401 | node && ca_name == NULL; |
| 402 | node = eb64_next(node)) { |
| 403 | ca_name = container_of(node, typeof(*ca_name), node); |
| 404 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 405 | ca_name = NULL; |
| 406 | } |
| 407 | /* find a duplicate */ |
| 408 | if (ca_name) |
| 409 | continue; |
| 410 | ca_name = calloc(1, sizeof *ca_name); |
| 411 | xn = X509_NAME_dup(xn); |
| 412 | if (!ca_name || |
| 413 | !xn || |
| 414 | !sk_X509_NAME_push(skn, xn)) { |
| 415 | free(ca_name); |
| 416 | X509_NAME_free(xn); |
| 417 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 418 | sk_X509_NAME_free(skn); |
| 419 | skn = NULL; |
| 420 | break; |
| 421 | } |
| 422 | ca_name->node.key = key; |
| 423 | ca_name->xname = xn; |
| 424 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 425 | } |
| 426 | ca_e->ca_list = skn; |
| 427 | /* remove temporary ca_name tree */ |
| 428 | node = eb64_first(&ca_name_tree); |
| 429 | while (node) { |
| 430 | ca_name = container_of(node, typeof(*ca_name), node); |
| 431 | back = eb64_next(node); |
| 432 | eb64_delete(node); |
| 433 | free(ca_name); |
| 434 | node = back; |
| 435 | } |
| 436 | } |
| 437 | return ca_e->ca_list; |
| 438 | } |
| 439 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 440 | struct pool_head *pool_head_ssl_capture = NULL; |
William Lallemand | 15e1694 | 2020-05-15 00:25:08 +0200 | [diff] [blame] | 441 | int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 442 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 443 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 444 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 445 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 446 | #endif |
| 447 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 448 | #ifndef OPENSSL_NO_ENGINE |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 449 | unsigned int openssl_engines_initialized; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 450 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 451 | struct ssl_engine_list { |
| 452 | struct list list; |
| 453 | ENGINE *e; |
| 454 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 455 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 456 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 457 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 458 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 459 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 460 | static DH *local_dh_1024 = NULL; |
| 461 | static DH *local_dh_2048 = NULL; |
| 462 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 463 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 464 | #endif /* OPENSSL_NO_DH */ |
| 465 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 466 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 467 | /* X509V3 Extensions that will be added on generated certificates */ |
| 468 | #define X509V3_EXT_SIZE 5 |
| 469 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 470 | "basicConstraints", |
| 471 | "nsComment", |
| 472 | "subjectKeyIdentifier", |
| 473 | "authorityKeyIdentifier", |
| 474 | "keyUsage", |
| 475 | }; |
| 476 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 477 | "CA:FALSE", |
| 478 | "\"OpenSSL Generated Certificate\"", |
| 479 | "hash", |
| 480 | "keyid,issuer:always", |
| 481 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 482 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 483 | /* LRU cache to store generated certificate */ |
| 484 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 485 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 486 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 487 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 488 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 489 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 490 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 491 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 492 | /* The order here matters for picking a default context, |
| 493 | * keep the most common keytype at the bottom of the list |
| 494 | */ |
| 495 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 496 | "dsa", |
| 497 | "ecdsa", |
| 498 | "rsa" |
| 499 | }; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 500 | #endif |
| 501 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 502 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 503 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 504 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 505 | /* Dedicated callback functions for heartbeat and clienthello. |
| 506 | */ |
| 507 | #ifdef TLS1_RT_HEARTBEAT |
| 508 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 509 | int content_type, const void *buf, size_t len, |
| 510 | SSL *ssl); |
| 511 | #endif |
| 512 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 513 | int content_type, const void *buf, size_t len, |
| 514 | SSL *ssl); |
| 515 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 516 | /* List head of all registered SSL/TLS protocol message callbacks. */ |
| 517 | struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks); |
| 518 | |
| 519 | /* Registers the function <func> in order to be called on SSL/TLS protocol |
| 520 | * message processing. It will return 0 if the function <func> is not set |
| 521 | * or if it fails to allocate memory. |
| 522 | */ |
| 523 | int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func) |
| 524 | { |
| 525 | struct ssl_sock_msg_callback *cbk; |
| 526 | |
| 527 | if (!func) |
| 528 | return 0; |
| 529 | |
| 530 | cbk = calloc(1, sizeof(*cbk)); |
| 531 | if (!cbk) { |
| 532 | ha_alert("out of memory in ssl_sock_register_msg_callback().\n"); |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | cbk->func = func; |
| 537 | |
| 538 | LIST_ADDQ(&ssl_sock_msg_callbacks, &cbk->list); |
| 539 | |
| 540 | return 1; |
| 541 | } |
| 542 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 543 | /* Used to register dedicated SSL/TLS protocol message callbacks. |
| 544 | */ |
| 545 | static int ssl_sock_register_msg_callbacks(void) |
| 546 | { |
| 547 | #ifdef TLS1_RT_HEARTBEAT |
| 548 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat)) |
| 549 | return ERR_ABORT; |
| 550 | #endif |
| 551 | if (global_ssl.capture_cipherlist > 0) { |
| 552 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello)) |
| 553 | return ERR_ABORT; |
| 554 | } |
| 555 | return 0; |
| 556 | } |
| 557 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 558 | /* Used to free all SSL/TLS protocol message callbacks that were |
| 559 | * registered by using ssl_sock_register_msg_callback(). |
| 560 | */ |
| 561 | static void ssl_sock_unregister_msg_callbacks(void) |
| 562 | { |
| 563 | struct ssl_sock_msg_callback *cbk, *cbkback; |
| 564 | |
| 565 | list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) { |
| 566 | LIST_DEL(&cbk->list); |
| 567 | free(cbk); |
| 568 | } |
| 569 | } |
| 570 | |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 571 | SSL *ssl_sock_get_ssl_object(struct connection *conn) |
| 572 | { |
| 573 | if (!ssl_sock_is_ssl(conn)) |
| 574 | return NULL; |
| 575 | |
| 576 | return ((struct ssl_sock_ctx *)(conn->xprt_ctx))->ssl; |
| 577 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 578 | /* |
| 579 | * This function gives the detail of the SSL error. It is used only |
| 580 | * if the debug mode and the verbose mode are activated. It dump all |
| 581 | * the SSL error until the stack was empty. |
| 582 | */ |
| 583 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 584 | { |
| 585 | unsigned long ret; |
| 586 | |
| 587 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 588 | while(1) { |
| 589 | ret = ERR_get_error(); |
| 590 | if (ret == 0) |
| 591 | return; |
| 592 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 593 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 594 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 599 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 600 | #ifndef OPENSSL_NO_ENGINE |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 601 | int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 602 | { |
| 603 | int err_code = ERR_ABORT; |
| 604 | ENGINE *engine; |
| 605 | struct ssl_engine_list *el; |
| 606 | |
| 607 | /* grab the structural reference to the engine */ |
| 608 | engine = ENGINE_by_id(engine_id); |
| 609 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 610 | ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 611 | goto fail_get; |
| 612 | } |
| 613 | |
| 614 | if (!ENGINE_init(engine)) { |
| 615 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 616 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 617 | goto fail_init; |
| 618 | } |
| 619 | |
| 620 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 621 | ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 622 | goto fail_set_method; |
| 623 | } |
| 624 | |
| 625 | el = calloc(1, sizeof(*el)); |
| 626 | el->e = engine; |
| 627 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 628 | nb_engines++; |
| 629 | if (global_ssl.async) |
| 630 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 631 | return 0; |
| 632 | |
| 633 | fail_set_method: |
| 634 | /* release the functional reference from ENGINE_init() */ |
| 635 | ENGINE_finish(engine); |
| 636 | |
| 637 | fail_init: |
| 638 | /* release the structural reference from ENGINE_by_id() */ |
| 639 | ENGINE_free(engine); |
| 640 | |
| 641 | fail_get: |
| 642 | return err_code; |
| 643 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 644 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 645 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 646 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 647 | /* |
| 648 | * openssl async fd handler |
| 649 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 650 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 651 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 652 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 653 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 654 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 655 | * to poll this fd until it is requested |
| 656 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 657 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 658 | fd_cant_recv(fd); |
| 659 | |
| 660 | /* crypto engine is available, let's notify the associated |
| 661 | * connection that it can pursue its processing. |
| 662 | */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 663 | ssl_sock_io_cb(NULL, ctx, 0); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 666 | /* |
| 667 | * openssl async delayed SSL_free handler |
| 668 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 669 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 670 | { |
| 671 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 672 | OSSL_ASYNC_FD all_fd[32]; |
| 673 | size_t num_all_fds = 0; |
| 674 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 675 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 676 | /* We suppose that the async job for a same SSL * |
| 677 | * are serialized. So if we are awake it is |
| 678 | * because the running job has just finished |
| 679 | * and we can remove all async fds safely |
| 680 | */ |
| 681 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 682 | if (num_all_fds > 32) { |
| 683 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 688 | for (i=0 ; i < num_all_fds ; i++) |
| 689 | fd_remove(all_fd[i]); |
| 690 | |
| 691 | /* Now we can safely call SSL_free, no more pending job in engines */ |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 692 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 693 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 694 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 695 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 696 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 697 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 698 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 699 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 700 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 701 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 702 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 703 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 704 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 705 | size_t num_add_fds = 0; |
| 706 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 707 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 708 | |
| 709 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 710 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 711 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 712 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 713 | return; |
| 714 | } |
| 715 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 716 | SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 717 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 718 | /* We remove unused fds from the fdtab */ |
| 719 | for (i=0 ; i < num_del_fds ; i++) |
| 720 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 721 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 722 | /* We add new fds to the fdtab */ |
| 723 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 724 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 727 | num_add_fds = 0; |
| 728 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 729 | if (num_add_fds > 32) { |
| 730 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 731 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 732 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 733 | |
| 734 | /* We activate the polling for all known async fds */ |
| 735 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 736 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 737 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 738 | /* To ensure that the fd cache won't be used |
| 739 | * We'll prefer to catch a real RD event |
| 740 | * because handling an EAGAIN on this fd will |
| 741 | * result in a context switch and also |
| 742 | * some engines uses a fd in blocking mode. |
| 743 | */ |
| 744 | fd_cant_recv(add_fd[i]); |
| 745 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 746 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 747 | } |
| 748 | #endif |
| 749 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 750 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 751 | /* |
| 752 | * This function returns the number of seconds elapsed |
| 753 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 754 | * date presented un ASN1_GENERALIZEDTIME. |
| 755 | * |
| 756 | * In parsing error case, it returns -1. |
| 757 | */ |
| 758 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 759 | { |
| 760 | long epoch; |
| 761 | char *p, *end; |
| 762 | const unsigned short month_offset[12] = { |
| 763 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 764 | }; |
| 765 | int year, month; |
| 766 | |
| 767 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 768 | |
| 769 | p = (char *)d->data; |
| 770 | end = p + d->length; |
| 771 | |
| 772 | if (end - p < 4) return -1; |
| 773 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 774 | p += 4; |
| 775 | if (end - p < 2) return -1; |
| 776 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 777 | if (month < 1 || month > 12) return -1; |
| 778 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 779 | We consider leap years and the current month (<marsh or not) */ |
| 780 | epoch = ( ((year - 1970) * 365) |
| 781 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 782 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 783 | + month_offset[month-1] |
| 784 | ) * 24 * 60 * 60; |
| 785 | p += 2; |
| 786 | if (end - p < 2) return -1; |
| 787 | /* Add the number of seconds of completed days of current month */ |
| 788 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 789 | p += 2; |
| 790 | if (end - p < 2) return -1; |
| 791 | /* Add the completed hours of the current day */ |
| 792 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 793 | p += 2; |
| 794 | if (end - p < 2) return -1; |
| 795 | /* Add the completed minutes of the current hour */ |
| 796 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 797 | p += 2; |
| 798 | if (p == end) return -1; |
| 799 | /* Test if there is available seconds */ |
| 800 | if (p[0] < '0' || p[0] > '9') |
| 801 | goto nosec; |
| 802 | if (end - p < 2) return -1; |
| 803 | /* Add the seconds of the current minute */ |
| 804 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 805 | p += 2; |
| 806 | if (p == end) return -1; |
| 807 | /* Ignore seconds float part if present */ |
| 808 | if (p[0] == '.') { |
| 809 | do { |
| 810 | if (++p == end) return -1; |
| 811 | } while (p[0] >= '0' && p[0] <= '9'); |
| 812 | } |
| 813 | |
| 814 | nosec: |
| 815 | if (p[0] == 'Z') { |
| 816 | if (end - p != 1) return -1; |
| 817 | return epoch; |
| 818 | } |
| 819 | else if (p[0] == '+') { |
| 820 | if (end - p != 5) return -1; |
| 821 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 822 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 823 | } |
| 824 | else if (p[0] == '-') { |
| 825 | if (end - p != 5) return -1; |
| 826 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 827 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | return -1; |
| 831 | } |
| 832 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 833 | /* |
| 834 | * struct alignment works here such that the key.key is the same as key_data |
| 835 | * Do not change the placement of key_data |
| 836 | */ |
| 837 | struct certificate_ocsp { |
| 838 | struct ebmb_node key; |
| 839 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 840 | struct buffer response; |
| 841 | long expire; |
| 842 | }; |
| 843 | |
| 844 | struct ocsp_cbk_arg { |
| 845 | int is_single; |
| 846 | int single_kt; |
| 847 | union { |
| 848 | struct certificate_ocsp *s_ocsp; |
| 849 | /* |
| 850 | * m_ocsp will have multiple entries dependent on key type |
| 851 | * Entry 0 - DSA |
| 852 | * Entry 1 - ECDSA |
| 853 | * Entry 2 - RSA |
| 854 | */ |
| 855 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 856 | }; |
| 857 | }; |
| 858 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 859 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 860 | |
| 861 | /* This function starts to check if the OCSP response (in DER format) contained |
| 862 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 863 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 864 | * contained in the OCSP Response and exits on error if no match. |
| 865 | * If it's a valid OCSP Response: |
| 866 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 867 | * pointed by 'ocsp'. |
| 868 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 869 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 870 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 871 | * already present in the container, it will be overwritten. |
| 872 | * |
| 873 | * Note: OCSP response containing more than one OCSP Single response is not |
| 874 | * considered valid. |
| 875 | * |
| 876 | * Returns 0 on success, 1 in error case. |
| 877 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 878 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 879 | struct certificate_ocsp *ocsp, |
| 880 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 881 | { |
| 882 | OCSP_RESPONSE *resp; |
| 883 | OCSP_BASICRESP *bs = NULL; |
| 884 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 885 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 886 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 887 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 888 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 889 | int reason; |
| 890 | int ret = 1; |
| 891 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 892 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 893 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 894 | if (!resp) { |
| 895 | memprintf(err, "Unable to parse OCSP response"); |
| 896 | goto out; |
| 897 | } |
| 898 | |
| 899 | rc = OCSP_response_status(resp); |
| 900 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 901 | memprintf(err, "OCSP response status not successful"); |
| 902 | goto out; |
| 903 | } |
| 904 | |
| 905 | bs = OCSP_response_get1_basic(resp); |
| 906 | if (!bs) { |
| 907 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 908 | goto out; |
| 909 | } |
| 910 | |
| 911 | count_sr = OCSP_resp_count(bs); |
| 912 | if (count_sr > 1) { |
| 913 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 914 | goto out; |
| 915 | } |
| 916 | |
| 917 | sr = OCSP_resp_get0(bs, 0); |
| 918 | if (!sr) { |
| 919 | memprintf(err, "Failed to get OCSP single response"); |
| 920 | goto out; |
| 921 | } |
| 922 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 923 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 924 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 925 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 926 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 927 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 928 | goto out; |
| 929 | } |
| 930 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 931 | if (!nextupd) { |
| 932 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 933 | goto out; |
| 934 | } |
| 935 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 936 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 937 | if (!rc) { |
| 938 | memprintf(err, "OCSP single response: no longer valid."); |
| 939 | goto out; |
| 940 | } |
| 941 | |
| 942 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 943 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 944 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 945 | goto out; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | if (!ocsp) { |
| 950 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 951 | unsigned char *p; |
| 952 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 953 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 954 | if (!rc) { |
| 955 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 956 | goto out; |
| 957 | } |
| 958 | |
| 959 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 960 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 961 | goto out; |
| 962 | } |
| 963 | |
| 964 | p = key; |
| 965 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 966 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 967 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 968 | if (!ocsp) { |
| 969 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 970 | goto out; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | /* According to comments on "chunk_dup", the |
| 975 | previous chunk buffer will be freed */ |
| 976 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 977 | memprintf(err, "OCSP response: Memory allocation error"); |
| 978 | goto out; |
| 979 | } |
| 980 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 981 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 982 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 983 | ret = 0; |
| 984 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 985 | ERR_clear_error(); |
| 986 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 987 | if (bs) |
| 988 | OCSP_BASICRESP_free(bs); |
| 989 | |
| 990 | if (resp) |
| 991 | OCSP_RESPONSE_free(resp); |
| 992 | |
| 993 | return ret; |
| 994 | } |
| 995 | /* |
| 996 | * External function use to update the OCSP response in the OCSP response's |
| 997 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 998 | * to update in DER format. |
| 999 | * |
| 1000 | * Returns 0 on success, 1 in error case. |
| 1001 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1002 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1003 | { |
| 1004 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 1005 | } |
| 1006 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1007 | #endif |
| 1008 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1009 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 1010 | static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) |
| 1011 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1012 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1013 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1014 | struct connection *conn; |
| 1015 | int head; |
| 1016 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1017 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1018 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1019 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1020 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1021 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1022 | |
| 1023 | keys = ref->tlskeys; |
| 1024 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1025 | |
| 1026 | if (enc) { |
| 1027 | memcpy(key_name, keys[head].name, 16); |
| 1028 | |
| 1029 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1030 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1031 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1032 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1033 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1034 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 1035 | goto end; |
| 1036 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1037 | HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1038 | ret = 1; |
| 1039 | } |
| 1040 | else if (ref->key_size_bits == 256 ) { |
| 1041 | |
| 1042 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 1043 | goto end; |
| 1044 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1045 | HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1046 | ret = 1; |
| 1047 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1048 | } else { |
| 1049 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 1050 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 1051 | goto found; |
| 1052 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1053 | ret = 0; |
| 1054 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1055 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1056 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1057 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1058 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1059 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 1060 | goto end; |
| 1061 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1062 | ret = i ? 2 : 1; |
| 1063 | } |
| 1064 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1065 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1066 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 1067 | goto end; |
| 1068 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1069 | ret = i ? 2 : 1; |
| 1070 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1071 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1072 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1073 | end: |
| 1074 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1075 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1079 | { |
| 1080 | struct tls_keys_ref *ref; |
| 1081 | |
| 1082 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1083 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1084 | return ref; |
| 1085 | return NULL; |
| 1086 | } |
| 1087 | |
| 1088 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1089 | { |
| 1090 | struct tls_keys_ref *ref; |
| 1091 | |
| 1092 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1093 | if (ref->unique_id == unique_id) |
| 1094 | return ref; |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1098 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1099 | * match existing ones, this function returns -1 |
| 1100 | * else it returns 0 on success. |
| 1101 | */ |
| 1102 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1103 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1104 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1105 | if (ref->key_size_bits == 128) { |
| 1106 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1107 | return -1; |
| 1108 | } |
| 1109 | else if (ref->key_size_bits == 256) { |
| 1110 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1111 | return -1; |
| 1112 | } |
| 1113 | else |
| 1114 | return -1; |
| 1115 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1116 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1117 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1118 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1119 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1120 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1121 | |
| 1122 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1123 | } |
| 1124 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1125 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1126 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1127 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1128 | |
| 1129 | if(!ref) { |
| 1130 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1131 | return 1; |
| 1132 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1133 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1134 | memprintf(err, "Invalid key size"); |
| 1135 | return 1; |
| 1136 | } |
| 1137 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1138 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1139 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1140 | |
| 1141 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1142 | * automatic ids. It's called just after the basic checks. It returns |
| 1143 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1144 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1145 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1146 | { |
| 1147 | int i = 0; |
| 1148 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1149 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1150 | |
| 1151 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1152 | if (ref->unique_id == -1) { |
| 1153 | /* Look for the first free id. */ |
| 1154 | while (1) { |
| 1155 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1156 | if (ref2->unique_id == i) { |
| 1157 | i++; |
| 1158 | break; |
| 1159 | } |
| 1160 | } |
| 1161 | if (&ref2->list == &tlskeys_reference) |
| 1162 | break; |
| 1163 | } |
| 1164 | |
| 1165 | /* Uses the unique id and increment it for the next entry. */ |
| 1166 | ref->unique_id = i; |
| 1167 | i++; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | /* This sort the reference list by id. */ |
| 1172 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1173 | LIST_DEL(&ref->list); |
| 1174 | list_for_each_entry(ref3, &tkr, list) { |
| 1175 | if (ref->unique_id < ref3->unique_id) { |
| 1176 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1177 | break; |
| 1178 | } |
| 1179 | } |
| 1180 | if (&ref3->list == &tkr) |
| 1181 | LIST_ADDQ(&tkr, &ref->list); |
| 1182 | } |
| 1183 | |
| 1184 | /* swap root */ |
| 1185 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1186 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1187 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1188 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1189 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1190 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1191 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1192 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1193 | { |
| 1194 | switch (evp_keytype) { |
| 1195 | case EVP_PKEY_RSA: |
| 1196 | return 2; |
| 1197 | case EVP_PKEY_DSA: |
| 1198 | return 0; |
| 1199 | case EVP_PKEY_EC: |
| 1200 | return 1; |
| 1201 | } |
| 1202 | |
| 1203 | return -1; |
| 1204 | } |
| 1205 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1206 | /* |
| 1207 | * Callback used to set OCSP status extension content in server hello. |
| 1208 | */ |
| 1209 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1210 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1211 | struct certificate_ocsp *ocsp; |
| 1212 | struct ocsp_cbk_arg *ocsp_arg; |
| 1213 | char *ssl_buf; |
| 1214 | EVP_PKEY *ssl_pkey; |
| 1215 | int key_type; |
| 1216 | int index; |
| 1217 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1218 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1219 | |
| 1220 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1221 | if (!ssl_pkey) |
| 1222 | return SSL_TLSEXT_ERR_NOACK; |
| 1223 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1224 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1225 | |
| 1226 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1227 | ocsp = ocsp_arg->s_ocsp; |
| 1228 | else { |
| 1229 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1230 | * the certificate type |
| 1231 | */ |
| 1232 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1233 | |
| 1234 | if (index < 0) |
| 1235 | return SSL_TLSEXT_ERR_NOACK; |
| 1236 | |
| 1237 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1238 | |
| 1239 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1240 | |
| 1241 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1242 | !ocsp->response.area || |
| 1243 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1244 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1245 | return SSL_TLSEXT_ERR_NOACK; |
| 1246 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1247 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1248 | if (!ssl_buf) |
| 1249 | return SSL_TLSEXT_ERR_NOACK; |
| 1250 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1251 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1252 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1253 | |
| 1254 | return SSL_TLSEXT_ERR_OK; |
| 1255 | } |
| 1256 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1257 | #endif |
| 1258 | |
| 1259 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1260 | /* |
| 1261 | * This function enables the handling of OCSP status extension on 'ctx' if a |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1262 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1263 | * status extension, the issuer's certificate is mandatory. It should be |
| 1264 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1265 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1266 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1267 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1268 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1269 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1270 | * |
| 1271 | * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1272 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1273 | */ |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1274 | #ifndef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1275 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1276 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1277 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1278 | OCSP_CERTID *cid = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1279 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1280 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1281 | char *warn = NULL; |
| 1282 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1283 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1284 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1285 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1286 | x = ckch->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1287 | if (!x) |
| 1288 | goto out; |
| 1289 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1290 | issuer = ckch->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1291 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1292 | if (chain) { |
| 1293 | /* check if one of the certificate of the chain is the issuer */ |
| 1294 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1295 | X509 *ti = sk_X509_value(chain, i); |
| 1296 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1297 | issuer = ti; |
| 1298 | break; |
| 1299 | } |
| 1300 | } |
| 1301 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1302 | if (!issuer) |
| 1303 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1304 | |
| 1305 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1306 | if (!cid) |
| 1307 | goto out; |
| 1308 | |
| 1309 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1310 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1311 | goto out; |
| 1312 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1313 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1314 | if (!ocsp) |
| 1315 | goto out; |
| 1316 | |
| 1317 | p = ocsp->key_data; |
| 1318 | i2d_OCSP_CERTID(cid, &p); |
| 1319 | |
| 1320 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1321 | if (iocsp == ocsp) |
| 1322 | ocsp = NULL; |
| 1323 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1324 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1325 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1326 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1327 | #endif |
| 1328 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1329 | |
| 1330 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1331 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1332 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1333 | |
| 1334 | cb_arg->is_single = 1; |
| 1335 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1336 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1337 | pkey = X509_get_pubkey(x); |
| 1338 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1339 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1340 | |
| 1341 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1342 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1343 | } else { |
| 1344 | /* |
| 1345 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1346 | * Update that cb_arg with the new cert's staple |
| 1347 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1348 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1349 | struct certificate_ocsp *tmp_ocsp; |
| 1350 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1351 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1352 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1353 | |
| 1354 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1355 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1356 | #else |
| 1357 | cb_arg = ctx->tlsext_status_arg; |
| 1358 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1359 | |
| 1360 | /* |
| 1361 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1362 | * the order of operations below matter, take care when changing it |
| 1363 | */ |
| 1364 | tmp_ocsp = cb_arg->s_ocsp; |
| 1365 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1366 | cb_arg->s_ocsp = NULL; |
| 1367 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1368 | cb_arg->is_single = 0; |
| 1369 | cb_arg->single_kt = 0; |
| 1370 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1371 | pkey = X509_get_pubkey(x); |
| 1372 | key_type = EVP_PKEY_base_id(pkey); |
| 1373 | EVP_PKEY_free(pkey); |
| 1374 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1375 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1376 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1377 | cb_arg->m_ocsp[index] = iocsp; |
| 1378 | |
| 1379 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1380 | |
| 1381 | ret = 0; |
| 1382 | |
| 1383 | warn = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1384 | if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1385 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1386 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | out: |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1390 | if (cid) |
| 1391 | OCSP_CERTID_free(cid); |
| 1392 | |
| 1393 | if (ocsp) |
| 1394 | free(ocsp); |
| 1395 | |
| 1396 | if (warn) |
| 1397 | free(warn); |
| 1398 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1399 | return ret; |
| 1400 | } |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1401 | #else /* OPENSSL_IS_BORINGSSL */ |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1402 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain) |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1403 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1404 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)ckch->ocsp_response->area, ckch->ocsp_response->data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1405 | } |
| 1406 | #endif |
| 1407 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1408 | #endif |
| 1409 | |
| 1410 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1411 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1412 | |
| 1413 | #define CT_EXTENSION_TYPE 18 |
| 1414 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 1415 | int sctl_ex_index = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1416 | |
| 1417 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1418 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1419 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1420 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1421 | *out = (unsigned char *) sctl->area; |
| 1422 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1423 | |
| 1424 | return 1; |
| 1425 | } |
| 1426 | |
| 1427 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1428 | { |
| 1429 | return 1; |
| 1430 | } |
| 1431 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1432 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1433 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1434 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1435 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1436 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1437 | goto out; |
| 1438 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1439 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1440 | |
| 1441 | ret = 0; |
| 1442 | |
| 1443 | out: |
| 1444 | return ret; |
| 1445 | } |
| 1446 | |
| 1447 | #endif |
| 1448 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1449 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1450 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1451 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1452 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1453 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1454 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1455 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1456 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1457 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1458 | * change this to #if and do not assign a default value to this macro! |
| 1459 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1460 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1461 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1462 | if ((conn->flags & (CO_FL_WAIT_L6_CONN | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == 0) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1463 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1464 | conn->err_code = CO_ER_SSL_RENEG; |
| 1465 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1466 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1467 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1468 | |
| 1469 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1470 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1471 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1472 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1473 | consider that the buffering was activated, |
| 1474 | so we rise the output buffer size from 4k |
| 1475 | to 16k */ |
| 1476 | write_bio = SSL_get_wbio(ssl); |
| 1477 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1478 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1479 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1483 | } |
| 1484 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1485 | /* Callback is called for each certificate of the chain during a verify |
| 1486 | ok is set to 1 if preverify detect no error on current certificate. |
| 1487 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1488 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1489 | { |
| 1490 | SSL *ssl; |
| 1491 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1492 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1493 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1494 | |
| 1495 | ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1496 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1497 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1498 | ctx = conn->xprt_ctx; |
| 1499 | |
| 1500 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1501 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1502 | if (ok) /* no errors */ |
| 1503 | return ok; |
| 1504 | |
| 1505 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1506 | err = X509_STORE_CTX_get_error(x_store); |
| 1507 | |
| 1508 | /* check if CA error needs to be ignored */ |
| 1509 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1510 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1511 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1512 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1513 | } |
| 1514 | |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1515 | if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) { |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 1516 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1517 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1518 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1519 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1520 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1521 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1522 | return 0; |
| 1523 | } |
| 1524 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1525 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1526 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1527 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1528 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1529 | if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) { |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 1530 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1531 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1532 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1533 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1534 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1535 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1536 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1537 | } |
| 1538 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 1539 | #ifdef TLS1_RT_HEARTBEAT |
| 1540 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 1541 | int content_type, const void *buf, size_t len, |
| 1542 | SSL *ssl) |
| 1543 | { |
| 1544 | /* test heartbeat received (write_p is set to 0 |
| 1545 | for a received record) */ |
| 1546 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
| 1547 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 1548 | const unsigned char *p = buf; |
| 1549 | unsigned int payload; |
| 1550 | |
| 1551 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
| 1552 | |
| 1553 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1554 | if (*p != TLS1_HB_REQUEST) |
| 1555 | return; |
| 1556 | |
| 1557 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
| 1558 | goto kill_it; |
| 1559 | |
| 1560 | payload = (p[1] * 256) + p[2]; |
| 1561 | if (3 + payload + 16 <= len) |
| 1562 | return; /* OK no problem */ |
| 1563 | kill_it: |
| 1564 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1565 | * advertised payload is larger than the advertised packet |
| 1566 | * length, so we have garbage in the buffer between the |
| 1567 | * payload and the end of the buffer (p+len). We can't know |
| 1568 | * if the SSL stack is patched, and we don't know if we can |
| 1569 | * safely wipe out the area between p+3+len and payload. |
| 1570 | * So instead, we prevent the response from being sent by |
| 1571 | * setting the max_send_fragment to 0 and we report an SSL |
| 1572 | * error, which will kill this connection. It will be reported |
| 1573 | * above as SSL_ERROR_SSL while an other handshake failure with |
| 1574 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1575 | */ |
| 1576 | ssl->max_send_fragment = 0; |
| 1577 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1578 | } |
| 1579 | } |
| 1580 | #endif |
| 1581 | |
| 1582 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 1583 | int content_type, const void *buf, size_t len, |
| 1584 | SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1585 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1586 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1587 | unsigned char *msg; |
| 1588 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1589 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1590 | |
| 1591 | /* This function is called for "from client" and "to server" |
| 1592 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1593 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1594 | */ |
| 1595 | |
| 1596 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1597 | * otherwise it is set to 1. |
| 1598 | */ |
| 1599 | if (write_p != 0) |
| 1600 | return; |
| 1601 | |
| 1602 | /* content_type contains the type of message received or sent |
| 1603 | * according with the SSL/TLS protocol spec. This message is |
| 1604 | * encoded with one byte. The value 256 (two bytes) is used |
| 1605 | * for designing the SSL/TLS record layer. According with the |
| 1606 | * rfc6101, the expected message (other than 256) are: |
| 1607 | * - change_cipher_spec(20) |
| 1608 | * - alert(21) |
| 1609 | * - handshake(22) |
| 1610 | * - application_data(23) |
| 1611 | * - (255) |
| 1612 | * We are interessed by the handshake and specially the client |
| 1613 | * hello. |
| 1614 | */ |
| 1615 | if (content_type != 22) |
| 1616 | return; |
| 1617 | |
| 1618 | /* The message length is at least 4 bytes, containing the |
| 1619 | * message type and the message length. |
| 1620 | */ |
| 1621 | if (len < 4) |
| 1622 | return; |
| 1623 | |
| 1624 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1625 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1626 | * - hello_request(0) |
| 1627 | * - client_hello(1) |
| 1628 | * - server_hello(2) |
| 1629 | * - certificate(11) |
| 1630 | * - server_key_exchange (12) |
| 1631 | * - certificate_request(13) |
| 1632 | * - server_hello_done(14) |
| 1633 | * We are interested by the client hello. |
| 1634 | */ |
| 1635 | msg = (unsigned char *)buf; |
| 1636 | if (msg[0] != 1) |
| 1637 | return; |
| 1638 | |
| 1639 | /* Next three bytes are the length of the message. The total length |
| 1640 | * must be this decoded length + 4. If the length given as argument |
| 1641 | * is not the same, we abort the protocol dissector. |
| 1642 | */ |
| 1643 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1644 | if (len < rec_len + 4) |
| 1645 | return; |
| 1646 | msg += 4; |
| 1647 | end = msg + rec_len; |
| 1648 | if (end < msg) |
| 1649 | return; |
| 1650 | |
| 1651 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1652 | * for minor, the random, composed by 4 bytes for the unix time and |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1653 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1654 | */ |
| 1655 | msg += 1 + 1 + 4 + 28; |
| 1656 | if (msg > end) |
| 1657 | return; |
| 1658 | |
| 1659 | /* Next, is session id: |
| 1660 | * if present, we have to jump by length + 1 for the size information |
| 1661 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1662 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1663 | if (msg[0] > 0) |
| 1664 | msg += msg[0]; |
| 1665 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1666 | if (msg > end) |
| 1667 | return; |
| 1668 | |
| 1669 | /* Next two bytes are the ciphersuite length. */ |
| 1670 | if (msg + 2 > end) |
| 1671 | return; |
| 1672 | rec_len = (msg[0] << 8) + msg[1]; |
| 1673 | msg += 2; |
| 1674 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1675 | return; |
| 1676 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1677 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1678 | if (!capture) |
| 1679 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1680 | /* Compute the xxh64 of the ciphersuite. */ |
| 1681 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1682 | |
| 1683 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1684 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1685 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1686 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1687 | |
| 1688 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1689 | } |
| 1690 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1691 | /* Callback is called for ssl protocol analyse */ |
| 1692 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1693 | { |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1694 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1695 | struct ssl_sock_msg_callback *cbk; |
| 1696 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1697 | /* Try to call all callback functions that were registered by using |
| 1698 | * ssl_sock_register_msg_callback(). |
| 1699 | */ |
| 1700 | list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) { |
| 1701 | cbk->func(conn, write_p, version, content_type, buf, len, ssl); |
| 1702 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1703 | } |
| 1704 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1705 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1706 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1707 | const unsigned char *in, unsigned int inlen, |
| 1708 | void *arg) |
| 1709 | { |
| 1710 | struct server *srv = arg; |
| 1711 | |
| 1712 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1713 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1714 | return SSL_TLSEXT_ERR_OK; |
| 1715 | return SSL_TLSEXT_ERR_NOACK; |
| 1716 | } |
| 1717 | #endif |
| 1718 | |
| 1719 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1720 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1721 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1722 | */ |
| 1723 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1724 | unsigned int *len, void *arg) |
| 1725 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1726 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1727 | |
| 1728 | *data = (const unsigned char *)conf->npn_str; |
| 1729 | *len = conf->npn_len; |
| 1730 | return SSL_TLSEXT_ERR_OK; |
| 1731 | } |
| 1732 | #endif |
| 1733 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1734 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1735 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1736 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1737 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1738 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1739 | unsigned char *outlen, |
| 1740 | const unsigned char *server, |
| 1741 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1742 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1743 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1744 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1745 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1746 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1747 | return SSL_TLSEXT_ERR_NOACK; |
| 1748 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1749 | return SSL_TLSEXT_ERR_OK; |
| 1750 | } |
| 1751 | #endif |
| 1752 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1753 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1754 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1755 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1756 | /* Create a X509 certificate with the specified servername and serial. This |
| 1757 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1758 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1759 | ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1760 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1761 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1762 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1763 | SSL_CTX *ssl_ctx = NULL; |
| 1764 | X509 *newcrt = NULL; |
| 1765 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1766 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1767 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1768 | X509_NAME *name; |
| 1769 | const EVP_MD *digest; |
| 1770 | X509V3_CTX ctx; |
| 1771 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1772 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1773 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1774 | /* Get the private key of the default certificate and use it */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1775 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L) |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1776 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1777 | #else |
| 1778 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1779 | if (tmp_ssl) |
| 1780 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1781 | #endif |
| 1782 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1783 | goto mkcert_error; |
| 1784 | |
| 1785 | /* Create the certificate */ |
| 1786 | if (!(newcrt = X509_new())) |
| 1787 | goto mkcert_error; |
| 1788 | |
| 1789 | /* Set version number for the certificate (X509v3) and the serial |
| 1790 | * number */ |
| 1791 | if (X509_set_version(newcrt, 2L) != 1) |
| 1792 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 1793 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1794 | |
| 1795 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1796 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1797 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1798 | goto mkcert_error; |
| 1799 | |
| 1800 | /* set public key in the certificate */ |
| 1801 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1802 | goto mkcert_error; |
| 1803 | |
| 1804 | /* Set issuer name from the CA */ |
| 1805 | if (!(name = X509_get_subject_name(cacert))) |
| 1806 | goto mkcert_error; |
| 1807 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1808 | goto mkcert_error; |
| 1809 | |
| 1810 | /* Set the subject name using the same, but the CN */ |
| 1811 | name = X509_NAME_dup(name); |
| 1812 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1813 | (const unsigned char *)servername, |
| 1814 | -1, -1, 0) != 1) { |
| 1815 | X509_NAME_free(name); |
| 1816 | goto mkcert_error; |
| 1817 | } |
| 1818 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1819 | X509_NAME_free(name); |
| 1820 | goto mkcert_error; |
| 1821 | } |
| 1822 | X509_NAME_free(name); |
| 1823 | |
| 1824 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1825 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1826 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1827 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1828 | X509_EXTENSION *ext; |
| 1829 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1830 | if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i]))) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1831 | goto mkcert_error; |
| 1832 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1833 | X509_EXTENSION_free(ext); |
| 1834 | goto mkcert_error; |
| 1835 | } |
| 1836 | X509_EXTENSION_free(ext); |
| 1837 | } |
| 1838 | |
| 1839 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1840 | |
| 1841 | key_type = EVP_PKEY_base_id(capkey); |
| 1842 | |
| 1843 | if (key_type == EVP_PKEY_DSA) |
| 1844 | digest = EVP_sha1(); |
| 1845 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1846 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1847 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1848 | digest = EVP_sha256(); |
| 1849 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 1850 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1851 | int nid; |
| 1852 | |
| 1853 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1854 | goto mkcert_error; |
| 1855 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1856 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1857 | #else |
| 1858 | goto mkcert_error; |
| 1859 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1860 | } |
| 1861 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1862 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1863 | goto mkcert_error; |
| 1864 | |
| 1865 | /* Create and set the new SSL_CTX */ |
| 1866 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1867 | goto mkcert_error; |
| 1868 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1869 | goto mkcert_error; |
| 1870 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1871 | goto mkcert_error; |
| 1872 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1873 | goto mkcert_error; |
| 1874 | |
| 1875 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1876 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1877 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1878 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1879 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1880 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1881 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1882 | const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE); |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1883 | EC_KEY *ecc; |
| 1884 | int nid; |
| 1885 | |
| 1886 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1887 | goto end; |
| 1888 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1889 | goto end; |
| 1890 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1891 | EC_KEY_free(ecc); |
| 1892 | } |
| 1893 | #endif |
| 1894 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1895 | return ssl_ctx; |
| 1896 | |
| 1897 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1898 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1899 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1900 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1901 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1902 | return NULL; |
| 1903 | } |
| 1904 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1905 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1906 | ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1907 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1908 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1909 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1910 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1911 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1912 | } |
| 1913 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1914 | /* Do a lookup for a certificate in the LRU cache used to store generated |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1915 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1916 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1917 | ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1918 | { |
| 1919 | struct lru64 *lru = NULL; |
| 1920 | |
| 1921 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1922 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1923 | lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1924 | if (lru && lru->domain) { |
| 1925 | if (ssl) |
| 1926 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1927 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1928 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1929 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1930 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1931 | } |
| 1932 | return NULL; |
| 1933 | } |
| 1934 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1935 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 1936 | * function is not thread-safe, it should only be used to check if a certificate |
| 1937 | * exists in the lru cache (with no warranty it will not be removed by another |
| 1938 | * thread). It is kept for backward compatibility. */ |
| 1939 | SSL_CTX * |
| 1940 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 1941 | { |
| 1942 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 1943 | } |
| 1944 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1945 | /* Set a certificate int the LRU cache used to store generated |
| 1946 | * certificate. Return 0 on success, otherwise -1 */ |
| 1947 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1948 | ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1949 | { |
| 1950 | struct lru64 *lru = NULL; |
| 1951 | |
| 1952 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1953 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1954 | lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1955 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1956 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1957 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1958 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1959 | if (lru->domain && lru->data) |
| 1960 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1961 | lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1962 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1963 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1964 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1965 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1966 | } |
| 1967 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1968 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1969 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1970 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1971 | { |
| 1972 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1973 | } |
| 1974 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1975 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1976 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1977 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1978 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1979 | ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1980 | { |
| 1981 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1982 | SSL_CTX *ssl_ctx = NULL; |
| 1983 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1984 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1985 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1986 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1987 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1988 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1989 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1990 | if (lru && lru->domain) |
| 1991 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1992 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1993 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1994 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1995 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1996 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1997 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1998 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1999 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2000 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2001 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2002 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2003 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2004 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2005 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2006 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2007 | return 0; |
| 2008 | } |
| 2009 | static int |
| 2010 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2011 | { |
| 2012 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2013 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2014 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2015 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2016 | key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst)); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2017 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2018 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2019 | } |
| 2020 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2021 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2022 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2023 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2024 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2025 | |
| 2026 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2027 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2028 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2029 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2030 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2031 | #endif |
| 2032 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2033 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2034 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2035 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2036 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2037 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2038 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2039 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2040 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2041 | #endif |
| 2042 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2043 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2044 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2045 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2046 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2047 | #endif |
| 2048 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2049 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2050 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2051 | /* Unusable in this context. */ |
| 2052 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2053 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2054 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2055 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2056 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2057 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2058 | |
| 2059 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2060 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2061 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2062 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2063 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2064 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2065 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2066 | } |
| 2067 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2068 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2069 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2070 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2071 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2072 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2073 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2074 | } |
| 2075 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2076 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2077 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2078 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2079 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2080 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2081 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2082 | } |
| 2083 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2084 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2085 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2086 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2087 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2088 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2089 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2090 | } |
| 2091 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) { |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2092 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2093 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2094 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2095 | #endif |
| 2096 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2097 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2098 | #if SSL_OP_NO_TLSv1_3 |
| 2099 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2100 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2101 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2102 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2103 | #endif |
| 2104 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2105 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2106 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 2107 | struct methodVersions methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2108 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2109 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2110 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2111 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2112 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2113 | {SSL_OP_NO_TLSv1_3, MC_SSL_O_NO_TLSV13, ctx_set_TLSv13_func, ssl_set_TLSv13_func, "TLSv1.3"}, /* CONF_TLSV13 */ |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2114 | }; |
| 2115 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2116 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2117 | { |
| 2118 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2119 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2120 | SSL_set_SSL_CTX(ssl, ctx); |
| 2121 | } |
| 2122 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2123 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2124 | |
| 2125 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2126 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2127 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2128 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2129 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2130 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2131 | return SSL_TLSEXT_ERR_OK; |
| 2132 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2133 | } |
| 2134 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2135 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2136 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2137 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2138 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2139 | #else |
| 2140 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2141 | { |
| 2142 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2143 | struct connection *conn; |
| 2144 | struct bind_conf *s; |
| 2145 | const uint8_t *extension_data; |
| 2146 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2147 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2148 | |
| 2149 | char *wildp = NULL; |
| 2150 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2151 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2152 | struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2153 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2154 | int i; |
| 2155 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2156 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2157 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2158 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2159 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2160 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2161 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2162 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2163 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2164 | #else |
| 2165 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2166 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2167 | /* |
| 2168 | * The server_name extension was given too much extensibility when it |
| 2169 | * was written, so parsing the normal case is a bit complex. |
| 2170 | */ |
| 2171 | size_t len; |
| 2172 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2173 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2174 | /* Extract the length of the supplied list of names. */ |
| 2175 | len = (*extension_data++) << 8; |
| 2176 | len |= *extension_data++; |
| 2177 | if (len + 2 != extension_len) |
| 2178 | goto abort; |
| 2179 | /* |
| 2180 | * The list in practice only has a single element, so we only consider |
| 2181 | * the first one. |
| 2182 | */ |
| 2183 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2184 | goto abort; |
| 2185 | extension_len = len - 1; |
| 2186 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2187 | if (extension_len <= 2) |
| 2188 | goto abort; |
| 2189 | len = (*extension_data++) << 8; |
| 2190 | len |= *extension_data++; |
| 2191 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2192 | || memchr(extension_data, 0, len) != NULL) |
| 2193 | goto abort; |
| 2194 | servername = extension_data; |
| 2195 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2196 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2197 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2198 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2199 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2200 | } |
| 2201 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2202 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2203 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2204 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2205 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2206 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2207 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2208 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2209 | goto abort; |
| 2210 | } |
| 2211 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2212 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2213 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2214 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2215 | #else |
| 2216 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2217 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2218 | uint8_t sign; |
| 2219 | size_t len; |
| 2220 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2221 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2222 | len = (*extension_data++) << 8; |
| 2223 | len |= *extension_data++; |
| 2224 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2225 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2226 | if (len % 2 != 0) |
| 2227 | goto abort; |
| 2228 | for (; len > 0; len -= 2) { |
| 2229 | extension_data++; /* hash */ |
| 2230 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2231 | switch (sign) { |
| 2232 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2233 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2234 | break; |
| 2235 | case TLSEXT_signature_ecdsa: |
| 2236 | has_ecdsa_sig = 1; |
| 2237 | break; |
| 2238 | default: |
| 2239 | continue; |
| 2240 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2241 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2242 | break; |
| 2243 | } |
| 2244 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2245 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2246 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2247 | } |
| 2248 | if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2249 | const SSL_CIPHER *cipher; |
| 2250 | size_t len; |
| 2251 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2252 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2253 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2254 | len = ctx->cipher_suites_len; |
| 2255 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2256 | #else |
| 2257 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2258 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2259 | if (len % 2 != 0) |
| 2260 | goto abort; |
| 2261 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2262 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2263 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2264 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2265 | #else |
| 2266 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2267 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2268 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2269 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2270 | break; |
| 2271 | } |
| 2272 | } |
| 2273 | } |
| 2274 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2275 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2276 | trash.area[i] = tolower(servername[i]); |
| 2277 | if (!wildp && (trash.area[i] == '.')) |
| 2278 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2279 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2280 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2281 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2282 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2283 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2284 | for (i = 0; i < 2; i++) { |
| 2285 | if (i == 0) /* lookup in full qualified names */ |
| 2286 | node = ebst_lookup(&s->sni_ctx, trash.area); |
| 2287 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
| 2288 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2289 | else |
| 2290 | break; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2291 | for (n = node; n; n = ebmb_next_dup(n)) { |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2292 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2293 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2294 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2295 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2296 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2297 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2298 | break; |
| 2299 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2300 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2301 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2302 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2303 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2304 | if (!node_anonymous) |
| 2305 | node_anonymous = n; |
| 2306 | break; |
| 2307 | } |
| 2308 | } |
| 2309 | } |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2310 | /* select by key_signature priority order */ |
| 2311 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2312 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2313 | : (node_anonymous ? node_anonymous |
| 2314 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2315 | : node_rsa /* no rsa signature case (far far away) */ |
| 2316 | ))); |
| 2317 | if (node) { |
| 2318 | /* switch ctx */ |
| 2319 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2320 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2321 | if (conf) { |
| 2322 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2323 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2324 | if (conf->early_data) |
| 2325 | allow_early = 1; |
| 2326 | } |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2327 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2328 | goto allow_early; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2329 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2330 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2331 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2332 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2333 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2334 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2335 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2336 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2337 | } |
| 2338 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2339 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2340 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2341 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2342 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2343 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2344 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2345 | allow_early: |
| 2346 | #ifdef OPENSSL_IS_BORINGSSL |
| 2347 | if (allow_early) |
| 2348 | SSL_set_early_data_enabled(ssl, 1); |
| 2349 | #else |
| 2350 | if (!allow_early) |
| 2351 | SSL_set_max_early_data(ssl, 0); |
| 2352 | #endif |
| 2353 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2354 | abort: |
| 2355 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2356 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2357 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2358 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2359 | #else |
| 2360 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2361 | return 0; |
| 2362 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2366 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2367 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2368 | * warning when no match is found, which implies the default (first) cert |
| 2369 | * will keep being used. |
| 2370 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2371 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2372 | { |
| 2373 | const char *servername; |
| 2374 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2375 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2376 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2377 | int i; |
| 2378 | (void)al; /* shut gcc stupid warning */ |
| 2379 | |
| 2380 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2381 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2382 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2383 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2384 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2385 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2386 | if (s->strict_sni) |
| 2387 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2388 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2389 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2390 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2391 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2392 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2393 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2394 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2395 | if (!servername[i]) |
| 2396 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2397 | trash.area[i] = tolower(servername[i]); |
| 2398 | if (!wildp && (trash.area[i] == '.')) |
| 2399 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2400 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2401 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2402 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2403 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2404 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2405 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2406 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2407 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2408 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2409 | node = n; |
| 2410 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2411 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2412 | } |
| 2413 | if (!node && wildp) { |
| 2414 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2415 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2416 | /* lookup a not neg filter */ |
| 2417 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2418 | node = n; |
| 2419 | break; |
| 2420 | } |
| 2421 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2422 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2423 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2424 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2425 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2426 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2427 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2428 | return SSL_TLSEXT_ERR_OK; |
| 2429 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2430 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2431 | if (s->strict_sni) { |
| 2432 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2433 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2434 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2435 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2436 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2437 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2441 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2442 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2443 | return SSL_TLSEXT_ERR_OK; |
| 2444 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2445 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2446 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2447 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2448 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2449 | |
| 2450 | static DH * ssl_get_dh_1024(void) |
| 2451 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2452 | static unsigned char dh1024_p[]={ |
| 2453 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2454 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2455 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2456 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2457 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2458 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2459 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2460 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2461 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2462 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2463 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2464 | }; |
| 2465 | static unsigned char dh1024_g[]={ |
| 2466 | 0x02, |
| 2467 | }; |
| 2468 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2469 | BIGNUM *p; |
| 2470 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2471 | DH *dh = DH_new(); |
| 2472 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2473 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2474 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2475 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2476 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2477 | DH_free(dh); |
| 2478 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2479 | } else { |
| 2480 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | return dh; |
| 2484 | } |
| 2485 | |
| 2486 | static DH *ssl_get_dh_2048(void) |
| 2487 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2488 | static unsigned char dh2048_p[]={ |
| 2489 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2490 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2491 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2492 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2493 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2494 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2495 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2496 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2497 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2498 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2499 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2500 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2501 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2502 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2503 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2504 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2505 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2506 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2507 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2508 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2509 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2510 | 0xB7,0x1F,0x77,0xF3, |
| 2511 | }; |
| 2512 | static unsigned char dh2048_g[]={ |
| 2513 | 0x02, |
| 2514 | }; |
| 2515 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2516 | BIGNUM *p; |
| 2517 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2518 | DH *dh = DH_new(); |
| 2519 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2520 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2521 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2522 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2523 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2524 | DH_free(dh); |
| 2525 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2526 | } else { |
| 2527 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2528 | } |
| 2529 | } |
| 2530 | return dh; |
| 2531 | } |
| 2532 | |
| 2533 | static DH *ssl_get_dh_4096(void) |
| 2534 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2535 | static unsigned char dh4096_p[]={ |
| 2536 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2537 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2538 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2539 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2540 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2541 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2542 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2543 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2544 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2545 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2546 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2547 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2548 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2549 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2550 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2551 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2552 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2553 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2554 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2555 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2556 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2557 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2558 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2559 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2560 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2561 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2562 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2563 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2564 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2565 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2566 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2567 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2568 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2569 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2570 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2571 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2572 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2573 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2574 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2575 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2576 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2577 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2578 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2579 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2580 | static unsigned char dh4096_g[]={ |
| 2581 | 0x02, |
| 2582 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2583 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2584 | BIGNUM *p; |
| 2585 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2586 | DH *dh = DH_new(); |
| 2587 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2588 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2589 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2590 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2591 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2592 | DH_free(dh); |
| 2593 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2594 | } else { |
| 2595 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2596 | } |
| 2597 | } |
| 2598 | return dh; |
| 2599 | } |
| 2600 | |
| 2601 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2602 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2603 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2604 | { |
| 2605 | DH *dh = NULL; |
| 2606 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2607 | int type; |
| 2608 | |
| 2609 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2610 | |
| 2611 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2612 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2613 | */ |
| 2614 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2615 | keylen = EVP_PKEY_bits(pkey); |
| 2616 | } |
| 2617 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2618 | if (keylen > global_ssl.default_dh_param) { |
| 2619 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2620 | } |
| 2621 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2622 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2623 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2624 | } |
| 2625 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2626 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2627 | } |
| 2628 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2629 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | return dh; |
| 2633 | } |
| 2634 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2635 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2636 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2637 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2638 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2639 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2640 | if (in == NULL) |
| 2641 | goto end; |
| 2642 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2643 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2644 | goto end; |
| 2645 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2646 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2647 | |
| 2648 | end: |
| 2649 | if (in) |
| 2650 | BIO_free(in); |
| 2651 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2652 | ERR_clear_error(); |
| 2653 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2654 | return dh; |
| 2655 | } |
| 2656 | |
| 2657 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2658 | { |
| 2659 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2660 | |
| 2661 | if (global_dh) { |
| 2662 | return 0; |
| 2663 | } |
| 2664 | |
| 2665 | return -1; |
| 2666 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2667 | #endif |
| 2668 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2669 | /* This function allocates a sni_ctx and adds it to the ckch_inst */ |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2670 | static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst, |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2671 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2672 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2673 | { |
| 2674 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2675 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2676 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2677 | if (*name == '!') { |
| 2678 | neg = 1; |
| 2679 | name++; |
| 2680 | } |
| 2681 | if (*name == '*') { |
| 2682 | wild = 1; |
| 2683 | name++; |
| 2684 | } |
| 2685 | /* !* filter is a nop */ |
| 2686 | if (neg && wild) |
| 2687 | return order; |
| 2688 | if (*name) { |
| 2689 | int j, len; |
| 2690 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2691 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2692 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2693 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2694 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2695 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2696 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2697 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2698 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2699 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2700 | memcpy(sc->name.key, trash.area, len + 1); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2701 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2702 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2703 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2704 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2705 | sc->order = order++; |
| 2706 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2707 | sc->wild = wild; |
| 2708 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 2709 | sc->ckch_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2710 | LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2711 | } |
| 2712 | return order; |
| 2713 | } |
| 2714 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2715 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2716 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 2717 | * This function can't return an error. |
| 2718 | * |
| 2719 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 2720 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 2721 | void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf) |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2722 | { |
| 2723 | |
| 2724 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 2725 | struct ebmb_node *node; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2726 | int def = 0; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2727 | |
| 2728 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 2729 | |
| 2730 | /* ignore if sc0 was already inserted in a tree */ |
| 2731 | if (sc0->name.node.leaf_p) |
| 2732 | continue; |
| 2733 | |
| 2734 | /* Check for duplicates. */ |
| 2735 | if (sc0->wild) |
| 2736 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 2737 | else |
| 2738 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 2739 | |
| 2740 | for (; node; node = ebmb_next_dup(node)) { |
| 2741 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 2742 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 2743 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 2744 | /* it's a duplicate, we should remove and free it */ |
| 2745 | LIST_DEL(&sc0->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2746 | SSL_CTX_free(sc0->ctx); |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2747 | free(sc0); |
| 2748 | sc0 = NULL; |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 2749 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2750 | } |
| 2751 | } |
| 2752 | |
| 2753 | /* if duplicate, ignore the insertion */ |
| 2754 | if (!sc0) |
| 2755 | continue; |
| 2756 | |
| 2757 | if (sc0->wild) |
| 2758 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 2759 | else |
| 2760 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2761 | |
| 2762 | /* replace the default_ctx if required with the first ctx */ |
| 2763 | if (ckch_inst->is_default && !def) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2764 | SSL_CTX_free(bind_conf->default_ctx); |
| 2765 | SSL_CTX_up_ref(sc0->ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2766 | bind_conf->default_ctx = sc0->ctx; |
| 2767 | def = 1; |
| 2768 | } |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2773 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2774 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2775 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2776 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 2777 | /* tree of crtlist (crt-list/directory) */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 2778 | struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2779 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2780 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2781 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2782 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 2783 | * DH parameter available in ckchs nor in global, the default |
| 2784 | * DH parameters are applied on the SSL_CTX. |
| 2785 | * Returns a bitfield containing the flags: |
| 2786 | * ERR_FATAL in any fatal error case |
| 2787 | * ERR_ALERT if a reason of the error is availabine in err |
| 2788 | * ERR_WARN if a warning is available into err |
| 2789 | * The value 0 means there is no error nor warning and |
| 2790 | * the operation succeed. |
| 2791 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2792 | #ifndef OPENSSL_NO_DH |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2793 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, |
| 2794 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2795 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2796 | int ret = 0; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2797 | DH *dh = NULL; |
| 2798 | |
William Lallemand | a8c7374 | 2019-07-31 18:31:34 +0200 | [diff] [blame] | 2799 | if (ckch && ckch->dh) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2800 | dh = ckch->dh; |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2801 | if (!SSL_CTX_set_tmp_dh(ctx, dh)) { |
| 2802 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 2803 | err && *err ? *err : "", path); |
| 2804 | #if defined(SSL_CTX_set_dh_auto) |
| 2805 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2806 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2807 | err && *err ? *err : ""); |
| 2808 | #else |
| 2809 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2810 | err && *err ? *err : ""); |
| 2811 | #endif |
| 2812 | ret |= ERR_WARN; |
| 2813 | goto end; |
| 2814 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2815 | |
| 2816 | if (ssl_dh_ptr_index >= 0) { |
| 2817 | /* store a pointer to the DH params to avoid complaining about |
| 2818 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2819 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2820 | } |
| 2821 | } |
| 2822 | else if (global_dh) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2823 | if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) { |
| 2824 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 2825 | err && *err ? *err : "", path); |
| 2826 | #if defined(SSL_CTX_set_dh_auto) |
| 2827 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2828 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2829 | err && *err ? *err : ""); |
| 2830 | #else |
| 2831 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2832 | err && *err ? *err : ""); |
| 2833 | #endif |
| 2834 | ret |= ERR_WARN; |
| 2835 | goto end; |
| 2836 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2837 | } |
| 2838 | else { |
| 2839 | /* Clear openssl global errors stack */ |
| 2840 | ERR_clear_error(); |
| 2841 | |
| 2842 | if (global_ssl.default_dh_param <= 1024) { |
| 2843 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 2844 | if (local_dh_1024 == NULL) |
| 2845 | local_dh_1024 = ssl_get_dh_1024(); |
| 2846 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2847 | if (local_dh_1024 == NULL) { |
| 2848 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 2849 | err && *err ? *err : "", path); |
| 2850 | ret |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2851 | goto end; |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2852 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2853 | |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2854 | if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) { |
| 2855 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 2856 | err && *err ? *err : "", path); |
| 2857 | #if defined(SSL_CTX_set_dh_auto) |
| 2858 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2859 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2860 | err && *err ? *err : ""); |
| 2861 | #else |
| 2862 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2863 | err && *err ? *err : ""); |
| 2864 | #endif |
| 2865 | ret |= ERR_WARN; |
| 2866 | goto end; |
| 2867 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2868 | } |
| 2869 | else { |
| 2870 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2871 | } |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 2872 | } |
| 2873 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 2874 | end: |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 2875 | ERR_clear_error(); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 2876 | return ret; |
| 2877 | } |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 2878 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2879 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2880 | /* Loads the info in ckch into ctx |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2881 | * Returns a bitfield containing the flags: |
| 2882 | * ERR_FATAL in any fatal error case |
| 2883 | * ERR_ALERT if the reason of the error is available in err |
| 2884 | * ERR_WARN if a warning is available into err |
| 2885 | * The value 0 means there is no error nor warning and |
| 2886 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2887 | */ |
| 2888 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2889 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2890 | int errcode = 0; |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 2891 | STACK_OF(X509) *find_chain = NULL; |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2892 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2893 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2894 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2895 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2896 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2897 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2901 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2902 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2903 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2904 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2905 | } |
| 2906 | |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 2907 | if (ckch->chain) { |
| 2908 | find_chain = ckch->chain; |
| 2909 | } else { |
| 2910 | /* Find Certificate Chain in global */ |
| 2911 | struct issuer_chain *issuer; |
Emmanuel Hocdet | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 2912 | issuer = ssl_get0_issuer_chain(ckch->cert); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 2913 | if (issuer) |
| 2914 | find_chain = issuer->chain; |
| 2915 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 2916 | |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 2917 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2918 | if (find_chain) |
| 2919 | #ifdef SSL_CTX_set1_chain |
| 2920 | if (!SSL_CTX_set1_chain(ctx, find_chain)) { |
| 2921 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2922 | err && *err ? *err : "", path); |
| 2923 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2924 | goto end; |
| 2925 | } |
| 2926 | #else |
| 2927 | { /* legacy compat (< openssl 1.0.2) */ |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 2928 | X509 *ca; |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 2929 | STACK_OF(X509) *chain; |
| 2930 | chain = X509_chain_up_ref(find_chain); |
| 2931 | while ((ca = sk_X509_shift(chain))) |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 2932 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 2933 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 2934 | err && *err ? *err : "", path); |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 2935 | X509_free(ca); |
| 2936 | sk_X509_pop_free(chain, X509_free); |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 2937 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2938 | goto end; |
| 2939 | } |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 2940 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 2941 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2942 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2943 | #ifndef OPENSSL_NO_DH |
| 2944 | /* store a NULL pointer to indicate we have not yet loaded |
| 2945 | a custom DH param file */ |
| 2946 | if (ssl_dh_ptr_index >= 0) { |
| 2947 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 2948 | } |
| 2949 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2950 | errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err); |
| 2951 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2952 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2953 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2954 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2955 | } |
| 2956 | #endif |
| 2957 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 2958 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 2959 | if (sctl_ex_index >= 0 && ckch->sctl) { |
| 2960 | if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) { |
| 2961 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 2962 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2963 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2964 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 2965 | } |
| 2966 | } |
| 2967 | #endif |
| 2968 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 2969 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 2970 | /* Load OCSP Info into context */ |
| 2971 | if (ckch->ocsp_response) { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 2972 | if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 2973 | memprintf(err, "%s '%s.ocsp' is present and activates OCSP but it is impossible to compute the OCSP certificate ID (maybe the issuer could not be found)'.\n", |
| 2974 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2975 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2976 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 2977 | } |
| 2978 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 2979 | #endif |
| 2980 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2981 | end: |
| 2982 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2983 | } |
| 2984 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 2985 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2986 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 2987 | static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2988 | { |
| 2989 | struct sni_keytype *s_kt = NULL; |
| 2990 | struct ebmb_node *node; |
| 2991 | int i; |
| 2992 | |
| 2993 | for (i = 0; i < trash.size; i++) { |
| 2994 | if (!str[i]) |
| 2995 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2996 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2997 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2998 | trash.area[i] = 0; |
| 2999 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3000 | if (!node) { |
| 3001 | /* CN not found in tree */ |
| 3002 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3003 | /* Using memcpy here instead of strncpy. |
| 3004 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3005 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3006 | */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3007 | if (!s_kt) |
| 3008 | return -1; |
| 3009 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3010 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3011 | s_kt->keytypes = 0; |
| 3012 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3013 | } else { |
| 3014 | /* CN found in tree */ |
| 3015 | s_kt = container_of(node, struct sni_keytype, name); |
| 3016 | } |
| 3017 | |
| 3018 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3019 | s_kt->keytypes |= 1<<key_index; |
| 3020 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3021 | return 0; |
| 3022 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3023 | } |
| 3024 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3025 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3026 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3027 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3028 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3029 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3030 | * Take a ckch_store which contains a multi-certificate bundle. |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3031 | * Group these certificates into a set of SSL_CTX* |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3032 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 3033 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3034 | * This will allow the user to explicitly group multiple cert/keys for a single purpose |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3035 | * |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3036 | * Returns a bitfield containing the flags: |
| 3037 | * ERR_FATAL in any fatal error case |
| 3038 | * ERR_ALERT if the reason of the error is available in err |
| 3039 | * ERR_WARN if a warning is available into err |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3040 | * |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3041 | */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 3042 | int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 3043 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3044 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3045 | { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3046 | int i = 0, n = 0; |
| 3047 | struct cert_key_and_chain *certs_and_keys; |
William Lallemand | 4b989f2 | 2019-10-04 18:36:55 +0200 | [diff] [blame] | 3048 | struct eb_root sni_keytypes_map = EB_ROOT; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3049 | struct ebmb_node *node; |
| 3050 | struct ebmb_node *next; |
| 3051 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 3052 | * of keytypes |
| 3053 | */ |
| 3054 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3055 | int errcode = 0; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3056 | X509_NAME *xname = NULL; |
| 3057 | char *str = NULL; |
| 3058 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3059 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 3060 | #endif |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3061 | struct ckch_inst *ckch_inst; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3062 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3063 | *ckchi = NULL; |
| 3064 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3065 | if (!ckchs || !ckchs->ckch || !ckchs->multi) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3066 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3067 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3068 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | ckch_inst = ckch_inst_new(); |
| 3072 | if (!ckch_inst) { |
| 3073 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3074 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3075 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3076 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3077 | } |
| 3078 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3079 | certs_and_keys = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3080 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3081 | /* Process each ckch and update keytypes for each CN/SAN |
| 3082 | * for example, if CN/SAN www.a.com is associated with |
| 3083 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3084 | * www.a.com will have: |
| 3085 | * keyindex = 0 | 1 | 4 = 5 |
| 3086 | */ |
| 3087 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3088 | int ret; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3089 | |
| 3090 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3091 | continue; |
| 3092 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3093 | if (fcount) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3094 | for (i = 0; i < fcount; i++) { |
| 3095 | ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3096 | if (ret < 0) { |
| 3097 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3098 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3099 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3100 | goto end; |
| 3101 | } |
| 3102 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3103 | } else { |
| 3104 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 3105 | * so the line that contains logic is marked via comments |
| 3106 | */ |
| 3107 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 3108 | i = -1; |
| 3109 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3110 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3111 | ASN1_STRING *value; |
| 3112 | value = X509_NAME_ENTRY_get_data(entry); |
| 3113 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3114 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3115 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3116 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3117 | OPENSSL_free(str); |
| 3118 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3119 | if (ret < 0) { |
| 3120 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3121 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3122 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3123 | goto end; |
| 3124 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3125 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3126 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3127 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3128 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3129 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3130 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 3131 | if (names) { |
| 3132 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3133 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3134 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3135 | if (name->type == GEN_DNS) { |
| 3136 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 3137 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3138 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3139 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3140 | OPENSSL_free(str); |
| 3141 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3142 | if (ret < 0) { |
| 3143 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3144 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3145 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3146 | goto end; |
| 3147 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3148 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3149 | } |
| 3150 | } |
| 3151 | } |
| 3152 | } |
| 3153 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3154 | } |
| 3155 | |
| 3156 | /* If no files found, return error */ |
| 3157 | if (eb_is_empty(&sni_keytypes_map)) { |
| 3158 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3159 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3160 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3161 | goto end; |
| 3162 | } |
| 3163 | |
| 3164 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 3165 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 3166 | * and add each CTX to the SNI tree |
| 3167 | * |
| 3168 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3169 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3170 | * combination is denoted by the key in the map. Each key |
| 3171 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 3172 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 3173 | * entry in the array to correspond to the unique combo (key) |
| 3174 | * associated with i. This unique key combo (i) will be associated |
| 3175 | * with combos[i-1] |
| 3176 | */ |
| 3177 | |
| 3178 | node = ebmb_first(&sni_keytypes_map); |
| 3179 | while (node) { |
| 3180 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3181 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3182 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3183 | |
| 3184 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 3185 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 3186 | cur_ctx = key_combos[i-1].ctx; |
| 3187 | |
| 3188 | if (cur_ctx == NULL) { |
| 3189 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3190 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3191 | if (cur_ctx == NULL) { |
| 3192 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3193 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3194 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3195 | goto end; |
| 3196 | } |
| 3197 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3198 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3199 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3200 | if (i & (1<<n)) { |
| 3201 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3202 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3203 | errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err); |
| 3204 | if (errcode & ERR_CODE) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3205 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3206 | } |
| 3207 | } |
| 3208 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3209 | /* Update key_combos */ |
| 3210 | key_combos[i-1].ctx = cur_ctx; |
| 3211 | } |
| 3212 | |
| 3213 | /* Update SNI Tree */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3214 | |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3215 | key_combos[i-1].order = ckch_inst_add_cert_sni(cur_ctx, ckch_inst, bind_conf, ssl_conf, |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3216 | kinfo, str, key_combos[i-1].order); |
| 3217 | if (key_combos[i-1].order < 0) { |
| 3218 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3219 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3220 | goto end; |
| 3221 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3222 | node = ebmb_next(node); |
| 3223 | } |
| 3224 | |
| 3225 | |
| 3226 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3227 | if (!bind_conf->default_ctx) { |
| 3228 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3229 | if (key_combos[i].ctx) { |
| 3230 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3231 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3232 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3233 | SSL_CTX_up_ref(bind_conf->default_ctx); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3234 | break; |
| 3235 | } |
| 3236 | } |
| 3237 | } |
| 3238 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3239 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3240 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3241 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3242 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3243 | end: |
| 3244 | |
| 3245 | if (names) |
| 3246 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3247 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3248 | node = ebmb_first(&sni_keytypes_map); |
| 3249 | while (node) { |
| 3250 | next = ebmb_next(node); |
| 3251 | ebmb_delete(node); |
William Lallemand | 8ed5b96 | 2019-10-04 17:24:39 +0200 | [diff] [blame] | 3252 | free(ebmb_entry(node, struct sni_keytype, name)); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3253 | node = next; |
| 3254 | } |
| 3255 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3256 | /* we need to free the ctx since we incremented the refcount where it's used */ |
| 3257 | for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) { |
| 3258 | if (key_combos[i].ctx) |
| 3259 | SSL_CTX_free(key_combos[i].ctx); |
| 3260 | } |
| 3261 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3262 | if (errcode & ERR_CODE && ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3263 | if (ckch_inst->is_default) { |
| 3264 | SSL_CTX_free(bind_conf->default_ctx); |
| 3265 | bind_conf->default_ctx = NULL; |
| 3266 | } |
| 3267 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3268 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3269 | ckch_inst = NULL; |
William Lallemand | 0c6d12f | 2019-10-04 18:38:51 +0200 | [diff] [blame] | 3270 | } |
| 3271 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3272 | *ckchi = ckch_inst; |
| 3273 | return errcode; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3274 | } |
| 3275 | #else |
| 3276 | /* This is a dummy, that just logs an error and returns error */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 3277 | int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 3278 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3279 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3280 | { |
| 3281 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3282 | err && *err ? *err : "", path, strerror(errno)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3283 | return ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3284 | } |
| 3285 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3286 | #endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3287 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3288 | /* |
| 3289 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3290 | * |
| 3291 | * Returns a bitfield containing the flags: |
| 3292 | * ERR_FATAL in any fatal error case |
| 3293 | * ERR_ALERT if the reason of the error is available in err |
| 3294 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3295 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3296 | int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf, |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3297 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3298 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3299 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3300 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3301 | int order = 0; |
| 3302 | X509_NAME *xname; |
| 3303 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3304 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3305 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3306 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3307 | STACK_OF(GENERAL_NAME) *names; |
| 3308 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3309 | struct cert_key_and_chain *ckch; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3310 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3311 | int errcode = 0; |
| 3312 | |
| 3313 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 3314 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3315 | if (!ckchs || !ckchs->ckch) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3316 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3317 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3318 | ckch = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3319 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3320 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 3321 | if (!ctx) { |
| 3322 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3323 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3324 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3325 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3326 | } |
| 3327 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3328 | errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err); |
| 3329 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3330 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3331 | |
| 3332 | ckch_inst = ckch_inst_new(); |
| 3333 | if (!ckch_inst) { |
| 3334 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3335 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3336 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3337 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3338 | } |
| 3339 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3340 | pkey = X509_get_pubkey(ckch->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3341 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3342 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3343 | switch(EVP_PKEY_base_id(pkey)) { |
| 3344 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3345 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3346 | break; |
| 3347 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3348 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3349 | break; |
| 3350 | case EVP_PKEY_DSA: |
| 3351 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3352 | break; |
| 3353 | } |
| 3354 | EVP_PKEY_free(pkey); |
| 3355 | } |
| 3356 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3357 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3358 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3359 | order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, sni_filter[fcount], order); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3360 | if (order < 0) { |
| 3361 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3362 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3363 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3364 | } |
| 3365 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3366 | } |
| 3367 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3368 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3369 | names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3370 | if (names) { |
| 3371 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3372 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3373 | if (name->type == GEN_DNS) { |
| 3374 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3375 | order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3376 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3377 | if (order < 0) { |
| 3378 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3379 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3380 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3381 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3382 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3383 | } |
| 3384 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3385 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3386 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3387 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3388 | xname = X509_get_subject_name(ckch->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3389 | i = -1; |
| 3390 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3391 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3392 | ASN1_STRING *value; |
| 3393 | |
| 3394 | value = X509_NAME_ENTRY_get_data(entry); |
| 3395 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3396 | order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3397 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3398 | if (order < 0) { |
| 3399 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3400 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3401 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3402 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3403 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3404 | } |
| 3405 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3406 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3407 | * the tree, so it will be discovered and cleaned in time. |
| 3408 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3409 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3410 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3411 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3412 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3413 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3414 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3415 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3416 | } |
| 3417 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3418 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3419 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3420 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3421 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3422 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3423 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3424 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3425 | /* everything succeed, the ckch instance can be used */ |
| 3426 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3427 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3428 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3429 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3430 | SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */ |
| 3431 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3432 | *ckchi = ckch_inst; |
| 3433 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3434 | |
| 3435 | error: |
| 3436 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3437 | if (ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3438 | if (ckch_inst->is_default) |
| 3439 | SSL_CTX_free(ctx); |
| 3440 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3441 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3442 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3443 | } |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3444 | SSL_CTX_free(ctx); |
| 3445 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3446 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3447 | } |
| 3448 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 3449 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3450 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 3451 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3452 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3453 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3454 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3455 | |
| 3456 | /* we found the ckchs in the tree, we can use it directly */ |
| 3457 | if (ckchs->multi) |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3458 | errcode |= ckch_inst_new_load_multi_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, ckch_inst, err); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3459 | else |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3460 | errcode |= ckch_inst_new_load_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, ckch_inst, err); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3461 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3462 | if (errcode & ERR_CODE) |
| 3463 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3464 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3465 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3466 | |
| 3467 | /* succeed, add the instance to the ckch_store's list of instance */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3468 | LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3469 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3470 | } |
| 3471 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3472 | |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 3473 | |
| 3474 | |
| 3475 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3476 | * done once. Zero is returned if the operation fails. No error is returned |
| 3477 | * if the random is said as not implemented, because we expect that openssl |
| 3478 | * will use another method once needed. |
| 3479 | */ |
| 3480 | static int ssl_initialize_random() |
| 3481 | { |
| 3482 | unsigned char random; |
| 3483 | static int random_initialized = 0; |
| 3484 | |
| 3485 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3486 | random_initialized = 1; |
| 3487 | |
| 3488 | return random_initialized; |
| 3489 | } |
| 3490 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3491 | /* Load a crt-list file, this is done in 2 parts: |
| 3492 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 3493 | * - generate the instances by iterating on entries in the crtlist struct |
| 3494 | * |
| 3495 | * Nothing is locked there, this function is used in the configuration parser. |
| 3496 | * |
| 3497 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 3498 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3499 | int ssl_sock_load_cert_list_file(char *file, int dir, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3500 | { |
| 3501 | struct crtlist *crtlist = NULL; |
| 3502 | struct ebmb_node *eb; |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3503 | struct crtlist_entry *entry = NULL; |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3504 | struct bind_conf_list *bind_conf_node = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3505 | int cfgerr = 0; |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3506 | char *end; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3507 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3508 | bind_conf_node = malloc(sizeof(*bind_conf_node)); |
| 3509 | if (!bind_conf_node) { |
| 3510 | memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : ""); |
| 3511 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3512 | goto error; |
| 3513 | } |
| 3514 | bind_conf_node->next = NULL; |
| 3515 | bind_conf_node->bind_conf = bind_conf; |
| 3516 | |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3517 | /* strip trailing slashes, including first one */ |
| 3518 | for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--) |
| 3519 | *end = 0; |
| 3520 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3521 | /* look for an existing crtlist or create one */ |
| 3522 | eb = ebst_lookup(&crtlists_tree, file); |
| 3523 | if (eb) { |
| 3524 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 3525 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3526 | /* load a crt-list OR a directory */ |
| 3527 | if (dir) |
| 3528 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 3529 | else |
| 3530 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 3531 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3532 | if (!(cfgerr & ERR_CODE)) |
| 3533 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 3534 | } |
| 3535 | |
| 3536 | if (cfgerr & ERR_CODE) { |
| 3537 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3538 | goto error; |
| 3539 | } |
| 3540 | |
| 3541 | /* generates ckch instance from the crtlist_entry */ |
| 3542 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3543 | struct ckch_store *store; |
| 3544 | struct ckch_inst *ckch_inst = NULL; |
| 3545 | |
| 3546 | store = entry->node.key; |
| 3547 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 3548 | if (cfgerr & ERR_CODE) { |
| 3549 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 3550 | goto error; |
| 3551 | } |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3552 | LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry); |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 3553 | ckch_inst->crtlist_entry = entry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3554 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3555 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3556 | /* add the bind_conf to the list */ |
| 3557 | bind_conf_node->next = crtlist->bind_conf; |
| 3558 | crtlist->bind_conf = bind_conf_node; |
| 3559 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3560 | return cfgerr; |
| 3561 | error: |
| 3562 | { |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3563 | struct crtlist_entry *lastentry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3564 | struct ckch_inst *inst, *s_inst; |
| 3565 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3566 | lastentry = entry; /* which entry we tried to generate last */ |
| 3567 | if (lastentry) { |
| 3568 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3569 | if (entry == lastentry) /* last entry we tried to generate, no need to go further */ |
| 3570 | break; |
| 3571 | |
| 3572 | list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) { |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3573 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3574 | /* this was not generated for this bind_conf, skip */ |
| 3575 | if (inst->bind_conf != bind_conf) |
| 3576 | continue; |
| 3577 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3578 | /* free the sni_ctx and instance */ |
| 3579 | ckch_inst_free(inst); |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3580 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3581 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3582 | } |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3583 | free(bind_conf_node); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3584 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3585 | return cfgerr; |
| 3586 | } |
| 3587 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3588 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 3589 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 3590 | { |
| 3591 | struct stat buf; |
| 3592 | char fp[MAXPATHLEN+1]; |
| 3593 | int cfgerr = 0; |
| 3594 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3595 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3596 | |
| 3597 | if ((ckchs = ckchs_lookup(path))) { |
| 3598 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3599 | return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3600 | } |
| 3601 | if (stat(path, &buf) == 0) { |
| 3602 | if (S_ISDIR(buf.st_mode) == 0) { |
| 3603 | ckchs = ckchs_load_cert_file(path, 0, err); |
| 3604 | if (!ckchs) |
| 3605 | return ERR_ALERT | ERR_FATAL; |
| 3606 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3607 | return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3608 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3609 | return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3610 | } |
| 3611 | } else { |
| 3612 | /* stat failed, could be a bundle */ |
| 3613 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
| 3614 | /* try to load a bundle if it is permitted */ |
| 3615 | ckchs = ckchs_load_cert_file(path, 1, err); |
| 3616 | if (!ckchs) |
| 3617 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3618 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3619 | } else { |
| 3620 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3621 | err && *err ? *err : "", fp, strerror(errno)); |
| 3622 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | return cfgerr; |
| 3627 | } |
| 3628 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3629 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3630 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3631 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3632 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3633 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3634 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3635 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3636 | SSL_OP_NO_SSLv2 | |
| 3637 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3638 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3639 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3640 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 3641 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3642 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3643 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3644 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3645 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3646 | SSL_MODE_RELEASE_BUFFERS | |
| 3647 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3648 | struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3649 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3650 | int flags = MC_SSL_O_ALL; |
| 3651 | int cfgerr = 0; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 3652 | const int default_min_ver = CONF_TLSV12; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3653 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3654 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3655 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3656 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3657 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3658 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3659 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3660 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3661 | else |
| 3662 | flags = conf_ssl_methods->flags; |
| 3663 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3664 | min = conf_ssl_methods->min; |
| 3665 | max = conf_ssl_methods->max; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 3666 | |
| 3667 | /* default minimum is TLSV12, */ |
| 3668 | if (!min) { |
| 3669 | if (!max || (max >= default_min_ver)) { |
| 3670 | min = default_min_ver; |
| 3671 | } else { |
| 3672 | ha_warning("Proxy '%s': Ambiguous configuration for bind '%s' at [%s:%d]: the ssl-min-ver value is not configured and the ssl-max-ver value is lower than the default ssl-min-ver value (%s). " |
| 3673 | "Setting the ssl-min-ver to %s. Use 'ssl-min-ver' to fix this.\n", |
| 3674 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, methodVersions[default_min_ver].name, methodVersions[max].name); |
| 3675 | min = max; |
| 3676 | } |
| 3677 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3678 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3679 | if (min) |
| 3680 | flags |= (methodVersions[min].flag - 1); |
| 3681 | if (max) |
| 3682 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3683 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3684 | min = max = CONF_TLSV_NONE; |
| 3685 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3686 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3687 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3688 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3689 | if (min) { |
| 3690 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3691 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3692 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3693 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3694 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3695 | hole = 0; |
| 3696 | } |
| 3697 | max = i; |
| 3698 | } |
| 3699 | else { |
| 3700 | min = max = i; |
| 3701 | } |
| 3702 | } |
| 3703 | else { |
| 3704 | if (min) |
| 3705 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3706 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3707 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3708 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3709 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3710 | cfgerr += 1; |
| 3711 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3712 | /* save real min/max in bind_conf */ |
| 3713 | conf_ssl_methods->min = min; |
| 3714 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3715 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3716 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3717 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3718 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3719 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3720 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3721 | else |
| 3722 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3723 | if (flags & methodVersions[i].flag) |
| 3724 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3725 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3726 | /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3727 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3728 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3729 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3730 | |
| 3731 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3732 | options |= SSL_OP_NO_TICKET; |
| 3733 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3734 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 3735 | |
| 3736 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 3737 | options |= SSL_OP_NO_RENEGOTIATION; |
| 3738 | #endif |
| 3739 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3740 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3741 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3742 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3743 | if (global_ssl.async) |
| 3744 | mode |= SSL_MODE_ASYNC; |
| 3745 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3746 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3747 | if (global_ssl.life_time) |
| 3748 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3749 | |
| 3750 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3751 | #ifdef OPENSSL_IS_BORINGSSL |
| 3752 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3753 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Ilya Shipitsin | e9ff899 | 2020-01-19 12:20:14 +0500 | [diff] [blame] | 3754 | #elif defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 3755 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 3756 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3757 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 3758 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3759 | #else |
| 3760 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3761 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3762 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3763 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3764 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3765 | } |
| 3766 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3767 | |
| 3768 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 3769 | { |
| 3770 | if (first == block) { |
| 3771 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3772 | if (first->len > 0) |
| 3773 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | /* return first block from sh_ssl_sess */ |
| 3778 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 3779 | { |
| 3780 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 3781 | |
| 3782 | } |
| 3783 | |
| 3784 | /* store a session into the cache |
| 3785 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 3786 | * data: asn1 encoded session |
| 3787 | * data_len: asn1 encoded session length |
| 3788 | * Returns 1 id session was stored (else 0) |
| 3789 | */ |
| 3790 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 3791 | { |
| 3792 | struct shared_block *first; |
| 3793 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 3794 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3795 | first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr)); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3796 | if (!first) { |
| 3797 | /* Could not retrieve enough free blocks to store that session */ |
| 3798 | return 0; |
| 3799 | } |
| 3800 | |
| 3801 | /* STORE the key in the first elem */ |
| 3802 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3803 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 3804 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3805 | |
| 3806 | /* it returns the already existing node |
| 3807 | or current node if none, never returns null */ |
| 3808 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 3809 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 3810 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 3811 | /* release the reserved row */ |
| 3812 | shctx_row_dec_hot(ssl_shctx, first); |
| 3813 | /* replace the previous session already in the tree */ |
| 3814 | sh_ssl_sess = oldsh_ssl_sess; |
| 3815 | /* ignore the previous session data, only use the header */ |
| 3816 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 3817 | shctx_row_inc_hot(ssl_shctx, first); |
| 3818 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3819 | } |
| 3820 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3821 | if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) { |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3822 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3823 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3824 | } |
| 3825 | |
| 3826 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3827 | |
| 3828 | return 1; |
| 3829 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3830 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3831 | /* SSL callback used when a new session is created while connecting to a server */ |
| 3832 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 3833 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 3834 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3835 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3836 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 3837 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3838 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3839 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 3840 | int len; |
| 3841 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3842 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3843 | len = i2d_SSL_SESSION(sess, NULL); |
| 3844 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 3845 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 3846 | } else { |
| 3847 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3848 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 3849 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 3850 | } |
| 3851 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 3852 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 3853 | &ptr); |
| 3854 | } |
| 3855 | } else { |
| 3856 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3857 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 3858 | } |
| 3859 | |
| 3860 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3861 | } |
| 3862 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3863 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3864 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3865 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3866 | { |
| 3867 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 3868 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 3869 | unsigned char *p; |
| 3870 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3871 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3872 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3873 | |
| 3874 | /* Session id is already stored in to key and session id is known |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 3875 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3876 | * note: SSL_SESSION_set1_id is using |
| 3877 | * a memcpy so we need to use a different pointer |
| 3878 | * than sid_data or sid_ctx_data to avoid valgrind |
| 3879 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3880 | */ |
| 3881 | |
| 3882 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3883 | |
| 3884 | /* copy value in an other buffer */ |
| 3885 | memcpy(encid, sid_data, sid_length); |
| 3886 | |
| 3887 | /* pad with 0 */ |
| 3888 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 3889 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 3890 | |
| 3891 | /* force length to zero to avoid ASN1 encoding */ |
| 3892 | SSL_SESSION_set1_id(sess, encid, 0); |
| 3893 | |
| 3894 | /* force length to zero to avoid ASN1 encoding */ |
| 3895 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3896 | |
| 3897 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 3898 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 3899 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 3900 | goto err; |
| 3901 | |
| 3902 | p = encsess; |
| 3903 | |
| 3904 | /* process ASN1 session encoding before the lock */ |
| 3905 | i2d_SSL_SESSION(sess, &p); |
| 3906 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3907 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3908 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3909 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3910 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3911 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3912 | err: |
| 3913 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3914 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 3915 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3916 | |
| 3917 | return 0; /* do not increment session reference count */ |
| 3918 | } |
| 3919 | |
| 3920 | /* SSL callback used on lookup an existing session cause none found in internal cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3921 | SSL_SESSION *sh_ssl_sess_get_cb(SSL *ssl, __OPENSSL_110_CONST__ unsigned char *key, int key_len, int *do_copy) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3922 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3923 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3924 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 3925 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3926 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3927 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3928 | |
| 3929 | global.shctx_lookups++; |
| 3930 | |
| 3931 | /* allow the session to be freed automatically by openssl */ |
| 3932 | *do_copy = 0; |
| 3933 | |
| 3934 | /* tree key is zeros padded sessionid */ |
| 3935 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3936 | memcpy(tmpkey, key, key_len); |
| 3937 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 3938 | key = tmpkey; |
| 3939 | } |
| 3940 | |
| 3941 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3942 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3943 | |
| 3944 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3945 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 3946 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3947 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3948 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3949 | global.shctx_misses++; |
| 3950 | return NULL; |
| 3951 | } |
| 3952 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3953 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 3954 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3955 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3956 | shctx_row_data_get(ssl_shctx, first, data, sizeof(struct sh_ssl_sess_hdr), first->len-sizeof(struct sh_ssl_sess_hdr)); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3957 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3958 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3959 | |
| 3960 | /* decode ASN1 session */ |
| 3961 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3962 | sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr)); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3963 | /* Reset session id and session id contenxt */ |
| 3964 | if (sess) { |
| 3965 | SSL_SESSION_set1_id(sess, key, key_len); |
| 3966 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 3967 | } |
| 3968 | |
| 3969 | return sess; |
| 3970 | } |
| 3971 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3972 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3973 | /* SSL callback used to signal session is no more used in internal cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3974 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3975 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3976 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3977 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 3978 | unsigned int sid_length; |
| 3979 | const unsigned char *sid_data; |
| 3980 | (void)ctx; |
| 3981 | |
| 3982 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 3983 | /* tree key is zeros padded sessionid */ |
| 3984 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3985 | memcpy(tmpkey, sid_data, sid_length); |
| 3986 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 3987 | sid_data = tmpkey; |
| 3988 | } |
| 3989 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3990 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3991 | |
| 3992 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3993 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 3994 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3995 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3996 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3997 | } |
| 3998 | |
| 3999 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4000 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | /* Set session cache mode to server and disable openssl internal cache. |
| 4004 | * Set shared cache callbacks on an ssl context. |
| 4005 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4006 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4007 | { |
| 4008 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4009 | |
| 4010 | if (!ssl_shctx) { |
| 4011 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4012 | return; |
| 4013 | } |
| 4014 | |
| 4015 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4016 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4017 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4018 | |
| 4019 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4020 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4021 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4022 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4023 | } |
| 4024 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4025 | /* |
| 4026 | * This function applies the SSL configuration on a SSL_CTX |
| 4027 | * It returns an error code and fills the <err> buffer |
| 4028 | */ |
| 4029 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx, char **err) |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4030 | { |
| 4031 | struct proxy *curproxy = bind_conf->frontend; |
| 4032 | int cfgerr = 0; |
| 4033 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4034 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4035 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4036 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4037 | const char *conf_ciphersuites; |
| 4038 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4039 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4040 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4041 | if (ssl_conf) { |
| 4042 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4043 | int i, min, max; |
| 4044 | int flags = MC_SSL_O_ALL; |
| 4045 | |
| 4046 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 4047 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4048 | max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4049 | if (min) |
| 4050 | flags |= (methodVersions[min].flag - 1); |
| 4051 | if (max) |
| 4052 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4053 | min = max = CONF_TLSV_NONE; |
| 4054 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4055 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4056 | if (min) |
| 4057 | max = i; |
| 4058 | else |
| 4059 | min = max = i; |
| 4060 | } |
| 4061 | /* save real min/max */ |
| 4062 | conf_ssl_methods->min = min; |
| 4063 | conf_ssl_methods->max = max; |
| 4064 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4065 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4066 | err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4067 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4068 | } |
| 4069 | } |
| 4070 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4071 | switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) { |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4072 | case SSL_SOCK_VERIFY_NONE: |
| 4073 | verify = SSL_VERIFY_NONE; |
| 4074 | break; |
| 4075 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4076 | verify = SSL_VERIFY_PEER; |
| 4077 | break; |
| 4078 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4079 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4080 | break; |
| 4081 | } |
| 4082 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4083 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4084 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4085 | char *ca_verify_file = (ssl_conf && ssl_conf->ca_verify_file) ? ssl_conf->ca_verify_file : bind_conf->ssl_conf.ca_verify_file; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4086 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4087 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4088 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4089 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4090 | memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n", |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4091 | err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4092 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4093 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4094 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 4095 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 4096 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4097 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4098 | } |
| 4099 | if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4100 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 4101 | SSL_CTX_set_client_CA_list(ctx, SSL_dup_CA_list(ssl_get_client_ca_file(ca_file))); |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4102 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4103 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4104 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4105 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4106 | err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4107 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4108 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4109 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4110 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4111 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4112 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 4113 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4114 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4115 | err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4116 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4117 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4118 | else { |
| 4119 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4120 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4121 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4122 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4123 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4124 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4125 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 4126 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4127 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4128 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4129 | err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4130 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4131 | } |
| 4132 | } |
| 4133 | #endif |
| 4134 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4135 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4136 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4137 | if (conf_ciphers && |
| 4138 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4139 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4140 | err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4141 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4142 | } |
| 4143 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4144 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4145 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4146 | if (conf_ciphersuites && |
| 4147 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4148 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4149 | err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4150 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4151 | } |
| 4152 | #endif |
| 4153 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4154 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4155 | /* If tune.ssl.default-dh-param has not been set, |
| 4156 | neither has ssl-default-dh-file and no static DH |
| 4157 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4158 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4159 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4160 | (ssl_dh_ptr_index == -1 || |
| 4161 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Willy Tarreau | 3ba77d2 | 2020-05-08 09:31:18 +0200 | [diff] [blame] | 4162 | /* default to dh-param 2048 */ |
| 4163 | global_ssl.default_dh_param = 2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4164 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4165 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4166 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4167 | if (local_dh_1024 == NULL) { |
| 4168 | local_dh_1024 = ssl_get_dh_1024(); |
| 4169 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4170 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4171 | if (local_dh_2048 == NULL) { |
| 4172 | local_dh_2048 = ssl_get_dh_2048(); |
| 4173 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4174 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4175 | if (local_dh_4096 == NULL) { |
| 4176 | local_dh_4096 = ssl_get_dh_4096(); |
| 4177 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4178 | } |
| 4179 | } |
| 4180 | } |
| 4181 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4182 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4183 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4184 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4185 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4186 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4187 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4188 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4189 | ssl_conf_cur = NULL; |
| 4190 | if (ssl_conf && ssl_conf->npn_str) |
| 4191 | ssl_conf_cur = ssl_conf; |
| 4192 | else if (bind_conf->ssl_conf.npn_str) |
| 4193 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4194 | if (ssl_conf_cur) |
| 4195 | SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 4196 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4197 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4198 | ssl_conf_cur = NULL; |
| 4199 | if (ssl_conf && ssl_conf->alpn_str) |
| 4200 | ssl_conf_cur = ssl_conf; |
| 4201 | else if (bind_conf->ssl_conf.alpn_str) |
| 4202 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4203 | if (ssl_conf_cur) |
| 4204 | SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 4205 | #endif |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 4206 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4207 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4208 | if (conf_curves) { |
| 4209 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4210 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4211 | err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4212 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4213 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4214 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4215 | } |
| 4216 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4217 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4218 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4219 | int i; |
| 4220 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4221 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4222 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4223 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4224 | NULL); |
| 4225 | |
| 4226 | if (ecdhe == NULL) { |
Eric Salama | 3c8bde8 | 2019-11-20 11:33:40 +0100 | [diff] [blame] | 4227 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4228 | return cfgerr; |
| 4229 | } |
| 4230 | #else |
| 4231 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4232 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4233 | ECDHE_DEFAULT_CURVE); |
| 4234 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4235 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4236 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4237 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4238 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4239 | err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4240 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4241 | } |
| 4242 | else { |
| 4243 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4244 | EC_KEY_free(ecdh); |
| 4245 | } |
| 4246 | } |
| 4247 | #endif |
| 4248 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4249 | return cfgerr; |
| 4250 | } |
| 4251 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4252 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4253 | { |
| 4254 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4255 | size_t prefixlen, suffixlen; |
| 4256 | |
| 4257 | /* Trivial case */ |
| 4258 | if (strcmp(pattern, hostname) == 0) |
| 4259 | return 1; |
| 4260 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4261 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4262 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4263 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4264 | pattern_wildcard = NULL; |
| 4265 | pattern_left_label_end = pattern; |
| 4266 | while (*pattern_left_label_end != '.') { |
| 4267 | switch (*pattern_left_label_end) { |
| 4268 | case 0: |
| 4269 | /* End of label not found */ |
| 4270 | return 0; |
| 4271 | case '*': |
| 4272 | /* If there is more than one wildcards */ |
| 4273 | if (pattern_wildcard) |
| 4274 | return 0; |
| 4275 | pattern_wildcard = pattern_left_label_end; |
| 4276 | break; |
| 4277 | } |
| 4278 | pattern_left_label_end++; |
| 4279 | } |
| 4280 | |
| 4281 | /* If it's not trivial and there is no wildcard, it can't |
| 4282 | * match */ |
| 4283 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4284 | return 0; |
| 4285 | |
| 4286 | /* Make sure all labels match except the leftmost */ |
| 4287 | hostname_left_label_end = strchr(hostname, '.'); |
| 4288 | if (!hostname_left_label_end |
| 4289 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 4290 | return 0; |
| 4291 | |
| 4292 | /* Make sure the leftmost label of the hostname is long enough |
| 4293 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4294 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4295 | return 0; |
| 4296 | |
| 4297 | /* Finally compare the string on either side of the |
| 4298 | * wildcard */ |
| 4299 | prefixlen = pattern_wildcard - pattern; |
| 4300 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4301 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 4302 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4303 | return 0; |
| 4304 | |
| 4305 | return 1; |
| 4306 | } |
| 4307 | |
| 4308 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4309 | { |
| 4310 | SSL *ssl; |
| 4311 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4312 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4313 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4314 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4315 | |
| 4316 | int depth; |
| 4317 | X509 *cert; |
| 4318 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4319 | int i; |
| 4320 | X509_NAME *cert_subject; |
| 4321 | char *str; |
| 4322 | |
| 4323 | if (ok == 0) |
| 4324 | return ok; |
| 4325 | |
| 4326 | ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx()); |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4327 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4328 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4329 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4330 | /* We're checking if the provided hostnames match the desired one. The |
| 4331 | * desired hostname comes from the SNI we presented if any, or if not |
| 4332 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4333 | * directive. If neither is set, we don't care about the name so the |
| 4334 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4335 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4336 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4337 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4338 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4339 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4340 | if (!servername) |
| 4341 | return ok; |
| 4342 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4343 | |
| 4344 | /* We only need to verify the CN on the actual server cert, |
| 4345 | * not the indirect CAs */ |
| 4346 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4347 | if (depth != 0) |
| 4348 | return ok; |
| 4349 | |
| 4350 | /* At this point, the cert is *not* OK unless we can find a |
| 4351 | * hostname match */ |
| 4352 | ok = 0; |
| 4353 | |
| 4354 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4355 | /* It seems like this might happen if verify peer isn't set */ |
| 4356 | if (!cert) |
| 4357 | return ok; |
| 4358 | |
| 4359 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4360 | if (alt_names) { |
| 4361 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4362 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4363 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4364 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4365 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4366 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4367 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4368 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4369 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4370 | OPENSSL_free(str); |
| 4371 | } |
| 4372 | } |
| 4373 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4374 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | cert_subject = X509_get_subject_name(cert); |
| 4378 | i = -1; |
| 4379 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4380 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4381 | ASN1_STRING *value; |
| 4382 | value = X509_NAME_ENTRY_get_data(entry); |
| 4383 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4384 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4385 | OPENSSL_free(str); |
| 4386 | } |
| 4387 | } |
| 4388 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4389 | /* report the mismatch and indicate if SNI was used or not */ |
| 4390 | if (!ok && !conn->err_code) |
| 4391 | conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4392 | return ok; |
| 4393 | } |
| 4394 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4395 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4396 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4397 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4398 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4399 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4400 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4401 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4402 | SSL_OP_NO_SSLv2 | |
| 4403 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4404 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4405 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4406 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4407 | SSL_MODE_RELEASE_BUFFERS | |
| 4408 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4409 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4410 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4411 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4412 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4413 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4414 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4415 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4416 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4417 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4418 | cfgerr++; |
| 4419 | } |
| 4420 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4421 | /* Automatic memory computations need to know we use SSL there */ |
| 4422 | global.ssl_used_backend = 1; |
| 4423 | |
| 4424 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4425 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4426 | if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4427 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 4428 | curproxy->id, srv->id, |
| 4429 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4430 | cfgerr++; |
| 4431 | return cfgerr; |
| 4432 | } |
| 4433 | } |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 4434 | if (srv->use_ssl == 1) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4435 | srv->xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4436 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4437 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4438 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4439 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4440 | proxy_type_str(curproxy), curproxy->id, |
| 4441 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4442 | cfgerr++; |
| 4443 | return cfgerr; |
| 4444 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4445 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4446 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4447 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4448 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4449 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4450 | else |
| 4451 | flags = conf_ssl_methods->flags; |
| 4452 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4453 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4454 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4455 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4456 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4457 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4458 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4459 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4460 | min = max = CONF_TLSV_NONE; |
| 4461 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4462 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4463 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4464 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4465 | if (min) { |
| 4466 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4467 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 4468 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4469 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4470 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4471 | hole = 0; |
| 4472 | } |
| 4473 | max = i; |
| 4474 | } |
| 4475 | else { |
| 4476 | min = max = i; |
| 4477 | } |
| 4478 | } |
| 4479 | else { |
| 4480 | if (min) |
| 4481 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4482 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4483 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4484 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4485 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4486 | cfgerr += 1; |
| 4487 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4488 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4489 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4490 | /* Keep force-xxx implementation as it is in older haproxy. It's a |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 4491 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4492 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4493 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4494 | else |
| 4495 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4496 | if (flags & methodVersions[i].flag) |
| 4497 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4498 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4499 | /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4500 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4501 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4502 | #endif |
| 4503 | |
| 4504 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4505 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4506 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4507 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4508 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4509 | if (global_ssl.async) |
| 4510 | mode |= SSL_MODE_ASYNC; |
| 4511 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4512 | SSL_CTX_set_mode(ctx, mode); |
| 4513 | srv->ssl_ctx.ctx = ctx; |
| 4514 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4515 | if (srv->ssl_ctx.client_crt) { |
| 4516 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4517 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4518 | proxy_type_str(curproxy), curproxy->id, |
| 4519 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4520 | cfgerr++; |
| 4521 | } |
| 4522 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4523 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4524 | proxy_type_str(curproxy), curproxy->id, |
| 4525 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4526 | cfgerr++; |
| 4527 | } |
| 4528 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4529 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4530 | proxy_type_str(curproxy), curproxy->id, |
| 4531 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4532 | cfgerr++; |
| 4533 | } |
| 4534 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4535 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4536 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4537 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4538 | switch (srv->ssl_ctx.verify) { |
| 4539 | case SSL_SOCK_VERIFY_NONE: |
| 4540 | verify = SSL_VERIFY_NONE; |
| 4541 | break; |
| 4542 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4543 | verify = SSL_VERIFY_PEER; |
| 4544 | break; |
| 4545 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4546 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4547 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4548 | (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4549 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4550 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4551 | /* set CAfile to verify */ |
| 4552 | if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) { |
| 4553 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4554 | curproxy->id, srv->id, |
| 4555 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4556 | cfgerr++; |
| 4557 | } |
| 4558 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4559 | else { |
| 4560 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4561 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled by default but no CA file specified. If you're running on a LAN where you're certain to trust the server's certificate, please set an explicit 'verify none' statement on the 'server' line, or use 'ssl-server-verify none' in the global section to disable server-side verifications by default.\n", |
| 4562 | curproxy->id, srv->id, |
| 4563 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4564 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4565 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 4566 | curproxy->id, srv->id, |
| 4567 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4568 | cfgerr++; |
| 4569 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4570 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4571 | if (srv->ssl_ctx.crl_file) { |
| 4572 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4573 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 4574 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4575 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 4576 | curproxy->id, srv->id, |
| 4577 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4578 | cfgerr++; |
| 4579 | } |
| 4580 | else { |
| 4581 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4582 | } |
| 4583 | } |
| 4584 | #endif |
| 4585 | } |
| 4586 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4587 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 4588 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 4589 | SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4590 | if (srv->ssl_ctx.ciphers && |
| 4591 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4592 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4593 | curproxy->id, srv->id, |
| 4594 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4595 | cfgerr++; |
| 4596 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4597 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4598 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4599 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 4600 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4601 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 4602 | curproxy->id, srv->id, |
| 4603 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 4604 | cfgerr++; |
| 4605 | } |
| 4606 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4607 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 4608 | if (srv->ssl_ctx.npn_str) |
| 4609 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 4610 | #endif |
| 4611 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4612 | if (srv->ssl_ctx.alpn_str) |
| 4613 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 4614 | #endif |
| 4615 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4616 | |
| 4617 | return cfgerr; |
| 4618 | } |
| 4619 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4620 | /* Walks down the two trees in bind_conf and prepares all certs. The pointer may |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4621 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4622 | * encountered. |
| 4623 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4624 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4625 | { |
| 4626 | struct ebmb_node *node; |
| 4627 | struct sni_ctx *sni; |
| 4628 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4629 | int errcode = 0; |
| 4630 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4631 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4632 | /* Automatic memory computations need to know we use SSL there */ |
| 4633 | global.ssl_used_frontend = 1; |
| 4634 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4635 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4636 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4637 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4638 | err++; |
| 4639 | } |
| 4640 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4641 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4642 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4643 | /* It should not be necessary to call this function, but it's |
| 4644 | necessary first to check and move all initialisation related |
| 4645 | to initial_ctx in ssl_sock_initial_ctx. */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4646 | errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4647 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4648 | if (bind_conf->default_ctx) |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4649 | errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4650 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4651 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4652 | while (node) { |
| 4653 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4654 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4655 | /* only initialize the CTX on its first occurrence and |
| 4656 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4657 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4658 | node = ebmb_next(node); |
| 4659 | } |
| 4660 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4661 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4662 | while (node) { |
| 4663 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4664 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4665 | /* only initialize the CTX on its first occurrence and |
| 4666 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4667 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
| 4668 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4669 | node = ebmb_next(node); |
| 4670 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4671 | |
| 4672 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 4673 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4674 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 4675 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4676 | err++; |
| 4677 | } |
| 4678 | |
| 4679 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4680 | return err; |
| 4681 | } |
| 4682 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4683 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4684 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4685 | * alerts are directly emitted since the rest of the stack does it below. |
| 4686 | */ |
| 4687 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4688 | { |
| 4689 | struct proxy *px = bind_conf->frontend; |
| 4690 | int alloc_ctx; |
| 4691 | int err; |
| 4692 | |
| 4693 | if (!bind_conf->is_ssl) { |
| 4694 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4695 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4696 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4697 | } |
| 4698 | return 0; |
| 4699 | } |
| 4700 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4701 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4702 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 4703 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4704 | } |
| 4705 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4706 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4707 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4708 | return -1; |
| 4709 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4710 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 4711 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4712 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 4713 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4714 | sizeof(*sh_ssl_sess_tree), |
| 4715 | ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0); |
Frédéric Lécaille | 4c8aa11 | 2018-10-25 20:22:46 +0200 | [diff] [blame] | 4716 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4717 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4718 | ha_alert("Unable to initialize the lock for the shared SSL session cache. You can retry using the global statement 'tune.ssl.force-private-cache' but it could increase CPU usage due to renegotiations if nbproc > 1.\n"); |
| 4719 | else |
| 4720 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 4721 | return -1; |
| 4722 | } |
| 4723 | /* free block callback */ |
| 4724 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 4725 | /* init the root tree within the extra space */ |
| 4726 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 4727 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4728 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4729 | err = 0; |
| 4730 | /* initialize all certificate contexts */ |
| 4731 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4732 | |
| 4733 | /* initialize CA variables if the certificates generation is enabled */ |
| 4734 | err += ssl_sock_load_ca(bind_conf); |
| 4735 | |
| 4736 | return -err; |
| 4737 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4738 | |
| 4739 | /* release ssl context allocated for servers. */ |
| 4740 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4741 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4742 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4743 | if (srv->ssl_ctx.alpn_str) |
| 4744 | free(srv->ssl_ctx.alpn_str); |
| 4745 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 4746 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4747 | if (srv->ssl_ctx.npn_str) |
| 4748 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 4749 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4750 | if (srv->ssl_ctx.ctx) |
| 4751 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4752 | } |
| 4753 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4754 | /* Walks down the two trees in bind_conf and frees all the certs. The pointer may |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4755 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4756 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4757 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4758 | { |
| 4759 | struct ebmb_node *node, *back; |
| 4760 | struct sni_ctx *sni; |
| 4761 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4762 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4763 | while (node) { |
| 4764 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4765 | back = ebmb_next(node); |
| 4766 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4767 | SSL_CTX_free(sni->ctx); |
| 4768 | if (!sni->order) { /* only free the CTX conf on its first occurrence */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4769 | ssl_sock_free_ssl_conf(sni->conf); |
| 4770 | free(sni->conf); |
| 4771 | sni->conf = NULL; |
| 4772 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4773 | free(sni); |
| 4774 | node = back; |
| 4775 | } |
| 4776 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4777 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4778 | while (node) { |
| 4779 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4780 | back = ebmb_next(node); |
| 4781 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4782 | SSL_CTX_free(sni->ctx); |
| 4783 | if (!sni->order) { /* only free the SSL conf its first occurrence */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4784 | ssl_sock_free_ssl_conf(sni->conf); |
| 4785 | free(sni->conf); |
| 4786 | sni->conf = NULL; |
| 4787 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4788 | free(sni); |
| 4789 | node = back; |
| 4790 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4791 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4792 | bind_conf->initial_ctx = NULL; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4793 | SSL_CTX_free(bind_conf->default_ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4794 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4795 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4796 | } |
| 4797 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4798 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4799 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4800 | { |
| 4801 | ssl_sock_free_ca(bind_conf); |
| 4802 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4803 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4804 | free(bind_conf->ca_sign_file); |
| 4805 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 4806 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4807 | free(bind_conf->keys_ref->filename); |
| 4808 | free(bind_conf->keys_ref->tlskeys); |
| 4809 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4810 | free(bind_conf->keys_ref); |
| 4811 | } |
| 4812 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4813 | bind_conf->ca_sign_pass = NULL; |
| 4814 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4815 | } |
| 4816 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4817 | /* Load CA cert file and private key used to generate certificates */ |
| 4818 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4819 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4820 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4821 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4822 | FILE *fp; |
| 4823 | X509 *cacert = NULL; |
| 4824 | EVP_PKEY *capkey = NULL; |
| 4825 | int err = 0; |
| 4826 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 4827 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4828 | return err; |
| 4829 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4830 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4831 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4832 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4833 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4834 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4835 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 4836 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4837 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4838 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4839 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 4840 | "no CA certificate File configured at [%s:%d].\n", |
| 4841 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4842 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4843 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4844 | |
| 4845 | /* read in the CA certificate */ |
| 4846 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4847 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4848 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4849 | goto load_error; |
| 4850 | } |
| 4851 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4852 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4853 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4854 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4855 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4856 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4857 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4858 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 4859 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4860 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4861 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4862 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4863 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4864 | bind_conf->ca_sign_cert = cacert; |
| 4865 | bind_conf->ca_sign_pkey = capkey; |
| 4866 | return err; |
| 4867 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4868 | read_error: |
| 4869 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4870 | if (capkey) EVP_PKEY_free(capkey); |
| 4871 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4872 | load_error: |
| 4873 | bind_conf->generate_certs = 0; |
| 4874 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4875 | return err; |
| 4876 | } |
| 4877 | |
| 4878 | /* Release CA cert and private key used to generate certificated */ |
| 4879 | void |
| 4880 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 4881 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4882 | if (bind_conf->ca_sign_pkey) |
| 4883 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 4884 | if (bind_conf->ca_sign_cert) |
| 4885 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 4886 | bind_conf->ca_sign_pkey = NULL; |
| 4887 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4888 | } |
| 4889 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4890 | /* |
| 4891 | * This function is called if SSL * context is not yet allocated. The function |
| 4892 | * is designed to be called before any other data-layer operation and sets the |
| 4893 | * handshake flag on the connection. It is safe to call it multiple times. |
| 4894 | * It returns 0 on success and -1 in error case. |
| 4895 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 4896 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4897 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4898 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4899 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 4900 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4901 | return 0; |
| 4902 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4903 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4904 | return 0; |
| 4905 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4906 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 4907 | if (!ctx) { |
| 4908 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4909 | return -1; |
| 4910 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 4911 | ctx->wait_event.tasklet = tasklet_new(); |
| 4912 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4913 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4914 | pool_free(ssl_sock_ctx_pool, ctx); |
| 4915 | return -1; |
| 4916 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 4917 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 4918 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4919 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 4920 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 4921 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4922 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 4923 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 4924 | ctx->xprt_st = 0; |
| 4925 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4926 | |
| 4927 | /* Only work with sockets for now, this should be adapted when we'll |
| 4928 | * add QUIC support. |
| 4929 | */ |
| 4930 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 4931 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4932 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 4933 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 4934 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4935 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4936 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 4937 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4938 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4939 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4940 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4941 | /* If it is in client mode initiate SSL session |
| 4942 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4943 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4944 | int may_retry = 1; |
| 4945 | |
| 4946 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4947 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4948 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 4949 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4950 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4951 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4952 | goto retry_connect; |
| 4953 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4954 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4955 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4956 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4957 | ctx->bio = BIO_new(ha_meth); |
| 4958 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 4959 | SSL_free(ctx->ssl); |
| 4960 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4961 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4962 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4963 | goto retry_connect; |
| 4964 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4965 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4966 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4967 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4968 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4969 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4970 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4971 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4972 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 4973 | SSL_free(ctx->ssl); |
| 4974 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4975 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4976 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4977 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4978 | goto retry_connect; |
| 4979 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4980 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4981 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4982 | } |
| 4983 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4984 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4985 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 4986 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 4987 | SSL_SESSION *sess = d2i_SSL_SESSION(NULL, &ptr, __objt_server(conn->target)->ssl_ctx.reused_sess[tid].size); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4988 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4989 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4990 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 4991 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4992 | } else if (sess) { |
| 4993 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4994 | } |
| 4995 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4996 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4997 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4998 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4999 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5000 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5001 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5002 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5003 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5004 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5005 | return 0; |
| 5006 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5007 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5008 | int may_retry = 1; |
| 5009 | |
| 5010 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5011 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5012 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 5013 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5014 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5015 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5016 | goto retry_accept; |
| 5017 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5018 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5019 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5020 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5021 | ctx->bio = BIO_new(ha_meth); |
| 5022 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 5023 | SSL_free(ctx->ssl); |
| 5024 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5025 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5026 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5027 | goto retry_accept; |
| 5028 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5029 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5030 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5031 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5032 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5033 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5034 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5035 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5036 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5037 | SSL_free(ctx->ssl); |
| 5038 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5039 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5040 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5041 | goto retry_accept; |
| 5042 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5043 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5044 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5045 | } |
| 5046 | |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 5047 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5048 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) { |
| 5049 | b_alloc(&ctx->early_buf); |
| 5050 | SSL_set_max_early_data(ctx->ssl, |
| 5051 | /* Only allow early data if we managed to allocate |
| 5052 | * a buffer. |
| 5053 | */ |
| 5054 | (!b_is_null(&ctx->early_buf)) ? |
| 5055 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 5056 | } |
| 5057 | #endif |
| 5058 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5059 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5060 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5061 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5062 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5063 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5064 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 5065 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5066 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5067 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5068 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5069 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5070 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5071 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5072 | return 0; |
| 5073 | } |
| 5074 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5075 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5076 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5077 | if (ctx && ctx->wait_event.tasklet) |
| 5078 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5079 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5080 | return -1; |
| 5081 | } |
| 5082 | |
| 5083 | |
| 5084 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5085 | * updates the FD status if it wants some polling before being called again. |
| 5086 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5087 | * otherwise it returns non-zero and removes itself from the connection's |
| 5088 | * flags (the bit is provided in <flag> by the caller). |
| 5089 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 5090 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5091 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5092 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5093 | int ret; |
| 5094 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5095 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5096 | return 0; |
| 5097 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5098 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5099 | goto out_error; |
| 5100 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5101 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5102 | /* |
| 5103 | * Check if we have early data. If we do, we have to read them |
| 5104 | * before SSL_do_handshake() is called, And there's no way to |
| 5105 | * detect early data, except to try to read them |
| 5106 | */ |
| 5107 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5108 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5109 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5110 | while (1) { |
| 5111 | ret = SSL_read_early_data(ctx->ssl, |
| 5112 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 5113 | &read_data); |
| 5114 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5115 | goto check_error; |
| 5116 | if (read_data > 0) { |
| 5117 | conn->flags |= CO_FL_EARLY_DATA; |
| 5118 | b_add(&ctx->early_buf, read_data); |
| 5119 | } |
| 5120 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5121 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5122 | if (!b_data(&ctx->early_buf)) |
| 5123 | b_free(&ctx->early_buf); |
| 5124 | break; |
| 5125 | } |
| 5126 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5127 | } |
| 5128 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5129 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5130 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5131 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5132 | * the reneg handshake. |
| 5133 | * Here we use SSL_peek as a workaround for reneg. |
| 5134 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5135 | if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5136 | char c; |
| 5137 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5138 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5139 | if (ret <= 0) { |
| 5140 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5141 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5142 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5143 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5144 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5145 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5146 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5147 | return 0; |
| 5148 | } |
| 5149 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5150 | /* handshake may have been completed but we have |
| 5151 | * no more data to read. |
| 5152 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5153 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5154 | ret = 1; |
| 5155 | goto reneg_ok; |
| 5156 | } |
| 5157 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5158 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5159 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5160 | return 0; |
| 5161 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5162 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5163 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5164 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5165 | return 0; |
| 5166 | } |
| 5167 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5168 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5169 | /* if errno is null, then connection was successfully established */ |
| 5170 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5171 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5172 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5173 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5174 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5175 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5176 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5177 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5178 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5179 | /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5180 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5181 | empty_handshake = state == TLS_ST_BEFORE; |
| 5182 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5183 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5184 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5185 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5186 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5187 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5188 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5189 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5190 | else |
| 5191 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5192 | } |
| 5193 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5194 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5195 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5196 | else |
| 5197 | conn->err_code = CO_ER_SSL_ABORT; |
| 5198 | } |
| 5199 | } |
| 5200 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5201 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5202 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5203 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5204 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5205 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5206 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5207 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5208 | goto out_error; |
| 5209 | } |
| 5210 | else { |
| 5211 | /* Fail on all other handshake errors */ |
| 5212 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5213 | * buffer, causing an RST to be emitted upon close() on |
| 5214 | * TCP sockets. We first try to drain possibly pending |
| 5215 | * data to avoid this as much as possible. |
| 5216 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5217 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5218 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5219 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5220 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5221 | goto out_error; |
| 5222 | } |
| 5223 | } |
| 5224 | /* read some data: consider handshake completed */ |
| 5225 | goto reneg_ok; |
| 5226 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5227 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5228 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5229 | if (ret != 1) { |
| 5230 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5231 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5232 | |
| 5233 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5234 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5235 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5236 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5237 | return 0; |
| 5238 | } |
| 5239 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5240 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5241 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5242 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5243 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5244 | return 0; |
| 5245 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5246 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5247 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5248 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5249 | return 0; |
| 5250 | } |
| 5251 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5252 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5253 | /* if errno is null, then connection was successfully established */ |
| 5254 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5255 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5256 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5257 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5258 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5259 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5260 | #else |
| 5261 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5262 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5263 | /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5264 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5265 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5266 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5267 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5268 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5269 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5270 | if (empty_handshake) { |
| 5271 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5272 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5273 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5274 | else |
| 5275 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5276 | } |
| 5277 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5278 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5279 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5280 | else |
| 5281 | conn->err_code = CO_ER_SSL_ABORT; |
| 5282 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5283 | } |
| 5284 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5285 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5286 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5287 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5288 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5289 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5290 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5291 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5292 | goto out_error; |
| 5293 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5294 | else { |
| 5295 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5296 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5297 | * buffer, causing an RST to be emitted upon close() on |
| 5298 | * TCP sockets. We first try to drain possibly pending |
| 5299 | * data to avoid this as much as possible. |
| 5300 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5301 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5302 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5303 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5304 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5305 | goto out_error; |
| 5306 | } |
| 5307 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5308 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5309 | else { |
| 5310 | /* |
| 5311 | * If the server refused the early data, we have to send a |
| 5312 | * 425 to the client, as we no longer have the data to sent |
| 5313 | * them again. |
| 5314 | */ |
| 5315 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5316 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5317 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5318 | goto out_error; |
| 5319 | } |
| 5320 | } |
| 5321 | } |
| 5322 | #endif |
| 5323 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5324 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5325 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5326 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5327 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5328 | /* ASYNC engine API doesn't support moving read/write |
| 5329 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5330 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5331 | */ |
| 5332 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5333 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5334 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5335 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5336 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5337 | if (objt_server(conn->target)) { |
| 5338 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5339 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5340 | global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5341 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5342 | else { |
| 5343 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5344 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5345 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5346 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | /* The connection is now established at both layers, it's time to leave */ |
| 5350 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5351 | return 1; |
| 5352 | |
| 5353 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5354 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5355 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5356 | ERR_clear_error(); |
| 5357 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5358 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5359 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5360 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5361 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5362 | } |
| 5363 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5364 | /* Fail on all other handshake errors */ |
| 5365 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5366 | if (!conn->err_code) |
| 5367 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5368 | return 0; |
| 5369 | } |
| 5370 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5371 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5372 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5373 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5374 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 5375 | * unless the transport layer was already released. |
| 5376 | */ |
| 5377 | static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5378 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5379 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5380 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 5381 | if (!ctx) |
| 5382 | return -1; |
| 5383 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5384 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5385 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5386 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5387 | ctx->subs = es; |
| 5388 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5389 | |
| 5390 | /* we may have to subscribe to lower layers for new events */ |
| 5391 | event_type &= ~ctx->wait_event.events; |
| 5392 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 5393 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5394 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5395 | } |
| 5396 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5397 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5398 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5399 | * subscribe() call. It always returns zero. |
| 5400 | */ |
| 5401 | static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es) |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5402 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5403 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5404 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5405 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5406 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5407 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5408 | es->events &= ~event_type; |
| 5409 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5410 | ctx->subs = NULL; |
| 5411 | |
| 5412 | /* If we subscribed, and we're not doing the handshake, |
| 5413 | * then we subscribed because the upper layer asked for it, |
| 5414 | * as the upper layer is no longer interested, we can |
| 5415 | * unsubscribe too. |
| 5416 | */ |
| 5417 | event_type &= ctx->wait_event.events; |
| 5418 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 5419 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5420 | |
| 5421 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5422 | } |
| 5423 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 5424 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 5425 | * Returns 0 on success, and non-zero on failure. |
| 5426 | */ |
| 5427 | static int ssl_add_xprt(struct connection *conn, void *xprt_ctx, void *toadd_ctx, const struct xprt_ops *toadd_ops, void **oldxprt_ctx, const struct xprt_ops **oldxprt_ops) |
| 5428 | { |
| 5429 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5430 | |
| 5431 | if (oldxprt_ops != NULL) |
| 5432 | *oldxprt_ops = ctx->xprt; |
| 5433 | if (oldxprt_ctx != NULL) |
| 5434 | *oldxprt_ctx = ctx->xprt_ctx; |
| 5435 | ctx->xprt = toadd_ops; |
| 5436 | ctx->xprt_ctx = toadd_ctx; |
| 5437 | return 0; |
| 5438 | } |
| 5439 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 5440 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 5441 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 5442 | * XPRT. |
| 5443 | */ |
| 5444 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 5445 | { |
| 5446 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5447 | |
| 5448 | if (ctx->xprt_ctx == toremove_ctx) { |
| 5449 | ctx->xprt_ctx = newctx; |
| 5450 | ctx->xprt = newops; |
| 5451 | return 0; |
| 5452 | } |
| 5453 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 5454 | } |
| 5455 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5456 | static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
| 5457 | { |
| 5458 | struct ssl_sock_ctx *ctx = context; |
| 5459 | |
| 5460 | /* First if we're doing an handshake, try that */ |
| 5461 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) |
| 5462 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
| 5463 | /* If we had an error, or the handshake is done and I/O is available, |
| 5464 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5465 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5466 | * we can't be sure conn_fd_handler() will be called again. |
| 5467 | */ |
| 5468 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 5469 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 5470 | int ret = 0; |
| 5471 | int woke = 0; |
| 5472 | |
| 5473 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5474 | if (ctx->subs) { |
| 5475 | tasklet_wakeup(ctx->subs->tasklet); |
| 5476 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5477 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5478 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5479 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5480 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5481 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5482 | * upper layers know. If we have no mux, create it, |
| 5483 | * and once we have a mux, call its wake method if we didn't |
| 5484 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5485 | */ |
| 5486 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5487 | if (!ctx->conn->mux) |
| 5488 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5489 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 5490 | ctx->conn->mux->wake(ctx->conn); |
| 5491 | return NULL; |
| 5492 | } |
| 5493 | } |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5494 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5495 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5496 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 5497 | ctx->subs->events & SUB_RETRY_RECV) { |
| 5498 | tasklet_wakeup(ctx->subs->tasklet); |
| 5499 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 5500 | if (!ctx->subs->events) |
| 5501 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5502 | } |
| 5503 | #endif |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5504 | return NULL; |
| 5505 | } |
| 5506 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5507 | /* Receive up to <count> bytes from connection <conn>'s socket and store them |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5508 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5509 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5510 | * flags are updated with whatever special event is detected (error, read0, |
| 5511 | * empty). The caller is responsible for taking care of those events and |
| 5512 | * avoiding the call if inappropriate. The function does not call the |
| 5513 | * connection's polling update function, so the caller is responsible for this. |
| 5514 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5515 | static size_t ssl_sock_to_buf(struct connection *conn, void *xprt_ctx, struct buffer *buf, size_t count, int flags) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5516 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5517 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 5518 | ssize_t ret; |
| 5519 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5520 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5521 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5522 | goto out_error; |
| 5523 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5524 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5525 | if (b_data(&ctx->early_buf)) { |
| 5526 | try = b_contig_space(buf); |
| 5527 | if (try > b_data(&ctx->early_buf)) |
| 5528 | try = b_data(&ctx->early_buf); |
| 5529 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 5530 | b_add(buf, try); |
| 5531 | b_del(&ctx->early_buf, try); |
| 5532 | if (b_data(&ctx->early_buf) == 0) |
| 5533 | b_free(&ctx->early_buf); |
| 5534 | return try; |
| 5535 | } |
| 5536 | #endif |
| 5537 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5538 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5539 | /* a handshake was requested */ |
| 5540 | return 0; |
| 5541 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5542 | /* read the largest possible block. For this, we perform only one call |
| 5543 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5544 | * in which case we accept to do it once again. A new attempt is made on |
| 5545 | * EINTR too. |
| 5546 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5547 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5548 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5549 | try = b_contig_space(buf); |
| 5550 | if (!try) |
| 5551 | break; |
| 5552 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5553 | if (try > count) |
| 5554 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5555 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5556 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5557 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5558 | if (conn->flags & CO_FL_ERROR) { |
| 5559 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5560 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5561 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5562 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5563 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5564 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5565 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5566 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5567 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5568 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5569 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5570 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5571 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5572 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5573 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5574 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5575 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5576 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5577 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5578 | break; |
| 5579 | } |
| 5580 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5581 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5582 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5583 | SUB_RETRY_RECV, |
| 5584 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5585 | /* handshake is running, and it may need to re-enable read */ |
| 5586 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5587 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5588 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5589 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5590 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5591 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5592 | break; |
| 5593 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5594 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5595 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5596 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5597 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5598 | * stack before shutting down the connection for |
| 5599 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5600 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5601 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5602 | /* otherwise it's a real error */ |
| 5603 | goto out_error; |
| 5604 | } |
| 5605 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5606 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5607 | return done; |
| 5608 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5609 | clear_ssl_error: |
| 5610 | /* Clear openssl global errors stack */ |
| 5611 | ssl_sock_dump_errors(conn); |
| 5612 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5613 | read0: |
| 5614 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5615 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5616 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5617 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5618 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5619 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5620 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5621 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5622 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5626 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5627 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5628 | * other pending data for example, but this flag is ignored at the moment. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5629 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5630 | * a second call may be performed. The connection's flags are updated with |
| 5631 | * whatever special event is detected (error, empty). The caller is responsible |
| 5632 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5633 | * function does not call the connection's polling update function, so the caller |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5634 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 5635 | * caller to take care of this. It's up to the caller to update the buffer's |
| 5636 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5637 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5638 | static size_t ssl_sock_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5639 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5640 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5641 | ssize_t ret; |
| 5642 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5643 | |
| 5644 | done = 0; |
| 5645 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5646 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5647 | goto out_error; |
| 5648 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5649 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5650 | /* a handshake was requested */ |
| 5651 | return 0; |
| 5652 | |
| 5653 | /* send the largest possible block. For this we perform only one call |
| 5654 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5655 | * in which case we accept to do it once again. |
| 5656 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5657 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5658 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5659 | size_t written_data; |
| 5660 | #endif |
| 5661 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5662 | try = b_contig_data(buf, done); |
| 5663 | if (try > count) |
| 5664 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5665 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5666 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5667 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5668 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5669 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5670 | } |
| 5671 | else { |
| 5672 | /* we need to keep the information about the fact that |
| 5673 | * we're not limiting the upcoming send(), because if it |
| 5674 | * fails, we'll have to retry with at least as many data. |
| 5675 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5676 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5677 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5678 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5679 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5680 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5681 | unsigned int max_early; |
| 5682 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5683 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5684 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5685 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5686 | if (SSL_get0_session(ctx->ssl)) |
| 5687 | max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl)); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5688 | else |
| 5689 | max_early = 0; |
| 5690 | } |
| 5691 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5692 | if (try + ctx->sent_early_data > max_early) { |
| 5693 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5694 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5695 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5696 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5697 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5698 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5699 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5700 | ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5701 | if (ret == 1) { |
| 5702 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5703 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5704 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5705 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5706 | /* Initiate the handshake, now */ |
| 5707 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 5708 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5709 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5710 | } |
| 5711 | |
| 5712 | } else |
| 5713 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5714 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5715 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5716 | if (conn->flags & CO_FL_ERROR) { |
| 5717 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5718 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5719 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5720 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5721 | /* A send succeeded, so we can consider ourself connected */ |
| 5722 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5723 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5724 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5725 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5726 | } |
| 5727 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5728 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5729 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5730 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5731 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5732 | /* handshake is running, and it may need to re-enable write */ |
| 5733 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5734 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5735 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5736 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5737 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5738 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5739 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5740 | break; |
| 5741 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5742 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5743 | break; |
| 5744 | } |
| 5745 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5746 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5747 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5748 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5749 | SUB_RETRY_RECV, |
| 5750 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5751 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5752 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5753 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5754 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5755 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5756 | break; |
| 5757 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5758 | goto out_error; |
| 5759 | } |
| 5760 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5761 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5762 | return done; |
| 5763 | |
| 5764 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5765 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5766 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5767 | ERR_clear_error(); |
| 5768 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5769 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5770 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5771 | } |
| 5772 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5773 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5774 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5775 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5776 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5777 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5778 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5779 | if (ctx->wait_event.events != 0) |
| 5780 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 5781 | ctx->wait_event.events, |
| 5782 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5783 | if (ctx->subs) { |
| 5784 | ctx->subs->events = 0; |
| 5785 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5786 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5787 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 5788 | if (ctx->xprt->close) |
| 5789 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5790 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5791 | if (global_ssl.async) { |
| 5792 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5793 | size_t num_all_fds = 0; |
| 5794 | int i; |
| 5795 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5796 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5797 | if (num_all_fds > 32) { |
| 5798 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5799 | return; |
| 5800 | } |
| 5801 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5802 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5803 | |
| 5804 | /* If an async job is pending, we must try to |
| 5805 | to catch the end using polling before calling |
| 5806 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5807 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5808 | for (i=0 ; i < num_all_fds ; i++) { |
| 5809 | /* switch on an handler designed to |
| 5810 | * handle the SSL_free |
| 5811 | */ |
| 5812 | afd = all_fd[i]; |
| 5813 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5814 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5815 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5816 | /* To ensure that the fd cache won't be used |
| 5817 | * and we'll catch a real RD event. |
| 5818 | */ |
| 5819 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5820 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5821 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5822 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5823 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5824 | return; |
| 5825 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5826 | /* Else we can remove the fds from the fdtab |
| 5827 | * and call SSL_free. |
| 5828 | * note: we do a fd_remove and not a delete |
| 5829 | * because the fd is owned by the engine. |
| 5830 | * the engine is responsible to close |
| 5831 | */ |
| 5832 | for (i=0 ; i < num_all_fds ; i++) |
| 5833 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5834 | } |
| 5835 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5836 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5837 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5838 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5839 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5840 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5841 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5842 | } |
| 5843 | |
| 5844 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5845 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5846 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5847 | static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5848 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5849 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5850 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5851 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5852 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5853 | if (!clean) |
| 5854 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5855 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5856 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5857 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5858 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5859 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5860 | ERR_clear_error(); |
| 5861 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5862 | } |
| 5863 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5864 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5865 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 5866 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 5867 | { |
| 5868 | struct ssl_sock_ctx *ctx; |
| 5869 | X509 *crt; |
| 5870 | |
| 5871 | if (!ssl_sock_is_ssl(conn)) |
| 5872 | return 0; |
| 5873 | |
| 5874 | ctx = conn->xprt_ctx; |
| 5875 | |
| 5876 | crt = SSL_get_certificate(ctx->ssl); |
| 5877 | if (!crt) |
| 5878 | return 0; |
| 5879 | |
| 5880 | return cert_get_pkey_algo(crt, out); |
| 5881 | } |
| 5882 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5883 | /* used for ppv2 cert signature (can be used for logging) */ |
| 5884 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 5885 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5886 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5887 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5888 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 5889 | X509 *crt; |
| 5890 | |
| 5891 | if (!ssl_sock_is_ssl(conn)) |
| 5892 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5893 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5894 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5895 | if (!crt) |
| 5896 | return NULL; |
| 5897 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 5898 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 5899 | } |
| 5900 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5901 | /* used for ppv2 authority */ |
| 5902 | const char *ssl_sock_get_sni(struct connection *conn) |
| 5903 | { |
| 5904 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5905 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5906 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5907 | if (!ssl_sock_is_ssl(conn)) |
| 5908 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5909 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5910 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5911 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5912 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5913 | #endif |
| 5914 | } |
| 5915 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5916 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5917 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 5918 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5919 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5920 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5921 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5922 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5923 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5924 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5925 | } |
| 5926 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5927 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5928 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 5929 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5930 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5931 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5932 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5933 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5934 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5935 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5936 | } |
| 5937 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 5938 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 5939 | { |
| 5940 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5941 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5942 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 5943 | if (!ssl_sock_is_ssl(conn)) |
| 5944 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5945 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5946 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 5947 | #endif |
| 5948 | } |
| 5949 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5950 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 5951 | * to disable SNI. |
| 5952 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5953 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 5954 | { |
| 5955 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5956 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5957 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5958 | char *prev_name; |
| 5959 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5960 | if (!ssl_sock_is_ssl(conn)) |
| 5961 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5962 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5963 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5964 | /* if the SNI changes, we must destroy the reusable context so that a |
| 5965 | * new connection will present a new SNI. As an optimization we could |
| 5966 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 5967 | * server. |
| 5968 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5969 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5970 | if ((!prev_name && hostname) || |
| 5971 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5972 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5973 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5974 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5975 | #endif |
| 5976 | } |
| 5977 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5978 | /* Extract peer certificate's common name into the chunk dest |
| 5979 | * Returns |
| 5980 | * the len of the extracted common name |
| 5981 | * or 0 if no CN found in DN |
| 5982 | * or -1 on error case (i.e. no peer certificate) |
| 5983 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5984 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 5985 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5986 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5987 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5988 | X509 *crt = NULL; |
| 5989 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5990 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5991 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5992 | .area = (char *)&find_cn, |
| 5993 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5994 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5995 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5996 | |
| 5997 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5998 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5999 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6000 | |
| 6001 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6002 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6003 | if (!crt) |
| 6004 | goto out; |
| 6005 | |
| 6006 | name = X509_get_subject_name(crt); |
| 6007 | if (!name) |
| 6008 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6009 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6010 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6011 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6012 | if (crt) |
| 6013 | X509_free(crt); |
| 6014 | |
| 6015 | return result; |
| 6016 | } |
| 6017 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6018 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6019 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6020 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6021 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6022 | X509 *crt = NULL; |
| 6023 | |
| 6024 | if (!ssl_sock_is_ssl(conn)) |
| 6025 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6026 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6027 | |
| 6028 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6029 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6030 | if (!crt) |
| 6031 | return 0; |
| 6032 | |
| 6033 | X509_free(crt); |
| 6034 | return 1; |
| 6035 | } |
| 6036 | |
| 6037 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6038 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6039 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6040 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6041 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6042 | if (!ssl_sock_is_ssl(conn)) |
| 6043 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6044 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6045 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6046 | } |
| 6047 | |
| 6048 | /* returns result from SSL verify */ |
| 6049 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6050 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6051 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6052 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6053 | if (!ssl_sock_is_ssl(conn)) |
| 6054 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6055 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6056 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6057 | } |
| 6058 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6059 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6060 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6061 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6062 | * freed by the caller. NPN is also checked if available since older versions |
| 6063 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6064 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6065 | static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6066 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6067 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 6068 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6069 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6070 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6071 | return 0; |
| 6072 | |
| 6073 | *str = NULL; |
| 6074 | |
| 6075 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6076 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6077 | if (*str) |
| 6078 | return 1; |
| 6079 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6080 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6081 | SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6082 | if (*str) |
| 6083 | return 1; |
| 6084 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6085 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6086 | return 0; |
| 6087 | } |
| 6088 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6089 | /* "issuers-chain-path" load chain certificate in global */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6090 | int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err) |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6091 | { |
| 6092 | X509 *ca; |
| 6093 | X509_NAME *name = NULL; |
| 6094 | ASN1_OCTET_STRING *skid = NULL; |
| 6095 | STACK_OF(X509) *chain = NULL; |
| 6096 | struct issuer_chain *issuer; |
| 6097 | struct eb64_node *node; |
| 6098 | char *path; |
| 6099 | u64 key; |
| 6100 | int ret = 0; |
| 6101 | |
| 6102 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 6103 | if (chain == NULL) { |
| 6104 | chain = sk_X509_new_null(); |
| 6105 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 6106 | name = X509_get_subject_name(ca); |
| 6107 | } |
| 6108 | if (!sk_X509_push(chain, ca)) { |
| 6109 | X509_free(ca); |
| 6110 | goto end; |
| 6111 | } |
| 6112 | } |
| 6113 | if (!chain) { |
| 6114 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 6115 | goto end; |
| 6116 | } |
| 6117 | if (!skid) { |
| 6118 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 6119 | goto end; |
| 6120 | } |
| 6121 | if (!name) { |
| 6122 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 6123 | goto end; |
| 6124 | } |
| 6125 | key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6126 | for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) { |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6127 | issuer = container_of(node, typeof(*issuer), node); |
| 6128 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 6129 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 6130 | goto end; |
| 6131 | } |
| 6132 | } |
| 6133 | issuer = calloc(1, sizeof *issuer); |
| 6134 | path = strdup(fp); |
| 6135 | if (!issuer || !path) { |
| 6136 | free(issuer); |
| 6137 | free(path); |
| 6138 | goto end; |
| 6139 | } |
| 6140 | issuer->node.key = key; |
| 6141 | issuer->path = path; |
| 6142 | issuer->chain = chain; |
| 6143 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6144 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6145 | ret = 1; |
| 6146 | end: |
| 6147 | if (skid) |
| 6148 | ASN1_OCTET_STRING_free(skid); |
| 6149 | if (chain) |
| 6150 | sk_X509_pop_free(chain, X509_free); |
| 6151 | return ret; |
| 6152 | } |
| 6153 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 6154 | struct issuer_chain* ssl_get0_issuer_chain(X509 *cert) |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6155 | { |
| 6156 | AUTHORITY_KEYID *akid; |
| 6157 | struct issuer_chain *issuer = NULL; |
| 6158 | |
| 6159 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
| 6160 | if (akid) { |
| 6161 | struct eb64_node *node; |
| 6162 | u64 hk; |
| 6163 | hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0); |
| 6164 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 6165 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 6166 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 6167 | issuer = ti; |
| 6168 | break; |
| 6169 | } |
| 6170 | } |
| 6171 | AUTHORITY_KEYID_free(akid); |
| 6172 | } |
| 6173 | return issuer; |
| 6174 | } |
| 6175 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6176 | void ssl_free_global_issuers(void) |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6177 | { |
| 6178 | struct eb64_node *node, *back; |
| 6179 | struct issuer_chain *issuer; |
| 6180 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6181 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6182 | while (node) { |
| 6183 | issuer = container_of(node, typeof(*issuer), node); |
| 6184 | back = eb64_next(node); |
| 6185 | eb64_delete(node); |
| 6186 | free(issuer->path); |
| 6187 | sk_X509_pop_free(issuer->chain, X509_free); |
| 6188 | free(issuer); |
| 6189 | node = back; |
| 6190 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6191 | } |
| 6192 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6193 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6194 | static int ssl_check_async_engine_count(void) { |
| 6195 | int err_code = 0; |
| 6196 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6197 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6198 | ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n"); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6199 | err_code = ERR_ABORT; |
| 6200 | } |
| 6201 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6202 | } |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6203 | #endif |
| 6204 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6205 | /* This function is used with TLS ticket keys management. It permits to browse |
| 6206 | * each reference. The variable <getnext> must contain the current node, |
| 6207 | * <end> point to the root node. |
| 6208 | */ |
| 6209 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6210 | static inline |
| 6211 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 6212 | { |
| 6213 | struct tls_keys_ref *ref = getnext; |
| 6214 | |
| 6215 | while (1) { |
| 6216 | |
| 6217 | /* Get next list entry. */ |
| 6218 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 6219 | |
| 6220 | /* If the entry is the last of the list, return NULL. */ |
| 6221 | if (&ref->list == end) |
| 6222 | return NULL; |
| 6223 | |
| 6224 | return ref; |
| 6225 | } |
| 6226 | } |
| 6227 | |
| 6228 | static inline |
| 6229 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 6230 | { |
| 6231 | int id; |
| 6232 | char *error; |
| 6233 | |
| 6234 | /* If the reference starts by a '#', this is numeric id. */ |
| 6235 | if (reference[0] == '#') { |
| 6236 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 6237 | id = strtol(reference + 1, &error, 10); |
| 6238 | if (*error != '\0') |
| 6239 | return NULL; |
| 6240 | |
| 6241 | /* Perform the unique id lookup. */ |
| 6242 | return tlskeys_ref_lookupid(id); |
| 6243 | } |
| 6244 | |
| 6245 | /* Perform the string lookup. */ |
| 6246 | return tlskeys_ref_lookup(reference); |
| 6247 | } |
| 6248 | #endif |
| 6249 | |
| 6250 | |
| 6251 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6252 | |
| 6253 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 6254 | |
| 6255 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 6256 | return cli_io_handler_tlskeys_files(appctx); |
| 6257 | } |
| 6258 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6259 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 6260 | * (next index to be dumped), and cli.p0 (next key reference). |
| 6261 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6262 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 6263 | |
| 6264 | struct stream_interface *si = appctx->owner; |
| 6265 | |
| 6266 | switch (appctx->st2) { |
| 6267 | case STAT_ST_INIT: |
| 6268 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 6269 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6270 | * later and restart at the state "STAT_ST_INIT". |
| 6271 | */ |
| 6272 | chunk_reset(&trash); |
| 6273 | |
| 6274 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 6275 | chunk_appendf(&trash, "# id secret\n"); |
| 6276 | else |
| 6277 | chunk_appendf(&trash, "# id (file)\n"); |
| 6278 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6279 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6280 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6281 | return 0; |
| 6282 | } |
| 6283 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6284 | /* Now, we start the browsing of the references lists. |
| 6285 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 6286 | * available field of this pointer is <list>. It is used with the function |
| 6287 | * tlskeys_list_get_next() for retruning the first available entry |
| 6288 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6289 | if (appctx->ctx.cli.p0 == NULL) { |
| 6290 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 6291 | appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6292 | } |
| 6293 | |
| 6294 | appctx->st2 = STAT_ST_LIST; |
| 6295 | /* fall through */ |
| 6296 | |
| 6297 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6298 | while (appctx->ctx.cli.p0) { |
| 6299 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6300 | |
| 6301 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6302 | if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6303 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6304 | |
| 6305 | if (appctx->ctx.cli.i1 == 0) |
| 6306 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 6307 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6308 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6309 | int head; |
| 6310 | |
| 6311 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 6312 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6313 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6314 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6315 | |
| 6316 | chunk_reset(t2); |
| 6317 | /* should never fail here because we dump only a key in the t2 buffer */ |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 6318 | if (ref->key_size_bits == 128) { |
| 6319 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 6320 | sizeof(struct tls_sess_key_128), |
| 6321 | t2->area, t2->size); |
| 6322 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 6323 | t2->area); |
| 6324 | } |
| 6325 | else if (ref->key_size_bits == 256) { |
| 6326 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 6327 | sizeof(struct tls_sess_key_256), |
| 6328 | t2->area, t2->size); |
| 6329 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 6330 | t2->area); |
| 6331 | } |
| 6332 | else { |
| 6333 | /* This case should never happen */ |
| 6334 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 6335 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6336 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6337 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6338 | /* let's try again later from this stream. We add ourselves into |
| 6339 | * this stream's users so that it can remove us upon termination. |
| 6340 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6341 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6342 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6343 | return 0; |
| 6344 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6345 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6346 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6347 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6348 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6349 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6350 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6351 | /* let's try again later from this stream. We add ourselves into |
| 6352 | * this stream's users so that it can remove us upon termination. |
| 6353 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6354 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6355 | return 0; |
| 6356 | } |
| 6357 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6358 | if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6359 | break; |
| 6360 | |
| 6361 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6362 | appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6363 | } |
| 6364 | |
| 6365 | appctx->st2 = STAT_ST_FIN; |
| 6366 | /* fall through */ |
| 6367 | |
| 6368 | default: |
| 6369 | appctx->st2 = STAT_ST_FIN; |
| 6370 | return 1; |
| 6371 | } |
| 6372 | return 0; |
| 6373 | } |
| 6374 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6375 | /* sets cli.i0 to non-zero if only file lists should be dumped */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 6376 | static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6377 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6378 | /* no parameter, shows only file list */ |
| 6379 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6380 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6381 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6382 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6383 | } |
| 6384 | |
| 6385 | if (args[2][0] == '*') { |
| 6386 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6387 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6388 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6389 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6390 | if (!appctx->ctx.cli.p0) |
| 6391 | return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6392 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6393 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6394 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6395 | } |
| 6396 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 6397 | static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6398 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6399 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6400 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6401 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6402 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6403 | if (!*args[3] || !*args[4]) |
| 6404 | return cli_err(appctx, "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6405 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6406 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6407 | if (!ref) |
| 6408 | return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6409 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6410 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6411 | if (ret < 0) |
| 6412 | return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n"); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 6413 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6414 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6415 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 6416 | return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6417 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6418 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6419 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6420 | #endif |
William Lallemand | 419e634 | 2020-04-08 12:05:39 +0200 | [diff] [blame] | 6421 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 6422 | static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6423 | { |
| 6424 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 6425 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6426 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 6427 | |
| 6428 | if (!payload) |
| 6429 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6430 | |
| 6431 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6432 | if (!*payload) |
| 6433 | return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n"); |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 6434 | |
| 6435 | /* remove \r and \n from the payload */ |
| 6436 | for (i = 0, j = 0; payload[i]; i++) { |
| 6437 | if (payload[i] == '\r' || payload[i] == '\n') |
| 6438 | continue; |
| 6439 | payload[j++] = payload[i]; |
| 6440 | } |
| 6441 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6442 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6443 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6444 | if (ret < 0) |
| 6445 | return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6446 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6447 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6448 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6449 | if (err) |
| 6450 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 6451 | else |
| 6452 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6453 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6454 | |
| 6455 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6456 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6457 | return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6458 | #endif |
| 6459 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 6460 | } |
| 6461 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6462 | /* register cli keywords */ |
| 6463 | static struct cli_kw_list cli_kws = {{ },{ |
| 6464 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6465 | { { "show", "tls-keys", NULL }, "show tls-keys [id|*]: show tls keys references or dump tls ticket keys when id specified", cli_parse_show_tlskeys, NULL }, |
Lukas Tribus | f4bbc43 | 2017-10-24 12:26:31 +0200 | [diff] [blame] | 6466 | { { "set", "ssl", "tls-key", NULL }, "set ssl tls-key [id|keyfile] <tlskey>: set the next TLS key for the <id> or <keyfile> listener to <tlskey>", cli_parse_set_tlskeys, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6467 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6468 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6469 | { { NULL }, NULL, NULL, NULL } |
| 6470 | }}; |
| 6471 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6472 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6473 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 6474 | /* transport-layer operations for SSL sockets */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6475 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6476 | .snd_buf = ssl_sock_from_buf, |
| 6477 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6478 | .subscribe = ssl_subscribe, |
| 6479 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6480 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6481 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6482 | .rcv_pipe = NULL, |
| 6483 | .snd_pipe = NULL, |
| 6484 | .shutr = NULL, |
| 6485 | .shutw = ssl_sock_shutw, |
| 6486 | .close = ssl_sock_close, |
| 6487 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6488 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6489 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 6490 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 6491 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6492 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 6493 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6494 | }; |
| 6495 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6496 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 6497 | struct session *sess, struct stream *s, int flags) |
| 6498 | { |
| 6499 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6500 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6501 | |
| 6502 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6503 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6504 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6505 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6506 | if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) { |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6507 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6508 | s->req.flags |= CF_READ_NULL; |
| 6509 | return ACT_RET_YIELD; |
| 6510 | } |
| 6511 | } |
| 6512 | return (ACT_RET_CONT); |
| 6513 | } |
| 6514 | |
| 6515 | static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err) |
| 6516 | { |
| 6517 | rule->action_ptr = ssl_action_wait_for_hs; |
| 6518 | |
| 6519 | return ACT_RET_PRS_OK; |
| 6520 | } |
| 6521 | |
| 6522 | static struct action_kw_list http_req_actions = {ILH, { |
| 6523 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 6524 | { /* END */ } |
| 6525 | }}; |
| 6526 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6527 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 6528 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6529 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 6530 | |
| 6531 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6532 | { |
| 6533 | if (ptr) { |
| 6534 | chunk_destroy(ptr); |
| 6535 | free(ptr); |
| 6536 | } |
| 6537 | } |
| 6538 | |
| 6539 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6540 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6541 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6542 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6543 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 6544 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6545 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6546 | static void __ssl_sock_init(void) |
| 6547 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6548 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6549 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6550 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6551 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6552 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6553 | if (global_ssl.listen_default_ciphers) |
| 6554 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 6555 | if (global_ssl.connect_default_ciphers) |
| 6556 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 6557 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6558 | if (global_ssl.listen_default_ciphersuites) |
| 6559 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 6560 | if (global_ssl.connect_default_ciphersuites) |
| 6561 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 6562 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 6563 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 6564 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 6565 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6566 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6567 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6568 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6569 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6570 | n = sk_SSL_COMP_num(cm); |
| 6571 | while (n--) { |
| 6572 | (void) sk_SSL_COMP_pop(cm); |
| 6573 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6574 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6575 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6576 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6577 | ssl_locking_init(); |
| 6578 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6579 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 6580 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 6581 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 6582 | ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
Thierry FOURNIER | 16ff050 | 2018-06-17 21:33:01 +0200 | [diff] [blame] | 6583 | ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6584 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6585 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6586 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6587 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 6588 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6589 | hap_register_post_check(tlskeys_finalize_config); |
| 6590 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6591 | |
| 6592 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 6593 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 6594 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6595 | hap_register_post_deinit(ssl_free_global_issuers); |
| 6596 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6597 | #ifndef OPENSSL_NO_DH |
| 6598 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 6599 | hap_register_post_deinit(ssl_free_dh); |
| 6600 | #endif |
| 6601 | #ifndef OPENSSL_NO_ENGINE |
| 6602 | hap_register_post_deinit(ssl_free_engines); |
| 6603 | #endif |
| 6604 | /* Load SSL string for the verbose & debug mode. */ |
| 6605 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6606 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 6607 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 6608 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 6609 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 6610 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 6611 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 6612 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 6613 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 6614 | |
| 6615 | HA_SPIN_INIT(&ckch_lock); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 6616 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 6617 | /* Try to register dedicated SSL/TLS protocol message callbacks for |
| 6618 | * heartbleed attack (CVE-2014-0160) and clienthello. |
| 6619 | */ |
| 6620 | hap_register_post_check(ssl_sock_register_msg_callbacks); |
| 6621 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 6622 | /* Try to free all callbacks that were registered by using |
| 6623 | * ssl_sock_register_msg_callback(). |
| 6624 | */ |
| 6625 | hap_register_post_deinit(ssl_sock_unregister_msg_callbacks); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6626 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 6627 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6628 | /* Compute and register the version string */ |
| 6629 | static void ssl_register_build_options() |
| 6630 | { |
| 6631 | char *ptr = NULL; |
| 6632 | int i; |
| 6633 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6634 | memprintf(&ptr, "Built with OpenSSL version : " |
| 6635 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 6636 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6637 | #else /* OPENSSL_IS_BORINGSSL */ |
| 6638 | OPENSSL_VERSION_TEXT |
| 6639 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6640 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 6641 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6642 | #endif |
| 6643 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 6644 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6645 | "no (library version too old)" |
| 6646 | #elif defined(OPENSSL_NO_TLSEXT) |
| 6647 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 6648 | #else |
| 6649 | "yes" |
| 6650 | #endif |
| 6651 | "", ptr); |
| 6652 | |
| 6653 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 6654 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 6655 | "yes" |
| 6656 | #else |
| 6657 | #ifdef OPENSSL_NO_TLSEXT |
| 6658 | "no (because of OPENSSL_NO_TLSEXT)" |
| 6659 | #else |
| 6660 | "no (version might be too old, 0.9.8f min needed)" |
| 6661 | #endif |
| 6662 | #endif |
| 6663 | "", ptr); |
| 6664 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 6665 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 6666 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 6667 | if (methodVersions[i].option) |
| 6668 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 6669 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6670 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6671 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6672 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6673 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 6674 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6675 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6676 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6677 | void ssl_free_engines(void) { |
| 6678 | struct ssl_engine_list *wl, *wlb; |
| 6679 | /* free up engine list */ |
| 6680 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 6681 | ENGINE_finish(wl->e); |
| 6682 | ENGINE_free(wl->e); |
| 6683 | LIST_DEL(&wl->list); |
| 6684 | free(wl); |
| 6685 | } |
| 6686 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6687 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6688 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6689 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6690 | void ssl_free_dh(void) { |
| 6691 | if (local_dh_1024) { |
| 6692 | DH_free(local_dh_1024); |
| 6693 | local_dh_1024 = NULL; |
| 6694 | } |
| 6695 | if (local_dh_2048) { |
| 6696 | DH_free(local_dh_2048); |
| 6697 | local_dh_2048 = NULL; |
| 6698 | } |
| 6699 | if (local_dh_4096) { |
| 6700 | DH_free(local_dh_4096); |
| 6701 | local_dh_4096 = NULL; |
| 6702 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 6703 | if (global_dh) { |
| 6704 | DH_free(global_dh); |
| 6705 | global_dh = NULL; |
| 6706 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6707 | } |
| 6708 | #endif |
| 6709 | |
| 6710 | __attribute__((destructor)) |
| 6711 | static void __ssl_sock_deinit(void) |
| 6712 | { |
| 6713 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6714 | if (ssl_ctx_lru_tree) { |
| 6715 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 6716 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6717 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6718 | #endif |
| 6719 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6720 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6721 | ERR_remove_state(0); |
| 6722 | ERR_free_strings(); |
| 6723 | |
| 6724 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6725 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6726 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6727 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6728 | CRYPTO_cleanup_all_ex_data(); |
| 6729 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6730 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6731 | } |
| 6732 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6733 | /* |
| 6734 | * Local variables: |
| 6735 | * c-indent-level: 8 |
| 6736 | * c-basic-offset: 8 |
| 6737 | * End: |
| 6738 | */ |