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 | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 49 | #include <haproxy/connection.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 50 | #include <haproxy/errors.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 51 | #include <haproxy/frontend.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 52 | #include <haproxy/global.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 53 | #include <haproxy/http_rules.h> |
Willy Tarreau | 6019fab | 2020-05-27 16:26:00 +0200 | [diff] [blame] | 54 | #include <haproxy/openssl-compat.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 55 | #include <haproxy/pattern-t.h> |
Willy Tarreau | 334099c | 2020-06-03 18:38:48 +0200 | [diff] [blame] | 56 | #include <haproxy/shctx.h> |
Willy Tarreau | 47d7f90 | 2020-06-04 14:25:47 +0200 | [diff] [blame] | 57 | #include <haproxy/ssl_ckch.h> |
Willy Tarreau | 52d8872 | 2020-06-04 14:29:23 +0200 | [diff] [blame] | 58 | #include <haproxy/ssl_crtlist.h> |
Willy Tarreau | b2bd865 | 2020-06-04 14:21:22 +0200 | [diff] [blame] | 59 | #include <haproxy/ssl_utils.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 60 | #include <haproxy/task.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 61 | #include <haproxy/tools.h> |
Willy Tarreau | c2f7c58 | 2020-06-02 18:15:32 +0200 | [diff] [blame] | 62 | #include <haproxy/ticks.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 63 | #include <haproxy/time.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 64 | #include <haproxy/base64.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 65 | #include <haproxy/vars.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 66 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 67 | #include <import/ebpttree.h> |
| 68 | #include <import/ebsttree.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 69 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 70 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 71 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 72 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 73 | |
Willy Tarreau | aa74c4e | 2020-06-04 10:19:23 +0200 | [diff] [blame] | 74 | #include <haproxy/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 75 | #include <proto/channel.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 76 | #include <proto/cli.h> |
Willy Tarreau | 0f6ffd6 | 2020-06-03 19:33:00 +0200 | [diff] [blame] | 77 | #include <haproxy/fd.h> |
Willy Tarreau | 6634794 | 2020-06-01 12:18:08 +0200 | [diff] [blame] | 78 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | fc77454 | 2020-06-04 17:31:04 +0200 | [diff] [blame] | 79 | #include <haproxy/proto_tcp.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 80 | #include <proto/http_ana.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 81 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 82 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 83 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 84 | #include <proto/proxy.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 85 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 86 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 87 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 88 | /* ***** READ THIS before adding code here! ***** |
| 89 | * |
| 90 | * Due to API incompatibilities between multiple OpenSSL versions and their |
| 91 | * derivatives, it's often tempting to add macros to (re-)define certain |
| 92 | * symbols. Please do not do this here, and do it in common/openssl-compat.h |
| 93 | * exclusively so that the whole code consistently uses the same macros. |
| 94 | * |
| 95 | * Whenever possible if a macro is missing in certain versions, it's better |
| 96 | * to conditionally define it in openssl-compat.h than using lots of ifdefs. |
| 97 | */ |
| 98 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 99 | int sslconns = 0; |
| 100 | int totalsslconns = 0; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 101 | int nb_engines = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 102 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 103 | static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */ |
| 104 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 105 | struct global_ssl global_ssl = { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 106 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 107 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 108 | #endif |
| 109 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 110 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 111 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 112 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 113 | #ifdef LISTEN_DEFAULT_CIPHERSUITES |
| 114 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
| 115 | #endif |
| 116 | #ifdef CONNECT_DEFAULT_CIPHERSUITES |
| 117 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 118 | #endif |
| 119 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 120 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 121 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 122 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 123 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 124 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 125 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 126 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 127 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 128 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 129 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 130 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 131 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 132 | #endif |
| 133 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 134 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 135 | .capture_cipherlist = 0, |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 136 | .extra_files = SSL_GF_ALL, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 139 | static BIO_METHOD *ha_meth; |
| 140 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 141 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx)); |
| 142 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 143 | static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short); |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 144 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 145 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 146 | /* Methods to implement OpenSSL BIO */ |
| 147 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 148 | { |
| 149 | struct buffer tmpbuf; |
| 150 | struct ssl_sock_ctx *ctx; |
| 151 | int ret; |
| 152 | |
| 153 | ctx = BIO_get_data(h); |
| 154 | tmpbuf.size = num; |
| 155 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 156 | tmpbuf.data = num; |
| 157 | tmpbuf.head = 0; |
| 158 | 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] | 159 | 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] | 160 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 161 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 162 | } else if (ret == 0) |
| 163 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 168 | { |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int ha_ssl_puts(BIO *h, const char *str) |
| 174 | { |
| 175 | |
| 176 | return ha_ssl_write(h, str, strlen(str)); |
| 177 | } |
| 178 | |
| 179 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 180 | { |
| 181 | struct buffer tmpbuf; |
| 182 | struct ssl_sock_ctx *ctx; |
| 183 | int ret; |
| 184 | |
| 185 | ctx = BIO_get_data(h); |
| 186 | tmpbuf.size = size; |
| 187 | tmpbuf.area = buf; |
| 188 | tmpbuf.data = 0; |
| 189 | tmpbuf.head = 0; |
| 190 | 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] | 191 | 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] | 192 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 193 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 194 | } else if (ret == 0) |
| 195 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 201 | { |
| 202 | int ret = 0; |
| 203 | switch (cmd) { |
| 204 | case BIO_CTRL_DUP: |
| 205 | case BIO_CTRL_FLUSH: |
| 206 | ret = 1; |
| 207 | break; |
| 208 | } |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static int ha_ssl_new(BIO *h) |
| 213 | { |
| 214 | BIO_set_init(h, 1); |
| 215 | BIO_set_data(h, NULL); |
| 216 | BIO_clear_flags(h, ~0); |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | static int ha_ssl_free(BIO *data) |
| 221 | { |
| 222 | |
| 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 227 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 228 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 229 | static HA_RWLOCK_T *ssl_rwlocks; |
| 230 | |
| 231 | |
| 232 | unsigned long ssl_id_function(void) |
| 233 | { |
| 234 | return (unsigned long)tid; |
| 235 | } |
| 236 | |
| 237 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 238 | { |
| 239 | if (mode & CRYPTO_LOCK) { |
| 240 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 241 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 242 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 243 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 244 | } |
| 245 | else { |
| 246 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 247 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 248 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 249 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | static int ssl_locking_init(void) |
| 254 | { |
| 255 | int i; |
| 256 | |
| 257 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 258 | if (!ssl_rwlocks) |
| 259 | return -1; |
| 260 | |
| 261 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 262 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 263 | |
| 264 | CRYPTO_set_id_callback(ssl_id_function); |
| 265 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 266 | |
| 267 | return 0; |
| 268 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 269 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 270 | #endif |
| 271 | |
Willy Tarreau | af613e8 | 2020-06-05 08:40:51 +0200 | [diff] [blame] | 272 | __decl_thread(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 273 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 274 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 275 | /* |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 276 | * deduplicate cafile (and crlfile) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 277 | */ |
| 278 | struct cafile_entry { |
| 279 | X509_STORE *ca_store; |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 280 | STACK_OF(X509_NAME) *ca_list; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 281 | struct ebmb_node node; |
| 282 | char path[0]; |
| 283 | }; |
| 284 | |
| 285 | static struct eb_root cafile_tree = EB_ROOT_UNIQUE; |
| 286 | |
| 287 | static X509_STORE* ssl_store_get0_locations_file(char *path) |
| 288 | { |
| 289 | struct ebmb_node *eb; |
| 290 | |
| 291 | eb = ebst_lookup(&cafile_tree, path); |
| 292 | if (eb) { |
| 293 | struct cafile_entry *ca_e; |
| 294 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 295 | return ca_e->ca_store; |
| 296 | } |
| 297 | return NULL; |
| 298 | } |
| 299 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 300 | int ssl_store_load_locations_file(char *path) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 301 | { |
| 302 | if (ssl_store_get0_locations_file(path) == NULL) { |
| 303 | struct cafile_entry *ca_e; |
| 304 | X509_STORE *store = X509_STORE_new(); |
| 305 | if (X509_STORE_load_locations(store, path, NULL)) { |
| 306 | int pathlen; |
| 307 | pathlen = strlen(path); |
| 308 | ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1); |
| 309 | if (ca_e) { |
| 310 | memcpy(ca_e->path, path, pathlen + 1); |
| 311 | ca_e->ca_store = store; |
| 312 | ebst_insert(&cafile_tree, &ca_e->node); |
| 313 | return 1; |
| 314 | } |
| 315 | } |
| 316 | X509_STORE_free(store); |
| 317 | return 0; |
| 318 | } |
| 319 | return 1; |
| 320 | } |
| 321 | |
| 322 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 323 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 324 | { |
| 325 | X509_STORE *store; |
| 326 | store = ssl_store_get0_locations_file(path); |
| 327 | if (store_ctx && store) { |
| 328 | int i; |
| 329 | X509_OBJECT *obj; |
| 330 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 331 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 332 | obj = sk_X509_OBJECT_value(objs, i); |
| 333 | switch (X509_OBJECT_get_type(obj)) { |
| 334 | case X509_LU_X509: |
| 335 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 336 | break; |
| 337 | case X509_LU_CRL: |
| 338 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 339 | break; |
| 340 | default: |
| 341 | break; |
| 342 | } |
| 343 | } |
| 344 | return 1; |
| 345 | } |
| 346 | return 0; |
| 347 | } |
| 348 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 349 | /* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */ |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 350 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 351 | { |
| 352 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 353 | return ssl_set_cert_crl_file(store_ctx, path); |
| 354 | } |
| 355 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 356 | /* |
| 357 | Extract CA_list from CA_file already in tree. |
| 358 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 359 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 360 | */ |
| 361 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 362 | { |
| 363 | struct ebmb_node *eb; |
| 364 | struct cafile_entry *ca_e; |
| 365 | |
| 366 | eb = ebst_lookup(&cafile_tree, path); |
| 367 | if (!eb) |
| 368 | return NULL; |
| 369 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 370 | |
| 371 | if (ca_e->ca_list == NULL) { |
| 372 | int i; |
| 373 | unsigned long key; |
| 374 | struct eb_root ca_name_tree = EB_ROOT; |
| 375 | struct eb64_node *node, *back; |
| 376 | struct { |
| 377 | struct eb64_node node; |
| 378 | X509_NAME *xname; |
| 379 | } *ca_name; |
| 380 | STACK_OF(X509_OBJECT) *objs; |
| 381 | STACK_OF(X509_NAME) *skn; |
| 382 | X509 *x; |
| 383 | X509_NAME *xn; |
| 384 | |
| 385 | skn = sk_X509_NAME_new_null(); |
| 386 | /* take x509 from cafile_tree */ |
| 387 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 388 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 389 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 390 | if (!x) |
| 391 | continue; |
| 392 | xn = X509_get_subject_name(x); |
| 393 | if (!xn) |
| 394 | continue; |
| 395 | /* Check for duplicates. */ |
| 396 | key = X509_NAME_hash(xn); |
| 397 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 398 | node && ca_name == NULL; |
| 399 | node = eb64_next(node)) { |
| 400 | ca_name = container_of(node, typeof(*ca_name), node); |
| 401 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 402 | ca_name = NULL; |
| 403 | } |
| 404 | /* find a duplicate */ |
| 405 | if (ca_name) |
| 406 | continue; |
| 407 | ca_name = calloc(1, sizeof *ca_name); |
| 408 | xn = X509_NAME_dup(xn); |
| 409 | if (!ca_name || |
| 410 | !xn || |
| 411 | !sk_X509_NAME_push(skn, xn)) { |
| 412 | free(ca_name); |
| 413 | X509_NAME_free(xn); |
| 414 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 415 | sk_X509_NAME_free(skn); |
| 416 | skn = NULL; |
| 417 | break; |
| 418 | } |
| 419 | ca_name->node.key = key; |
| 420 | ca_name->xname = xn; |
| 421 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 422 | } |
| 423 | ca_e->ca_list = skn; |
| 424 | /* remove temporary ca_name tree */ |
| 425 | node = eb64_first(&ca_name_tree); |
| 426 | while (node) { |
| 427 | ca_name = container_of(node, typeof(*ca_name), node); |
| 428 | back = eb64_next(node); |
| 429 | eb64_delete(node); |
| 430 | free(ca_name); |
| 431 | node = back; |
| 432 | } |
| 433 | } |
| 434 | return ca_e->ca_list; |
| 435 | } |
| 436 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 437 | struct pool_head *pool_head_ssl_capture = NULL; |
William Lallemand | 15e1694 | 2020-05-15 00:25:08 +0200 | [diff] [blame] | 438 | int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 439 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 440 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 441 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 442 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 443 | #endif |
| 444 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 445 | #ifndef OPENSSL_NO_ENGINE |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 446 | unsigned int openssl_engines_initialized; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 447 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 448 | struct ssl_engine_list { |
| 449 | struct list list; |
| 450 | ENGINE *e; |
| 451 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 452 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 453 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 454 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 455 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 456 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 457 | static DH *local_dh_1024 = NULL; |
| 458 | static DH *local_dh_2048 = NULL; |
| 459 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 460 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 461 | #endif /* OPENSSL_NO_DH */ |
| 462 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 463 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 464 | /* X509V3 Extensions that will be added on generated certificates */ |
| 465 | #define X509V3_EXT_SIZE 5 |
| 466 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 467 | "basicConstraints", |
| 468 | "nsComment", |
| 469 | "subjectKeyIdentifier", |
| 470 | "authorityKeyIdentifier", |
| 471 | "keyUsage", |
| 472 | }; |
| 473 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 474 | "CA:FALSE", |
| 475 | "\"OpenSSL Generated Certificate\"", |
| 476 | "hash", |
| 477 | "keyid,issuer:always", |
| 478 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 479 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 480 | /* LRU cache to store generated certificate */ |
| 481 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 482 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 483 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 484 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 485 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 486 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 487 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 488 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 489 | /* The order here matters for picking a default context, |
| 490 | * keep the most common keytype at the bottom of the list |
| 491 | */ |
| 492 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 493 | "dsa", |
| 494 | "ecdsa", |
| 495 | "rsa" |
| 496 | }; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 497 | #endif |
| 498 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 499 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 500 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 501 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 502 | /* Dedicated callback functions for heartbeat and clienthello. |
| 503 | */ |
| 504 | #ifdef TLS1_RT_HEARTBEAT |
| 505 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 506 | int content_type, const void *buf, size_t len, |
| 507 | SSL *ssl); |
| 508 | #endif |
| 509 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 510 | int content_type, const void *buf, size_t len, |
| 511 | SSL *ssl); |
| 512 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 513 | /* List head of all registered SSL/TLS protocol message callbacks. */ |
| 514 | struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks); |
| 515 | |
| 516 | /* Registers the function <func> in order to be called on SSL/TLS protocol |
| 517 | * message processing. It will return 0 if the function <func> is not set |
| 518 | * or if it fails to allocate memory. |
| 519 | */ |
| 520 | int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func) |
| 521 | { |
| 522 | struct ssl_sock_msg_callback *cbk; |
| 523 | |
| 524 | if (!func) |
| 525 | return 0; |
| 526 | |
| 527 | cbk = calloc(1, sizeof(*cbk)); |
| 528 | if (!cbk) { |
| 529 | ha_alert("out of memory in ssl_sock_register_msg_callback().\n"); |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | cbk->func = func; |
| 534 | |
| 535 | LIST_ADDQ(&ssl_sock_msg_callbacks, &cbk->list); |
| 536 | |
| 537 | return 1; |
| 538 | } |
| 539 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 540 | /* Used to register dedicated SSL/TLS protocol message callbacks. |
| 541 | */ |
| 542 | static int ssl_sock_register_msg_callbacks(void) |
| 543 | { |
| 544 | #ifdef TLS1_RT_HEARTBEAT |
| 545 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat)) |
| 546 | return ERR_ABORT; |
| 547 | #endif |
| 548 | if (global_ssl.capture_cipherlist > 0) { |
| 549 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello)) |
| 550 | return ERR_ABORT; |
| 551 | } |
| 552 | return 0; |
| 553 | } |
| 554 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 555 | /* Used to free all SSL/TLS protocol message callbacks that were |
| 556 | * registered by using ssl_sock_register_msg_callback(). |
| 557 | */ |
| 558 | static void ssl_sock_unregister_msg_callbacks(void) |
| 559 | { |
| 560 | struct ssl_sock_msg_callback *cbk, *cbkback; |
| 561 | |
| 562 | list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) { |
| 563 | LIST_DEL(&cbk->list); |
| 564 | free(cbk); |
| 565 | } |
| 566 | } |
| 567 | |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 568 | SSL *ssl_sock_get_ssl_object(struct connection *conn) |
| 569 | { |
| 570 | if (!ssl_sock_is_ssl(conn)) |
| 571 | return NULL; |
| 572 | |
| 573 | return ((struct ssl_sock_ctx *)(conn->xprt_ctx))->ssl; |
| 574 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 575 | /* |
| 576 | * This function gives the detail of the SSL error. It is used only |
| 577 | * if the debug mode and the verbose mode are activated. It dump all |
| 578 | * the SSL error until the stack was empty. |
| 579 | */ |
| 580 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 581 | { |
| 582 | unsigned long ret; |
| 583 | |
| 584 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 585 | while(1) { |
| 586 | ret = ERR_get_error(); |
| 587 | if (ret == 0) |
| 588 | return; |
| 589 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 590 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 591 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 596 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 597 | #ifndef OPENSSL_NO_ENGINE |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 598 | 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] | 599 | { |
| 600 | int err_code = ERR_ABORT; |
| 601 | ENGINE *engine; |
| 602 | struct ssl_engine_list *el; |
| 603 | |
| 604 | /* grab the structural reference to the engine */ |
| 605 | engine = ENGINE_by_id(engine_id); |
| 606 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 607 | 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] | 608 | goto fail_get; |
| 609 | } |
| 610 | |
| 611 | if (!ENGINE_init(engine)) { |
| 612 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 613 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 614 | goto fail_init; |
| 615 | } |
| 616 | |
| 617 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 618 | 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] | 619 | goto fail_set_method; |
| 620 | } |
| 621 | |
| 622 | el = calloc(1, sizeof(*el)); |
| 623 | el->e = engine; |
| 624 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 625 | nb_engines++; |
| 626 | if (global_ssl.async) |
| 627 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 628 | return 0; |
| 629 | |
| 630 | fail_set_method: |
| 631 | /* release the functional reference from ENGINE_init() */ |
| 632 | ENGINE_finish(engine); |
| 633 | |
| 634 | fail_init: |
| 635 | /* release the structural reference from ENGINE_by_id() */ |
| 636 | ENGINE_free(engine); |
| 637 | |
| 638 | fail_get: |
| 639 | return err_code; |
| 640 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 641 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 642 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 643 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 644 | /* |
| 645 | * openssl async fd handler |
| 646 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 647 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 648 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 649 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 650 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 651 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 652 | * to poll this fd until it is requested |
| 653 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 654 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 655 | fd_cant_recv(fd); |
| 656 | |
| 657 | /* crypto engine is available, let's notify the associated |
| 658 | * connection that it can pursue its processing. |
| 659 | */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 660 | ssl_sock_io_cb(NULL, ctx, 0); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 663 | /* |
| 664 | * openssl async delayed SSL_free handler |
| 665 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 666 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 667 | { |
| 668 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 669 | OSSL_ASYNC_FD all_fd[32]; |
| 670 | size_t num_all_fds = 0; |
| 671 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 672 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 673 | /* We suppose that the async job for a same SSL * |
| 674 | * are serialized. So if we are awake it is |
| 675 | * because the running job has just finished |
| 676 | * and we can remove all async fds safely |
| 677 | */ |
| 678 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 679 | if (num_all_fds > 32) { |
| 680 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 685 | for (i=0 ; i < num_all_fds ; i++) |
| 686 | fd_remove(all_fd[i]); |
| 687 | |
| 688 | /* 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] | 689 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 690 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 691 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 692 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 693 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 694 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 695 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 696 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 697 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 698 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 699 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 700 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 701 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 702 | size_t num_add_fds = 0; |
| 703 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 704 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 705 | |
| 706 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 707 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 708 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 709 | 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] | 710 | return; |
| 711 | } |
| 712 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 713 | 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] | 714 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 715 | /* We remove unused fds from the fdtab */ |
| 716 | for (i=0 ; i < num_del_fds ; i++) |
| 717 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 718 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 719 | /* We add new fds to the fdtab */ |
| 720 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 721 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 724 | num_add_fds = 0; |
| 725 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 726 | if (num_add_fds > 32) { |
| 727 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 728 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 729 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 730 | |
| 731 | /* We activate the polling for all known async fds */ |
| 732 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 733 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 734 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 735 | /* To ensure that the fd cache won't be used |
| 736 | * We'll prefer to catch a real RD event |
| 737 | * because handling an EAGAIN on this fd will |
| 738 | * result in a context switch and also |
| 739 | * some engines uses a fd in blocking mode. |
| 740 | */ |
| 741 | fd_cant_recv(add_fd[i]); |
| 742 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 743 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 744 | } |
| 745 | #endif |
| 746 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 747 | #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] | 748 | /* |
| 749 | * This function returns the number of seconds elapsed |
| 750 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 751 | * date presented un ASN1_GENERALIZEDTIME. |
| 752 | * |
| 753 | * In parsing error case, it returns -1. |
| 754 | */ |
| 755 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 756 | { |
| 757 | long epoch; |
| 758 | char *p, *end; |
| 759 | const unsigned short month_offset[12] = { |
| 760 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 761 | }; |
| 762 | int year, month; |
| 763 | |
| 764 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 765 | |
| 766 | p = (char *)d->data; |
| 767 | end = p + d->length; |
| 768 | |
| 769 | if (end - p < 4) return -1; |
| 770 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 771 | p += 4; |
| 772 | if (end - p < 2) return -1; |
| 773 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 774 | if (month < 1 || month > 12) return -1; |
| 775 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 776 | We consider leap years and the current month (<marsh or not) */ |
| 777 | epoch = ( ((year - 1970) * 365) |
| 778 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 779 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 780 | + month_offset[month-1] |
| 781 | ) * 24 * 60 * 60; |
| 782 | p += 2; |
| 783 | if (end - p < 2) return -1; |
| 784 | /* Add the number of seconds of completed days of current month */ |
| 785 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 786 | p += 2; |
| 787 | if (end - p < 2) return -1; |
| 788 | /* Add the completed hours of the current day */ |
| 789 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 790 | p += 2; |
| 791 | if (end - p < 2) return -1; |
| 792 | /* Add the completed minutes of the current hour */ |
| 793 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 794 | p += 2; |
| 795 | if (p == end) return -1; |
| 796 | /* Test if there is available seconds */ |
| 797 | if (p[0] < '0' || p[0] > '9') |
| 798 | goto nosec; |
| 799 | if (end - p < 2) return -1; |
| 800 | /* Add the seconds of the current minute */ |
| 801 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 802 | p += 2; |
| 803 | if (p == end) return -1; |
| 804 | /* Ignore seconds float part if present */ |
| 805 | if (p[0] == '.') { |
| 806 | do { |
| 807 | if (++p == end) return -1; |
| 808 | } while (p[0] >= '0' && p[0] <= '9'); |
| 809 | } |
| 810 | |
| 811 | nosec: |
| 812 | if (p[0] == 'Z') { |
| 813 | if (end - p != 1) return -1; |
| 814 | return epoch; |
| 815 | } |
| 816 | else if (p[0] == '+') { |
| 817 | if (end - p != 5) return -1; |
| 818 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 819 | 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] | 820 | } |
| 821 | else if (p[0] == '-') { |
| 822 | if (end - p != 5) return -1; |
| 823 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 824 | 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] | 825 | } |
| 826 | |
| 827 | return -1; |
| 828 | } |
| 829 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 830 | /* |
| 831 | * struct alignment works here such that the key.key is the same as key_data |
| 832 | * Do not change the placement of key_data |
| 833 | */ |
| 834 | struct certificate_ocsp { |
| 835 | struct ebmb_node key; |
| 836 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 837 | struct buffer response; |
| 838 | long expire; |
| 839 | }; |
| 840 | |
| 841 | struct ocsp_cbk_arg { |
| 842 | int is_single; |
| 843 | int single_kt; |
| 844 | union { |
| 845 | struct certificate_ocsp *s_ocsp; |
| 846 | /* |
| 847 | * m_ocsp will have multiple entries dependent on key type |
| 848 | * Entry 0 - DSA |
| 849 | * Entry 1 - ECDSA |
| 850 | * Entry 2 - RSA |
| 851 | */ |
| 852 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 853 | }; |
| 854 | }; |
| 855 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 856 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 857 | |
| 858 | /* This function starts to check if the OCSP response (in DER format) contained |
| 859 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 860 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 861 | * contained in the OCSP Response and exits on error if no match. |
| 862 | * If it's a valid OCSP Response: |
| 863 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 864 | * pointed by 'ocsp'. |
| 865 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 866 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 867 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 868 | * already present in the container, it will be overwritten. |
| 869 | * |
| 870 | * Note: OCSP response containing more than one OCSP Single response is not |
| 871 | * considered valid. |
| 872 | * |
| 873 | * Returns 0 on success, 1 in error case. |
| 874 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 875 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 876 | struct certificate_ocsp *ocsp, |
| 877 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 878 | { |
| 879 | OCSP_RESPONSE *resp; |
| 880 | OCSP_BASICRESP *bs = NULL; |
| 881 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 882 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 883 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 884 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 885 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 886 | int reason; |
| 887 | int ret = 1; |
| 888 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 889 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 890 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 891 | if (!resp) { |
| 892 | memprintf(err, "Unable to parse OCSP response"); |
| 893 | goto out; |
| 894 | } |
| 895 | |
| 896 | rc = OCSP_response_status(resp); |
| 897 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 898 | memprintf(err, "OCSP response status not successful"); |
| 899 | goto out; |
| 900 | } |
| 901 | |
| 902 | bs = OCSP_response_get1_basic(resp); |
| 903 | if (!bs) { |
| 904 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 905 | goto out; |
| 906 | } |
| 907 | |
| 908 | count_sr = OCSP_resp_count(bs); |
| 909 | if (count_sr > 1) { |
| 910 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 911 | goto out; |
| 912 | } |
| 913 | |
| 914 | sr = OCSP_resp_get0(bs, 0); |
| 915 | if (!sr) { |
| 916 | memprintf(err, "Failed to get OCSP single response"); |
| 917 | goto out; |
| 918 | } |
| 919 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 920 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 921 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 922 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 923 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 924 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 925 | goto out; |
| 926 | } |
| 927 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 928 | if (!nextupd) { |
| 929 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 930 | goto out; |
| 931 | } |
| 932 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 933 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 934 | if (!rc) { |
| 935 | memprintf(err, "OCSP single response: no longer valid."); |
| 936 | goto out; |
| 937 | } |
| 938 | |
| 939 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 940 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 941 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 942 | goto out; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | if (!ocsp) { |
| 947 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 948 | unsigned char *p; |
| 949 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 950 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 951 | if (!rc) { |
| 952 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 953 | goto out; |
| 954 | } |
| 955 | |
| 956 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 957 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 958 | goto out; |
| 959 | } |
| 960 | |
| 961 | p = key; |
| 962 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 963 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 964 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 965 | if (!ocsp) { |
| 966 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 967 | goto out; |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /* According to comments on "chunk_dup", the |
| 972 | previous chunk buffer will be freed */ |
| 973 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 974 | memprintf(err, "OCSP response: Memory allocation error"); |
| 975 | goto out; |
| 976 | } |
| 977 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 978 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 979 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 980 | ret = 0; |
| 981 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 982 | ERR_clear_error(); |
| 983 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 984 | if (bs) |
| 985 | OCSP_BASICRESP_free(bs); |
| 986 | |
| 987 | if (resp) |
| 988 | OCSP_RESPONSE_free(resp); |
| 989 | |
| 990 | return ret; |
| 991 | } |
| 992 | /* |
| 993 | * External function use to update the OCSP response in the OCSP response's |
| 994 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 995 | * to update in DER format. |
| 996 | * |
| 997 | * Returns 0 on success, 1 in error case. |
| 998 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 999 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1000 | { |
| 1001 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 1002 | } |
| 1003 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1004 | #endif |
| 1005 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1006 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 1007 | 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) |
| 1008 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1009 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1010 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1011 | struct connection *conn; |
| 1012 | int head; |
| 1013 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1014 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1015 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1016 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1017 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1018 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1019 | |
| 1020 | keys = ref->tlskeys; |
| 1021 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1022 | |
| 1023 | if (enc) { |
| 1024 | memcpy(key_name, keys[head].name, 16); |
| 1025 | |
| 1026 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1027 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1028 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1029 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1030 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1031 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 1032 | goto end; |
| 1033 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1034 | 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] | 1035 | ret = 1; |
| 1036 | } |
| 1037 | else if (ref->key_size_bits == 256 ) { |
| 1038 | |
| 1039 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 1040 | goto end; |
| 1041 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1042 | 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] | 1043 | ret = 1; |
| 1044 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1045 | } else { |
| 1046 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 1047 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 1048 | goto found; |
| 1049 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1050 | ret = 0; |
| 1051 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1052 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1053 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1054 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1055 | 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] | 1056 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 1057 | goto end; |
| 1058 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1059 | ret = i ? 2 : 1; |
| 1060 | } |
| 1061 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1062 | 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] | 1063 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 1064 | goto end; |
| 1065 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1066 | ret = i ? 2 : 1; |
| 1067 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1068 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1069 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1070 | end: |
| 1071 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1072 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1076 | { |
| 1077 | struct tls_keys_ref *ref; |
| 1078 | |
| 1079 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1080 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1081 | return ref; |
| 1082 | return NULL; |
| 1083 | } |
| 1084 | |
| 1085 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1086 | { |
| 1087 | struct tls_keys_ref *ref; |
| 1088 | |
| 1089 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1090 | if (ref->unique_id == unique_id) |
| 1091 | return ref; |
| 1092 | return NULL; |
| 1093 | } |
| 1094 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1095 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1096 | * match existing ones, this function returns -1 |
| 1097 | * else it returns 0 on success. |
| 1098 | */ |
| 1099 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1100 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1101 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1102 | if (ref->key_size_bits == 128) { |
| 1103 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1104 | return -1; |
| 1105 | } |
| 1106 | else if (ref->key_size_bits == 256) { |
| 1107 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1108 | return -1; |
| 1109 | } |
| 1110 | else |
| 1111 | return -1; |
| 1112 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1113 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1114 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1115 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1116 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1117 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1118 | |
| 1119 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1120 | } |
| 1121 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1122 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1123 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1124 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1125 | |
| 1126 | if(!ref) { |
| 1127 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1128 | return 1; |
| 1129 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1130 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1131 | memprintf(err, "Invalid key size"); |
| 1132 | return 1; |
| 1133 | } |
| 1134 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1135 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1136 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1137 | |
| 1138 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1139 | * automatic ids. It's called just after the basic checks. It returns |
| 1140 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1141 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1142 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1143 | { |
| 1144 | int i = 0; |
| 1145 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1146 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1147 | |
| 1148 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1149 | if (ref->unique_id == -1) { |
| 1150 | /* Look for the first free id. */ |
| 1151 | while (1) { |
| 1152 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1153 | if (ref2->unique_id == i) { |
| 1154 | i++; |
| 1155 | break; |
| 1156 | } |
| 1157 | } |
| 1158 | if (&ref2->list == &tlskeys_reference) |
| 1159 | break; |
| 1160 | } |
| 1161 | |
| 1162 | /* Uses the unique id and increment it for the next entry. */ |
| 1163 | ref->unique_id = i; |
| 1164 | i++; |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | /* This sort the reference list by id. */ |
| 1169 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1170 | LIST_DEL(&ref->list); |
| 1171 | list_for_each_entry(ref3, &tkr, list) { |
| 1172 | if (ref->unique_id < ref3->unique_id) { |
| 1173 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1174 | break; |
| 1175 | } |
| 1176 | } |
| 1177 | if (&ref3->list == &tkr) |
| 1178 | LIST_ADDQ(&tkr, &ref->list); |
| 1179 | } |
| 1180 | |
| 1181 | /* swap root */ |
| 1182 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1183 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1184 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1185 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1186 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1187 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1188 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1189 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1190 | { |
| 1191 | switch (evp_keytype) { |
| 1192 | case EVP_PKEY_RSA: |
| 1193 | return 2; |
| 1194 | case EVP_PKEY_DSA: |
| 1195 | return 0; |
| 1196 | case EVP_PKEY_EC: |
| 1197 | return 1; |
| 1198 | } |
| 1199 | |
| 1200 | return -1; |
| 1201 | } |
| 1202 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1203 | /* |
| 1204 | * Callback used to set OCSP status extension content in server hello. |
| 1205 | */ |
| 1206 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1207 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1208 | struct certificate_ocsp *ocsp; |
| 1209 | struct ocsp_cbk_arg *ocsp_arg; |
| 1210 | char *ssl_buf; |
| 1211 | EVP_PKEY *ssl_pkey; |
| 1212 | int key_type; |
| 1213 | int index; |
| 1214 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1215 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1216 | |
| 1217 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1218 | if (!ssl_pkey) |
| 1219 | return SSL_TLSEXT_ERR_NOACK; |
| 1220 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1221 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1222 | |
| 1223 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1224 | ocsp = ocsp_arg->s_ocsp; |
| 1225 | else { |
| 1226 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1227 | * the certificate type |
| 1228 | */ |
| 1229 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1230 | |
| 1231 | if (index < 0) |
| 1232 | return SSL_TLSEXT_ERR_NOACK; |
| 1233 | |
| 1234 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1235 | |
| 1236 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1237 | |
| 1238 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1239 | !ocsp->response.area || |
| 1240 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1241 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1242 | return SSL_TLSEXT_ERR_NOACK; |
| 1243 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1244 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1245 | if (!ssl_buf) |
| 1246 | return SSL_TLSEXT_ERR_NOACK; |
| 1247 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1248 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1249 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1250 | |
| 1251 | return SSL_TLSEXT_ERR_OK; |
| 1252 | } |
| 1253 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1254 | #endif |
| 1255 | |
| 1256 | #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] | 1257 | /* |
| 1258 | * 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] | 1259 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1260 | * status extension, the issuer's certificate is mandatory. It should be |
| 1261 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1262 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1263 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1264 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1265 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1266 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1267 | * |
| 1268 | * 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] | 1269 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1270 | */ |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1271 | #ifndef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1272 | 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] | 1273 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1274 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1275 | OCSP_CERTID *cid = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1276 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1277 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1278 | char *warn = NULL; |
| 1279 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1280 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1281 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1282 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1283 | x = ckch->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1284 | if (!x) |
| 1285 | goto out; |
| 1286 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1287 | issuer = ckch->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1288 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1289 | if (chain) { |
| 1290 | /* check if one of the certificate of the chain is the issuer */ |
| 1291 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1292 | X509 *ti = sk_X509_value(chain, i); |
| 1293 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1294 | issuer = ti; |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
| 1298 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1299 | if (!issuer) |
| 1300 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1301 | |
| 1302 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1303 | if (!cid) |
| 1304 | goto out; |
| 1305 | |
| 1306 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1307 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1308 | goto out; |
| 1309 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1310 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1311 | if (!ocsp) |
| 1312 | goto out; |
| 1313 | |
| 1314 | p = ocsp->key_data; |
| 1315 | i2d_OCSP_CERTID(cid, &p); |
| 1316 | |
| 1317 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1318 | if (iocsp == ocsp) |
| 1319 | ocsp = NULL; |
| 1320 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1321 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1322 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1323 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1324 | #endif |
| 1325 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1326 | |
| 1327 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1328 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1329 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1330 | |
| 1331 | cb_arg->is_single = 1; |
| 1332 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1333 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1334 | pkey = X509_get_pubkey(x); |
| 1335 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1336 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1337 | |
| 1338 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1339 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1340 | } else { |
| 1341 | /* |
| 1342 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1343 | * Update that cb_arg with the new cert's staple |
| 1344 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1345 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1346 | struct certificate_ocsp *tmp_ocsp; |
| 1347 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1348 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1349 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1350 | |
| 1351 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1352 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1353 | #else |
| 1354 | cb_arg = ctx->tlsext_status_arg; |
| 1355 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1356 | |
| 1357 | /* |
| 1358 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1359 | * the order of operations below matter, take care when changing it |
| 1360 | */ |
| 1361 | tmp_ocsp = cb_arg->s_ocsp; |
| 1362 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1363 | cb_arg->s_ocsp = NULL; |
| 1364 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1365 | cb_arg->is_single = 0; |
| 1366 | cb_arg->single_kt = 0; |
| 1367 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1368 | pkey = X509_get_pubkey(x); |
| 1369 | key_type = EVP_PKEY_base_id(pkey); |
| 1370 | EVP_PKEY_free(pkey); |
| 1371 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1372 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1373 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1374 | cb_arg->m_ocsp[index] = iocsp; |
| 1375 | |
| 1376 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1377 | |
| 1378 | ret = 0; |
| 1379 | |
| 1380 | warn = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1381 | if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1382 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1383 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | out: |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1387 | if (cid) |
| 1388 | OCSP_CERTID_free(cid); |
| 1389 | |
| 1390 | if (ocsp) |
| 1391 | free(ocsp); |
| 1392 | |
| 1393 | if (warn) |
| 1394 | free(warn); |
| 1395 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1396 | return ret; |
| 1397 | } |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1398 | #else /* OPENSSL_IS_BORINGSSL */ |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1399 | 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] | 1400 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1401 | 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] | 1402 | } |
| 1403 | #endif |
| 1404 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1405 | #endif |
| 1406 | |
| 1407 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1408 | #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] | 1409 | |
| 1410 | #define CT_EXTENSION_TYPE 18 |
| 1411 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 1412 | int sctl_ex_index = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1413 | |
| 1414 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1415 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1416 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1417 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1418 | *out = (unsigned char *) sctl->area; |
| 1419 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1420 | |
| 1421 | return 1; |
| 1422 | } |
| 1423 | |
| 1424 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1425 | { |
| 1426 | return 1; |
| 1427 | } |
| 1428 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1429 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1430 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1431 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1432 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1433 | 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] | 1434 | goto out; |
| 1435 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1436 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1437 | |
| 1438 | ret = 0; |
| 1439 | |
| 1440 | out: |
| 1441 | return ret; |
| 1442 | } |
| 1443 | |
| 1444 | #endif |
| 1445 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1446 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1447 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1448 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1449 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1450 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1451 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1452 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1453 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1454 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1455 | * change this to #if and do not assign a default value to this macro! |
| 1456 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1457 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1458 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1459 | 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] | 1460 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1461 | conn->err_code = CO_ER_SSL_RENEG; |
| 1462 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1463 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1464 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1465 | |
| 1466 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1467 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1468 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1469 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1470 | consider that the buffering was activated, |
| 1471 | so we rise the output buffer size from 4k |
| 1472 | to 16k */ |
| 1473 | write_bio = SSL_get_wbio(ssl); |
| 1474 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1475 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1476 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1480 | } |
| 1481 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1482 | /* Callback is called for each certificate of the chain during a verify |
| 1483 | ok is set to 1 if preverify detect no error on current certificate. |
| 1484 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1485 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1486 | { |
| 1487 | SSL *ssl; |
| 1488 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1489 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1490 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1491 | |
| 1492 | 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] | 1493 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1494 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1495 | ctx = conn->xprt_ctx; |
| 1496 | |
| 1497 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1498 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1499 | if (ok) /* no errors */ |
| 1500 | return ok; |
| 1501 | |
| 1502 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1503 | err = X509_STORE_CTX_get_error(x_store); |
| 1504 | |
| 1505 | /* check if CA error needs to be ignored */ |
| 1506 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1507 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1508 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1509 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1510 | } |
| 1511 | |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1512 | 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] | 1513 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1514 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1515 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1516 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1517 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1518 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1519 | return 0; |
| 1520 | } |
| 1521 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1522 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1523 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1524 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1525 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1526 | 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] | 1527 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1528 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1529 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1530 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1531 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1532 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1533 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1534 | } |
| 1535 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 1536 | #ifdef TLS1_RT_HEARTBEAT |
| 1537 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 1538 | int content_type, const void *buf, size_t len, |
| 1539 | SSL *ssl) |
| 1540 | { |
| 1541 | /* test heartbeat received (write_p is set to 0 |
| 1542 | for a received record) */ |
| 1543 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
| 1544 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 1545 | const unsigned char *p = buf; |
| 1546 | unsigned int payload; |
| 1547 | |
| 1548 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
| 1549 | |
| 1550 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1551 | if (*p != TLS1_HB_REQUEST) |
| 1552 | return; |
| 1553 | |
| 1554 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
| 1555 | goto kill_it; |
| 1556 | |
| 1557 | payload = (p[1] * 256) + p[2]; |
| 1558 | if (3 + payload + 16 <= len) |
| 1559 | return; /* OK no problem */ |
| 1560 | kill_it: |
| 1561 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1562 | * advertised payload is larger than the advertised packet |
| 1563 | * length, so we have garbage in the buffer between the |
| 1564 | * payload and the end of the buffer (p+len). We can't know |
| 1565 | * if the SSL stack is patched, and we don't know if we can |
| 1566 | * safely wipe out the area between p+3+len and payload. |
| 1567 | * So instead, we prevent the response from being sent by |
| 1568 | * setting the max_send_fragment to 0 and we report an SSL |
| 1569 | * error, which will kill this connection. It will be reported |
| 1570 | * above as SSL_ERROR_SSL while an other handshake failure with |
| 1571 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1572 | */ |
| 1573 | ssl->max_send_fragment = 0; |
| 1574 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1575 | } |
| 1576 | } |
| 1577 | #endif |
| 1578 | |
| 1579 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 1580 | int content_type, const void *buf, size_t len, |
| 1581 | SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1582 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1583 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1584 | unsigned char *msg; |
| 1585 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1586 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1587 | |
| 1588 | /* This function is called for "from client" and "to server" |
| 1589 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1590 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1591 | */ |
| 1592 | |
| 1593 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1594 | * otherwise it is set to 1. |
| 1595 | */ |
| 1596 | if (write_p != 0) |
| 1597 | return; |
| 1598 | |
| 1599 | /* content_type contains the type of message received or sent |
| 1600 | * according with the SSL/TLS protocol spec. This message is |
| 1601 | * encoded with one byte. The value 256 (two bytes) is used |
| 1602 | * for designing the SSL/TLS record layer. According with the |
| 1603 | * rfc6101, the expected message (other than 256) are: |
| 1604 | * - change_cipher_spec(20) |
| 1605 | * - alert(21) |
| 1606 | * - handshake(22) |
| 1607 | * - application_data(23) |
| 1608 | * - (255) |
| 1609 | * We are interessed by the handshake and specially the client |
| 1610 | * hello. |
| 1611 | */ |
| 1612 | if (content_type != 22) |
| 1613 | return; |
| 1614 | |
| 1615 | /* The message length is at least 4 bytes, containing the |
| 1616 | * message type and the message length. |
| 1617 | */ |
| 1618 | if (len < 4) |
| 1619 | return; |
| 1620 | |
| 1621 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1622 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1623 | * - hello_request(0) |
| 1624 | * - client_hello(1) |
| 1625 | * - server_hello(2) |
| 1626 | * - certificate(11) |
| 1627 | * - server_key_exchange (12) |
| 1628 | * - certificate_request(13) |
| 1629 | * - server_hello_done(14) |
| 1630 | * We are interested by the client hello. |
| 1631 | */ |
| 1632 | msg = (unsigned char *)buf; |
| 1633 | if (msg[0] != 1) |
| 1634 | return; |
| 1635 | |
| 1636 | /* Next three bytes are the length of the message. The total length |
| 1637 | * must be this decoded length + 4. If the length given as argument |
| 1638 | * is not the same, we abort the protocol dissector. |
| 1639 | */ |
| 1640 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1641 | if (len < rec_len + 4) |
| 1642 | return; |
| 1643 | msg += 4; |
| 1644 | end = msg + rec_len; |
| 1645 | if (end < msg) |
| 1646 | return; |
| 1647 | |
| 1648 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1649 | * 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] | 1650 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1651 | */ |
| 1652 | msg += 1 + 1 + 4 + 28; |
| 1653 | if (msg > end) |
| 1654 | return; |
| 1655 | |
| 1656 | /* Next, is session id: |
| 1657 | * if present, we have to jump by length + 1 for the size information |
| 1658 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1659 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1660 | if (msg[0] > 0) |
| 1661 | msg += msg[0]; |
| 1662 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1663 | if (msg > end) |
| 1664 | return; |
| 1665 | |
| 1666 | /* Next two bytes are the ciphersuite length. */ |
| 1667 | if (msg + 2 > end) |
| 1668 | return; |
| 1669 | rec_len = (msg[0] << 8) + msg[1]; |
| 1670 | msg += 2; |
| 1671 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1672 | return; |
| 1673 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1674 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1675 | if (!capture) |
| 1676 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1677 | /* Compute the xxh64 of the ciphersuite. */ |
| 1678 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1679 | |
| 1680 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1681 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1682 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1683 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1684 | |
| 1685 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1686 | } |
| 1687 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1688 | /* Callback is called for ssl protocol analyse */ |
| 1689 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1690 | { |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1691 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1692 | struct ssl_sock_msg_callback *cbk; |
| 1693 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1694 | /* Try to call all callback functions that were registered by using |
| 1695 | * ssl_sock_register_msg_callback(). |
| 1696 | */ |
| 1697 | list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) { |
| 1698 | cbk->func(conn, write_p, version, content_type, buf, len, ssl); |
| 1699 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1700 | } |
| 1701 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1702 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1703 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1704 | const unsigned char *in, unsigned int inlen, |
| 1705 | void *arg) |
| 1706 | { |
| 1707 | struct server *srv = arg; |
| 1708 | |
| 1709 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1710 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1711 | return SSL_TLSEXT_ERR_OK; |
| 1712 | return SSL_TLSEXT_ERR_NOACK; |
| 1713 | } |
| 1714 | #endif |
| 1715 | |
| 1716 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1717 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1718 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1719 | */ |
| 1720 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1721 | unsigned int *len, void *arg) |
| 1722 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1723 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1724 | |
| 1725 | *data = (const unsigned char *)conf->npn_str; |
| 1726 | *len = conf->npn_len; |
| 1727 | return SSL_TLSEXT_ERR_OK; |
| 1728 | } |
| 1729 | #endif |
| 1730 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1731 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1732 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1733 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1734 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1735 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1736 | unsigned char *outlen, |
| 1737 | const unsigned char *server, |
| 1738 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1739 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1740 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1741 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1742 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1743 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1744 | return SSL_TLSEXT_ERR_NOACK; |
| 1745 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1746 | return SSL_TLSEXT_ERR_OK; |
| 1747 | } |
| 1748 | #endif |
| 1749 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1750 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1751 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1752 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1753 | /* Create a X509 certificate with the specified servername and serial. This |
| 1754 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1755 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1756 | 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] | 1757 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1758 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1759 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1760 | SSL_CTX *ssl_ctx = NULL; |
| 1761 | X509 *newcrt = NULL; |
| 1762 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1763 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1764 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1765 | X509_NAME *name; |
| 1766 | const EVP_MD *digest; |
| 1767 | X509V3_CTX ctx; |
| 1768 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1769 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1770 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1771 | /* Get the private key of the default certificate and use it */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1772 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L) |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1773 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1774 | #else |
| 1775 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1776 | if (tmp_ssl) |
| 1777 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1778 | #endif |
| 1779 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1780 | goto mkcert_error; |
| 1781 | |
| 1782 | /* Create the certificate */ |
| 1783 | if (!(newcrt = X509_new())) |
| 1784 | goto mkcert_error; |
| 1785 | |
| 1786 | /* Set version number for the certificate (X509v3) and the serial |
| 1787 | * number */ |
| 1788 | if (X509_set_version(newcrt, 2L) != 1) |
| 1789 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 1790 | 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] | 1791 | |
| 1792 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1793 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1794 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1795 | goto mkcert_error; |
| 1796 | |
| 1797 | /* set public key in the certificate */ |
| 1798 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1799 | goto mkcert_error; |
| 1800 | |
| 1801 | /* Set issuer name from the CA */ |
| 1802 | if (!(name = X509_get_subject_name(cacert))) |
| 1803 | goto mkcert_error; |
| 1804 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1805 | goto mkcert_error; |
| 1806 | |
| 1807 | /* Set the subject name using the same, but the CN */ |
| 1808 | name = X509_NAME_dup(name); |
| 1809 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1810 | (const unsigned char *)servername, |
| 1811 | -1, -1, 0) != 1) { |
| 1812 | X509_NAME_free(name); |
| 1813 | goto mkcert_error; |
| 1814 | } |
| 1815 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1816 | X509_NAME_free(name); |
| 1817 | goto mkcert_error; |
| 1818 | } |
| 1819 | X509_NAME_free(name); |
| 1820 | |
| 1821 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1822 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1823 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1824 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1825 | X509_EXTENSION *ext; |
| 1826 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1827 | 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] | 1828 | goto mkcert_error; |
| 1829 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1830 | X509_EXTENSION_free(ext); |
| 1831 | goto mkcert_error; |
| 1832 | } |
| 1833 | X509_EXTENSION_free(ext); |
| 1834 | } |
| 1835 | |
| 1836 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1837 | |
| 1838 | key_type = EVP_PKEY_base_id(capkey); |
| 1839 | |
| 1840 | if (key_type == EVP_PKEY_DSA) |
| 1841 | digest = EVP_sha1(); |
| 1842 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1843 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1844 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1845 | digest = EVP_sha256(); |
| 1846 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 1847 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1848 | int nid; |
| 1849 | |
| 1850 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1851 | goto mkcert_error; |
| 1852 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1853 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1854 | #else |
| 1855 | goto mkcert_error; |
| 1856 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1857 | } |
| 1858 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1859 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1860 | goto mkcert_error; |
| 1861 | |
| 1862 | /* Create and set the new SSL_CTX */ |
| 1863 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1864 | goto mkcert_error; |
| 1865 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1866 | goto mkcert_error; |
| 1867 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1868 | goto mkcert_error; |
| 1869 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1870 | goto mkcert_error; |
| 1871 | |
| 1872 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1873 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1874 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1875 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1876 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1877 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1878 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1879 | 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] | 1880 | EC_KEY *ecc; |
| 1881 | int nid; |
| 1882 | |
| 1883 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1884 | goto end; |
| 1885 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1886 | goto end; |
| 1887 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1888 | EC_KEY_free(ecc); |
| 1889 | } |
| 1890 | #endif |
| 1891 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1892 | return ssl_ctx; |
| 1893 | |
| 1894 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1895 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1896 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1897 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1898 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1899 | return NULL; |
| 1900 | } |
| 1901 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1902 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1903 | 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] | 1904 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1905 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1906 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1907 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1908 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1909 | } |
| 1910 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1911 | /* 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] | 1912 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1913 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1914 | 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] | 1915 | { |
| 1916 | struct lru64 *lru = NULL; |
| 1917 | |
| 1918 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1919 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1920 | 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] | 1921 | if (lru && lru->domain) { |
| 1922 | if (ssl) |
| 1923 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1924 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1925 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1926 | } |
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 | } |
| 1929 | return NULL; |
| 1930 | } |
| 1931 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1932 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 1933 | * function is not thread-safe, it should only be used to check if a certificate |
| 1934 | * exists in the lru cache (with no warranty it will not be removed by another |
| 1935 | * thread). It is kept for backward compatibility. */ |
| 1936 | SSL_CTX * |
| 1937 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 1938 | { |
| 1939 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 1940 | } |
| 1941 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1942 | /* Set a certificate int the LRU cache used to store generated |
| 1943 | * certificate. Return 0 on success, otherwise -1 */ |
| 1944 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1945 | 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] | 1946 | { |
| 1947 | struct lru64 *lru = NULL; |
| 1948 | |
| 1949 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1950 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1951 | 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] | 1952 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1953 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1954 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1955 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1956 | if (lru->domain && lru->data) |
| 1957 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1958 | 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] | 1959 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1960 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1961 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1962 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1963 | } |
| 1964 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1965 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1966 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1967 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1968 | { |
| 1969 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 1970 | } |
| 1971 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1972 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 1973 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 1974 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1975 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1976 | 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] | 1977 | { |
| 1978 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1979 | SSL_CTX *ssl_ctx = NULL; |
| 1980 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1981 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1982 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1983 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1984 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1985 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1986 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1987 | if (lru && lru->domain) |
| 1988 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1989 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1990 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1991 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1992 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1993 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1994 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 1995 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1996 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1997 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1998 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 1999 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2000 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2001 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2002 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2003 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2004 | return 0; |
| 2005 | } |
| 2006 | static int |
| 2007 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2008 | { |
| 2009 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2010 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2011 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2012 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2013 | 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] | 2014 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2015 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2016 | } |
| 2017 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2018 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2019 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2020 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2021 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2022 | |
| 2023 | 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] | 2024 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2025 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2026 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2027 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2028 | #endif |
| 2029 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2030 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2031 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2032 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2033 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2034 | 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] | 2035 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2036 | 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] | 2037 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2038 | #endif |
| 2039 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2040 | 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] | 2041 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2042 | 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] | 2043 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2044 | #endif |
| 2045 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2046 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2047 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2048 | /* Unusable in this context. */ |
| 2049 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2050 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2051 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2052 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2053 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2054 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2055 | |
| 2056 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2057 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2058 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2059 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2060 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2061 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2062 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2063 | } |
| 2064 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2065 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2066 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2067 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2068 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2069 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2070 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2071 | } |
| 2072 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2073 | 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] | 2074 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2075 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2076 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2077 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2078 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2079 | } |
| 2080 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2081 | 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] | 2082 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2083 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2084 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2085 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2086 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2087 | } |
| 2088 | 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] | 2089 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2090 | 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] | 2091 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2092 | #endif |
| 2093 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2094 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2095 | #if SSL_OP_NO_TLSv1_3 |
| 2096 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2097 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2098 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2099 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2100 | #endif |
| 2101 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2102 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2103 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 2104 | struct methodVersions methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2105 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2106 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2107 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2108 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2109 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2110 | {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] | 2111 | }; |
| 2112 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2113 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2114 | { |
| 2115 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2116 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2117 | SSL_set_SSL_CTX(ssl, ctx); |
| 2118 | } |
| 2119 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2120 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2121 | |
| 2122 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2123 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2124 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2125 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2126 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2127 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2128 | return SSL_TLSEXT_ERR_OK; |
| 2129 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2130 | } |
| 2131 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2132 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2133 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2134 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2135 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2136 | #else |
| 2137 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2138 | { |
| 2139 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2140 | struct connection *conn; |
| 2141 | struct bind_conf *s; |
| 2142 | const uint8_t *extension_data; |
| 2143 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2144 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2145 | |
| 2146 | char *wildp = NULL; |
| 2147 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2148 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2149 | 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] | 2150 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2151 | int i; |
| 2152 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2153 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2154 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2155 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2156 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2157 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2158 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2159 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2160 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2161 | #else |
| 2162 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2163 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2164 | /* |
| 2165 | * The server_name extension was given too much extensibility when it |
| 2166 | * was written, so parsing the normal case is a bit complex. |
| 2167 | */ |
| 2168 | size_t len; |
| 2169 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2170 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2171 | /* Extract the length of the supplied list of names. */ |
| 2172 | len = (*extension_data++) << 8; |
| 2173 | len |= *extension_data++; |
| 2174 | if (len + 2 != extension_len) |
| 2175 | goto abort; |
| 2176 | /* |
| 2177 | * The list in practice only has a single element, so we only consider |
| 2178 | * the first one. |
| 2179 | */ |
| 2180 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2181 | goto abort; |
| 2182 | extension_len = len - 1; |
| 2183 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2184 | if (extension_len <= 2) |
| 2185 | goto abort; |
| 2186 | len = (*extension_data++) << 8; |
| 2187 | len |= *extension_data++; |
| 2188 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2189 | || memchr(extension_data, 0, len) != NULL) |
| 2190 | goto abort; |
| 2191 | servername = extension_data; |
| 2192 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2193 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2194 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2195 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2196 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2197 | } |
| 2198 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2199 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2200 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2201 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2202 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2203 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2204 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2205 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2206 | goto abort; |
| 2207 | } |
| 2208 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2209 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2210 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2211 | 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] | 2212 | #else |
| 2213 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2214 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2215 | uint8_t sign; |
| 2216 | size_t len; |
| 2217 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2218 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2219 | len = (*extension_data++) << 8; |
| 2220 | len |= *extension_data++; |
| 2221 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2222 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2223 | if (len % 2 != 0) |
| 2224 | goto abort; |
| 2225 | for (; len > 0; len -= 2) { |
| 2226 | extension_data++; /* hash */ |
| 2227 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2228 | switch (sign) { |
| 2229 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2230 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2231 | break; |
| 2232 | case TLSEXT_signature_ecdsa: |
| 2233 | has_ecdsa_sig = 1; |
| 2234 | break; |
| 2235 | default: |
| 2236 | continue; |
| 2237 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2238 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2239 | break; |
| 2240 | } |
| 2241 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2242 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2243 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2244 | } |
| 2245 | 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] | 2246 | const SSL_CIPHER *cipher; |
| 2247 | size_t len; |
| 2248 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2249 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2250 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2251 | len = ctx->cipher_suites_len; |
| 2252 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2253 | #else |
| 2254 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2255 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2256 | if (len % 2 != 0) |
| 2257 | goto abort; |
| 2258 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2259 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2260 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2261 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2262 | #else |
| 2263 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2264 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2265 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2266 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2267 | break; |
| 2268 | } |
| 2269 | } |
| 2270 | } |
| 2271 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2272 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2273 | trash.area[i] = tolower(servername[i]); |
| 2274 | if (!wildp && (trash.area[i] == '.')) |
| 2275 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2276 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2277 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2278 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2279 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2280 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2281 | for (i = 0; i < 2; i++) { |
| 2282 | if (i == 0) /* lookup in full qualified names */ |
| 2283 | node = ebst_lookup(&s->sni_ctx, trash.area); |
| 2284 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
| 2285 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2286 | else |
| 2287 | break; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2288 | for (n = node; n; n = ebmb_next_dup(n)) { |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2289 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2290 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2291 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2292 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2293 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2294 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2295 | break; |
| 2296 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2297 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2298 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2299 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2300 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2301 | if (!node_anonymous) |
| 2302 | node_anonymous = n; |
| 2303 | break; |
| 2304 | } |
| 2305 | } |
| 2306 | } |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2307 | /* select by key_signature priority order */ |
| 2308 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2309 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2310 | : (node_anonymous ? node_anonymous |
| 2311 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2312 | : node_rsa /* no rsa signature case (far far away) */ |
| 2313 | ))); |
| 2314 | if (node) { |
| 2315 | /* switch ctx */ |
| 2316 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2317 | 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] | 2318 | if (conf) { |
| 2319 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2320 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2321 | if (conf->early_data) |
| 2322 | allow_early = 1; |
| 2323 | } |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2324 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2325 | goto allow_early; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2326 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2327 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2328 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2329 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2330 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2331 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2332 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2333 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2334 | } |
| 2335 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2336 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2337 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2338 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2339 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2340 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2341 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2342 | allow_early: |
| 2343 | #ifdef OPENSSL_IS_BORINGSSL |
| 2344 | if (allow_early) |
| 2345 | SSL_set_early_data_enabled(ssl, 1); |
| 2346 | #else |
| 2347 | if (!allow_early) |
| 2348 | SSL_set_max_early_data(ssl, 0); |
| 2349 | #endif |
| 2350 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2351 | abort: |
| 2352 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2353 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2354 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2355 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2356 | #else |
| 2357 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2358 | return 0; |
| 2359 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2360 | } |
| 2361 | |
| 2362 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2363 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2364 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2365 | * warning when no match is found, which implies the default (first) cert |
| 2366 | * will keep being used. |
| 2367 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2368 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2369 | { |
| 2370 | const char *servername; |
| 2371 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2372 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2373 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2374 | int i; |
| 2375 | (void)al; /* shut gcc stupid warning */ |
| 2376 | |
| 2377 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2378 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2379 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2380 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2381 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2382 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2383 | if (s->strict_sni) |
| 2384 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2385 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2386 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2387 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2388 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2389 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2390 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2391 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2392 | if (!servername[i]) |
| 2393 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2394 | trash.area[i] = tolower(servername[i]); |
| 2395 | if (!wildp && (trash.area[i] == '.')) |
| 2396 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2397 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2398 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2399 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2400 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2401 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2402 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2403 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2404 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2405 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2406 | node = n; |
| 2407 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2408 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2409 | } |
| 2410 | if (!node && wildp) { |
| 2411 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2412 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2413 | /* lookup a not neg filter */ |
| 2414 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2415 | node = n; |
| 2416 | break; |
| 2417 | } |
| 2418 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2419 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2420 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2421 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2422 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2423 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2424 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2425 | return SSL_TLSEXT_ERR_OK; |
| 2426 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2427 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2428 | if (s->strict_sni) { |
| 2429 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2430 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2431 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2432 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2433 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2434 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2438 | 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] | 2439 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2440 | return SSL_TLSEXT_ERR_OK; |
| 2441 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2442 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2443 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2444 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2445 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2446 | |
| 2447 | static DH * ssl_get_dh_1024(void) |
| 2448 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2449 | static unsigned char dh1024_p[]={ |
| 2450 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2451 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2452 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2453 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2454 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2455 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2456 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2457 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2458 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2459 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2460 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2461 | }; |
| 2462 | static unsigned char dh1024_g[]={ |
| 2463 | 0x02, |
| 2464 | }; |
| 2465 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2466 | BIGNUM *p; |
| 2467 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2468 | DH *dh = DH_new(); |
| 2469 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2470 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2471 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2472 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2473 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2474 | DH_free(dh); |
| 2475 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2476 | } else { |
| 2477 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2478 | } |
| 2479 | } |
| 2480 | return dh; |
| 2481 | } |
| 2482 | |
| 2483 | static DH *ssl_get_dh_2048(void) |
| 2484 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2485 | static unsigned char dh2048_p[]={ |
| 2486 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2487 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2488 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2489 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2490 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2491 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2492 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2493 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2494 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2495 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2496 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2497 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2498 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2499 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2500 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2501 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2502 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2503 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2504 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2505 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2506 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2507 | 0xB7,0x1F,0x77,0xF3, |
| 2508 | }; |
| 2509 | static unsigned char dh2048_g[]={ |
| 2510 | 0x02, |
| 2511 | }; |
| 2512 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2513 | BIGNUM *p; |
| 2514 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2515 | DH *dh = DH_new(); |
| 2516 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2517 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2518 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2519 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2520 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2521 | DH_free(dh); |
| 2522 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2523 | } else { |
| 2524 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2525 | } |
| 2526 | } |
| 2527 | return dh; |
| 2528 | } |
| 2529 | |
| 2530 | static DH *ssl_get_dh_4096(void) |
| 2531 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2532 | static unsigned char dh4096_p[]={ |
| 2533 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2534 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2535 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2536 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2537 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2538 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2539 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2540 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2541 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2542 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2543 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2544 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2545 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2546 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2547 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2548 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2549 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2550 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2551 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2552 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2553 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2554 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2555 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2556 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2557 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2558 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2559 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2560 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2561 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2562 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2563 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2564 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2565 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2566 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2567 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2568 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2569 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2570 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2571 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2572 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2573 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2574 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2575 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2576 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2577 | static unsigned char dh4096_g[]={ |
| 2578 | 0x02, |
| 2579 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2580 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2581 | BIGNUM *p; |
| 2582 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2583 | DH *dh = DH_new(); |
| 2584 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2585 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2586 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2587 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2588 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2589 | DH_free(dh); |
| 2590 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2591 | } else { |
| 2592 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | return dh; |
| 2596 | } |
| 2597 | |
| 2598 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2599 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2600 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2601 | { |
| 2602 | DH *dh = NULL; |
| 2603 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2604 | int type; |
| 2605 | |
| 2606 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2607 | |
| 2608 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2609 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2610 | */ |
| 2611 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2612 | keylen = EVP_PKEY_bits(pkey); |
| 2613 | } |
| 2614 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2615 | if (keylen > global_ssl.default_dh_param) { |
| 2616 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2617 | } |
| 2618 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2619 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2620 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2621 | } |
| 2622 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2623 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2624 | } |
| 2625 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2626 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | return dh; |
| 2630 | } |
| 2631 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2632 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2633 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2634 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2635 | BIO *in = BIO_new(BIO_s_file()); |
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 | if (in == NULL) |
| 2638 | goto end; |
| 2639 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2640 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2641 | goto end; |
| 2642 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2643 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2644 | |
| 2645 | end: |
| 2646 | if (in) |
| 2647 | BIO_free(in); |
| 2648 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2649 | ERR_clear_error(); |
| 2650 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2651 | return dh; |
| 2652 | } |
| 2653 | |
| 2654 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2655 | { |
| 2656 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2657 | |
| 2658 | if (global_dh) { |
| 2659 | return 0; |
| 2660 | } |
| 2661 | |
| 2662 | return -1; |
| 2663 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2664 | #endif |
| 2665 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2666 | /* 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] | 2667 | 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] | 2668 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2669 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2670 | { |
| 2671 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2672 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2673 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2674 | if (*name == '!') { |
| 2675 | neg = 1; |
| 2676 | name++; |
| 2677 | } |
| 2678 | if (*name == '*') { |
| 2679 | wild = 1; |
| 2680 | name++; |
| 2681 | } |
| 2682 | /* !* filter is a nop */ |
| 2683 | if (neg && wild) |
| 2684 | return order; |
| 2685 | if (*name) { |
| 2686 | int j, len; |
| 2687 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2688 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2689 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2690 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2691 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2692 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2693 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2694 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2695 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2696 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2697 | memcpy(sc->name.key, trash.area, len + 1); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2698 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2699 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2700 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2701 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2702 | sc->order = order++; |
| 2703 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2704 | sc->wild = wild; |
| 2705 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 2706 | sc->ckch_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2707 | LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2708 | } |
| 2709 | return order; |
| 2710 | } |
| 2711 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2712 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2713 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 2714 | * This function can't return an error. |
| 2715 | * |
| 2716 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 2717 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 2718 | 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] | 2719 | { |
| 2720 | |
| 2721 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 2722 | struct ebmb_node *node; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2723 | int def = 0; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2724 | |
| 2725 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 2726 | |
| 2727 | /* ignore if sc0 was already inserted in a tree */ |
| 2728 | if (sc0->name.node.leaf_p) |
| 2729 | continue; |
| 2730 | |
| 2731 | /* Check for duplicates. */ |
| 2732 | if (sc0->wild) |
| 2733 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 2734 | else |
| 2735 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 2736 | |
| 2737 | for (; node; node = ebmb_next_dup(node)) { |
| 2738 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 2739 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 2740 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 2741 | /* it's a duplicate, we should remove and free it */ |
| 2742 | LIST_DEL(&sc0->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2743 | SSL_CTX_free(sc0->ctx); |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2744 | free(sc0); |
| 2745 | sc0 = NULL; |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 2746 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2747 | } |
| 2748 | } |
| 2749 | |
| 2750 | /* if duplicate, ignore the insertion */ |
| 2751 | if (!sc0) |
| 2752 | continue; |
| 2753 | |
| 2754 | if (sc0->wild) |
| 2755 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 2756 | else |
| 2757 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2758 | |
| 2759 | /* replace the default_ctx if required with the first ctx */ |
| 2760 | if (ckch_inst->is_default && !def) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2761 | SSL_CTX_free(bind_conf->default_ctx); |
| 2762 | SSL_CTX_up_ref(sc0->ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2763 | bind_conf->default_ctx = sc0->ctx; |
| 2764 | def = 1; |
| 2765 | } |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2770 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2771 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2772 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2773 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 2774 | /* tree of crtlist (crt-list/directory) */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 2775 | struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2776 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2777 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2778 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2779 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 2780 | * DH parameter available in ckchs nor in global, the default |
| 2781 | * DH parameters are applied on the SSL_CTX. |
| 2782 | * Returns a bitfield containing the flags: |
| 2783 | * ERR_FATAL in any fatal error case |
| 2784 | * ERR_ALERT if a reason of the error is availabine in err |
| 2785 | * ERR_WARN if a warning is available into err |
| 2786 | * The value 0 means there is no error nor warning and |
| 2787 | * the operation succeed. |
| 2788 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2789 | #ifndef OPENSSL_NO_DH |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2790 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, |
| 2791 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2792 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2793 | int ret = 0; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2794 | DH *dh = NULL; |
| 2795 | |
William Lallemand | a8c7374 | 2019-07-31 18:31:34 +0200 | [diff] [blame] | 2796 | if (ckch && ckch->dh) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2797 | dh = ckch->dh; |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2798 | if (!SSL_CTX_set_tmp_dh(ctx, dh)) { |
| 2799 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 2800 | err && *err ? *err : "", path); |
| 2801 | #if defined(SSL_CTX_set_dh_auto) |
| 2802 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2803 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2804 | err && *err ? *err : ""); |
| 2805 | #else |
| 2806 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2807 | err && *err ? *err : ""); |
| 2808 | #endif |
| 2809 | ret |= ERR_WARN; |
| 2810 | goto end; |
| 2811 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2812 | |
| 2813 | if (ssl_dh_ptr_index >= 0) { |
| 2814 | /* store a pointer to the DH params to avoid complaining about |
| 2815 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2816 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2817 | } |
| 2818 | } |
| 2819 | else if (global_dh) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2820 | if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) { |
| 2821 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 2822 | err && *err ? *err : "", path); |
| 2823 | #if defined(SSL_CTX_set_dh_auto) |
| 2824 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2825 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2826 | err && *err ? *err : ""); |
| 2827 | #else |
| 2828 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2829 | err && *err ? *err : ""); |
| 2830 | #endif |
| 2831 | ret |= ERR_WARN; |
| 2832 | goto end; |
| 2833 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2834 | } |
| 2835 | else { |
| 2836 | /* Clear openssl global errors stack */ |
| 2837 | ERR_clear_error(); |
| 2838 | |
| 2839 | if (global_ssl.default_dh_param <= 1024) { |
| 2840 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 2841 | if (local_dh_1024 == NULL) |
| 2842 | local_dh_1024 = ssl_get_dh_1024(); |
| 2843 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2844 | if (local_dh_1024 == NULL) { |
| 2845 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 2846 | err && *err ? *err : "", path); |
| 2847 | ret |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2848 | goto end; |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2849 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2850 | |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2851 | if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) { |
| 2852 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 2853 | err && *err ? *err : "", path); |
| 2854 | #if defined(SSL_CTX_set_dh_auto) |
| 2855 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2856 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2857 | err && *err ? *err : ""); |
| 2858 | #else |
| 2859 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2860 | err && *err ? *err : ""); |
| 2861 | #endif |
| 2862 | ret |= ERR_WARN; |
| 2863 | goto end; |
| 2864 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2865 | } |
| 2866 | else { |
| 2867 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2868 | } |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 2869 | } |
| 2870 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 2871 | end: |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 2872 | ERR_clear_error(); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 2873 | return ret; |
| 2874 | } |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 2875 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2876 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2877 | /* Loads the info in ckch into ctx |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2878 | * Returns a bitfield containing the flags: |
| 2879 | * ERR_FATAL in any fatal error case |
| 2880 | * ERR_ALERT if the reason of the error is available in err |
| 2881 | * ERR_WARN if a warning is available into err |
| 2882 | * The value 0 means there is no error nor warning and |
| 2883 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2884 | */ |
| 2885 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2886 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2887 | int errcode = 0; |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 2888 | STACK_OF(X509) *find_chain = NULL; |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2889 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2890 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2891 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2892 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2893 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2894 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2898 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 2899 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2900 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2901 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2902 | } |
| 2903 | |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 2904 | if (ckch->chain) { |
| 2905 | find_chain = ckch->chain; |
| 2906 | } else { |
| 2907 | /* Find Certificate Chain in global */ |
| 2908 | struct issuer_chain *issuer; |
Emmanuel Hocdet | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 2909 | issuer = ssl_get0_issuer_chain(ckch->cert); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 2910 | if (issuer) |
| 2911 | find_chain = issuer->chain; |
| 2912 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 2913 | |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 2914 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 2915 | if (find_chain) |
| 2916 | #ifdef SSL_CTX_set1_chain |
| 2917 | if (!SSL_CTX_set1_chain(ctx, find_chain)) { |
| 2918 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 2919 | err && *err ? *err : "", path); |
| 2920 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2921 | goto end; |
| 2922 | } |
| 2923 | #else |
| 2924 | { /* legacy compat (< openssl 1.0.2) */ |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 2925 | X509 *ca; |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 2926 | STACK_OF(X509) *chain; |
| 2927 | chain = X509_chain_up_ref(find_chain); |
| 2928 | while ((ca = sk_X509_shift(chain))) |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 2929 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 2930 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 2931 | err && *err ? *err : "", path); |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 2932 | X509_free(ca); |
| 2933 | sk_X509_pop_free(chain, X509_free); |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 2934 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2935 | goto end; |
| 2936 | } |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 2937 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 2938 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2939 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2940 | #ifndef OPENSSL_NO_DH |
| 2941 | /* store a NULL pointer to indicate we have not yet loaded |
| 2942 | a custom DH param file */ |
| 2943 | if (ssl_dh_ptr_index >= 0) { |
| 2944 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 2945 | } |
| 2946 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2947 | errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err); |
| 2948 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2949 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 2950 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2951 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2952 | } |
| 2953 | #endif |
| 2954 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 2955 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 2956 | if (sctl_ex_index >= 0 && ckch->sctl) { |
| 2957 | if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) { |
| 2958 | 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] | 2959 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2960 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2961 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 2962 | } |
| 2963 | } |
| 2964 | #endif |
| 2965 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 2966 | #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] | 2967 | /* Load OCSP Info into context */ |
| 2968 | if (ckch->ocsp_response) { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 2969 | if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 2970 | 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", |
| 2971 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2972 | errcode |= ERR_ALERT | ERR_FATAL; |
| 2973 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 2974 | } |
| 2975 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 2976 | #endif |
| 2977 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 2978 | end: |
| 2979 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2980 | } |
| 2981 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 2982 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2983 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 2984 | 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] | 2985 | { |
| 2986 | struct sni_keytype *s_kt = NULL; |
| 2987 | struct ebmb_node *node; |
| 2988 | int i; |
| 2989 | |
| 2990 | for (i = 0; i < trash.size; i++) { |
| 2991 | if (!str[i]) |
| 2992 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2993 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2994 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2995 | trash.area[i] = 0; |
| 2996 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2997 | if (!node) { |
| 2998 | /* CN not found in tree */ |
| 2999 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3000 | /* Using memcpy here instead of strncpy. |
| 3001 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3002 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3003 | */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3004 | if (!s_kt) |
| 3005 | return -1; |
| 3006 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3007 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3008 | s_kt->keytypes = 0; |
| 3009 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3010 | } else { |
| 3011 | /* CN found in tree */ |
| 3012 | s_kt = container_of(node, struct sni_keytype, name); |
| 3013 | } |
| 3014 | |
| 3015 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3016 | s_kt->keytypes |= 1<<key_index; |
| 3017 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3018 | return 0; |
| 3019 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3020 | } |
| 3021 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3022 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3023 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3024 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3025 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3026 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3027 | * Take a ckch_store which contains a multi-certificate bundle. |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3028 | * Group these certificates into a set of SSL_CTX* |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3029 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 3030 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3031 | * 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] | 3032 | * |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3033 | * Returns a bitfield containing the flags: |
| 3034 | * ERR_FATAL in any fatal error case |
| 3035 | * ERR_ALERT if the reason of the error is available in err |
| 3036 | * ERR_WARN if a warning is available into err |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3037 | * |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3038 | */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 3039 | int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 3040 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3041 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3042 | { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3043 | int i = 0, n = 0; |
| 3044 | struct cert_key_and_chain *certs_and_keys; |
William Lallemand | 4b989f2 | 2019-10-04 18:36:55 +0200 | [diff] [blame] | 3045 | struct eb_root sni_keytypes_map = EB_ROOT; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3046 | struct ebmb_node *node; |
| 3047 | struct ebmb_node *next; |
| 3048 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 3049 | * of keytypes |
| 3050 | */ |
| 3051 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3052 | int errcode = 0; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3053 | X509_NAME *xname = NULL; |
| 3054 | char *str = NULL; |
| 3055 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3056 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 3057 | #endif |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3058 | struct ckch_inst *ckch_inst; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3059 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3060 | *ckchi = NULL; |
| 3061 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3062 | if (!ckchs || !ckchs->ckch || !ckchs->multi) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3063 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3064 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3065 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | ckch_inst = ckch_inst_new(); |
| 3069 | if (!ckch_inst) { |
| 3070 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3071 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3072 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3073 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3074 | } |
| 3075 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3076 | certs_and_keys = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3077 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3078 | /* Process each ckch and update keytypes for each CN/SAN |
| 3079 | * for example, if CN/SAN www.a.com is associated with |
| 3080 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3081 | * www.a.com will have: |
| 3082 | * keyindex = 0 | 1 | 4 = 5 |
| 3083 | */ |
| 3084 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3085 | int ret; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3086 | |
| 3087 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3088 | continue; |
| 3089 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3090 | if (fcount) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3091 | for (i = 0; i < fcount; i++) { |
| 3092 | ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3093 | if (ret < 0) { |
| 3094 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3095 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3096 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3097 | goto end; |
| 3098 | } |
| 3099 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3100 | } else { |
| 3101 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 3102 | * so the line that contains logic is marked via comments |
| 3103 | */ |
| 3104 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 3105 | i = -1; |
| 3106 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3107 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3108 | ASN1_STRING *value; |
| 3109 | value = X509_NAME_ENTRY_get_data(entry); |
| 3110 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3111 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3112 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3113 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3114 | OPENSSL_free(str); |
| 3115 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3116 | if (ret < 0) { |
| 3117 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3118 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3119 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3120 | goto end; |
| 3121 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3122 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3123 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3124 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3125 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3126 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3127 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 3128 | if (names) { |
| 3129 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3130 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3131 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3132 | if (name->type == GEN_DNS) { |
| 3133 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 3134 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3135 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3136 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3137 | OPENSSL_free(str); |
| 3138 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3139 | if (ret < 0) { |
| 3140 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3141 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3142 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3143 | goto end; |
| 3144 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3145 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3146 | } |
| 3147 | } |
| 3148 | } |
| 3149 | } |
| 3150 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3151 | } |
| 3152 | |
| 3153 | /* If no files found, return error */ |
| 3154 | if (eb_is_empty(&sni_keytypes_map)) { |
| 3155 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3156 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3157 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3158 | goto end; |
| 3159 | } |
| 3160 | |
| 3161 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 3162 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 3163 | * and add each CTX to the SNI tree |
| 3164 | * |
| 3165 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3166 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3167 | * combination is denoted by the key in the map. Each key |
| 3168 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 3169 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 3170 | * entry in the array to correspond to the unique combo (key) |
| 3171 | * associated with i. This unique key combo (i) will be associated |
| 3172 | * with combos[i-1] |
| 3173 | */ |
| 3174 | |
| 3175 | node = ebmb_first(&sni_keytypes_map); |
| 3176 | while (node) { |
| 3177 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3178 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3179 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3180 | |
| 3181 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 3182 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 3183 | cur_ctx = key_combos[i-1].ctx; |
| 3184 | |
| 3185 | if (cur_ctx == NULL) { |
| 3186 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3187 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3188 | if (cur_ctx == NULL) { |
| 3189 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3190 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3191 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3192 | goto end; |
| 3193 | } |
| 3194 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3195 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3196 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3197 | if (i & (1<<n)) { |
| 3198 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3199 | 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] | 3200 | errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err); |
| 3201 | if (errcode & ERR_CODE) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3202 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3203 | } |
| 3204 | } |
| 3205 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3206 | /* Update key_combos */ |
| 3207 | key_combos[i-1].ctx = cur_ctx; |
| 3208 | } |
| 3209 | |
| 3210 | /* Update SNI Tree */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3211 | |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3212 | 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] | 3213 | kinfo, str, key_combos[i-1].order); |
| 3214 | if (key_combos[i-1].order < 0) { |
| 3215 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3216 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3217 | goto end; |
| 3218 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3219 | node = ebmb_next(node); |
| 3220 | } |
| 3221 | |
| 3222 | |
| 3223 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3224 | if (!bind_conf->default_ctx) { |
| 3225 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3226 | if (key_combos[i].ctx) { |
| 3227 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3228 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3229 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3230 | SSL_CTX_up_ref(bind_conf->default_ctx); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3231 | break; |
| 3232 | } |
| 3233 | } |
| 3234 | } |
| 3235 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3236 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3237 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3238 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3239 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3240 | end: |
| 3241 | |
| 3242 | if (names) |
| 3243 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3244 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3245 | node = ebmb_first(&sni_keytypes_map); |
| 3246 | while (node) { |
| 3247 | next = ebmb_next(node); |
| 3248 | ebmb_delete(node); |
William Lallemand | 8ed5b96 | 2019-10-04 17:24:39 +0200 | [diff] [blame] | 3249 | free(ebmb_entry(node, struct sni_keytype, name)); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3250 | node = next; |
| 3251 | } |
| 3252 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3253 | /* we need to free the ctx since we incremented the refcount where it's used */ |
| 3254 | for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) { |
| 3255 | if (key_combos[i].ctx) |
| 3256 | SSL_CTX_free(key_combos[i].ctx); |
| 3257 | } |
| 3258 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3259 | if (errcode & ERR_CODE && ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3260 | if (ckch_inst->is_default) { |
| 3261 | SSL_CTX_free(bind_conf->default_ctx); |
| 3262 | bind_conf->default_ctx = NULL; |
| 3263 | } |
| 3264 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3265 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3266 | ckch_inst = NULL; |
William Lallemand | 0c6d12f | 2019-10-04 18:38:51 +0200 | [diff] [blame] | 3267 | } |
| 3268 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3269 | *ckchi = ckch_inst; |
| 3270 | return errcode; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3271 | } |
| 3272 | #else |
| 3273 | /* This is a dummy, that just logs an error and returns error */ |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 3274 | int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 3275 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3276 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3277 | { |
| 3278 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3279 | err && *err ? *err : "", path, strerror(errno)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3280 | return ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3281 | } |
| 3282 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3283 | #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] | 3284 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3285 | /* |
| 3286 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3287 | * |
| 3288 | * Returns a bitfield containing the flags: |
| 3289 | * ERR_FATAL in any fatal error case |
| 3290 | * ERR_ALERT if the reason of the error is available in err |
| 3291 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3292 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3293 | 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] | 3294 | 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] | 3295 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3296 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3297 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3298 | int order = 0; |
| 3299 | X509_NAME *xname; |
| 3300 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3301 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3302 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3303 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3304 | STACK_OF(GENERAL_NAME) *names; |
| 3305 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3306 | struct cert_key_and_chain *ckch; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3307 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3308 | int errcode = 0; |
| 3309 | |
| 3310 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 3311 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3312 | if (!ckchs || !ckchs->ckch) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3313 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3314 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3315 | ckch = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3316 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3317 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 3318 | if (!ctx) { |
| 3319 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3320 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3321 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3322 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3323 | } |
| 3324 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3325 | errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err); |
| 3326 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3327 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3328 | |
| 3329 | ckch_inst = ckch_inst_new(); |
| 3330 | if (!ckch_inst) { |
| 3331 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3332 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3333 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3334 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3335 | } |
| 3336 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3337 | pkey = X509_get_pubkey(ckch->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3338 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3339 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3340 | switch(EVP_PKEY_base_id(pkey)) { |
| 3341 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3342 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3343 | break; |
| 3344 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3345 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3346 | break; |
| 3347 | case EVP_PKEY_DSA: |
| 3348 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3349 | break; |
| 3350 | } |
| 3351 | EVP_PKEY_free(pkey); |
| 3352 | } |
| 3353 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3354 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3355 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3356 | 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] | 3357 | if (order < 0) { |
| 3358 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3359 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3360 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3361 | } |
| 3362 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3363 | } |
| 3364 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3365 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3366 | 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] | 3367 | if (names) { |
| 3368 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3369 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3370 | if (name->type == GEN_DNS) { |
| 3371 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3372 | 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] | 3373 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3374 | if (order < 0) { |
| 3375 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3376 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3377 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3378 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3379 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3380 | } |
| 3381 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3382 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3383 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3384 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3385 | xname = X509_get_subject_name(ckch->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3386 | i = -1; |
| 3387 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3388 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3389 | ASN1_STRING *value; |
| 3390 | |
| 3391 | value = X509_NAME_ENTRY_get_data(entry); |
| 3392 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3393 | 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] | 3394 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3395 | if (order < 0) { |
| 3396 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3397 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3398 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3399 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3400 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3401 | } |
| 3402 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3403 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3404 | * the tree, so it will be discovered and cleaned in time. |
| 3405 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3406 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3407 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3408 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3409 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3410 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3411 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3412 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3413 | } |
| 3414 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3415 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3416 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3417 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3418 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3419 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3420 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3421 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3422 | /* everything succeed, the ckch instance can be used */ |
| 3423 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3424 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3425 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3426 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3427 | SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */ |
| 3428 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3429 | *ckchi = ckch_inst; |
| 3430 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3431 | |
| 3432 | error: |
| 3433 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3434 | if (ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3435 | if (ckch_inst->is_default) |
| 3436 | SSL_CTX_free(ctx); |
| 3437 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3438 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3439 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3440 | } |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3441 | SSL_CTX_free(ctx); |
| 3442 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3443 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3444 | } |
| 3445 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 3446 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3447 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 3448 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3449 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3450 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3451 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3452 | |
| 3453 | /* we found the ckchs in the tree, we can use it directly */ |
| 3454 | if (ckchs->multi) |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3455 | 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] | 3456 | else |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3457 | 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] | 3458 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3459 | if (errcode & ERR_CODE) |
| 3460 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3461 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3462 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3463 | |
| 3464 | /* succeed, add the instance to the ckch_store's list of instance */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3465 | LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3466 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3467 | } |
| 3468 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3469 | |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 3470 | |
| 3471 | |
| 3472 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3473 | * done once. Zero is returned if the operation fails. No error is returned |
| 3474 | * if the random is said as not implemented, because we expect that openssl |
| 3475 | * will use another method once needed. |
| 3476 | */ |
| 3477 | static int ssl_initialize_random() |
| 3478 | { |
| 3479 | unsigned char random; |
| 3480 | static int random_initialized = 0; |
| 3481 | |
| 3482 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3483 | random_initialized = 1; |
| 3484 | |
| 3485 | return random_initialized; |
| 3486 | } |
| 3487 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3488 | /* Load a crt-list file, this is done in 2 parts: |
| 3489 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 3490 | * - generate the instances by iterating on entries in the crtlist struct |
| 3491 | * |
| 3492 | * Nothing is locked there, this function is used in the configuration parser. |
| 3493 | * |
| 3494 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 3495 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3496 | 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] | 3497 | { |
| 3498 | struct crtlist *crtlist = NULL; |
| 3499 | struct ebmb_node *eb; |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3500 | struct crtlist_entry *entry = NULL; |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3501 | struct bind_conf_list *bind_conf_node = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3502 | int cfgerr = 0; |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3503 | char *end; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3504 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3505 | bind_conf_node = malloc(sizeof(*bind_conf_node)); |
| 3506 | if (!bind_conf_node) { |
| 3507 | memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : ""); |
| 3508 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3509 | goto error; |
| 3510 | } |
| 3511 | bind_conf_node->next = NULL; |
| 3512 | bind_conf_node->bind_conf = bind_conf; |
| 3513 | |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3514 | /* strip trailing slashes, including first one */ |
| 3515 | for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--) |
| 3516 | *end = 0; |
| 3517 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3518 | /* look for an existing crtlist or create one */ |
| 3519 | eb = ebst_lookup(&crtlists_tree, file); |
| 3520 | if (eb) { |
| 3521 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 3522 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3523 | /* load a crt-list OR a directory */ |
| 3524 | if (dir) |
| 3525 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 3526 | else |
| 3527 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 3528 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3529 | if (!(cfgerr & ERR_CODE)) |
| 3530 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 3531 | } |
| 3532 | |
| 3533 | if (cfgerr & ERR_CODE) { |
| 3534 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3535 | goto error; |
| 3536 | } |
| 3537 | |
| 3538 | /* generates ckch instance from the crtlist_entry */ |
| 3539 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3540 | struct ckch_store *store; |
| 3541 | struct ckch_inst *ckch_inst = NULL; |
| 3542 | |
| 3543 | store = entry->node.key; |
| 3544 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 3545 | if (cfgerr & ERR_CODE) { |
| 3546 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 3547 | goto error; |
| 3548 | } |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3549 | LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry); |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 3550 | ckch_inst->crtlist_entry = entry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3551 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3552 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3553 | /* add the bind_conf to the list */ |
| 3554 | bind_conf_node->next = crtlist->bind_conf; |
| 3555 | crtlist->bind_conf = bind_conf_node; |
| 3556 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3557 | return cfgerr; |
| 3558 | error: |
| 3559 | { |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3560 | struct crtlist_entry *lastentry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3561 | struct ckch_inst *inst, *s_inst; |
| 3562 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3563 | lastentry = entry; /* which entry we tried to generate last */ |
| 3564 | if (lastentry) { |
| 3565 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3566 | if (entry == lastentry) /* last entry we tried to generate, no need to go further */ |
| 3567 | break; |
| 3568 | |
| 3569 | 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] | 3570 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3571 | /* this was not generated for this bind_conf, skip */ |
| 3572 | if (inst->bind_conf != bind_conf) |
| 3573 | continue; |
| 3574 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3575 | /* free the sni_ctx and instance */ |
| 3576 | ckch_inst_free(inst); |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3577 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3578 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3579 | } |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3580 | free(bind_conf_node); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3581 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3582 | return cfgerr; |
| 3583 | } |
| 3584 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3585 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 3586 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 3587 | { |
| 3588 | struct stat buf; |
| 3589 | char fp[MAXPATHLEN+1]; |
| 3590 | int cfgerr = 0; |
| 3591 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3592 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3593 | |
| 3594 | if ((ckchs = ckchs_lookup(path))) { |
| 3595 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3596 | 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] | 3597 | } |
| 3598 | if (stat(path, &buf) == 0) { |
| 3599 | if (S_ISDIR(buf.st_mode) == 0) { |
| 3600 | ckchs = ckchs_load_cert_file(path, 0, err); |
| 3601 | if (!ckchs) |
| 3602 | return ERR_ALERT | ERR_FATAL; |
| 3603 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3604 | 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] | 3605 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3606 | 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] | 3607 | } |
| 3608 | } else { |
| 3609 | /* stat failed, could be a bundle */ |
| 3610 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
| 3611 | /* try to load a bundle if it is permitted */ |
| 3612 | ckchs = ckchs_load_cert_file(path, 1, err); |
| 3613 | if (!ckchs) |
| 3614 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3615 | 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] | 3616 | } else { |
| 3617 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3618 | err && *err ? *err : "", fp, strerror(errno)); |
| 3619 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3620 | } |
| 3621 | } |
| 3622 | |
| 3623 | return cfgerr; |
| 3624 | } |
| 3625 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3626 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3627 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3628 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3629 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3630 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3631 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3632 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3633 | SSL_OP_NO_SSLv2 | |
| 3634 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3635 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3636 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3637 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 3638 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3639 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3640 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3641 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3642 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3643 | SSL_MODE_RELEASE_BUFFERS | |
| 3644 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3645 | 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] | 3646 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3647 | int flags = MC_SSL_O_ALL; |
| 3648 | int cfgerr = 0; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 3649 | const int default_min_ver = CONF_TLSV12; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3650 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3651 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3652 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3653 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3654 | 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] | 3655 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3656 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3657 | 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] | 3658 | else |
| 3659 | flags = conf_ssl_methods->flags; |
| 3660 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3661 | min = conf_ssl_methods->min; |
| 3662 | max = conf_ssl_methods->max; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 3663 | |
| 3664 | /* default minimum is TLSV12, */ |
| 3665 | if (!min) { |
| 3666 | if (!max || (max >= default_min_ver)) { |
| 3667 | min = default_min_ver; |
| 3668 | } else { |
| 3669 | 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). " |
| 3670 | "Setting the ssl-min-ver to %s. Use 'ssl-min-ver' to fix this.\n", |
| 3671 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, methodVersions[default_min_ver].name, methodVersions[max].name); |
| 3672 | min = max; |
| 3673 | } |
| 3674 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3675 | /* 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] | 3676 | if (min) |
| 3677 | flags |= (methodVersions[min].flag - 1); |
| 3678 | if (max) |
| 3679 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3680 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3681 | min = max = CONF_TLSV_NONE; |
| 3682 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3683 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3684 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3685 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3686 | if (min) { |
| 3687 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3688 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3689 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3690 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3691 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3692 | hole = 0; |
| 3693 | } |
| 3694 | max = i; |
| 3695 | } |
| 3696 | else { |
| 3697 | min = max = i; |
| 3698 | } |
| 3699 | } |
| 3700 | else { |
| 3701 | if (min) |
| 3702 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3703 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3704 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3705 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3706 | 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] | 3707 | cfgerr += 1; |
| 3708 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3709 | /* save real min/max in bind_conf */ |
| 3710 | conf_ssl_methods->min = min; |
| 3711 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3712 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3713 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3714 | /* 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] | 3715 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3716 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3717 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3718 | else |
| 3719 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3720 | if (flags & methodVersions[i].flag) |
| 3721 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3722 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3723 | /* 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] | 3724 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3725 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3726 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3727 | |
| 3728 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3729 | options |= SSL_OP_NO_TICKET; |
| 3730 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3731 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 3732 | |
| 3733 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 3734 | options |= SSL_OP_NO_RENEGOTIATION; |
| 3735 | #endif |
| 3736 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3737 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3738 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3739 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3740 | if (global_ssl.async) |
| 3741 | mode |= SSL_MODE_ASYNC; |
| 3742 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3743 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3744 | if (global_ssl.life_time) |
| 3745 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3746 | |
| 3747 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3748 | #ifdef OPENSSL_IS_BORINGSSL |
| 3749 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3750 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Ilya Shipitsin | e9ff899 | 2020-01-19 12:20:14 +0500 | [diff] [blame] | 3751 | #elif defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 3752 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 3753 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3754 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 3755 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3756 | #else |
| 3757 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3758 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3759 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3760 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3761 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3762 | } |
| 3763 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3764 | |
| 3765 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 3766 | { |
| 3767 | if (first == block) { |
| 3768 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3769 | if (first->len > 0) |
| 3770 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 3771 | } |
| 3772 | } |
| 3773 | |
| 3774 | /* return first block from sh_ssl_sess */ |
| 3775 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 3776 | { |
| 3777 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 3778 | |
| 3779 | } |
| 3780 | |
| 3781 | /* store a session into the cache |
| 3782 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 3783 | * data: asn1 encoded session |
| 3784 | * data_len: asn1 encoded session length |
| 3785 | * Returns 1 id session was stored (else 0) |
| 3786 | */ |
| 3787 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 3788 | { |
| 3789 | struct shared_block *first; |
| 3790 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 3791 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3792 | 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] | 3793 | if (!first) { |
| 3794 | /* Could not retrieve enough free blocks to store that session */ |
| 3795 | return 0; |
| 3796 | } |
| 3797 | |
| 3798 | /* STORE the key in the first elem */ |
| 3799 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3800 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 3801 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3802 | |
| 3803 | /* it returns the already existing node |
| 3804 | or current node if none, never returns null */ |
| 3805 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 3806 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 3807 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 3808 | /* release the reserved row */ |
| 3809 | shctx_row_dec_hot(ssl_shctx, first); |
| 3810 | /* replace the previous session already in the tree */ |
| 3811 | sh_ssl_sess = oldsh_ssl_sess; |
| 3812 | /* ignore the previous session data, only use the header */ |
| 3813 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 3814 | shctx_row_inc_hot(ssl_shctx, first); |
| 3815 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3816 | } |
| 3817 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3818 | 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] | 3819 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3820 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3824 | |
| 3825 | return 1; |
| 3826 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3827 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3828 | /* SSL callback used when a new session is created while connecting to a server */ |
| 3829 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 3830 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 3831 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3832 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3833 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 3834 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3835 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3836 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 3837 | int len; |
| 3838 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3839 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3840 | len = i2d_SSL_SESSION(sess, NULL); |
| 3841 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 3842 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 3843 | } else { |
| 3844 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3845 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 3846 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 3847 | } |
| 3848 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 3849 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 3850 | &ptr); |
| 3851 | } |
| 3852 | } else { |
| 3853 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3854 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 3855 | } |
| 3856 | |
| 3857 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3858 | } |
| 3859 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3860 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3861 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3862 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3863 | { |
| 3864 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 3865 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 3866 | unsigned char *p; |
| 3867 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3868 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3869 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3870 | |
| 3871 | /* 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] | 3872 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3873 | * note: SSL_SESSION_set1_id is using |
| 3874 | * a memcpy so we need to use a different pointer |
| 3875 | * than sid_data or sid_ctx_data to avoid valgrind |
| 3876 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3877 | */ |
| 3878 | |
| 3879 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3880 | |
| 3881 | /* copy value in an other buffer */ |
| 3882 | memcpy(encid, sid_data, sid_length); |
| 3883 | |
| 3884 | /* pad with 0 */ |
| 3885 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 3886 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 3887 | |
| 3888 | /* force length to zero to avoid ASN1 encoding */ |
| 3889 | SSL_SESSION_set1_id(sess, encid, 0); |
| 3890 | |
| 3891 | /* force length to zero to avoid ASN1 encoding */ |
| 3892 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3893 | |
| 3894 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 3895 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 3896 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 3897 | goto err; |
| 3898 | |
| 3899 | p = encsess; |
| 3900 | |
| 3901 | /* process ASN1 session encoding before the lock */ |
| 3902 | i2d_SSL_SESSION(sess, &p); |
| 3903 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3904 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3905 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3906 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3907 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3908 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3909 | err: |
| 3910 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3911 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 3912 | 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] | 3913 | |
| 3914 | return 0; /* do not increment session reference count */ |
| 3915 | } |
| 3916 | |
| 3917 | /* 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] | 3918 | 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] | 3919 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3920 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3921 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 3922 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3923 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3924 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3925 | |
| 3926 | global.shctx_lookups++; |
| 3927 | |
| 3928 | /* allow the session to be freed automatically by openssl */ |
| 3929 | *do_copy = 0; |
| 3930 | |
| 3931 | /* tree key is zeros padded sessionid */ |
| 3932 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3933 | memcpy(tmpkey, key, key_len); |
| 3934 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 3935 | key = tmpkey; |
| 3936 | } |
| 3937 | |
| 3938 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3939 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3940 | |
| 3941 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3942 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 3943 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3944 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3945 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3946 | global.shctx_misses++; |
| 3947 | return NULL; |
| 3948 | } |
| 3949 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3950 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 3951 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3952 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3953 | 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] | 3954 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3955 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3956 | |
| 3957 | /* decode ASN1 session */ |
| 3958 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3959 | 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] | 3960 | /* Reset session id and session id contenxt */ |
| 3961 | if (sess) { |
| 3962 | SSL_SESSION_set1_id(sess, key, key_len); |
| 3963 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 3964 | } |
| 3965 | |
| 3966 | return sess; |
| 3967 | } |
| 3968 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3969 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3970 | /* 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] | 3971 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3972 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3973 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3974 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 3975 | unsigned int sid_length; |
| 3976 | const unsigned char *sid_data; |
| 3977 | (void)ctx; |
| 3978 | |
| 3979 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 3980 | /* tree key is zeros padded sessionid */ |
| 3981 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3982 | memcpy(tmpkey, sid_data, sid_length); |
| 3983 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 3984 | sid_data = tmpkey; |
| 3985 | } |
| 3986 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3987 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3988 | |
| 3989 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3990 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 3991 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3992 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3993 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
| 3996 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3997 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3998 | } |
| 3999 | |
| 4000 | /* Set session cache mode to server and disable openssl internal cache. |
| 4001 | * Set shared cache callbacks on an ssl context. |
| 4002 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4003 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4004 | { |
| 4005 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4006 | |
| 4007 | if (!ssl_shctx) { |
| 4008 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4009 | return; |
| 4010 | } |
| 4011 | |
| 4012 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4013 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4014 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4015 | |
| 4016 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4017 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4018 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4019 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4020 | } |
| 4021 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4022 | /* |
| 4023 | * This function applies the SSL configuration on a SSL_CTX |
| 4024 | * It returns an error code and fills the <err> buffer |
| 4025 | */ |
| 4026 | 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] | 4027 | { |
| 4028 | struct proxy *curproxy = bind_conf->frontend; |
| 4029 | int cfgerr = 0; |
| 4030 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4031 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4032 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4033 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4034 | const char *conf_ciphersuites; |
| 4035 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4036 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4037 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4038 | if (ssl_conf) { |
| 4039 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4040 | int i, min, max; |
| 4041 | int flags = MC_SSL_O_ALL; |
| 4042 | |
| 4043 | /* 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] | 4044 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4045 | 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] | 4046 | if (min) |
| 4047 | flags |= (methodVersions[min].flag - 1); |
| 4048 | if (max) |
| 4049 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4050 | min = max = CONF_TLSV_NONE; |
| 4051 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4052 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4053 | if (min) |
| 4054 | max = i; |
| 4055 | else |
| 4056 | min = max = i; |
| 4057 | } |
| 4058 | /* save real min/max */ |
| 4059 | conf_ssl_methods->min = min; |
| 4060 | conf_ssl_methods->max = max; |
| 4061 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4062 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4063 | 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] | 4064 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4065 | } |
| 4066 | } |
| 4067 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4068 | 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] | 4069 | case SSL_SOCK_VERIFY_NONE: |
| 4070 | verify = SSL_VERIFY_NONE; |
| 4071 | break; |
| 4072 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4073 | verify = SSL_VERIFY_PEER; |
| 4074 | break; |
| 4075 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4076 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4077 | break; |
| 4078 | } |
| 4079 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4080 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4081 | 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] | 4082 | 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] | 4083 | 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] | 4084 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4085 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4086 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4087 | 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] | 4088 | 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] | 4089 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4090 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4091 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 4092 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 4093 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4094 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4095 | } |
| 4096 | 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] | 4097 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 4098 | 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] | 4099 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4100 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4101 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4102 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4103 | 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] | 4104 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4105 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4106 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4107 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4108 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4109 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 4110 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4111 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4112 | 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] | 4113 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4114 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4115 | else { |
| 4116 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4117 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4118 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4119 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4120 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4121 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4122 | #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] | 4123 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4124 | 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] | 4125 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4126 | 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] | 4127 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4128 | } |
| 4129 | } |
| 4130 | #endif |
| 4131 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4132 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4133 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4134 | if (conf_ciphers && |
| 4135 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4136 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4137 | 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] | 4138 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4139 | } |
| 4140 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4141 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4142 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4143 | if (conf_ciphersuites && |
| 4144 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4145 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4146 | 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] | 4147 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4148 | } |
| 4149 | #endif |
| 4150 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4151 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4152 | /* If tune.ssl.default-dh-param has not been set, |
| 4153 | neither has ssl-default-dh-file and no static DH |
| 4154 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4155 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4156 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4157 | (ssl_dh_ptr_index == -1 || |
| 4158 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Willy Tarreau | 3ba77d2 | 2020-05-08 09:31:18 +0200 | [diff] [blame] | 4159 | /* default to dh-param 2048 */ |
| 4160 | global_ssl.default_dh_param = 2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4161 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4162 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4163 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4164 | if (local_dh_1024 == NULL) { |
| 4165 | local_dh_1024 = ssl_get_dh_1024(); |
| 4166 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4167 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4168 | if (local_dh_2048 == NULL) { |
| 4169 | local_dh_2048 = ssl_get_dh_2048(); |
| 4170 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4171 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4172 | if (local_dh_4096 == NULL) { |
| 4173 | local_dh_4096 = ssl_get_dh_4096(); |
| 4174 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4175 | } |
| 4176 | } |
| 4177 | } |
| 4178 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4179 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4180 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4181 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4182 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4183 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4184 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4185 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4186 | ssl_conf_cur = NULL; |
| 4187 | if (ssl_conf && ssl_conf->npn_str) |
| 4188 | ssl_conf_cur = ssl_conf; |
| 4189 | else if (bind_conf->ssl_conf.npn_str) |
| 4190 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4191 | if (ssl_conf_cur) |
| 4192 | 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] | 4193 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4194 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4195 | ssl_conf_cur = NULL; |
| 4196 | if (ssl_conf && ssl_conf->alpn_str) |
| 4197 | ssl_conf_cur = ssl_conf; |
| 4198 | else if (bind_conf->ssl_conf.alpn_str) |
| 4199 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4200 | if (ssl_conf_cur) |
| 4201 | 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] | 4202 | #endif |
Lukas Tribus | d14b49c | 2019-11-24 18:20:40 +0100 | [diff] [blame] | 4203 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER)) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4204 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4205 | if (conf_curves) { |
| 4206 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4207 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4208 | 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] | 4209 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4210 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4211 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4212 | } |
| 4213 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4214 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4215 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4216 | int i; |
| 4217 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4218 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4219 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4220 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4221 | NULL); |
| 4222 | |
| 4223 | if (ecdhe == NULL) { |
Eric Salama | 3c8bde8 | 2019-11-20 11:33:40 +0100 | [diff] [blame] | 4224 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4225 | return cfgerr; |
| 4226 | } |
| 4227 | #else |
| 4228 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4229 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4230 | ECDHE_DEFAULT_CURVE); |
| 4231 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4232 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4233 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4234 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4235 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4236 | 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] | 4237 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4238 | } |
| 4239 | else { |
| 4240 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4241 | EC_KEY_free(ecdh); |
| 4242 | } |
| 4243 | } |
| 4244 | #endif |
| 4245 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4246 | return cfgerr; |
| 4247 | } |
| 4248 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4249 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4250 | { |
| 4251 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4252 | size_t prefixlen, suffixlen; |
| 4253 | |
| 4254 | /* Trivial case */ |
| 4255 | if (strcmp(pattern, hostname) == 0) |
| 4256 | return 1; |
| 4257 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4258 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4259 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4260 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4261 | pattern_wildcard = NULL; |
| 4262 | pattern_left_label_end = pattern; |
| 4263 | while (*pattern_left_label_end != '.') { |
| 4264 | switch (*pattern_left_label_end) { |
| 4265 | case 0: |
| 4266 | /* End of label not found */ |
| 4267 | return 0; |
| 4268 | case '*': |
| 4269 | /* If there is more than one wildcards */ |
| 4270 | if (pattern_wildcard) |
| 4271 | return 0; |
| 4272 | pattern_wildcard = pattern_left_label_end; |
| 4273 | break; |
| 4274 | } |
| 4275 | pattern_left_label_end++; |
| 4276 | } |
| 4277 | |
| 4278 | /* If it's not trivial and there is no wildcard, it can't |
| 4279 | * match */ |
| 4280 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4281 | return 0; |
| 4282 | |
| 4283 | /* Make sure all labels match except the leftmost */ |
| 4284 | hostname_left_label_end = strchr(hostname, '.'); |
| 4285 | if (!hostname_left_label_end |
| 4286 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 4287 | return 0; |
| 4288 | |
| 4289 | /* Make sure the leftmost label of the hostname is long enough |
| 4290 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4291 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4292 | return 0; |
| 4293 | |
| 4294 | /* Finally compare the string on either side of the |
| 4295 | * wildcard */ |
| 4296 | prefixlen = pattern_wildcard - pattern; |
| 4297 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4298 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 4299 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4300 | return 0; |
| 4301 | |
| 4302 | return 1; |
| 4303 | } |
| 4304 | |
| 4305 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4306 | { |
| 4307 | SSL *ssl; |
| 4308 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4309 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4310 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4311 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4312 | |
| 4313 | int depth; |
| 4314 | X509 *cert; |
| 4315 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4316 | int i; |
| 4317 | X509_NAME *cert_subject; |
| 4318 | char *str; |
| 4319 | |
| 4320 | if (ok == 0) |
| 4321 | return ok; |
| 4322 | |
| 4323 | 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] | 4324 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4325 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4326 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4327 | /* We're checking if the provided hostnames match the desired one. The |
| 4328 | * desired hostname comes from the SNI we presented if any, or if not |
| 4329 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4330 | * directive. If neither is set, we don't care about the name so the |
| 4331 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4332 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4333 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4334 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4335 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4336 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4337 | if (!servername) |
| 4338 | return ok; |
| 4339 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4340 | |
| 4341 | /* We only need to verify the CN on the actual server cert, |
| 4342 | * not the indirect CAs */ |
| 4343 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4344 | if (depth != 0) |
| 4345 | return ok; |
| 4346 | |
| 4347 | /* At this point, the cert is *not* OK unless we can find a |
| 4348 | * hostname match */ |
| 4349 | ok = 0; |
| 4350 | |
| 4351 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4352 | /* It seems like this might happen if verify peer isn't set */ |
| 4353 | if (!cert) |
| 4354 | return ok; |
| 4355 | |
| 4356 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4357 | if (alt_names) { |
| 4358 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4359 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4360 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4361 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4362 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4363 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4364 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4365 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4366 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4367 | OPENSSL_free(str); |
| 4368 | } |
| 4369 | } |
| 4370 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4371 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4372 | } |
| 4373 | |
| 4374 | cert_subject = X509_get_subject_name(cert); |
| 4375 | i = -1; |
| 4376 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4377 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4378 | ASN1_STRING *value; |
| 4379 | value = X509_NAME_ENTRY_get_data(entry); |
| 4380 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4381 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4382 | OPENSSL_free(str); |
| 4383 | } |
| 4384 | } |
| 4385 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4386 | /* report the mismatch and indicate if SNI was used or not */ |
| 4387 | if (!ok && !conn->err_code) |
| 4388 | 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] | 4389 | return ok; |
| 4390 | } |
| 4391 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4392 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4393 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4394 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4395 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4396 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4397 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4398 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4399 | SSL_OP_NO_SSLv2 | |
| 4400 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4401 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4402 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4403 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4404 | SSL_MODE_RELEASE_BUFFERS | |
| 4405 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4406 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4407 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4408 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4409 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4410 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4411 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4412 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4413 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4414 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4415 | cfgerr++; |
| 4416 | } |
| 4417 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4418 | /* Automatic memory computations need to know we use SSL there */ |
| 4419 | global.ssl_used_backend = 1; |
| 4420 | |
| 4421 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4422 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4423 | 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] | 4424 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 4425 | curproxy->id, srv->id, |
| 4426 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4427 | cfgerr++; |
| 4428 | return cfgerr; |
| 4429 | } |
| 4430 | } |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 4431 | if (srv->use_ssl == 1) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4432 | srv->xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4433 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4434 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4435 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4436 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4437 | proxy_type_str(curproxy), curproxy->id, |
| 4438 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4439 | cfgerr++; |
| 4440 | return cfgerr; |
| 4441 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4442 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4443 | 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] | 4444 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4445 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4446 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4447 | else |
| 4448 | flags = conf_ssl_methods->flags; |
| 4449 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4450 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4451 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4452 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4453 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4454 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4455 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4456 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4457 | min = max = CONF_TLSV_NONE; |
| 4458 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4459 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4460 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4461 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4462 | if (min) { |
| 4463 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4464 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 4465 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4466 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4467 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4468 | hole = 0; |
| 4469 | } |
| 4470 | max = i; |
| 4471 | } |
| 4472 | else { |
| 4473 | min = max = i; |
| 4474 | } |
| 4475 | } |
| 4476 | else { |
| 4477 | if (min) |
| 4478 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4479 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4480 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4481 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4482 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4483 | cfgerr += 1; |
| 4484 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4485 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4486 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4487 | /* 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] | 4488 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4489 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4490 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4491 | else |
| 4492 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4493 | if (flags & methodVersions[i].flag) |
| 4494 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4495 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4496 | /* 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] | 4497 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4498 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4499 | #endif |
| 4500 | |
| 4501 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4502 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4503 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4504 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4505 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4506 | if (global_ssl.async) |
| 4507 | mode |= SSL_MODE_ASYNC; |
| 4508 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4509 | SSL_CTX_set_mode(ctx, mode); |
| 4510 | srv->ssl_ctx.ctx = ctx; |
| 4511 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4512 | if (srv->ssl_ctx.client_crt) { |
| 4513 | 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] | 4514 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4515 | proxy_type_str(curproxy), curproxy->id, |
| 4516 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4517 | cfgerr++; |
| 4518 | } |
| 4519 | 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] | 4520 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4521 | proxy_type_str(curproxy), curproxy->id, |
| 4522 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4523 | cfgerr++; |
| 4524 | } |
| 4525 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4526 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4527 | proxy_type_str(curproxy), curproxy->id, |
| 4528 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4529 | cfgerr++; |
| 4530 | } |
| 4531 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4532 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4533 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4534 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4535 | switch (srv->ssl_ctx.verify) { |
| 4536 | case SSL_SOCK_VERIFY_NONE: |
| 4537 | verify = SSL_VERIFY_NONE; |
| 4538 | break; |
| 4539 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4540 | verify = SSL_VERIFY_PEER; |
| 4541 | break; |
| 4542 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4543 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4544 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4545 | (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] | 4546 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4547 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4548 | /* set CAfile to verify */ |
| 4549 | if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) { |
| 4550 | 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] | 4551 | curproxy->id, srv->id, |
| 4552 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4553 | cfgerr++; |
| 4554 | } |
| 4555 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4556 | else { |
| 4557 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4558 | 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", |
| 4559 | curproxy->id, srv->id, |
| 4560 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4561 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4562 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 4563 | curproxy->id, srv->id, |
| 4564 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4565 | cfgerr++; |
| 4566 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4567 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4568 | if (srv->ssl_ctx.crl_file) { |
| 4569 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4570 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 4571 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4572 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 4573 | curproxy->id, srv->id, |
| 4574 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4575 | cfgerr++; |
| 4576 | } |
| 4577 | else { |
| 4578 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4579 | } |
| 4580 | } |
| 4581 | #endif |
| 4582 | } |
| 4583 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4584 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 4585 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 4586 | 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] | 4587 | if (srv->ssl_ctx.ciphers && |
| 4588 | !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] | 4589 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4590 | curproxy->id, srv->id, |
| 4591 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4592 | cfgerr++; |
| 4593 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4594 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4595 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4596 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 4597 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4598 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 4599 | curproxy->id, srv->id, |
| 4600 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 4601 | cfgerr++; |
| 4602 | } |
| 4603 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4604 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 4605 | if (srv->ssl_ctx.npn_str) |
| 4606 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 4607 | #endif |
| 4608 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4609 | if (srv->ssl_ctx.alpn_str) |
| 4610 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 4611 | #endif |
| 4612 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4613 | |
| 4614 | return cfgerr; |
| 4615 | } |
| 4616 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4617 | /* 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] | 4618 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4619 | * encountered. |
| 4620 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4621 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4622 | { |
| 4623 | struct ebmb_node *node; |
| 4624 | struct sni_ctx *sni; |
| 4625 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4626 | int errcode = 0; |
| 4627 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4628 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4629 | /* Automatic memory computations need to know we use SSL there */ |
| 4630 | global.ssl_used_frontend = 1; |
| 4631 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4632 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4633 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4634 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4635 | err++; |
| 4636 | } |
| 4637 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4638 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4639 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4640 | /* It should not be necessary to call this function, but it's |
| 4641 | necessary first to check and move all initialisation related |
| 4642 | to initial_ctx in ssl_sock_initial_ctx. */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4643 | 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] | 4644 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4645 | if (bind_conf->default_ctx) |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4646 | 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] | 4647 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4648 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4649 | while (node) { |
| 4650 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4651 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4652 | /* only initialize the CTX on its first occurrence and |
| 4653 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4654 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4655 | node = ebmb_next(node); |
| 4656 | } |
| 4657 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4658 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4659 | while (node) { |
| 4660 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4661 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4662 | /* only initialize the CTX on its first occurrence and |
| 4663 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4664 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
| 4665 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4666 | node = ebmb_next(node); |
| 4667 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4668 | |
| 4669 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 4670 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4671 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 4672 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4673 | err++; |
| 4674 | } |
| 4675 | |
| 4676 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4677 | return err; |
| 4678 | } |
| 4679 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4680 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4681 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4682 | * alerts are directly emitted since the rest of the stack does it below. |
| 4683 | */ |
| 4684 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4685 | { |
| 4686 | struct proxy *px = bind_conf->frontend; |
| 4687 | int alloc_ctx; |
| 4688 | int err; |
| 4689 | |
| 4690 | if (!bind_conf->is_ssl) { |
| 4691 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4692 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4693 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4694 | } |
| 4695 | return 0; |
| 4696 | } |
| 4697 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4698 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4699 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 4700 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4701 | } |
| 4702 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4703 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4704 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4705 | return -1; |
| 4706 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4707 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 4708 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4709 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 4710 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4711 | sizeof(*sh_ssl_sess_tree), |
| 4712 | ((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] | 4713 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4714 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4715 | 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"); |
| 4716 | else |
| 4717 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 4718 | return -1; |
| 4719 | } |
| 4720 | /* free block callback */ |
| 4721 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 4722 | /* init the root tree within the extra space */ |
| 4723 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 4724 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4725 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4726 | err = 0; |
| 4727 | /* initialize all certificate contexts */ |
| 4728 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4729 | |
| 4730 | /* initialize CA variables if the certificates generation is enabled */ |
| 4731 | err += ssl_sock_load_ca(bind_conf); |
| 4732 | |
| 4733 | return -err; |
| 4734 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4735 | |
| 4736 | /* release ssl context allocated for servers. */ |
| 4737 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4738 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4739 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4740 | if (srv->ssl_ctx.alpn_str) |
| 4741 | free(srv->ssl_ctx.alpn_str); |
| 4742 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 4743 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4744 | if (srv->ssl_ctx.npn_str) |
| 4745 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 4746 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4747 | if (srv->ssl_ctx.ctx) |
| 4748 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4749 | } |
| 4750 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4751 | /* 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] | 4752 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4753 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4754 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4755 | { |
| 4756 | struct ebmb_node *node, *back; |
| 4757 | struct sni_ctx *sni; |
| 4758 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4759 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4760 | while (node) { |
| 4761 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4762 | back = ebmb_next(node); |
| 4763 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4764 | SSL_CTX_free(sni->ctx); |
| 4765 | if (!sni->order) { /* only free the CTX conf on its first occurrence */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4766 | ssl_sock_free_ssl_conf(sni->conf); |
| 4767 | free(sni->conf); |
| 4768 | sni->conf = NULL; |
| 4769 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4770 | free(sni); |
| 4771 | node = back; |
| 4772 | } |
| 4773 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4774 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4775 | while (node) { |
| 4776 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4777 | back = ebmb_next(node); |
| 4778 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4779 | SSL_CTX_free(sni->ctx); |
| 4780 | if (!sni->order) { /* only free the SSL conf its first occurrence */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4781 | ssl_sock_free_ssl_conf(sni->conf); |
| 4782 | free(sni->conf); |
| 4783 | sni->conf = NULL; |
| 4784 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4785 | free(sni); |
| 4786 | node = back; |
| 4787 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4788 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4789 | bind_conf->initial_ctx = NULL; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4790 | SSL_CTX_free(bind_conf->default_ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4791 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4792 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4793 | } |
| 4794 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4795 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4796 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4797 | { |
| 4798 | ssl_sock_free_ca(bind_conf); |
| 4799 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4800 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4801 | free(bind_conf->ca_sign_file); |
| 4802 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 4803 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4804 | free(bind_conf->keys_ref->filename); |
| 4805 | free(bind_conf->keys_ref->tlskeys); |
| 4806 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4807 | free(bind_conf->keys_ref); |
| 4808 | } |
| 4809 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4810 | bind_conf->ca_sign_pass = NULL; |
| 4811 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4812 | } |
| 4813 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4814 | /* Load CA cert file and private key used to generate certificates */ |
| 4815 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4816 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4817 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4818 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4819 | FILE *fp; |
| 4820 | X509 *cacert = NULL; |
| 4821 | EVP_PKEY *capkey = NULL; |
| 4822 | int err = 0; |
| 4823 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 4824 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4825 | return err; |
| 4826 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4827 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4828 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4829 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4830 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4831 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4832 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 4833 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4834 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4835 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4836 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 4837 | "no CA certificate File configured at [%s:%d].\n", |
| 4838 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4839 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4840 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4841 | |
| 4842 | /* read in the CA certificate */ |
| 4843 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4844 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4845 | 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] | 4846 | goto load_error; |
| 4847 | } |
| 4848 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4849 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 4850 | 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] | 4851 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4852 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4853 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4854 | 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] | 4855 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 4856 | 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] | 4857 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4858 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4859 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4860 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4861 | bind_conf->ca_sign_cert = cacert; |
| 4862 | bind_conf->ca_sign_pkey = capkey; |
| 4863 | return err; |
| 4864 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4865 | read_error: |
| 4866 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4867 | if (capkey) EVP_PKEY_free(capkey); |
| 4868 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4869 | load_error: |
| 4870 | bind_conf->generate_certs = 0; |
| 4871 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4872 | return err; |
| 4873 | } |
| 4874 | |
| 4875 | /* Release CA cert and private key used to generate certificated */ |
| 4876 | void |
| 4877 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 4878 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4879 | if (bind_conf->ca_sign_pkey) |
| 4880 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 4881 | if (bind_conf->ca_sign_cert) |
| 4882 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 4883 | bind_conf->ca_sign_pkey = NULL; |
| 4884 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4885 | } |
| 4886 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4887 | /* |
| 4888 | * This function is called if SSL * context is not yet allocated. The function |
| 4889 | * is designed to be called before any other data-layer operation and sets the |
| 4890 | * handshake flag on the connection. It is safe to call it multiple times. |
| 4891 | * It returns 0 on success and -1 in error case. |
| 4892 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 4893 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4894 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4895 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4896 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 4897 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4898 | return 0; |
| 4899 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4900 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4901 | return 0; |
| 4902 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4903 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 4904 | if (!ctx) { |
| 4905 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4906 | return -1; |
| 4907 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 4908 | ctx->wait_event.tasklet = tasklet_new(); |
| 4909 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4910 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4911 | pool_free(ssl_sock_ctx_pool, ctx); |
| 4912 | return -1; |
| 4913 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 4914 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 4915 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4916 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 4917 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 4918 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4919 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 4920 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 4921 | ctx->xprt_st = 0; |
| 4922 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4923 | |
| 4924 | /* Only work with sockets for now, this should be adapted when we'll |
| 4925 | * add QUIC support. |
| 4926 | */ |
| 4927 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 4928 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4929 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 4930 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 4931 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4932 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4933 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 4934 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4935 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4936 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4937 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4938 | /* If it is in client mode initiate SSL session |
| 4939 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4940 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4941 | int may_retry = 1; |
| 4942 | |
| 4943 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4944 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4945 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 4946 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4947 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4948 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4949 | goto retry_connect; |
| 4950 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4951 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4952 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4953 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4954 | ctx->bio = BIO_new(ha_meth); |
| 4955 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 4956 | SSL_free(ctx->ssl); |
| 4957 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4958 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4959 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4960 | goto retry_connect; |
| 4961 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4962 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4963 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4964 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4965 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4966 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4967 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4968 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4969 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 4970 | SSL_free(ctx->ssl); |
| 4971 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4972 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4973 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4974 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4975 | goto retry_connect; |
| 4976 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4977 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4978 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4979 | } |
| 4980 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4981 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4982 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 4983 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 4984 | 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] | 4985 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4986 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4987 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 4988 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4989 | } else if (sess) { |
| 4990 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4991 | } |
| 4992 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4993 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4994 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4995 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4996 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 4997 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 4998 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 4999 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5000 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5001 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5002 | return 0; |
| 5003 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5004 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5005 | int may_retry = 1; |
| 5006 | |
| 5007 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5008 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5009 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 5010 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5011 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5012 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5013 | goto retry_accept; |
| 5014 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5015 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5016 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5017 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5018 | ctx->bio = BIO_new(ha_meth); |
| 5019 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 5020 | SSL_free(ctx->ssl); |
| 5021 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5022 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5023 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5024 | goto retry_accept; |
| 5025 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5026 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5027 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5028 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5029 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5030 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5031 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5032 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5033 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5034 | SSL_free(ctx->ssl); |
| 5035 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5036 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5037 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5038 | goto retry_accept; |
| 5039 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5040 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5041 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5042 | } |
| 5043 | |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 5044 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5045 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) { |
| 5046 | b_alloc(&ctx->early_buf); |
| 5047 | SSL_set_max_early_data(ctx->ssl, |
| 5048 | /* Only allow early data if we managed to allocate |
| 5049 | * a buffer. |
| 5050 | */ |
| 5051 | (!b_is_null(&ctx->early_buf)) ? |
| 5052 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 5053 | } |
| 5054 | #endif |
| 5055 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5056 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5057 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5058 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5059 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5060 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5061 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 5062 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5063 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5064 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5065 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5066 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5067 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5068 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5069 | return 0; |
| 5070 | } |
| 5071 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5072 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5073 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5074 | if (ctx && ctx->wait_event.tasklet) |
| 5075 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5076 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5077 | return -1; |
| 5078 | } |
| 5079 | |
| 5080 | |
| 5081 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5082 | * updates the FD status if it wants some polling before being called again. |
| 5083 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5084 | * otherwise it returns non-zero and removes itself from the connection's |
| 5085 | * flags (the bit is provided in <flag> by the caller). |
| 5086 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 5087 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5088 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5089 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5090 | int ret; |
| 5091 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5092 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5093 | return 0; |
| 5094 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5095 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5096 | goto out_error; |
| 5097 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5098 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5099 | /* |
| 5100 | * Check if we have early data. If we do, we have to read them |
| 5101 | * before SSL_do_handshake() is called, And there's no way to |
| 5102 | * detect early data, except to try to read them |
| 5103 | */ |
| 5104 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5105 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5106 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5107 | while (1) { |
| 5108 | ret = SSL_read_early_data(ctx->ssl, |
| 5109 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 5110 | &read_data); |
| 5111 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5112 | goto check_error; |
| 5113 | if (read_data > 0) { |
| 5114 | conn->flags |= CO_FL_EARLY_DATA; |
| 5115 | b_add(&ctx->early_buf, read_data); |
| 5116 | } |
| 5117 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5118 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5119 | if (!b_data(&ctx->early_buf)) |
| 5120 | b_free(&ctx->early_buf); |
| 5121 | break; |
| 5122 | } |
| 5123 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5124 | } |
| 5125 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5126 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5127 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5128 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5129 | * the reneg handshake. |
| 5130 | * Here we use SSL_peek as a workaround for reneg. |
| 5131 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5132 | 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] | 5133 | char c; |
| 5134 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5135 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5136 | if (ret <= 0) { |
| 5137 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5138 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5139 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5140 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5141 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5142 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5143 | 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] | 5144 | return 0; |
| 5145 | } |
| 5146 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5147 | /* handshake may have been completed but we have |
| 5148 | * no more data to read. |
| 5149 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5150 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5151 | ret = 1; |
| 5152 | goto reneg_ok; |
| 5153 | } |
| 5154 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5155 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5156 | 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] | 5157 | return 0; |
| 5158 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5159 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5160 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5161 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5162 | return 0; |
| 5163 | } |
| 5164 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5165 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5166 | /* if errno is null, then connection was successfully established */ |
| 5167 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5168 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5169 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5170 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5171 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5172 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5173 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5174 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5175 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5176 | /* 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] | 5177 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5178 | empty_handshake = state == TLS_ST_BEFORE; |
| 5179 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5180 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5181 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5182 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5183 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5184 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5185 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5186 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5187 | else |
| 5188 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5189 | } |
| 5190 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5191 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5192 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5193 | else |
| 5194 | conn->err_code = CO_ER_SSL_ABORT; |
| 5195 | } |
| 5196 | } |
| 5197 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5198 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5199 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5200 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5201 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5202 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5203 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5204 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5205 | goto out_error; |
| 5206 | } |
| 5207 | else { |
| 5208 | /* Fail on all other handshake errors */ |
| 5209 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5210 | * buffer, causing an RST to be emitted upon close() on |
| 5211 | * TCP sockets. We first try to drain possibly pending |
| 5212 | * data to avoid this as much as possible. |
| 5213 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5214 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5215 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5216 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5217 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5218 | goto out_error; |
| 5219 | } |
| 5220 | } |
| 5221 | /* read some data: consider handshake completed */ |
| 5222 | goto reneg_ok; |
| 5223 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5224 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5225 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5226 | if (ret != 1) { |
| 5227 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5228 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5229 | |
| 5230 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5231 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5232 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5233 | 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] | 5234 | return 0; |
| 5235 | } |
| 5236 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5237 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5238 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5239 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5240 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5241 | return 0; |
| 5242 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5243 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5244 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5245 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5246 | return 0; |
| 5247 | } |
| 5248 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5249 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5250 | /* if errno is null, then connection was successfully established */ |
| 5251 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5252 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5253 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5254 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5255 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5256 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5257 | #else |
| 5258 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5259 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5260 | /* 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] | 5261 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5262 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5263 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5264 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5265 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5266 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5267 | if (empty_handshake) { |
| 5268 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5269 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5270 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5271 | else |
| 5272 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5273 | } |
| 5274 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5275 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5276 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5277 | else |
| 5278 | conn->err_code = CO_ER_SSL_ABORT; |
| 5279 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5280 | } |
| 5281 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5282 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5283 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5284 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5285 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5286 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5287 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5288 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5289 | goto out_error; |
| 5290 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5291 | else { |
| 5292 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5293 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5294 | * buffer, causing an RST to be emitted upon close() on |
| 5295 | * TCP sockets. We first try to drain possibly pending |
| 5296 | * data to avoid this as much as possible. |
| 5297 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5298 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5299 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5300 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5301 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5302 | goto out_error; |
| 5303 | } |
| 5304 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5305 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5306 | else { |
| 5307 | /* |
| 5308 | * If the server refused the early data, we have to send a |
| 5309 | * 425 to the client, as we no longer have the data to sent |
| 5310 | * them again. |
| 5311 | */ |
| 5312 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5313 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5314 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5315 | goto out_error; |
| 5316 | } |
| 5317 | } |
| 5318 | } |
| 5319 | #endif |
| 5320 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5321 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5322 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5323 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5324 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5325 | /* ASYNC engine API doesn't support moving read/write |
| 5326 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5327 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5328 | */ |
| 5329 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5330 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5331 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5332 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5333 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5334 | if (objt_server(conn->target)) { |
| 5335 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5336 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5337 | 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] | 5338 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5339 | else { |
| 5340 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5341 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5342 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5343 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | /* The connection is now established at both layers, it's time to leave */ |
| 5347 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5348 | return 1; |
| 5349 | |
| 5350 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5351 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5352 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5353 | ERR_clear_error(); |
| 5354 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5355 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5356 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5357 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5358 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5359 | } |
| 5360 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5361 | /* Fail on all other handshake errors */ |
| 5362 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5363 | if (!conn->err_code) |
| 5364 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5365 | return 0; |
| 5366 | } |
| 5367 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5368 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5369 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5370 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5371 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 5372 | * unless the transport layer was already released. |
| 5373 | */ |
| 5374 | 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] | 5375 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5376 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5377 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 5378 | if (!ctx) |
| 5379 | return -1; |
| 5380 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5381 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5382 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5383 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5384 | ctx->subs = es; |
| 5385 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5386 | |
| 5387 | /* we may have to subscribe to lower layers for new events */ |
| 5388 | event_type &= ~ctx->wait_event.events; |
| 5389 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 5390 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5391 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5392 | } |
| 5393 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5394 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5395 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5396 | * subscribe() call. It always returns zero. |
| 5397 | */ |
| 5398 | 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] | 5399 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5400 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5401 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5402 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5403 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5404 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5405 | es->events &= ~event_type; |
| 5406 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5407 | ctx->subs = NULL; |
| 5408 | |
| 5409 | /* If we subscribed, and we're not doing the handshake, |
| 5410 | * then we subscribed because the upper layer asked for it, |
| 5411 | * as the upper layer is no longer interested, we can |
| 5412 | * unsubscribe too. |
| 5413 | */ |
| 5414 | event_type &= ctx->wait_event.events; |
| 5415 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 5416 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5417 | |
| 5418 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5419 | } |
| 5420 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 5421 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 5422 | * Returns 0 on success, and non-zero on failure. |
| 5423 | */ |
| 5424 | 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) |
| 5425 | { |
| 5426 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5427 | |
| 5428 | if (oldxprt_ops != NULL) |
| 5429 | *oldxprt_ops = ctx->xprt; |
| 5430 | if (oldxprt_ctx != NULL) |
| 5431 | *oldxprt_ctx = ctx->xprt_ctx; |
| 5432 | ctx->xprt = toadd_ops; |
| 5433 | ctx->xprt_ctx = toadd_ctx; |
| 5434 | return 0; |
| 5435 | } |
| 5436 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 5437 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 5438 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 5439 | * XPRT. |
| 5440 | */ |
| 5441 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 5442 | { |
| 5443 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5444 | |
| 5445 | if (ctx->xprt_ctx == toremove_ctx) { |
| 5446 | ctx->xprt_ctx = newctx; |
| 5447 | ctx->xprt = newops; |
| 5448 | return 0; |
| 5449 | } |
| 5450 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 5451 | } |
| 5452 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5453 | static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
| 5454 | { |
| 5455 | struct ssl_sock_ctx *ctx = context; |
| 5456 | |
| 5457 | /* First if we're doing an handshake, try that */ |
| 5458 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) |
| 5459 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
| 5460 | /* If we had an error, or the handshake is done and I/O is available, |
| 5461 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5462 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5463 | * we can't be sure conn_fd_handler() will be called again. |
| 5464 | */ |
| 5465 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 5466 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 5467 | int ret = 0; |
| 5468 | int woke = 0; |
| 5469 | |
| 5470 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5471 | if (ctx->subs) { |
| 5472 | tasklet_wakeup(ctx->subs->tasklet); |
| 5473 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5474 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5475 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5476 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5477 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5478 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5479 | * upper layers know. If we have no mux, create it, |
| 5480 | * and once we have a mux, call its wake method if we didn't |
| 5481 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5482 | */ |
| 5483 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5484 | if (!ctx->conn->mux) |
| 5485 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5486 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 5487 | ctx->conn->mux->wake(ctx->conn); |
| 5488 | return NULL; |
| 5489 | } |
| 5490 | } |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5491 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5492 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5493 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 5494 | ctx->subs->events & SUB_RETRY_RECV) { |
| 5495 | tasklet_wakeup(ctx->subs->tasklet); |
| 5496 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 5497 | if (!ctx->subs->events) |
| 5498 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5499 | } |
| 5500 | #endif |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5501 | return NULL; |
| 5502 | } |
| 5503 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5504 | /* 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] | 5505 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5506 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5507 | * flags are updated with whatever special event is detected (error, read0, |
| 5508 | * empty). The caller is responsible for taking care of those events and |
| 5509 | * avoiding the call if inappropriate. The function does not call the |
| 5510 | * connection's polling update function, so the caller is responsible for this. |
| 5511 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5512 | 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] | 5513 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5514 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 5515 | ssize_t ret; |
| 5516 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5517 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5518 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5519 | goto out_error; |
| 5520 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5521 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5522 | if (b_data(&ctx->early_buf)) { |
| 5523 | try = b_contig_space(buf); |
| 5524 | if (try > b_data(&ctx->early_buf)) |
| 5525 | try = b_data(&ctx->early_buf); |
| 5526 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 5527 | b_add(buf, try); |
| 5528 | b_del(&ctx->early_buf, try); |
| 5529 | if (b_data(&ctx->early_buf) == 0) |
| 5530 | b_free(&ctx->early_buf); |
| 5531 | return try; |
| 5532 | } |
| 5533 | #endif |
| 5534 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5535 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5536 | /* a handshake was requested */ |
| 5537 | return 0; |
| 5538 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5539 | /* read the largest possible block. For this, we perform only one call |
| 5540 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5541 | * in which case we accept to do it once again. A new attempt is made on |
| 5542 | * EINTR too. |
| 5543 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5544 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5545 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5546 | try = b_contig_space(buf); |
| 5547 | if (!try) |
| 5548 | break; |
| 5549 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5550 | if (try > count) |
| 5551 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5552 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5553 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5554 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5555 | if (conn->flags & CO_FL_ERROR) { |
| 5556 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5557 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5558 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5559 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5560 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5561 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5562 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5563 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5564 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5565 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5566 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5567 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5568 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5569 | 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] | 5570 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5571 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5572 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5573 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5574 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5575 | break; |
| 5576 | } |
| 5577 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5578 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5579 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5580 | SUB_RETRY_RECV, |
| 5581 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5582 | /* handshake is running, and it may need to re-enable read */ |
| 5583 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5584 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5585 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5586 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5587 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5588 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5589 | break; |
| 5590 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5591 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5592 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5593 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5594 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5595 | * stack before shutting down the connection for |
| 5596 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5597 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5598 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5599 | /* otherwise it's a real error */ |
| 5600 | goto out_error; |
| 5601 | } |
| 5602 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5603 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5604 | return done; |
| 5605 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5606 | clear_ssl_error: |
| 5607 | /* Clear openssl global errors stack */ |
| 5608 | ssl_sock_dump_errors(conn); |
| 5609 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5610 | read0: |
| 5611 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5612 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5613 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5614 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5615 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5616 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5617 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5618 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5619 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5620 | } |
| 5621 | |
| 5622 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5623 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5624 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5625 | * 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] | 5626 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5627 | * a second call may be performed. The connection's flags are updated with |
| 5628 | * whatever special event is detected (error, empty). The caller is responsible |
| 5629 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5630 | * 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] | 5631 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 5632 | * caller to take care of this. It's up to the caller to update the buffer's |
| 5633 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5634 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5635 | 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] | 5636 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5637 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5638 | ssize_t ret; |
| 5639 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5640 | |
| 5641 | done = 0; |
| 5642 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5643 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5644 | goto out_error; |
| 5645 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5646 | 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] | 5647 | /* a handshake was requested */ |
| 5648 | return 0; |
| 5649 | |
| 5650 | /* send the largest possible block. For this we perform only one call |
| 5651 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5652 | * in which case we accept to do it once again. |
| 5653 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5654 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5655 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5656 | size_t written_data; |
| 5657 | #endif |
| 5658 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5659 | try = b_contig_data(buf, done); |
| 5660 | if (try > count) |
| 5661 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5662 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5663 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5664 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5665 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5666 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5667 | } |
| 5668 | else { |
| 5669 | /* we need to keep the information about the fact that |
| 5670 | * we're not limiting the upcoming send(), because if it |
| 5671 | * fails, we'll have to retry with at least as many data. |
| 5672 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5673 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5674 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5675 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5676 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5677 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5678 | unsigned int max_early; |
| 5679 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5680 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5681 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5682 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5683 | if (SSL_get0_session(ctx->ssl)) |
| 5684 | 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] | 5685 | else |
| 5686 | max_early = 0; |
| 5687 | } |
| 5688 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5689 | if (try + ctx->sent_early_data > max_early) { |
| 5690 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5691 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5692 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5693 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5694 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5695 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5696 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5697 | 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] | 5698 | if (ret == 1) { |
| 5699 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5700 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5701 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5702 | 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] | 5703 | /* Initiate the handshake, now */ |
| 5704 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 5705 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5706 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5707 | } |
| 5708 | |
| 5709 | } else |
| 5710 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5711 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5712 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5713 | if (conn->flags & CO_FL_ERROR) { |
| 5714 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5715 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5716 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5717 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5718 | /* A send succeeded, so we can consider ourself connected */ |
| 5719 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5720 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5721 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5722 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5723 | } |
| 5724 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5725 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5726 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5727 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5728 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5729 | /* handshake is running, and it may need to re-enable write */ |
| 5730 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5731 | 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] | 5732 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5733 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5734 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5735 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5736 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5737 | break; |
| 5738 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5739 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5740 | break; |
| 5741 | } |
| 5742 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5743 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5744 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5745 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5746 | SUB_RETRY_RECV, |
| 5747 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5748 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5749 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5750 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5751 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5752 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5753 | break; |
| 5754 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5755 | goto out_error; |
| 5756 | } |
| 5757 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5758 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5759 | return done; |
| 5760 | |
| 5761 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5762 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5763 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5764 | ERR_clear_error(); |
| 5765 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5766 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5767 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5768 | } |
| 5769 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5770 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5771 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5772 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5773 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5774 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5775 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5776 | if (ctx->wait_event.events != 0) |
| 5777 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 5778 | ctx->wait_event.events, |
| 5779 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5780 | if (ctx->subs) { |
| 5781 | ctx->subs->events = 0; |
| 5782 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5783 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5784 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 5785 | if (ctx->xprt->close) |
| 5786 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5787 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5788 | if (global_ssl.async) { |
| 5789 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5790 | size_t num_all_fds = 0; |
| 5791 | int i; |
| 5792 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5793 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5794 | if (num_all_fds > 32) { |
| 5795 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5796 | return; |
| 5797 | } |
| 5798 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5799 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5800 | |
| 5801 | /* If an async job is pending, we must try to |
| 5802 | to catch the end using polling before calling |
| 5803 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5804 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5805 | for (i=0 ; i < num_all_fds ; i++) { |
| 5806 | /* switch on an handler designed to |
| 5807 | * handle the SSL_free |
| 5808 | */ |
| 5809 | afd = all_fd[i]; |
| 5810 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5811 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5812 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5813 | /* To ensure that the fd cache won't be used |
| 5814 | * and we'll catch a real RD event. |
| 5815 | */ |
| 5816 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5817 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5818 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5819 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5820 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5821 | return; |
| 5822 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5823 | /* Else we can remove the fds from the fdtab |
| 5824 | * and call SSL_free. |
| 5825 | * note: we do a fd_remove and not a delete |
| 5826 | * because the fd is owned by the engine. |
| 5827 | * the engine is responsible to close |
| 5828 | */ |
| 5829 | for (i=0 ; i < num_all_fds ; i++) |
| 5830 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5831 | } |
| 5832 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5833 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5834 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5835 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5836 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5837 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5838 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5839 | } |
| 5840 | |
| 5841 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5842 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5843 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5844 | 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] | 5845 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5846 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5847 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5848 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5849 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5850 | if (!clean) |
| 5851 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5852 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5853 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5854 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5855 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5856 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5857 | ERR_clear_error(); |
| 5858 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5859 | } |
| 5860 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5861 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5862 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 5863 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 5864 | { |
| 5865 | struct ssl_sock_ctx *ctx; |
| 5866 | X509 *crt; |
| 5867 | |
| 5868 | if (!ssl_sock_is_ssl(conn)) |
| 5869 | return 0; |
| 5870 | |
| 5871 | ctx = conn->xprt_ctx; |
| 5872 | |
| 5873 | crt = SSL_get_certificate(ctx->ssl); |
| 5874 | if (!crt) |
| 5875 | return 0; |
| 5876 | |
| 5877 | return cert_get_pkey_algo(crt, out); |
| 5878 | } |
| 5879 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5880 | /* used for ppv2 cert signature (can be used for logging) */ |
| 5881 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 5882 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5883 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5884 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5885 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 5886 | X509 *crt; |
| 5887 | |
| 5888 | if (!ssl_sock_is_ssl(conn)) |
| 5889 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5890 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5891 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5892 | if (!crt) |
| 5893 | return NULL; |
| 5894 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 5895 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 5896 | } |
| 5897 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5898 | /* used for ppv2 authority */ |
| 5899 | const char *ssl_sock_get_sni(struct connection *conn) |
| 5900 | { |
| 5901 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5902 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5903 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5904 | if (!ssl_sock_is_ssl(conn)) |
| 5905 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5906 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5907 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5908 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5909 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 5910 | #endif |
| 5911 | } |
| 5912 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5913 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5914 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 5915 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5916 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5917 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5918 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5919 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5920 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5921 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5922 | } |
| 5923 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5924 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5925 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 5926 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5927 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5928 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 5929 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5930 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5931 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5932 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 5933 | } |
| 5934 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 5935 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 5936 | { |
| 5937 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5938 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5939 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 5940 | if (!ssl_sock_is_ssl(conn)) |
| 5941 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5942 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5943 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 5944 | #endif |
| 5945 | } |
| 5946 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5947 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 5948 | * to disable SNI. |
| 5949 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5950 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 5951 | { |
| 5952 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5953 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5954 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5955 | char *prev_name; |
| 5956 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5957 | if (!ssl_sock_is_ssl(conn)) |
| 5958 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5959 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5960 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5961 | /* if the SNI changes, we must destroy the reusable context so that a |
| 5962 | * new connection will present a new SNI. As an optimization we could |
| 5963 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 5964 | * server. |
| 5965 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5966 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5967 | if ((!prev_name && hostname) || |
| 5968 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5969 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 5970 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5971 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 5972 | #endif |
| 5973 | } |
| 5974 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5975 | /* Extract peer certificate's common name into the chunk dest |
| 5976 | * Returns |
| 5977 | * the len of the extracted common name |
| 5978 | * or 0 if no CN found in DN |
| 5979 | * or -1 on error case (i.e. no peer certificate) |
| 5980 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5981 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 5982 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5983 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5984 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5985 | X509 *crt = NULL; |
| 5986 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5987 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5988 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5989 | .area = (char *)&find_cn, |
| 5990 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5991 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5992 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5993 | |
| 5994 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 5995 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5996 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 5997 | |
| 5998 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5999 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6000 | if (!crt) |
| 6001 | goto out; |
| 6002 | |
| 6003 | name = X509_get_subject_name(crt); |
| 6004 | if (!name) |
| 6005 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6006 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6007 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6008 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6009 | if (crt) |
| 6010 | X509_free(crt); |
| 6011 | |
| 6012 | return result; |
| 6013 | } |
| 6014 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6015 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6016 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6017 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6018 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6019 | X509 *crt = NULL; |
| 6020 | |
| 6021 | if (!ssl_sock_is_ssl(conn)) |
| 6022 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6023 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6024 | |
| 6025 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6026 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6027 | if (!crt) |
| 6028 | return 0; |
| 6029 | |
| 6030 | X509_free(crt); |
| 6031 | return 1; |
| 6032 | } |
| 6033 | |
| 6034 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6035 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6036 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6037 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6038 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6039 | if (!ssl_sock_is_ssl(conn)) |
| 6040 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6041 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6042 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6043 | } |
| 6044 | |
| 6045 | /* returns result from SSL verify */ |
| 6046 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6047 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6048 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6049 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6050 | if (!ssl_sock_is_ssl(conn)) |
| 6051 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6052 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6053 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6054 | } |
| 6055 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6056 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6057 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6058 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6059 | * freed by the caller. NPN is also checked if available since older versions |
| 6060 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6061 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6062 | 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] | 6063 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6064 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 6065 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6066 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6067 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6068 | return 0; |
| 6069 | |
| 6070 | *str = NULL; |
| 6071 | |
| 6072 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6073 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6074 | if (*str) |
| 6075 | return 1; |
| 6076 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6077 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6078 | 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] | 6079 | if (*str) |
| 6080 | return 1; |
| 6081 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6082 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6083 | return 0; |
| 6084 | } |
| 6085 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6086 | /* "issuers-chain-path" load chain certificate in global */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6087 | 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] | 6088 | { |
| 6089 | X509 *ca; |
| 6090 | X509_NAME *name = NULL; |
| 6091 | ASN1_OCTET_STRING *skid = NULL; |
| 6092 | STACK_OF(X509) *chain = NULL; |
| 6093 | struct issuer_chain *issuer; |
| 6094 | struct eb64_node *node; |
| 6095 | char *path; |
| 6096 | u64 key; |
| 6097 | int ret = 0; |
| 6098 | |
| 6099 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 6100 | if (chain == NULL) { |
| 6101 | chain = sk_X509_new_null(); |
| 6102 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 6103 | name = X509_get_subject_name(ca); |
| 6104 | } |
| 6105 | if (!sk_X509_push(chain, ca)) { |
| 6106 | X509_free(ca); |
| 6107 | goto end; |
| 6108 | } |
| 6109 | } |
| 6110 | if (!chain) { |
| 6111 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 6112 | goto end; |
| 6113 | } |
| 6114 | if (!skid) { |
| 6115 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 6116 | goto end; |
| 6117 | } |
| 6118 | if (!name) { |
| 6119 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 6120 | goto end; |
| 6121 | } |
| 6122 | key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6123 | 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] | 6124 | issuer = container_of(node, typeof(*issuer), node); |
| 6125 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 6126 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 6127 | goto end; |
| 6128 | } |
| 6129 | } |
| 6130 | issuer = calloc(1, sizeof *issuer); |
| 6131 | path = strdup(fp); |
| 6132 | if (!issuer || !path) { |
| 6133 | free(issuer); |
| 6134 | free(path); |
| 6135 | goto end; |
| 6136 | } |
| 6137 | issuer->node.key = key; |
| 6138 | issuer->path = path; |
| 6139 | issuer->chain = chain; |
| 6140 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6141 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6142 | ret = 1; |
| 6143 | end: |
| 6144 | if (skid) |
| 6145 | ASN1_OCTET_STRING_free(skid); |
| 6146 | if (chain) |
| 6147 | sk_X509_pop_free(chain, X509_free); |
| 6148 | return ret; |
| 6149 | } |
| 6150 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 6151 | struct issuer_chain* ssl_get0_issuer_chain(X509 *cert) |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6152 | { |
| 6153 | AUTHORITY_KEYID *akid; |
| 6154 | struct issuer_chain *issuer = NULL; |
| 6155 | |
| 6156 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
| 6157 | if (akid) { |
| 6158 | struct eb64_node *node; |
| 6159 | u64 hk; |
| 6160 | hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0); |
| 6161 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 6162 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 6163 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 6164 | issuer = ti; |
| 6165 | break; |
| 6166 | } |
| 6167 | } |
| 6168 | AUTHORITY_KEYID_free(akid); |
| 6169 | } |
| 6170 | return issuer; |
| 6171 | } |
| 6172 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6173 | void ssl_free_global_issuers(void) |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6174 | { |
| 6175 | struct eb64_node *node, *back; |
| 6176 | struct issuer_chain *issuer; |
| 6177 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6178 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6179 | while (node) { |
| 6180 | issuer = container_of(node, typeof(*issuer), node); |
| 6181 | back = eb64_next(node); |
| 6182 | eb64_delete(node); |
| 6183 | free(issuer->path); |
| 6184 | sk_X509_pop_free(issuer->chain, X509_free); |
| 6185 | free(issuer); |
| 6186 | node = back; |
| 6187 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6188 | } |
| 6189 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6190 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6191 | static int ssl_check_async_engine_count(void) { |
| 6192 | int err_code = 0; |
| 6193 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6194 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6195 | 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] | 6196 | err_code = ERR_ABORT; |
| 6197 | } |
| 6198 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6199 | } |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6200 | #endif |
| 6201 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6202 | /* This function is used with TLS ticket keys management. It permits to browse |
| 6203 | * each reference. The variable <getnext> must contain the current node, |
| 6204 | * <end> point to the root node. |
| 6205 | */ |
| 6206 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6207 | static inline |
| 6208 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 6209 | { |
| 6210 | struct tls_keys_ref *ref = getnext; |
| 6211 | |
| 6212 | while (1) { |
| 6213 | |
| 6214 | /* Get next list entry. */ |
| 6215 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 6216 | |
| 6217 | /* If the entry is the last of the list, return NULL. */ |
| 6218 | if (&ref->list == end) |
| 6219 | return NULL; |
| 6220 | |
| 6221 | return ref; |
| 6222 | } |
| 6223 | } |
| 6224 | |
| 6225 | static inline |
| 6226 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 6227 | { |
| 6228 | int id; |
| 6229 | char *error; |
| 6230 | |
| 6231 | /* If the reference starts by a '#', this is numeric id. */ |
| 6232 | if (reference[0] == '#') { |
| 6233 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 6234 | id = strtol(reference + 1, &error, 10); |
| 6235 | if (*error != '\0') |
| 6236 | return NULL; |
| 6237 | |
| 6238 | /* Perform the unique id lookup. */ |
| 6239 | return tlskeys_ref_lookupid(id); |
| 6240 | } |
| 6241 | |
| 6242 | /* Perform the string lookup. */ |
| 6243 | return tlskeys_ref_lookup(reference); |
| 6244 | } |
| 6245 | #endif |
| 6246 | |
| 6247 | |
| 6248 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6249 | |
| 6250 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 6251 | |
| 6252 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 6253 | return cli_io_handler_tlskeys_files(appctx); |
| 6254 | } |
| 6255 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6256 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 6257 | * (next index to be dumped), and cli.p0 (next key reference). |
| 6258 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6259 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 6260 | |
| 6261 | struct stream_interface *si = appctx->owner; |
| 6262 | |
| 6263 | switch (appctx->st2) { |
| 6264 | case STAT_ST_INIT: |
| 6265 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 6266 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6267 | * later and restart at the state "STAT_ST_INIT". |
| 6268 | */ |
| 6269 | chunk_reset(&trash); |
| 6270 | |
| 6271 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 6272 | chunk_appendf(&trash, "# id secret\n"); |
| 6273 | else |
| 6274 | chunk_appendf(&trash, "# id (file)\n"); |
| 6275 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6276 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6277 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6278 | return 0; |
| 6279 | } |
| 6280 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6281 | /* Now, we start the browsing of the references lists. |
| 6282 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 6283 | * available field of this pointer is <list>. It is used with the function |
| 6284 | * tlskeys_list_get_next() for retruning the first available entry |
| 6285 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6286 | if (appctx->ctx.cli.p0 == NULL) { |
| 6287 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 6288 | 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] | 6289 | } |
| 6290 | |
| 6291 | appctx->st2 = STAT_ST_LIST; |
| 6292 | /* fall through */ |
| 6293 | |
| 6294 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6295 | while (appctx->ctx.cli.p0) { |
| 6296 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6297 | |
| 6298 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6299 | 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] | 6300 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6301 | |
| 6302 | if (appctx->ctx.cli.i1 == 0) |
| 6303 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 6304 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6305 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6306 | int head; |
| 6307 | |
| 6308 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 6309 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6310 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6311 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6312 | |
| 6313 | chunk_reset(t2); |
| 6314 | /* 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] | 6315 | if (ref->key_size_bits == 128) { |
| 6316 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 6317 | sizeof(struct tls_sess_key_128), |
| 6318 | t2->area, t2->size); |
| 6319 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 6320 | t2->area); |
| 6321 | } |
| 6322 | else if (ref->key_size_bits == 256) { |
| 6323 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 6324 | sizeof(struct tls_sess_key_256), |
| 6325 | t2->area, t2->size); |
| 6326 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 6327 | t2->area); |
| 6328 | } |
| 6329 | else { |
| 6330 | /* This case should never happen */ |
| 6331 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 6332 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6333 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6334 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6335 | /* let's try again later from this stream. We add ourselves into |
| 6336 | * this stream's users so that it can remove us upon termination. |
| 6337 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6338 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6339 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6340 | return 0; |
| 6341 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6342 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6343 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6344 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6345 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6346 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6347 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6348 | /* let's try again later from this stream. We add ourselves into |
| 6349 | * this stream's users so that it can remove us upon termination. |
| 6350 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6351 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6352 | return 0; |
| 6353 | } |
| 6354 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6355 | 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] | 6356 | break; |
| 6357 | |
| 6358 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6359 | 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] | 6360 | } |
| 6361 | |
| 6362 | appctx->st2 = STAT_ST_FIN; |
| 6363 | /* fall through */ |
| 6364 | |
| 6365 | default: |
| 6366 | appctx->st2 = STAT_ST_FIN; |
| 6367 | return 1; |
| 6368 | } |
| 6369 | return 0; |
| 6370 | } |
| 6371 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6372 | /* 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] | 6373 | 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] | 6374 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6375 | /* no parameter, shows only file list */ |
| 6376 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6377 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6378 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6379 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6380 | } |
| 6381 | |
| 6382 | if (args[2][0] == '*') { |
| 6383 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6384 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6385 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6386 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6387 | if (!appctx->ctx.cli.p0) |
| 6388 | 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] | 6389 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6390 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6391 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6392 | } |
| 6393 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 6394 | 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] | 6395 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6396 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6397 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6398 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6399 | /* 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] | 6400 | if (!*args[3] || !*args[4]) |
| 6401 | 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] | 6402 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6403 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6404 | if (!ref) |
| 6405 | 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] | 6406 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6407 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6408 | if (ret < 0) |
| 6409 | 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] | 6410 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6411 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6412 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 6413 | 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] | 6414 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6415 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6416 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6417 | #endif |
William Lallemand | 419e634 | 2020-04-08 12:05:39 +0200 | [diff] [blame] | 6418 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 6419 | 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] | 6420 | { |
| 6421 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 6422 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6423 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 6424 | |
| 6425 | if (!payload) |
| 6426 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6427 | |
| 6428 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6429 | if (!*payload) |
| 6430 | 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] | 6431 | |
| 6432 | /* remove \r and \n from the payload */ |
| 6433 | for (i = 0, j = 0; payload[i]; i++) { |
| 6434 | if (payload[i] == '\r' || payload[i] == '\n') |
| 6435 | continue; |
| 6436 | payload[j++] = payload[i]; |
| 6437 | } |
| 6438 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6439 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6440 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6441 | if (ret < 0) |
| 6442 | 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] | 6443 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6444 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6445 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6446 | if (err) |
| 6447 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 6448 | else |
| 6449 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6450 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6451 | |
| 6452 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6453 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6454 | 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] | 6455 | #endif |
| 6456 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 6457 | } |
| 6458 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6459 | /* register cli keywords */ |
| 6460 | static struct cli_kw_list cli_kws = {{ },{ |
| 6461 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6462 | { { "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] | 6463 | { { "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] | 6464 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6465 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6466 | { { NULL }, NULL, NULL, NULL } |
| 6467 | }}; |
| 6468 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6469 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6470 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 6471 | /* transport-layer operations for SSL sockets */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6472 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6473 | .snd_buf = ssl_sock_from_buf, |
| 6474 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6475 | .subscribe = ssl_subscribe, |
| 6476 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6477 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6478 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6479 | .rcv_pipe = NULL, |
| 6480 | .snd_pipe = NULL, |
| 6481 | .shutr = NULL, |
| 6482 | .shutw = ssl_sock_shutw, |
| 6483 | .close = ssl_sock_close, |
| 6484 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6485 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6486 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 6487 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 6488 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6489 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 6490 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6491 | }; |
| 6492 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6493 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 6494 | struct session *sess, struct stream *s, int flags) |
| 6495 | { |
| 6496 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6497 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6498 | |
| 6499 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6500 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6501 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6502 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6503 | 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] | 6504 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6505 | s->req.flags |= CF_READ_NULL; |
| 6506 | return ACT_RET_YIELD; |
| 6507 | } |
| 6508 | } |
| 6509 | return (ACT_RET_CONT); |
| 6510 | } |
| 6511 | |
| 6512 | 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) |
| 6513 | { |
| 6514 | rule->action_ptr = ssl_action_wait_for_hs; |
| 6515 | |
| 6516 | return ACT_RET_PRS_OK; |
| 6517 | } |
| 6518 | |
| 6519 | static struct action_kw_list http_req_actions = {ILH, { |
| 6520 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 6521 | { /* END */ } |
| 6522 | }}; |
| 6523 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6524 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 6525 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6526 | #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] | 6527 | |
| 6528 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6529 | { |
| 6530 | if (ptr) { |
| 6531 | chunk_destroy(ptr); |
| 6532 | free(ptr); |
| 6533 | } |
| 6534 | } |
| 6535 | |
| 6536 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6537 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6538 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6539 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6540 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 6541 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6542 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6543 | static void __ssl_sock_init(void) |
| 6544 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6545 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6546 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6547 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6548 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6549 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6550 | if (global_ssl.listen_default_ciphers) |
| 6551 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 6552 | if (global_ssl.connect_default_ciphers) |
| 6553 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 6554 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6555 | if (global_ssl.listen_default_ciphersuites) |
| 6556 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 6557 | if (global_ssl.connect_default_ciphersuites) |
| 6558 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 6559 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 6560 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 6561 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 6562 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6563 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6564 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6565 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6566 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6567 | n = sk_SSL_COMP_num(cm); |
| 6568 | while (n--) { |
| 6569 | (void) sk_SSL_COMP_pop(cm); |
| 6570 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6571 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6572 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6573 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6574 | ssl_locking_init(); |
| 6575 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6576 | #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] | 6577 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 6578 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 6579 | 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] | 6580 | 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] | 6581 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6582 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6583 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6584 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 6585 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6586 | hap_register_post_check(tlskeys_finalize_config); |
| 6587 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6588 | |
| 6589 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 6590 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 6591 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6592 | hap_register_post_deinit(ssl_free_global_issuers); |
| 6593 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6594 | #ifndef OPENSSL_NO_DH |
| 6595 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 6596 | hap_register_post_deinit(ssl_free_dh); |
| 6597 | #endif |
| 6598 | #ifndef OPENSSL_NO_ENGINE |
| 6599 | hap_register_post_deinit(ssl_free_engines); |
| 6600 | #endif |
| 6601 | /* Load SSL string for the verbose & debug mode. */ |
| 6602 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6603 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 6604 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 6605 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 6606 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 6607 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 6608 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 6609 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 6610 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 6611 | |
| 6612 | HA_SPIN_INIT(&ckch_lock); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 6613 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 6614 | /* Try to register dedicated SSL/TLS protocol message callbacks for |
| 6615 | * heartbleed attack (CVE-2014-0160) and clienthello. |
| 6616 | */ |
| 6617 | hap_register_post_check(ssl_sock_register_msg_callbacks); |
| 6618 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 6619 | /* Try to free all callbacks that were registered by using |
| 6620 | * ssl_sock_register_msg_callback(). |
| 6621 | */ |
| 6622 | hap_register_post_deinit(ssl_sock_unregister_msg_callbacks); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6623 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 6624 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6625 | /* Compute and register the version string */ |
| 6626 | static void ssl_register_build_options() |
| 6627 | { |
| 6628 | char *ptr = NULL; |
| 6629 | int i; |
| 6630 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6631 | memprintf(&ptr, "Built with OpenSSL version : " |
| 6632 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 6633 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6634 | #else /* OPENSSL_IS_BORINGSSL */ |
| 6635 | OPENSSL_VERSION_TEXT |
| 6636 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6637 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 6638 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6639 | #endif |
| 6640 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 6641 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6642 | "no (library version too old)" |
| 6643 | #elif defined(OPENSSL_NO_TLSEXT) |
| 6644 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 6645 | #else |
| 6646 | "yes" |
| 6647 | #endif |
| 6648 | "", ptr); |
| 6649 | |
| 6650 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 6651 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 6652 | "yes" |
| 6653 | #else |
| 6654 | #ifdef OPENSSL_NO_TLSEXT |
| 6655 | "no (because of OPENSSL_NO_TLSEXT)" |
| 6656 | #else |
| 6657 | "no (version might be too old, 0.9.8f min needed)" |
| 6658 | #endif |
| 6659 | #endif |
| 6660 | "", ptr); |
| 6661 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 6662 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 6663 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 6664 | if (methodVersions[i].option) |
| 6665 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 6666 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6667 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6668 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6669 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6670 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 6671 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6672 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6673 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6674 | void ssl_free_engines(void) { |
| 6675 | struct ssl_engine_list *wl, *wlb; |
| 6676 | /* free up engine list */ |
| 6677 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 6678 | ENGINE_finish(wl->e); |
| 6679 | ENGINE_free(wl->e); |
| 6680 | LIST_DEL(&wl->list); |
| 6681 | free(wl); |
| 6682 | } |
| 6683 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6684 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6685 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6686 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6687 | void ssl_free_dh(void) { |
| 6688 | if (local_dh_1024) { |
| 6689 | DH_free(local_dh_1024); |
| 6690 | local_dh_1024 = NULL; |
| 6691 | } |
| 6692 | if (local_dh_2048) { |
| 6693 | DH_free(local_dh_2048); |
| 6694 | local_dh_2048 = NULL; |
| 6695 | } |
| 6696 | if (local_dh_4096) { |
| 6697 | DH_free(local_dh_4096); |
| 6698 | local_dh_4096 = NULL; |
| 6699 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 6700 | if (global_dh) { |
| 6701 | DH_free(global_dh); |
| 6702 | global_dh = NULL; |
| 6703 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6704 | } |
| 6705 | #endif |
| 6706 | |
| 6707 | __attribute__((destructor)) |
| 6708 | static void __ssl_sock_deinit(void) |
| 6709 | { |
| 6710 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6711 | if (ssl_ctx_lru_tree) { |
| 6712 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 6713 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6714 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6715 | #endif |
| 6716 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6717 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6718 | ERR_remove_state(0); |
| 6719 | ERR_free_strings(); |
| 6720 | |
| 6721 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6722 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6723 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6724 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6725 | CRYPTO_cleanup_all_ex_data(); |
| 6726 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6727 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6728 | } |
| 6729 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6730 | /* |
| 6731 | * Local variables: |
| 6732 | * c-indent-level: 8 |
| 6733 | * c-basic-offset: 8 |
| 6734 | * End: |
| 6735 | */ |